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

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: Rebase. Created 4 years, 2 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
« no previous file with comments | « components/arc/arc_bridge_service_impl.cc ('k') | components/arc/arc_service_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 auto bootstrap = base::MakeUnique<FakeArcBridgeBootstrap>();
61 bootstrap->SuspendBoot();
62 return bootstrap;
63 }
64
65 static std::unique_ptr<ArcBridgeBootstrap> CreateBootFailureBootstrap(
66 ArcBridgeService::StopReason reason) {
67 auto bootstrap = base::MakeUnique<FakeArcBridgeBootstrap>();
68 bootstrap->EnableBootFailureEmulation(reason);
69 return bootstrap;
70 }
71
53 private: 72 private:
54 void SetUp() override { 73 void SetUp() override {
55 chromeos::DBusThreadManager::Initialize(); 74 chromeos::DBusThreadManager::Initialize();
56 75
57 ready_ = false; 76 ready_ = false;
58 state_ = ArcBridgeService::State::STOPPED; 77 state_ = ArcBridgeService::State::STOPPED;
59 stop_reason_ = ArcBridgeService::StopReason::SHUTDOWN; 78 stop_reason_ = ArcBridgeService::StopReason::SHUTDOWN;
60 79
61 instance_.reset(new FakeArcBridgeInstance()); 80 service_.reset(new ArcBridgeServiceImpl());
62 service_.reset(new ArcBridgeServiceImpl( 81 service_->SetArcBridgeBootstrapFactoryForTesting(
63 base::MakeUnique<FakeArcBridgeBootstrap>(instance_.get()))); 82 base::Bind(FakeArcBridgeBootstrap::Create));
64
65 service_->AddObserver(this); 83 service_->AddObserver(this);
66 } 84 }
67 85
68 void TearDown() override { 86 void TearDown() override {
69 service_->RemoveObserver(this); 87 service_->RemoveObserver(this);
70 instance_.reset();
71 service_.reset(); 88 service_.reset();
72 89
73 chromeos::DBusThreadManager::Shutdown(); 90 chromeos::DBusThreadManager::Shutdown();
74 } 91 }
75 92
76 bool ready_; 93 bool ready_ = false;
77 ArcBridgeService::State state_; 94 ArcBridgeService::State state_;
78 base::MessageLoopForUI message_loop_; 95 base::MessageLoopForUI message_loop_;
79 96
80 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest); 97 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest);
81 }; 98 };
82 99
83 // Exercises the basic functionality of the ARC Bridge Service. A message from 100 // Exercises the basic functionality of the ARC Bridge Service. A message from
84 // within the instance should cause the observer to be notified. 101 // within the instance should cause the observer to be notified.
85 TEST_F(ArcBridgeTest, Basic) { 102 TEST_F(ArcBridgeTest, Basic) {
86 ASSERT_FALSE(ready()); 103 ASSERT_FALSE(ready());
87 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 104 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
88 105
89 service_->HandleStartup(); 106 service_->HandleStartup();
90 instance_->WaitForInitCall();
91 ASSERT_EQ(ArcBridgeService::State::READY, state()); 107 ASSERT_EQ(ArcBridgeService::State::READY, state());
92 108
93 service_->Shutdown(); 109 service_->Shutdown();
94 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 110 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
95 } 111 }
96 112
97 // If the ArcBridgeService is shut down, it should be stopped, even 113 // If the ArcBridgeService is shut down, it should be stopped, even
98 // mid-startup. 114 // mid-startup.
99 TEST_F(ArcBridgeTest, ShutdownMidStartup) { 115 TEST_F(ArcBridgeTest, ShutdownMidStartup) {
100 ASSERT_FALSE(ready()); 116 ASSERT_FALSE(ready());
101 117
118 service_->SetArcBridgeBootstrapFactoryForTesting(
119 base::Bind(ArcBridgeTest::CreateSuspendedBootstrap));
102 service_->HandleStartup(); 120 service_->HandleStartup();
103 // WaitForInitCall() omitted. 121 ASSERT_FALSE(service_->stopped());
104 ASSERT_EQ(ArcBridgeService::State::READY, state()); 122 ASSERT_FALSE(service_->ready());
105 123
106 service_->Shutdown(); 124 service_->Shutdown();
107 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 125 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
108 } 126 }
109 127
128 // If the boot procedure is failed, then restarting mechanism should not
129 // triggered.
130 TEST_F(ArcBridgeTest, BootFailure) {
131 ASSERT_TRUE(service_->stopped());
132
133 service_->SetArcBridgeBootstrapFactoryForTesting(
134 base::Bind(ArcBridgeTest::CreateBootFailureBootstrap,
135 ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE));
136 service_->HandleStartup();
137 EXPECT_EQ(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE, stop_reason_);
138 ASSERT_TRUE(service_->stopped());
139 }
140
110 // If the instance is stopped, it should be re-started. 141 // If the instance is stopped, it should be re-started.
111 TEST_F(ArcBridgeTest, Restart) { 142 TEST_F(ArcBridgeTest, Restart) {
112 ASSERT_FALSE(ready()); 143 ASSERT_FALSE(ready());
113 ASSERT_EQ(0, instance_->init_calls());
114 144
115 service_->HandleStartup(); 145 service_->HandleStartup();
116 instance_->WaitForInitCall();
117 ASSERT_EQ(ArcBridgeService::State::READY, state()); 146 ASSERT_EQ(ArcBridgeService::State::READY, state());
118 ASSERT_EQ(1, instance_->init_calls());
119 147
120 // Simulate a connection loss. 148 // Simulate a connection loss.
121 service_->DisableReconnectDelayForTesting(); 149 service_->DisableReconnectDelayForTesting();
122 instance_->Stop(ArcBridgeService::StopReason::CRASH); 150 ASSERT_TRUE(bootstrap());
123 instance_->WaitForInitCall(); 151 bootstrap()->StopWithReason(ArcBridgeService::StopReason::CRASH);
124 ASSERT_EQ(ArcBridgeService::State::READY, state()); 152 ASSERT_TRUE(service_->ready());
125 ASSERT_EQ(2, instance_->init_calls());
126 153
127 service_->Shutdown(); 154 service_->Shutdown();
128 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 155 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
129 } 156 }
130 157
131 // Makes sure OnBridgeStopped is called on stop. 158 // Makes sure OnBridgeStopped is called on stop.
132 TEST_F(ArcBridgeTest, OnBridgeStopped) { 159 TEST_F(ArcBridgeTest, OnBridgeStopped) {
133 ASSERT_FALSE(ready()); 160 ASSERT_FALSE(ready());
134 161
135 service_->DisableReconnectDelayForTesting(); 162 service_->DisableReconnectDelayForTesting();
136 service_->HandleStartup(); 163 service_->HandleStartup();
137 instance_->WaitForInitCall();
138 ASSERT_EQ(ArcBridgeService::State::READY, state()); 164 ASSERT_EQ(ArcBridgeService::State::READY, state());
139 165
140 // Simulate boot failure. 166 // Simulate boot failure.
141 instance_->Stop(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE); 167 ASSERT_TRUE(bootstrap());
142 instance_->WaitForInitCall(); 168 bootstrap()->StopWithReason(
143 ASSERT_EQ(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE, stop_reason_); 169 ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE);
144 ASSERT_EQ(ArcBridgeService::State::READY, state()); 170 EXPECT_EQ(ArcBridgeService::StopReason::GENERIC_BOOT_FAILURE, stop_reason_);
171 ASSERT_TRUE(service_->ready());
145 172
146 // Simulate crash. 173 // Simulate crash.
147 instance_->Stop(ArcBridgeService::StopReason::CRASH); 174 ASSERT_TRUE(bootstrap());
148 instance_->WaitForInitCall(); 175 bootstrap()->StopWithReason(ArcBridgeService::StopReason::CRASH);
149 ASSERT_EQ(ArcBridgeService::StopReason::CRASH, stop_reason_); 176 EXPECT_EQ(ArcBridgeService::StopReason::CRASH, stop_reason_);
150 ASSERT_EQ(ArcBridgeService::State::READY, state()); 177 ASSERT_TRUE(service_->ready());
151 178
152 // Graceful shutdown. 179 // Graceful shutdown.
153 service_->Shutdown(); 180 service_->Shutdown();
154 ASSERT_EQ(ArcBridgeService::StopReason::SHUTDOWN, stop_reason_); 181 ASSERT_EQ(ArcBridgeService::StopReason::SHUTDOWN, stop_reason_);
155 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 182 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
156 } 183 }
157 184
158 // Removing the same observer more than once should be okay. 185 // Removing the same observer more than once should be okay.
159 TEST_F(ArcBridgeTest, RemoveObserverTwice) { 186 TEST_F(ArcBridgeTest, RemoveObserverTwice) {
160 ASSERT_FALSE(ready()); 187 ASSERT_FALSE(ready());
161 service_->RemoveObserver(this); 188 auto dummy_observer = base::MakeUnique<DummyObserver>();
162 // The teardown method will also remove |this|. 189 service_->AddObserver(dummy_observer.get());
190 // Call RemoveObserver() twice.
191 service_->RemoveObserver(dummy_observer.get());
192 service_->RemoveObserver(dummy_observer.get());
163 } 193 }
164 194
165 // Removing an unknown observer should be allowed. 195 // Removing an unknown observer should be allowed.
166 TEST_F(ArcBridgeTest, RemoveUnknownObserver) { 196 TEST_F(ArcBridgeTest, RemoveUnknownObserver) {
167 ASSERT_FALSE(ready()); 197 ASSERT_FALSE(ready());
168 auto dummy_observer = base::MakeUnique<DummyObserver>(); 198 auto dummy_observer = base::MakeUnique<DummyObserver>();
169 service_->RemoveObserver(dummy_observer.get()); 199 service_->RemoveObserver(dummy_observer.get());
170 } 200 }
171 201
172 } // namespace arc 202 } // namespace arc
OLDNEW
« no previous file with comments | « components/arc/arc_bridge_service_impl.cc ('k') | components/arc/arc_service_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698