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

Side by Side Diff: content/browser/service_worker/embedded_worker_instance_mojo_unittest.cc

Issue 2307543002: ServiceWorker: Mojofication of EWInstance::StopWorker (Closed)
Patch Set: Added a unittest file 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "content/browser/service_worker/embedded_worker_instance.h" 5 #include "content/browser/service_worker/embedded_worker_instance.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "content/browser/service_worker/embedded_worker_registry.h" 15 #include "content/browser/service_worker/embedded_worker_registry.h"
16 #include "content/browser/service_worker/embedded_worker_status.h" 16 #include "content/browser/service_worker/embedded_worker_status.h"
17 #include "content/browser/service_worker/embedded_worker_test_helper.h" 17 #include "content/browser/service_worker/embedded_worker_test_helper.h"
18 #include "content/browser/service_worker/service_worker_context_core.h" 18 #include "content/browser/service_worker/service_worker_context_core.h"
19 #include "content/browser/service_worker/service_worker_context_wrapper.h" 19 #include "content/browser/service_worker/service_worker_context_wrapper.h"
20 #include "content/common/service_worker/embedded_worker.mojom.h" 20 #include "content/common/service_worker/embedded_worker.mojom.h"
21 #include "content/common/service_worker/embedded_worker_messages.h" 21 #include "content/common/service_worker/embedded_worker_messages.h"
22 #include "content/public/common/child_process_host.h" 22 #include "content/public/common/child_process_host.h"
23 #include "content/public/common/content_switches.h" 23 #include "content/public/common/content_switches.h"
24 #include "content/public/test/test_browser_thread_bundle.h" 24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "mojo/public/cpp/bindings/strong_binding.h" 25 #include "mojo/public/cpp/bindings/strong_binding.h"
26 #include "testing/gmock/include/gmock/gmock.h" 26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
28 28
29 using ::testing::_;
29 using ::testing::Eq; 30 using ::testing::Eq;
30 using ::testing::Field; 31 using ::testing::Field;
32 using ::testing::Invoke;
31 using ::testing::Pointee; 33 using ::testing::Pointee;
34 using ::testing::Return;
32 35
33 namespace content { 36 namespace content {
34 37
35 namespace { 38 namespace {
36 39
40 void NoOpStatusCallback(ServiceWorkerStatusCode code) {}
41
37 void SaveStatusAndCall(ServiceWorkerStatusCode* out, 42 void SaveStatusAndCall(ServiceWorkerStatusCode* out,
38 const base::Closure& callback, 43 const base::Closure& callback,
39 ServiceWorkerStatusCode status) { 44 ServiceWorkerStatusCode status) {
40 *out = status; 45 *out = status;
41 callback.Run(); 46 callback.Run();
42 } 47 }
43 48
44 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> 49 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params>
45 CreateStartParams(int version_id, const GURL& scope, const GURL& script_url) { 50 CreateStartParams(int version_id, const GURL& scope, const GURL& script_url) {
46 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( 51 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params(
47 new EmbeddedWorkerMsg_StartWorker_Params); 52 new EmbeddedWorkerMsg_StartWorker_Params);
48 params->service_worker_version_id = version_id; 53 params->service_worker_version_id = version_id;
49 params->scope = scope; 54 params->scope = scope;
50 params->script_url = script_url; 55 params->script_url = script_url;
51 params->pause_after_download = false; 56 params->pause_after_download = false;
52 return params; 57 return params;
53 } 58 }
54 59
55 bool IsMojoForServiceWorkerEnabled() { 60 bool IsMojoForServiceWorkerEnabled() {
56 return base::CommandLine::ForCurrentProcess()->HasSwitch( 61 return base::CommandLine::ForCurrentProcess()->HasSwitch(
57 switches::kMojoServiceWorker); 62 switches::kMojoServiceWorker);
58 } 63 }
59 64
60 } // namespace 65 } // namespace
61 66
62 class EmbeddedWorkerInstanceTest : public testing::Test, 67 class MojoEmbeddedWorkerInstanceTest : public testing::Test,
horo 2016/09/05 08:05:57 You can use TEST_P instead of duplicating tests. S
shimazu 2016/09/06 08:53:59 I didn't know that. Tried it.
63 public EmbeddedWorkerInstance::Listener { 68 public EmbeddedWorkerInstance::Listener {
64 protected: 69 protected:
65 EmbeddedWorkerInstanceTest() 70 MojoEmbeddedWorkerInstanceTest()
66 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} 71 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
67 72
68 enum EventType { 73 enum EventType {
69 PROCESS_ALLOCATED, 74 PROCESS_ALLOCATED,
70 START_WORKER_MESSAGE_SENT, 75 START_WORKER_MESSAGE_SENT,
71 STARTED, 76 STARTED,
72 STOPPED, 77 STOPPED,
73 DETACHED, 78 DETACHED,
74 }; 79 };
75 80
(...skipping 17 matching lines...) Expand all
93 void OnStopped(EmbeddedWorkerStatus old_status) override { 98 void OnStopped(EmbeddedWorkerStatus old_status) override {
94 RecordEvent(STOPPED, old_status); 99 RecordEvent(STOPPED, old_status);
95 } 100 }
96 void OnDetached(EmbeddedWorkerStatus old_status) override { 101 void OnDetached(EmbeddedWorkerStatus old_status) override {
97 RecordEvent(DETACHED, old_status); 102 RecordEvent(DETACHED, old_status);
98 } 103 }
99 104
100 bool OnMessageReceived(const IPC::Message& message) override { return false; } 105 bool OnMessageReceived(const IPC::Message& message) override { return false; }
101 106
102 void SetUp() override { 107 void SetUp() override {
108 base::CommandLine::ForCurrentProcess()->AppendSwitch(
109 switches::kMojoServiceWorker);
103 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); 110 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath()));
104 } 111 }
105 112
106 void TearDown() override { helper_.reset(); } 113 void TearDown() override { helper_.reset(); }
107 114
108 ServiceWorkerStatusCode StartWorker(EmbeddedWorkerInstance* worker, 115 ServiceWorkerStatusCode StartWorker(EmbeddedWorkerInstance* worker,
109 int id, const GURL& pattern, 116 int id,
117 const GURL& pattern,
110 const GURL& url) { 118 const GURL& url) {
111 ServiceWorkerStatusCode status; 119 ServiceWorkerStatusCode status;
112 base::RunLoop run_loop; 120 base::RunLoop run_loop;
113 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params = 121 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params =
114 CreateStartParams(id, pattern, url); 122 CreateStartParams(id, pattern, url);
115 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, 123 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status,
116 run_loop.QuitClosure())); 124 run_loop.QuitClosure()));
117 run_loop.Run(); 125 run_loop.Run();
118 return status; 126 return status;
119 } 127 }
120 128
121 ServiceWorkerContextCore* context() { return helper_->context(); } 129 ServiceWorkerContextCore* context() { return helper_->context(); }
122 130
123 EmbeddedWorkerRegistry* embedded_worker_registry() { 131 EmbeddedWorkerRegistry* embedded_worker_registry() {
124 DCHECK(context()); 132 DCHECK(context());
125 return context()->embedded_worker_registry(); 133 return context()->embedded_worker_registry();
126 } 134 }
127 135
128 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } 136 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); }
129 137
138 std::vector<std::unique_ptr<
139 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient>>*
140 mock_instance_clients() {
141 return helper_->mock_instance_clients();
142 }
143
130 TestBrowserThreadBundle thread_bundle_; 144 TestBrowserThreadBundle thread_bundle_;
131 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; 145 std::unique_ptr<EmbeddedWorkerTestHelper> helper_;
132 std::vector<EventLog> events_; 146 std::vector<EventLog> events_;
133 147
134 private: 148 private:
135 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); 149 DISALLOW_COPY_AND_ASSIGN(MojoEmbeddedWorkerInstanceTest);
136 };
137
138 class MojoEmbeddedWorkerInstanceTest : public EmbeddedWorkerInstanceTest {
139 protected:
140 void SetUp() override {
141 base::CommandLine::ForCurrentProcess()->AppendSwitch(
142 switches::kMojoServiceWorker);
143 EmbeddedWorkerInstanceTest::SetUp();
144 }
145
146 std::vector<std::unique_ptr<
147 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient>>*
148 mock_instance_clients() {
149 return helper_->mock_instance_clients();
150 }
151 }; 150 };
152 151
153 // A helper to simulate the start worker sequence is stalled in a worker 152 // A helper to simulate the start worker sequence is stalled in a worker
154 // process. 153 // process.
155 class StalledInStartWorkerHelper : public EmbeddedWorkerTestHelper { 154 class StalledInStartWorkerHelper : public EmbeddedWorkerTestHelper {
156 public: 155 public:
157 StalledInStartWorkerHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} 156 StalledInStartWorkerHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {}
158 ~StalledInStartWorkerHelper() override{}; 157 ~StalledInStartWorkerHelper() override{};
159 158
160 void OnStartWorker(int embedded_worker_id, 159 void OnStartWorker(int embedded_worker_id,
(...skipping 11 matching lines...) Expand all
172 } 171 }
173 172
174 void set_force_stall_in_start(bool force_stall_in_start) { 173 void set_force_stall_in_start(bool force_stall_in_start) {
175 force_stall_in_start_ = force_stall_in_start; 174 force_stall_in_start_ = force_stall_in_start;
176 } 175 }
177 176
178 private: 177 private:
179 bool force_stall_in_start_ = true; 178 bool force_stall_in_start_ = true;
180 }; 179 };
181 180
182 class FailToSendIPCHelper : public EmbeddedWorkerTestHelper {
183 public:
184 FailToSendIPCHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {}
185 ~FailToSendIPCHelper() override {}
186
187 bool Send(IPC::Message* message) override {
188 delete message;
189 return false;
190 }
191 };
192
193 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) {
194 std::unique_ptr<EmbeddedWorkerInstance> worker =
195 embedded_worker_registry()->CreateWorker();
196 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
197
198 const int64_t service_worker_version_id = 55L;
199 const GURL pattern("http://example.com/");
200 const GURL url("http://example.com/worker.js");
201
202 // Simulate adding one process to the pattern.
203 helper_->SimulateAddProcessToPattern(pattern,
204 helper_->mock_render_process_id());
205
206 // Start should succeed.
207 ServiceWorkerStatusCode status;
208 base::RunLoop run_loop;
209 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params =
210 CreateStartParams(service_worker_version_id, pattern, url);
211 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status,
212 run_loop.QuitClosure()));
213 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status());
214 run_loop.Run();
215 EXPECT_EQ(SERVICE_WORKER_OK, status);
216
217 // The 'WorkerStarted' message should have been sent by
218 // EmbeddedWorkerTestHelper.
219 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status());
220 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id());
221
222 // Stop the worker.
223 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop());
224 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, worker->status());
225 base::RunLoop().RunUntilIdle();
226
227 // The 'WorkerStopped' message should have been sent by
228 // EmbeddedWorkerTestHelper.
229 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
230
231 // Verify that we've sent two messages to start and terminate the worker.
232 ASSERT_TRUE(
233 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID));
234 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching(
235 EmbeddedWorkerMsg_StopWorker::ID));
236 }
237
238 TEST_F(MojoEmbeddedWorkerInstanceTest, StartAndStop) { 181 TEST_F(MojoEmbeddedWorkerInstanceTest, StartAndStop) {
239 using mojom::EmbeddedWorkerStartWorkerParams; 182 using mojom::EmbeddedWorkerStartWorkerParams;
240 183
241 ASSERT_TRUE(IsMojoForServiceWorkerEnabled()); 184 ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
242 185
243 std::unique_ptr<EmbeddedWorkerInstance> worker = 186 std::unique_ptr<EmbeddedWorkerInstance> worker =
244 embedded_worker_registry()->CreateWorker(); 187 embedded_worker_registry()->CreateWorker();
245 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); 188 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
246 189
247 const int64_t service_worker_version_id = 55L; 190 const int64_t service_worker_version_id = 55L;
248 const GURL pattern("http://example.com/"); 191 const GURL pattern("http://example.com/");
249 const GURL url("http://example.com/worker.js"); 192 const GURL url("http://example.com/worker.js");
250 193
251 // Simulate adding one process to the pattern. 194 // Simulate adding one process to the pattern.
252 helper_->SimulateAddProcessToPattern(pattern, 195 helper_->SimulateAddProcessToPattern(pattern,
253 helper_->mock_render_process_id()); 196 helper_->mock_render_process_id());
254 197
255 // Check if StartWorker will be called via mojo IPC. 198 // Check if StartWorker/StopWorker will be called via mojo IPC.
256 helper_->PrepareMockInstanceClients(1); 199 helper_->PrepareMockInstanceClients(1);
257 ASSERT_EQ(mock_instance_clients()->size(), 1UL); 200 ASSERT_EQ(mock_instance_clients()->size(), 1UL);
258 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient* instance_client = 201 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient* instance_client =
259 mock_instance_clients()->at(0).get(); 202 mock_instance_clients()->at(0).get();
260 EXPECT_CALL( 203 EXPECT_CALL(
261 *instance_client, 204 *instance_client,
262 MockStartWorker(AllOf( 205 MockStartWorker(AllOf(
263 Pointee( 206 Pointee(
264 Field(&EmbeddedWorkerStartWorkerParams::service_worker_version_id, 207 Field(&EmbeddedWorkerStartWorkerParams::service_worker_version_id,
265 Eq(service_worker_version_id))), 208 Eq(service_worker_version_id))),
266 Pointee(Field(&EmbeddedWorkerStartWorkerParams::scope, Eq(pattern))), 209 Pointee(Field(&EmbeddedWorkerStartWorkerParams::scope, Eq(pattern))),
267 Pointee( 210 Pointee(
268 Field(&EmbeddedWorkerStartWorkerParams::script_url, Eq(url)))))) 211 Field(&EmbeddedWorkerStartWorkerParams::script_url, Eq(url))))))
269 .Times(1); 212 .Times(1);
213 EXPECT_CALL(*instance_client, MockStopWorker(_)).Times(1);
270 214
271 // Start should succeed. 215 // Start should succeed.
272 ServiceWorkerStatusCode status; 216 ServiceWorkerStatusCode status;
273 base::RunLoop run_loop; 217 base::RunLoop run_loop;
274 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params = 218 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params =
275 CreateStartParams(service_worker_version_id, pattern, url); 219 CreateStartParams(service_worker_version_id, pattern, url);
276 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, 220 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status,
277 run_loop.QuitClosure())); 221 run_loop.QuitClosure()));
278 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status()); 222 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status());
279 run_loop.Run(); 223 run_loop.Run();
280 EXPECT_EQ(SERVICE_WORKER_OK, status); 224 EXPECT_EQ(SERVICE_WORKER_OK, status);
281 225
282 // The 'WorkerStarted' message should have been sent by 226 // The 'WorkerStarted' message should have been sent by
283 // EmbeddedWorkerTestHelper. 227 // EmbeddedWorkerTestHelper.
284 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status()); 228 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status());
285 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id()); 229 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id());
286 230
287 // Stop the worker. 231 // Stop the worker.
288 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); 232 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop());
289 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, worker->status()); 233 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, worker->status());
290 base::RunLoop().RunUntilIdle(); 234 base::RunLoop().RunUntilIdle();
291 235
292 // The 'WorkerStopped' message should have been sent by 236 // Callback passed by mojo IPC should have been called by
293 // EmbeddedWorkerTestHelper. 237 // EmbeddedWorkerTestHelper.
294 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); 238 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
295
296 // Ckeck if StopWorker has been sent via chromium IPC.
297 ASSERT_TRUE(
298 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StopWorker::ID));
299 } 239 }
300 240
301 // Test that a worker that failed twice will use a new render process 241 // Test that a worker that failed twice will use a new render process
302 // on the next attempt. 242 // on the next attempt.
303 TEST_F(EmbeddedWorkerInstanceTest, ForceNewProcess) { 243 TEST_F(MojoEmbeddedWorkerInstanceTest, ForceNewProcess) {
244 ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
245
304 std::unique_ptr<EmbeddedWorkerInstance> worker = 246 std::unique_ptr<EmbeddedWorkerInstance> worker =
305 embedded_worker_registry()->CreateWorker(); 247 embedded_worker_registry()->CreateWorker();
306 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); 248 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
307 249
308 const int64_t service_worker_version_id = 55L; 250 const int64_t service_worker_version_id = 55L;
309 const GURL pattern("http://example.com/"); 251 const GURL pattern("http://example.com/");
310 const GURL url("http://example.com/worker.js"); 252 const GURL url("http://example.com/worker.js");
311 253
312 // Simulate adding one process to the pattern. 254 // Simulate adding one process to the pattern.
313 helper_->SimulateAddProcessToPattern(pattern, 255 helper_->SimulateAddProcessToPattern(pattern,
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 EXPECT_EQ(SERVICE_WORKER_OK, status); 299 EXPECT_EQ(SERVICE_WORKER_OK, status);
358 300
359 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status()); 301 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status());
360 // The worker should be using the new render process. 302 // The worker should be using the new render process.
361 EXPECT_EQ(helper_->new_render_process_id(), worker->process_id()); 303 EXPECT_EQ(helper_->new_render_process_id(), worker->process_id());
362 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); 304 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop());
363 base::RunLoop().RunUntilIdle(); 305 base::RunLoop().RunUntilIdle();
364 } 306 }
365 } 307 }
366 308
367 TEST_F(EmbeddedWorkerInstanceTest, StopWhenDevToolsAttached) { 309 TEST_F(MojoEmbeddedWorkerInstanceTest, StopWhenDevToolsAttached) {
310 ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
311
368 std::unique_ptr<EmbeddedWorkerInstance> worker = 312 std::unique_ptr<EmbeddedWorkerInstance> worker =
369 embedded_worker_registry()->CreateWorker(); 313 embedded_worker_registry()->CreateWorker();
370 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); 314 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
371 315
372 const int64_t service_worker_version_id = 55L; 316 const int64_t service_worker_version_id = 55L;
373 const GURL pattern("http://example.com/"); 317 const GURL pattern("http://example.com/");
374 const GURL url("http://example.com/worker.js"); 318 const GURL url("http://example.com/worker.js");
375 319
376 // Simulate adding one process to the pattern. 320 // Simulate adding one process to the pattern.
377 helper_->SimulateAddProcessToPattern(pattern, 321 helper_->SimulateAddProcessToPattern(pattern,
(...skipping 26 matching lines...) Expand all
404 348
405 // Calling Stop() actually stops the worker regardless of whether devtools 349 // Calling Stop() actually stops the worker regardless of whether devtools
406 // is attached or not. 350 // is attached or not.
407 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); 351 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop());
408 base::RunLoop().RunUntilIdle(); 352 base::RunLoop().RunUntilIdle();
409 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); 353 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
410 } 354 }
411 355
412 // Test that the removal of a worker from the registry doesn't remove 356 // Test that the removal of a worker from the registry doesn't remove
413 // other workers in the same process. 357 // other workers in the same process.
414 TEST_F(EmbeddedWorkerInstanceTest, RemoveWorkerInSharedProcess) { 358 TEST_F(MojoEmbeddedWorkerInstanceTest, RemoveWorkerInSharedProcess) {
359 ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
360
415 std::unique_ptr<EmbeddedWorkerInstance> worker1 = 361 std::unique_ptr<EmbeddedWorkerInstance> worker1 =
416 embedded_worker_registry()->CreateWorker(); 362 embedded_worker_registry()->CreateWorker();
417 std::unique_ptr<EmbeddedWorkerInstance> worker2 = 363 std::unique_ptr<EmbeddedWorkerInstance> worker2 =
418 embedded_worker_registry()->CreateWorker(); 364 embedded_worker_registry()->CreateWorker();
419 365
420 const int64_t version_id1 = 55L; 366 const int64_t version_id1 = 55L;
421 const int64_t version_id2 = 56L; 367 const int64_t version_id2 = 56L;
422 const GURL pattern("http://example.com/"); 368 const GURL pattern("http://example.com/");
423 const GURL url("http://example.com/worker.js"); 369 const GURL url("http://example.com/worker.js");
424 int process_id = helper_->mock_render_process_id(); 370 int process_id = helper_->mock_render_process_id();
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 // Only worker1 should be removed from the registry's process_map. 405 // Only worker1 should be removed from the registry's process_map.
460 EmbeddedWorkerRegistry* registry = 406 EmbeddedWorkerRegistry* registry =
461 helper_->context()->embedded_worker_registry(); 407 helper_->context()->embedded_worker_registry();
462 EXPECT_EQ(0UL, registry->worker_process_map_[process_id].count(worker1_id)); 408 EXPECT_EQ(0UL, registry->worker_process_map_[process_id].count(worker1_id));
463 EXPECT_EQ(1UL, registry->worker_process_map_[process_id].count( 409 EXPECT_EQ(1UL, registry->worker_process_map_[process_id].count(
464 worker2->embedded_worker_id())); 410 worker2->embedded_worker_id()));
465 411
466 worker2->Stop(); 412 worker2->Stop();
467 } 413 }
468 414
469 TEST_F(EmbeddedWorkerInstanceTest, DetachDuringProcessAllocation) { 415 TEST_F(MojoEmbeddedWorkerInstanceTest, DetachDuringProcessAllocation) {
416 ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
417
470 const int64_t version_id = 55L; 418 const int64_t version_id = 55L;
471 const GURL scope("http://example.com/"); 419 const GURL scope("http://example.com/");
472 const GURL url("http://example.com/worker.js"); 420 const GURL url("http://example.com/worker.js");
473 421
474 std::unique_ptr<EmbeddedWorkerInstance> worker = 422 std::unique_ptr<EmbeddedWorkerInstance> worker =
475 embedded_worker_registry()->CreateWorker(); 423 embedded_worker_registry()->CreateWorker();
476 worker->AddListener(this); 424 worker->AddListener(this);
477 425
478 // Run the start worker sequence and detach during process allocation. 426 // Run the start worker sequence and detach during process allocation.
479 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; 427 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE;
(...skipping 10 matching lines...) Expand all
490 // The start callback should not be aborted by detach (see a comment on the 438 // The start callback should not be aborted by detach (see a comment on the
491 // dtor of EmbeddedWorkerInstance::StartTask). 439 // dtor of EmbeddedWorkerInstance::StartTask).
492 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); 440 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status);
493 441
494 // "PROCESS_ALLOCATED" event should not be recorded. 442 // "PROCESS_ALLOCATED" event should not be recorded.
495 ASSERT_EQ(1u, events_.size()); 443 ASSERT_EQ(1u, events_.size());
496 EXPECT_EQ(DETACHED, events_[0].type); 444 EXPECT_EQ(DETACHED, events_[0].type);
497 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[0].status); 445 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[0].status);
498 } 446 }
499 447
500 TEST_F(EmbeddedWorkerInstanceTest, DetachAfterSendingStartWorkerMessage) { 448 TEST_F(MojoEmbeddedWorkerInstanceTest, DetachAfterSendingStartWorkerMessage) {
449 ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
450
501 const int64_t version_id = 55L; 451 const int64_t version_id = 55L;
502 const GURL scope("http://example.com/"); 452 const GURL scope("http://example.com/");
503 const GURL url("http://example.com/worker.js"); 453 const GURL url("http://example.com/worker.js");
504 454
505 helper_.reset(new StalledInStartWorkerHelper()); 455 helper_.reset(new StalledInStartWorkerHelper());
506 std::unique_ptr<EmbeddedWorkerInstance> worker = 456 std::unique_ptr<EmbeddedWorkerInstance> worker =
507 embedded_worker_registry()->CreateWorker(); 457 embedded_worker_registry()->CreateWorker();
508 worker->AddListener(this); 458 worker->AddListener(this);
509 459
510 // Run the start worker sequence until a start worker message is sent. 460 // Run the start worker sequence until a start worker message is sent.
(...skipping 18 matching lines...) Expand all
529 // The start callback should not be aborted by detach (see a comment on the 479 // The start callback should not be aborted by detach (see a comment on the
530 // dtor of EmbeddedWorkerInstance::StartTask). 480 // dtor of EmbeddedWorkerInstance::StartTask).
531 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); 481 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status);
532 482
533 // "STARTED" event should not be recorded. 483 // "STARTED" event should not be recorded.
534 ASSERT_EQ(1u, events_.size()); 484 ASSERT_EQ(1u, events_.size());
535 EXPECT_EQ(DETACHED, events_[0].type); 485 EXPECT_EQ(DETACHED, events_[0].type);
536 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[0].status); 486 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[0].status);
537 } 487 }
538 488
539 TEST_F(EmbeddedWorkerInstanceTest, StopDuringProcessAllocation) { 489 TEST_F(MojoEmbeddedWorkerInstanceTest, StopDuringProcessAllocation) {
490 ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
491
540 const int64_t version_id = 55L; 492 const int64_t version_id = 55L;
541 const GURL scope("http://example.com/"); 493 const GURL scope("http://example.com/");
542 const GURL url("http://example.com/worker.js"); 494 const GURL url("http://example.com/worker.js");
543 495
544 std::unique_ptr<EmbeddedWorkerInstance> worker = 496 std::unique_ptr<EmbeddedWorkerInstance> worker =
545 embedded_worker_registry()->CreateWorker(); 497 embedded_worker_registry()->CreateWorker();
546 worker->AddListener(this); 498 worker->AddListener(this);
547 499
548 // Stop the start worker sequence before a process is allocated. 500 // Stop the start worker sequence before a process is allocated.
549 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; 501 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE;
550 502
551 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( 503 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params(
552 CreateStartParams(version_id, scope, url)); 504 CreateStartParams(version_id, scope, url));
553 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, 505 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status,
554 base::Bind(&base::DoNothing))); 506 base::Bind(&base::DoNothing)));
555 worker->Stop(); 507 worker->Stop();
556 base::RunLoop().RunUntilIdle(); 508 base::RunLoop().RunUntilIdle();
557 509
558 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); 510 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
559 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id()); 511 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id());
560 512
561 // The start callback should not be aborted by stop (see a comment on the dtor 513 // The start callback should not be aborted by stop (see a comment on the dtor
562 // of EmbeddedWorkerInstance::StartTask). 514 // of EmbeddedWorkerInstance::StartTask).
563 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); 515 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status);
564 516
565 // "PROCESS_ALLOCATED" event should not be recorded. 517 // "PROCESS_ALLOCATED" event should not be recorded.
566 ASSERT_EQ(1u, events_.size()); 518 ASSERT_EQ(1u, events_.size());
567 EXPECT_EQ(DETACHED, events_[0].type); 519 EXPECT_EQ(DETACHED, events_[0].type);
568 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[0].status);
569 events_.clear(); 520 events_.clear();
570 521
571 // Restart the worker. 522 // Restart the worker.
572 status = SERVICE_WORKER_ERROR_MAX_VALUE; 523 status = SERVICE_WORKER_ERROR_MAX_VALUE;
573 std::unique_ptr<base::RunLoop> run_loop(new base::RunLoop); 524 std::unique_ptr<base::RunLoop> run_loop(new base::RunLoop);
574 params = CreateStartParams(version_id, scope, url); 525 params = CreateStartParams(version_id, scope, url);
575 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, 526 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status,
576 run_loop->QuitClosure())); 527 run_loop->QuitClosure()));
577 run_loop->Run(); 528 run_loop->Run();
578 529
579 EXPECT_EQ(SERVICE_WORKER_OK, status); 530 EXPECT_EQ(SERVICE_WORKER_OK, status);
580 ASSERT_EQ(3u, events_.size()); 531 ASSERT_EQ(3u, events_.size());
581 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); 532 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type);
582 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); 533 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type);
583 EXPECT_EQ(STARTED, events_[2].type); 534 EXPECT_EQ(STARTED, events_[2].type);
584 535
585 // Tear down the worker. 536 // Tear down the worker.
586 worker->Stop(); 537 worker->Stop();
587 } 538 }
588 539
589 TEST_F(EmbeddedWorkerInstanceTest, StopDuringPausedAfterDownload) { 540 TEST_F(MojoEmbeddedWorkerInstanceTest, StopDuringPausedAfterDownload) {
541 ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
542
590 const int64_t version_id = 55L; 543 const int64_t version_id = 55L;
591 const GURL scope("http://example.com/"); 544 const GURL scope("http://example.com/");
592 const GURL url("http://example.com/worker.js"); 545 const GURL url("http://example.com/worker.js");
593 546
594 std::unique_ptr<EmbeddedWorkerInstance> worker = 547 std::unique_ptr<EmbeddedWorkerInstance> worker =
595 embedded_worker_registry()->CreateWorker(); 548 embedded_worker_registry()->CreateWorker();
596 worker->AddListener(this); 549 worker->AddListener(this);
597 550
598 // Run the start worker sequence until pause after download. 551 // Run the start worker sequence until pause after download.
599 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; 552 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE;
(...skipping 10 matching lines...) Expand all
610 worker->Stop(); 563 worker->Stop();
611 worker->ResumeAfterDownload(); 564 worker->ResumeAfterDownload();
612 base::RunLoop().RunUntilIdle(); 565 base::RunLoop().RunUntilIdle();
613 566
614 // The resume after download message should not have been sent. 567 // The resume after download message should not have been sent.
615 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); 568 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
616 EXPECT_FALSE(ipc_sink()->GetFirstMessageMatching( 569 EXPECT_FALSE(ipc_sink()->GetFirstMessageMatching(
617 EmbeddedWorkerMsg_ResumeAfterDownload::ID)); 570 EmbeddedWorkerMsg_ResumeAfterDownload::ID));
618 } 571 }
619 572
620 TEST_F(EmbeddedWorkerInstanceTest, StopAfterSendingStartWorkerMessage) { 573 TEST_F(MojoEmbeddedWorkerInstanceTest, StopAfterSendingStartWorkerMessage) {
574 ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
575
621 const int64_t version_id = 55L; 576 const int64_t version_id = 55L;
622 const GURL scope("http://example.com/"); 577 const GURL scope("http://example.com/");
623 const GURL url("http://example.com/worker.js"); 578 const GURL url("http://example.com/worker.js");
624 579
625 helper_.reset(new StalledInStartWorkerHelper); 580 helper_.reset(new StalledInStartWorkerHelper);
626 std::unique_ptr<EmbeddedWorkerInstance> worker = 581 std::unique_ptr<EmbeddedWorkerInstance> worker =
627 embedded_worker_registry()->CreateWorker(); 582 embedded_worker_registry()->CreateWorker();
628 worker->AddListener(this); 583 worker->AddListener(this);
629 584
630 // Run the start worker sequence until a start worker message is sent. 585 // Run the start worker sequence until a start worker message is sent.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 EXPECT_EQ(SERVICE_WORKER_OK, status); 626 EXPECT_EQ(SERVICE_WORKER_OK, status);
672 ASSERT_EQ(3u, events_.size()); 627 ASSERT_EQ(3u, events_.size());
673 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); 628 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type);
674 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); 629 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type);
675 EXPECT_EQ(STARTED, events_[2].type); 630 EXPECT_EQ(STARTED, events_[2].type);
676 631
677 // Tear down the worker. 632 // Tear down the worker.
678 worker->Stop(); 633 worker->Stop();
679 } 634 }
680 635
681 TEST_F(EmbeddedWorkerInstanceTest, Detach) { 636 TEST_F(MojoEmbeddedWorkerInstanceTest, Detach) {
637 ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
638
682 const int64_t version_id = 55L; 639 const int64_t version_id = 55L;
683 const GURL pattern("http://example.com/"); 640 const GURL pattern("http://example.com/");
684 const GURL url("http://example.com/worker.js"); 641 const GURL url("http://example.com/worker.js");
685 std::unique_ptr<EmbeddedWorkerInstance> worker = 642 std::unique_ptr<EmbeddedWorkerInstance> worker =
686 embedded_worker_registry()->CreateWorker(); 643 embedded_worker_registry()->CreateWorker();
687 helper_->SimulateAddProcessToPattern(pattern, 644 helper_->SimulateAddProcessToPattern(pattern,
688 helper_->mock_render_process_id()); 645 helper_->mock_render_process_id());
689 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; 646 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
690 worker->AddListener(this); 647 worker->AddListener(this);
691 648
(...skipping 10 matching lines...) Expand all
702 worker->Detach(); 659 worker->Detach();
703 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); 660 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
704 661
705 // Send the registry a message from the detached worker. Nothing should 662 // Send the registry a message from the detached worker. Nothing should
706 // happen. 663 // happen.
707 embedded_worker_registry()->OnWorkerStarted(process_id, 664 embedded_worker_registry()->OnWorkerStarted(process_id,
708 worker->embedded_worker_id()); 665 worker->embedded_worker_id());
709 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); 666 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
710 } 667 }
711 668
712 // Test for when sending the start IPC failed. 669 // Test for when sending the start IPC failed: sudden shutdown of remote process
713 TEST_F(EmbeddedWorkerInstanceTest, FailToSendStartIPC) { 670 TEST_F(MojoEmbeddedWorkerInstanceTest, RemoveRemoteInterface) {
714 helper_.reset(new FailToSendIPCHelper()); 671 ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
715 672
716 const int64_t version_id = 55L; 673 const int64_t version_id = 55L;
717 const GURL pattern("http://example.com/"); 674 const GURL pattern("http://example.com/");
718 const GURL url("http://example.com/worker.js"); 675 const GURL url("http://example.com/worker.js");
719 676
677 // Let StartWorker fail; binding is discarded in the middle of IPC
678 helper_->PrepareMockInstanceClients(1);
679 ASSERT_EQ(mock_instance_clients()->size(), 1UL);
680 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient* instance_client =
681 mock_instance_clients()->at(0).get();
682 EXPECT_CALL(*instance_client, MockStartWorker(_))
683 .WillOnce(WithoutArgs(
684 Invoke([this]() { helper_->mock_instance_clients()->clear(); })));
685
720 std::unique_ptr<EmbeddedWorkerInstance> worker = 686 std::unique_ptr<EmbeddedWorkerInstance> worker =
721 embedded_worker_registry()->CreateWorker(); 687 embedded_worker_registry()->CreateWorker();
722 helper_->SimulateAddProcessToPattern(pattern, 688 helper_->SimulateAddProcessToPattern(pattern,
723 helper_->mock_render_process_id()); 689 helper_->mock_render_process_id());
724 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
725 worker->AddListener(this); 690 worker->AddListener(this);
726 691
727 // Attempt to start the worker. 692 // Attempt to start the worker.
728 base::RunLoop run_loop;
729 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( 693 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params(
730 CreateStartParams(version_id, pattern, url)); 694 CreateStartParams(version_id, pattern, url));
731 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, 695 worker->Start(std::move(params), base::Bind(&NoOpStatusCallback));
732 run_loop.QuitClosure())); 696 base::RunLoop().RunUntilIdle();
733 run_loop.Run();
734 697
735 // The callback should have run, and we should have got an OnStopped message. 698 // Worker should handle the sudden shutdown as detach.
736 EXPECT_EQ(SERVICE_WORKER_ERROR_IPC_FAILED, status); 699 ASSERT_EQ(3u, events_.size());
737 ASSERT_EQ(2u, events_.size());
738 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); 700 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type);
739 EXPECT_EQ(STOPPED, events_[1].type); 701 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type);
740 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[1].status); 702 EXPECT_EQ(DETACHED, events_[2].type);
703 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[2].status);
704 }
705
706 // Test for when sending the start IPC failed: connection failure
707 TEST_F(MojoEmbeddedWorkerInstanceTest, FailToSendStartIPC) {
708 ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
709
710 const int64_t version_id = 55L;
711 const GURL pattern("http://example.com/");
712 const GURL url("http://example.com/worker.js");
713
714 // Let StartWorker fail; mojo IPC fails to connect to a remote interface.
715 helper_->PrepareMockInstanceClients(1);
716 ASSERT_EQ(mock_instance_clients()->size(), 1UL);
717 mock_instance_clients()->at(0).reset();
718
719 std::unique_ptr<EmbeddedWorkerInstance> worker =
720 embedded_worker_registry()->CreateWorker();
721 helper_->SimulateAddProcessToPattern(pattern,
722 helper_->mock_render_process_id());
723 worker->AddListener(this);
724
725 // Attempt to start the worker.
726 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params(
727 CreateStartParams(version_id, pattern, url));
728 worker->Start(std::move(params), base::Bind(&NoOpStatusCallback));
729 base::RunLoop().RunUntilIdle();
730
731 // Worker should handle the failure of binding as detach.
732 ASSERT_EQ(3u, events_.size());
733 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type);
734 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type);
735 EXPECT_EQ(DETACHED, events_[2].type);
736 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[2].status);
741 } 737 }
742 738
743 } // namespace content 739 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698