Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(374)

Side by Side Diff: components/arc/arc_bridge_service_unittest.cc

Issue 2194193002: Fix ArcBridgeBootstrap race issues. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <memory> 5 #include <memory>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
13 #include "chromeos/dbus/dbus_thread_manager.h" 13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "components/arc/arc_bridge_service_impl.h" 14 #include "components/arc/arc_bridge_service_impl.h"
15 #include "components/arc/test/fake_arc_bridge_bootstrap.h" 15 #include "components/arc/test/fake_arc_bridge_bootstrap.h"
16 #include "components/arc/test/fake_arc_bridge_instance.h" 16 #include "components/arc/test/fake_arc_bridge_instance.h"
17 #include "mojo/public/cpp/system/message_pipe.h" 17 #include "mojo/public/cpp/system/message_pipe.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 19
20 namespace arc { 20 namespace arc {
21 21
22 namespace { 22 namespace {
23 23
24 class DummyObserver : public ArcBridgeService::Observer {}; 24 class DummyObserver : public ArcBridgeService::Observer {};
25 25
26 } // namespace 26 } // namespace
27 27
28 class ArcBridgeTest : public testing::Test, public ArcBridgeService::Observer { 28 // TODO(hidehiko): ArcBridgeTest gets complicated and has stale code.
29 // Simplify the code.
30 class ArcBridgeTest : public testing::Test,
31 public ArcBridgeService::Observer {
29 public: 32 public:
30 ArcBridgeTest() : ready_(false) {} 33 ArcBridgeTest() = default;
31 ~ArcBridgeTest() override {}
32 34
33 void OnBridgeReady() override { 35 void OnBridgeReady() override {
34 state_ = ArcBridgeService::State::READY; 36 state_ = ArcBridgeService::State::READY;
35 ready_ = true; 37 ready_ = true;
36 } 38 }
37 39
38 void OnBridgeStopped(ArcBridgeService::StopReason stop_reason) override { 40 void OnBridgeStopped(ArcBridgeService::StopReason stop_reason) override {
41 // The instance is already destructed in ArcBridgeServiceImpl::OnStopped().
39 state_ = ArcBridgeService::State::STOPPED; 42 state_ = ArcBridgeService::State::STOPPED;
40 stop_reason_ = stop_reason; 43 stop_reason_ = stop_reason;
41 message_loop_.task_runner()->PostTask(FROM_HERE, 44 message_loop_.task_runner()->PostTask(FROM_HERE,
42 message_loop_.QuitWhenIdleClosure()); 45 message_loop_.QuitWhenIdleClosure());
43 } 46 }
44 47
45 bool ready() const { return ready_; } 48 bool ready() const { return ready_; }
46 ArcBridgeService::State state() const { return state_; } 49 ArcBridgeService::State state() const { return state_; }
50 FakeArcBridgeBootstrap* bootstrap() const {
51 return static_cast<FakeArcBridgeBootstrap*>(
52 service_->GetBootstrapForTesting());
53 }
47 54
48 protected: 55 protected:
49 std::unique_ptr<ArcBridgeServiceImpl> service_; 56 std::unique_ptr<ArcBridgeServiceImpl> service_;
50 std::unique_ptr<FakeArcBridgeInstance> instance_;
51 ArcBridgeService::StopReason stop_reason_; 57 ArcBridgeService::StopReason stop_reason_;
52 58
59 static std::unique_ptr<ArcBridgeBootstrap> CreateSuspendedBootstrap() {
60 std::unique_ptr<FakeArcBridgeBootstrap> bootstrap(
61 new FakeArcBridgeBootstrap);
Luis Héctor Chávez 2016/09/15 23:08:49 same comment about default/value initialization. I
hidehiko 2016/09/20 13:35:19 Done. Replaced "unique_ptr + new" by "auto + MakeU
62 bootstrap->SuspendBoot();
63 return bootstrap;
64 }
65
66 static std::unique_ptr<ArcBridgeBootstrap> CreateBootFailureBootstrap(
67 ArcBridgeService::StopReason reason) {
68 std::unique_ptr<FakeArcBridgeBootstrap> bootstrap(
69 new FakeArcBridgeBootstrap);
70 bootstrap->EnableBootFailureEmulation(reason);
71 return bootstrap;
72 }
73
53 private: 74 private:
54 void SetUp() override { 75 void SetUp() override {
55 chromeos::DBusThreadManager::Initialize(); 76 chromeos::DBusThreadManager::Initialize();
56 77
57 ready_ = false; 78 ready_ = false;
58 state_ = ArcBridgeService::State::STOPPED; 79 state_ = ArcBridgeService::State::STOPPED;
59 stop_reason_ = ArcBridgeService::StopReason::SHUTDOWN; 80 stop_reason_ = ArcBridgeService::StopReason::SHUTDOWN;
60 81
61 instance_.reset(new FakeArcBridgeInstance()); 82 service_.reset(new ArcBridgeServiceImpl);
62 service_.reset(new ArcBridgeServiceImpl( 83 service_->SetArcBridgeBootstrapFactoryForTesting(
63 base::MakeUnique<FakeArcBridgeBootstrap>(instance_.get()))); 84 base::Bind(FakeArcBridgeBootstrap::Create));
64
65 service_->AddObserver(this); 85 service_->AddObserver(this);
66 } 86 }
67 87
68 void TearDown() override { 88 void TearDown() override {
69 service_->RemoveObserver(this); 89 service_->RemoveObserver(this);
70 instance_.reset();
71 service_.reset(); 90 service_.reset();
72 91
73 chromeos::DBusThreadManager::Shutdown(); 92 chromeos::DBusThreadManager::Shutdown();
74 } 93 }
75 94
76 bool ready_; 95 bool ready_ = false;
77 ArcBridgeService::State state_; 96 ArcBridgeService::State state_;
78 base::MessageLoopForUI message_loop_; 97 base::MessageLoopForUI message_loop_;
79 98
80 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest); 99 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest);
81 }; 100 };
82 101
83 // Exercises the basic functionality of the ARC Bridge Service. A message from 102 // Exercises the basic functionality of the ARC Bridge Service. A message from
84 // within the instance should cause the observer to be notified. 103 // within the instance should cause the observer to be notified.
85 TEST_F(ArcBridgeTest, Basic) { 104 TEST_F(ArcBridgeTest, Basic) {
86 ASSERT_FALSE(ready()); 105 ASSERT_FALSE(ready());
87 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 106 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
88 107
89 service_->HandleStartup(); 108 service_->HandleStartup();
90 instance_->WaitForInitCall();
91 ASSERT_EQ(ArcBridgeService::State::READY, state()); 109 ASSERT_EQ(ArcBridgeService::State::READY, state());
92 110
93 service_->Shutdown(); 111 service_->Shutdown();
94 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 112 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
95 } 113 }
96 114
97 // If the ArcBridgeService is shut down, it should be stopped, even 115 // If the ArcBridgeService is shut down, it should be stopped, even
98 // mid-startup. 116 // mid-startup.
99 TEST_F(ArcBridgeTest, ShutdownMidStartup) { 117 TEST_F(ArcBridgeTest, ShutdownMidStartup) {
100 ASSERT_FALSE(ready()); 118 ASSERT_FALSE(ready());
101 119
120 service_->SetArcBridgeBootstrapFactoryForTesting(
121 base::Bind(ArcBridgeTest::CreateSuspendedBootstrap));
102 service_->HandleStartup(); 122 service_->HandleStartup();
103 // WaitForInitCall() omitted. 123 ASSERT_FALSE(service_->stopped());
104 ASSERT_EQ(ArcBridgeService::State::READY, state()); 124 ASSERT_FALSE(service_->ready());
105 125
106 service_->Shutdown(); 126 service_->Shutdown();
107 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 127 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
108 } 128 }
109 129
130 // If the boot procedure is failed, then restarting mechanism should not
131 // triggered.
132 TEST_F(ArcBridgeTest, BootFailure) {
133 ASSERT_TRUE(service_->stopped());
134
135 service_->SetArcBridgeBootstrapFactoryForTesting(
136 base::Bind(ArcBridgeTest::CreateBootFailureBootstrap,
137 ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE));
138 service_->HandleStartup();
139 EXPECT_EQ(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE, stop_reason_);
140 ASSERT_TRUE(service_->stopped());
141 }
142
110 // If the instance is stopped, it should be re-started. 143 // If the instance is stopped, it should be re-started.
111 TEST_F(ArcBridgeTest, Restart) { 144 TEST_F(ArcBridgeTest, Restart) {
112 ASSERT_FALSE(ready()); 145 ASSERT_FALSE(ready());
113 ASSERT_EQ(0, instance_->init_calls());
114 146
115 service_->HandleStartup(); 147 service_->HandleStartup();
116 instance_->WaitForInitCall();
117 ASSERT_EQ(ArcBridgeService::State::READY, state()); 148 ASSERT_EQ(ArcBridgeService::State::READY, state());
118 ASSERT_EQ(1, instance_->init_calls());
119 149
120 // Simulate a connection loss. 150 // Simulate a connection loss.
121 service_->DisableReconnectDelayForTesting(); 151 service_->DisableReconnectDelayForTesting();
122 instance_->Stop(ArcBridgeService::StopReason::CRASH); 152 ASSERT_TRUE(bootstrap());
123 instance_->WaitForInitCall(); 153 bootstrap()->StopWithReason(ArcBridgeService::StopReason::CRASH);
124 ASSERT_EQ(ArcBridgeService::State::READY, state()); 154 ASSERT_TRUE(service_->ready());
125 ASSERT_EQ(2, instance_->init_calls());
126 155
127 service_->Shutdown(); 156 service_->Shutdown();
128 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 157 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
129 } 158 }
130 159
131 // Makes sure OnBridgeStopped is called on stop. 160 // Makes sure OnBridgeStopped is called on stop.
132 TEST_F(ArcBridgeTest, OnBridgeStopped) { 161 TEST_F(ArcBridgeTest, OnBridgeStopped) {
133 ASSERT_FALSE(ready()); 162 ASSERT_FALSE(ready());
134 163
135 service_->DisableReconnectDelayForTesting(); 164 service_->DisableReconnectDelayForTesting();
136 service_->HandleStartup(); 165 service_->HandleStartup();
137 instance_->WaitForInitCall();
138 ASSERT_EQ(ArcBridgeService::State::READY, state()); 166 ASSERT_EQ(ArcBridgeService::State::READY, state());
139 167
140 // Simulate boot failure. 168 // Simulate boot failure.
141 instance_->Stop(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE); 169 ASSERT_TRUE(bootstrap());
142 instance_->WaitForInitCall(); 170 bootstrap()->StopWithReason(
143 ASSERT_EQ(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE, stop_reason_); 171 ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE);
144 ASSERT_EQ(ArcBridgeService::State::READY, state()); 172 EXPECT_EQ(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE, stop_reason_);
173 ASSERT_TRUE(service_->ready());
145 174
146 // Simulate crash. 175 // Simulate crash.
147 instance_->Stop(ArcBridgeService::StopReason::CRASH); 176 ASSERT_TRUE(bootstrap());
148 instance_->WaitForInitCall(); 177 bootstrap()->StopWithReason(ArcBridgeService::StopReason::CRASH);
149 ASSERT_EQ(ArcBridgeService::StopReason::CRASH, stop_reason_); 178 EXPECT_EQ(ArcBridgeService::StopReason::CRASH, stop_reason_);
150 ASSERT_EQ(ArcBridgeService::State::READY, state()); 179 ASSERT_TRUE(service_->ready());
151 180
152 // Graceful shutdown. 181 // Graceful shutdown.
153 service_->Shutdown(); 182 service_->Shutdown();
154 ASSERT_EQ(ArcBridgeService::StopReason::SHUTDOWN, stop_reason_); 183 ASSERT_EQ(ArcBridgeService::StopReason::SHUTDOWN, stop_reason_);
155 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 184 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
156 } 185 }
157 186
158 // Removing the same observer more than once should be okay. 187 // Removing the same observer more than once should be okay.
159 TEST_F(ArcBridgeTest, RemoveObserverTwice) { 188 TEST_F(ArcBridgeTest, RemoveObserverTwice) {
160 ASSERT_FALSE(ready()); 189 ASSERT_FALSE(ready());
161 service_->RemoveObserver(this); 190 auto dummy_observer = base::MakeUnique<DummyObserver>();
162 // The teardown method will also remove |this|. 191 service_->AddObserver(dummy_observer.get());
192 // Call RemoveObserver() twice.
193 service_->RemoveObserver(dummy_observer.get());
194 service_->RemoveObserver(dummy_observer.get());
163 } 195 }
164 196
165 // Removing an unknown observer should be allowed. 197 // Removing an unknown observer should be allowed.
166 TEST_F(ArcBridgeTest, RemoveUnknownObserver) { 198 TEST_F(ArcBridgeTest, RemoveUnknownObserver) {
167 ASSERT_FALSE(ready()); 199 ASSERT_FALSE(ready());
168 auto dummy_observer = base::MakeUnique<DummyObserver>(); 200 auto dummy_observer = base::MakeUnique<DummyObserver>();
169 service_->RemoveObserver(dummy_observer.get()); 201 service_->RemoveObserver(dummy_observer.get());
170 } 202 }
171 203
172 } // namespace arc 204 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698