OLD | NEW |
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 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 STARTED, | 57 STARTED, |
58 STOPPED, | 58 STOPPED, |
59 DETACHED, | 59 DETACHED, |
60 }; | 60 }; |
61 | 61 |
62 struct EventLog { | 62 struct EventLog { |
63 EventType type; | 63 EventType type; |
64 EmbeddedWorkerInstance::Status status; | 64 EmbeddedWorkerInstance::Status status; |
65 }; | 65 }; |
66 | 66 |
67 void RecordEvent( | 67 void RecordEvent(EventType type, |
68 EventType type, | 68 EmbeddedWorkerInstance::Status status = |
69 EmbeddedWorkerInstance::Status status = EmbeddedWorkerInstance::STOPPED) { | 69 EmbeddedWorkerInstance::Status::STOPPED) { |
70 EventLog log = {type, status}; | 70 EventLog log = {type, status}; |
71 events_.push_back(log); | 71 events_.push_back(log); |
72 } | 72 } |
73 | 73 |
74 void OnProcessAllocated() override { RecordEvent(PROCESS_ALLOCATED); } | 74 void OnProcessAllocated() override { RecordEvent(PROCESS_ALLOCATED); } |
75 void OnStartWorkerMessageSent() override { | 75 void OnStartWorkerMessageSent() override { |
76 RecordEvent(START_WORKER_MESSAGE_SENT); | 76 RecordEvent(START_WORKER_MESSAGE_SENT); |
77 } | 77 } |
78 void OnStarted() override { RecordEvent(STARTED); } | 78 void OnStarted() override { RecordEvent(STARTED); } |
79 void OnStopped(EmbeddedWorkerInstance::Status old_status) override { | 79 void OnStopped(EmbeddedWorkerInstance::Status old_status) override { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 | 157 |
158 bool Send(IPC::Message* message) override { | 158 bool Send(IPC::Message* message) override { |
159 delete message; | 159 delete message; |
160 return false; | 160 return false; |
161 } | 161 } |
162 }; | 162 }; |
163 | 163 |
164 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { | 164 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { |
165 std::unique_ptr<EmbeddedWorkerInstance> worker = | 165 std::unique_ptr<EmbeddedWorkerInstance> worker = |
166 embedded_worker_registry()->CreateWorker(); | 166 embedded_worker_registry()->CreateWorker(); |
167 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 167 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPED, worker->status()); |
168 | 168 |
169 const int64_t service_worker_version_id = 55L; | 169 const int64_t service_worker_version_id = 55L; |
170 const GURL pattern("http://example.com/"); | 170 const GURL pattern("http://example.com/"); |
171 const GURL url("http://example.com/worker.js"); | 171 const GURL url("http://example.com/worker.js"); |
172 | 172 |
173 // Simulate adding one process to the pattern. | 173 // Simulate adding one process to the pattern. |
174 helper_->SimulateAddProcessToPattern(pattern, | 174 helper_->SimulateAddProcessToPattern(pattern, |
175 helper_->mock_render_process_id()); | 175 helper_->mock_render_process_id()); |
176 | 176 |
177 // Start should succeed. | 177 // Start should succeed. |
178 ServiceWorkerStatusCode status; | 178 ServiceWorkerStatusCode status; |
179 base::RunLoop run_loop; | 179 base::RunLoop run_loop; |
180 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params = | 180 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params = |
181 CreateStartParams(service_worker_version_id, pattern, url); | 181 CreateStartParams(service_worker_version_id, pattern, url); |
182 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 182 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
183 run_loop.QuitClosure())); | 183 run_loop.QuitClosure())); |
184 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); | 184 EXPECT_EQ(EmbeddedWorkerInstance::Status::STARTING, worker->status()); |
185 run_loop.Run(); | 185 run_loop.Run(); |
186 EXPECT_EQ(SERVICE_WORKER_OK, status); | 186 EXPECT_EQ(SERVICE_WORKER_OK, status); |
187 | 187 |
188 // The 'WorkerStarted' message should have been sent by | 188 // The 'WorkerStarted' message should have been sent by |
189 // EmbeddedWorkerTestHelper. | 189 // EmbeddedWorkerTestHelper. |
190 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 190 EXPECT_EQ(EmbeddedWorkerInstance::Status::RUNNING, worker->status()); |
191 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id()); | 191 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id()); |
192 | 192 |
193 // Stop the worker. | 193 // Stop the worker. |
194 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); | 194 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); |
195 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, worker->status()); | 195 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPING, worker->status()); |
196 base::RunLoop().RunUntilIdle(); | 196 base::RunLoop().RunUntilIdle(); |
197 | 197 |
198 // The 'WorkerStopped' message should have been sent by | 198 // The 'WorkerStopped' message should have been sent by |
199 // EmbeddedWorkerTestHelper. | 199 // EmbeddedWorkerTestHelper. |
200 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 200 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPED, worker->status()); |
201 | 201 |
202 // Verify that we've sent two messages to start and terminate the worker. | 202 // Verify that we've sent two messages to start and terminate the worker. |
203 ASSERT_TRUE( | 203 ASSERT_TRUE( |
204 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID)); | 204 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID)); |
205 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( | 205 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( |
206 EmbeddedWorkerMsg_StopWorker::ID)); | 206 EmbeddedWorkerMsg_StopWorker::ID)); |
207 } | 207 } |
208 | 208 |
209 // Test that a worker that failed twice will use a new render process | 209 // Test that a worker that failed twice will use a new render process |
210 // on the next attempt. | 210 // on the next attempt. |
211 TEST_F(EmbeddedWorkerInstanceTest, ForceNewProcess) { | 211 TEST_F(EmbeddedWorkerInstanceTest, ForceNewProcess) { |
212 std::unique_ptr<EmbeddedWorkerInstance> worker = | 212 std::unique_ptr<EmbeddedWorkerInstance> worker = |
213 embedded_worker_registry()->CreateWorker(); | 213 embedded_worker_registry()->CreateWorker(); |
214 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 214 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPED, worker->status()); |
215 | 215 |
216 const int64_t service_worker_version_id = 55L; | 216 const int64_t service_worker_version_id = 55L; |
217 const GURL pattern("http://example.com/"); | 217 const GURL pattern("http://example.com/"); |
218 const GURL url("http://example.com/worker.js"); | 218 const GURL url("http://example.com/worker.js"); |
219 | 219 |
220 // Simulate adding one process to the pattern. | 220 // Simulate adding one process to the pattern. |
221 helper_->SimulateAddProcessToPattern(pattern, | 221 helper_->SimulateAddProcessToPattern(pattern, |
222 helper_->mock_render_process_id()); | 222 helper_->mock_render_process_id()); |
223 | 223 |
224 // Also simulate adding a "newly created" process to the pattern because | 224 // Also simulate adding a "newly created" process to the pattern because |
225 // unittests can't actually create a new process itself. | 225 // unittests can't actually create a new process itself. |
226 // ServiceWorkerProcessManager only chooses this process id in unittests if | 226 // ServiceWorkerProcessManager only chooses this process id in unittests if |
227 // can_use_existing_process is false. | 227 // can_use_existing_process is false. |
228 helper_->SimulateAddProcessToPattern(pattern, | 228 helper_->SimulateAddProcessToPattern(pattern, |
229 helper_->new_render_process_id()); | 229 helper_->new_render_process_id()); |
230 | 230 |
231 { | 231 { |
232 // Start once normally. | 232 // Start once normally. |
233 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 233 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
234 base::RunLoop run_loop; | 234 base::RunLoop run_loop; |
235 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( | 235 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
236 CreateStartParams(service_worker_version_id, pattern, url)); | 236 CreateStartParams(service_worker_version_id, pattern, url)); |
237 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 237 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
238 run_loop.QuitClosure())); | 238 run_loop.QuitClosure())); |
239 run_loop.Run(); | 239 run_loop.Run(); |
240 EXPECT_EQ(SERVICE_WORKER_OK, status); | 240 EXPECT_EQ(SERVICE_WORKER_OK, status); |
241 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 241 EXPECT_EQ(EmbeddedWorkerInstance::Status::RUNNING, worker->status()); |
242 // The worker should be using the default render process. | 242 // The worker should be using the default render process. |
243 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id()); | 243 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id()); |
244 | 244 |
245 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); | 245 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); |
246 base::RunLoop().RunUntilIdle(); | 246 base::RunLoop().RunUntilIdle(); |
247 } | 247 } |
248 | 248 |
249 // Fail twice. | 249 // Fail twice. |
250 context()->UpdateVersionFailureCount(service_worker_version_id, | 250 context()->UpdateVersionFailureCount(service_worker_version_id, |
251 SERVICE_WORKER_ERROR_FAILED); | 251 SERVICE_WORKER_ERROR_FAILED); |
252 context()->UpdateVersionFailureCount(service_worker_version_id, | 252 context()->UpdateVersionFailureCount(service_worker_version_id, |
253 SERVICE_WORKER_ERROR_FAILED); | 253 SERVICE_WORKER_ERROR_FAILED); |
254 | 254 |
255 { | 255 { |
256 // Start again. | 256 // Start again. |
257 ServiceWorkerStatusCode status; | 257 ServiceWorkerStatusCode status; |
258 base::RunLoop run_loop; | 258 base::RunLoop run_loop; |
259 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( | 259 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
260 CreateStartParams(service_worker_version_id, pattern, url)); | 260 CreateStartParams(service_worker_version_id, pattern, url)); |
261 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 261 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
262 run_loop.QuitClosure())); | 262 run_loop.QuitClosure())); |
263 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); | 263 EXPECT_EQ(EmbeddedWorkerInstance::Status::STARTING, worker->status()); |
264 run_loop.Run(); | 264 run_loop.Run(); |
265 EXPECT_EQ(SERVICE_WORKER_OK, status); | 265 EXPECT_EQ(SERVICE_WORKER_OK, status); |
266 | 266 |
267 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 267 EXPECT_EQ(EmbeddedWorkerInstance::Status::RUNNING, worker->status()); |
268 // The worker should be using the new render process. | 268 // The worker should be using the new render process. |
269 EXPECT_EQ(helper_->new_render_process_id(), worker->process_id()); | 269 EXPECT_EQ(helper_->new_render_process_id(), worker->process_id()); |
270 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); | 270 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); |
271 base::RunLoop().RunUntilIdle(); | 271 base::RunLoop().RunUntilIdle(); |
272 } | 272 } |
273 } | 273 } |
274 | 274 |
275 TEST_F(EmbeddedWorkerInstanceTest, StopWhenDevToolsAttached) { | 275 TEST_F(EmbeddedWorkerInstanceTest, StopWhenDevToolsAttached) { |
276 std::unique_ptr<EmbeddedWorkerInstance> worker = | 276 std::unique_ptr<EmbeddedWorkerInstance> worker = |
277 embedded_worker_registry()->CreateWorker(); | 277 embedded_worker_registry()->CreateWorker(); |
278 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 278 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPED, worker->status()); |
279 | 279 |
280 const int64_t service_worker_version_id = 55L; | 280 const int64_t service_worker_version_id = 55L; |
281 const GURL pattern("http://example.com/"); | 281 const GURL pattern("http://example.com/"); |
282 const GURL url("http://example.com/worker.js"); | 282 const GURL url("http://example.com/worker.js"); |
283 | 283 |
284 // Simulate adding one process to the pattern. | 284 // Simulate adding one process to the pattern. |
285 helper_->SimulateAddProcessToPattern(pattern, | 285 helper_->SimulateAddProcessToPattern(pattern, |
286 helper_->mock_render_process_id()); | 286 helper_->mock_render_process_id()); |
287 | 287 |
288 // Start the worker and then call StopIfIdle(). | 288 // Start the worker and then call StopIfIdle(). |
289 EXPECT_EQ(SERVICE_WORKER_OK, | 289 EXPECT_EQ(SERVICE_WORKER_OK, |
290 StartWorker(worker.get(), service_worker_version_id, pattern, url)); | 290 StartWorker(worker.get(), service_worker_version_id, pattern, url)); |
291 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 291 EXPECT_EQ(EmbeddedWorkerInstance::Status::RUNNING, worker->status()); |
292 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id()); | 292 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id()); |
293 worker->StopIfIdle(); | 293 worker->StopIfIdle(); |
294 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, worker->status()); | 294 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPING, worker->status()); |
295 base::RunLoop().RunUntilIdle(); | 295 base::RunLoop().RunUntilIdle(); |
296 | 296 |
297 // The worker must be stopped now. | 297 // The worker must be stopped now. |
298 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 298 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPED, worker->status()); |
299 | 299 |
300 // Set devtools_attached to true, and do the same. | 300 // Set devtools_attached to true, and do the same. |
301 worker->set_devtools_attached(true); | 301 worker->set_devtools_attached(true); |
302 | 302 |
303 EXPECT_EQ(SERVICE_WORKER_OK, | 303 EXPECT_EQ(SERVICE_WORKER_OK, |
304 StartWorker(worker.get(), service_worker_version_id, pattern, url)); | 304 StartWorker(worker.get(), service_worker_version_id, pattern, url)); |
305 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 305 EXPECT_EQ(EmbeddedWorkerInstance::Status::RUNNING, worker->status()); |
306 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id()); | 306 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id()); |
307 worker->StopIfIdle(); | 307 worker->StopIfIdle(); |
308 base::RunLoop().RunUntilIdle(); | 308 base::RunLoop().RunUntilIdle(); |
309 | 309 |
310 // The worker must not be stopped this time. | 310 // The worker must not be stopped this time. |
311 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 311 EXPECT_EQ(EmbeddedWorkerInstance::Status::RUNNING, worker->status()); |
312 | 312 |
313 // Calling Stop() actually stops the worker regardless of whether devtools | 313 // Calling Stop() actually stops the worker regardless of whether devtools |
314 // is attached or not. | 314 // is attached or not. |
315 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); | 315 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); |
316 base::RunLoop().RunUntilIdle(); | 316 base::RunLoop().RunUntilIdle(); |
317 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 317 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPED, worker->status()); |
318 } | 318 } |
319 | 319 |
320 // Test that the removal of a worker from the registry doesn't remove | 320 // Test that the removal of a worker from the registry doesn't remove |
321 // other workers in the same process. | 321 // other workers in the same process. |
322 TEST_F(EmbeddedWorkerInstanceTest, RemoveWorkerInSharedProcess) { | 322 TEST_F(EmbeddedWorkerInstanceTest, RemoveWorkerInSharedProcess) { |
323 std::unique_ptr<EmbeddedWorkerInstance> worker1 = | 323 std::unique_ptr<EmbeddedWorkerInstance> worker1 = |
324 embedded_worker_registry()->CreateWorker(); | 324 embedded_worker_registry()->CreateWorker(); |
325 std::unique_ptr<EmbeddedWorkerInstance> worker2 = | 325 std::unique_ptr<EmbeddedWorkerInstance> worker2 = |
326 embedded_worker_registry()->CreateWorker(); | 326 embedded_worker_registry()->CreateWorker(); |
327 | 327 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 | 385 |
386 // Run the start worker sequence and detach during process allocation. | 386 // Run the start worker sequence and detach during process allocation. |
387 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 387 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
388 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( | 388 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
389 CreateStartParams(version_id, scope, url)); | 389 CreateStartParams(version_id, scope, url)); |
390 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 390 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
391 base::Bind(&base::DoNothing))); | 391 base::Bind(&base::DoNothing))); |
392 worker->Detach(); | 392 worker->Detach(); |
393 base::RunLoop().RunUntilIdle(); | 393 base::RunLoop().RunUntilIdle(); |
394 | 394 |
395 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 395 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPED, worker->status()); |
396 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id()); | 396 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id()); |
397 | 397 |
398 // The start callback should not be aborted by detach (see a comment on the | 398 // The start callback should not be aborted by detach (see a comment on the |
399 // dtor of EmbeddedWorkerInstance::StartTask). | 399 // dtor of EmbeddedWorkerInstance::StartTask). |
400 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); | 400 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); |
401 | 401 |
402 // "PROCESS_ALLOCATED" event should not be recorded. | 402 // "PROCESS_ALLOCATED" event should not be recorded. |
403 ASSERT_EQ(1u, events_.size()); | 403 ASSERT_EQ(1u, events_.size()); |
404 EXPECT_EQ(DETACHED, events_[0].type); | 404 EXPECT_EQ(DETACHED, events_[0].type); |
405 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, events_[0].status); | 405 EXPECT_EQ(EmbeddedWorkerInstance::Status::STARTING, events_[0].status); |
406 } | 406 } |
407 | 407 |
408 TEST_F(EmbeddedWorkerInstanceTest, DetachAfterSendingStartWorkerMessage) { | 408 TEST_F(EmbeddedWorkerInstanceTest, DetachAfterSendingStartWorkerMessage) { |
409 const int64_t version_id = 55L; | 409 const int64_t version_id = 55L; |
410 const GURL scope("http://example.com/"); | 410 const GURL scope("http://example.com/"); |
411 const GURL url("http://example.com/worker.js"); | 411 const GURL url("http://example.com/worker.js"); |
412 | 412 |
413 helper_.reset(new StalledInStartWorkerHelper()); | 413 helper_.reset(new StalledInStartWorkerHelper()); |
414 std::unique_ptr<EmbeddedWorkerInstance> worker = | 414 std::unique_ptr<EmbeddedWorkerInstance> worker = |
415 embedded_worker_registry()->CreateWorker(); | 415 embedded_worker_registry()->CreateWorker(); |
416 worker->AddListener(this); | 416 worker->AddListener(this); |
417 | 417 |
418 // Run the start worker sequence until a start worker message is sent. | 418 // Run the start worker sequence until a start worker message is sent. |
419 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 419 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
420 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( | 420 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
421 CreateStartParams(version_id, scope, url)); | 421 CreateStartParams(version_id, scope, url)); |
422 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 422 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
423 base::Bind(&base::DoNothing))); | 423 base::Bind(&base::DoNothing))); |
424 base::RunLoop().RunUntilIdle(); | 424 base::RunLoop().RunUntilIdle(); |
425 | 425 |
426 ASSERT_EQ(2u, events_.size()); | 426 ASSERT_EQ(2u, events_.size()); |
427 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); | 427 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); |
428 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); | 428 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); |
429 events_.clear(); | 429 events_.clear(); |
430 | 430 |
431 worker->Detach(); | 431 worker->Detach(); |
432 base::RunLoop().RunUntilIdle(); | 432 base::RunLoop().RunUntilIdle(); |
433 | 433 |
434 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 434 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPED, worker->status()); |
435 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id()); | 435 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id()); |
436 | 436 |
437 // The start callback should not be aborted by detach (see a comment on the | 437 // The start callback should not be aborted by detach (see a comment on the |
438 // dtor of EmbeddedWorkerInstance::StartTask). | 438 // dtor of EmbeddedWorkerInstance::StartTask). |
439 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); | 439 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); |
440 | 440 |
441 // "STARTED" event should not be recorded. | 441 // "STARTED" event should not be recorded. |
442 ASSERT_EQ(1u, events_.size()); | 442 ASSERT_EQ(1u, events_.size()); |
443 EXPECT_EQ(DETACHED, events_[0].type); | 443 EXPECT_EQ(DETACHED, events_[0].type); |
444 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, events_[0].status); | 444 EXPECT_EQ(EmbeddedWorkerInstance::Status::STARTING, events_[0].status); |
445 } | 445 } |
446 | 446 |
447 TEST_F(EmbeddedWorkerInstanceTest, StopDuringProcessAllocation) { | 447 TEST_F(EmbeddedWorkerInstanceTest, StopDuringProcessAllocation) { |
448 const int64_t version_id = 55L; | 448 const int64_t version_id = 55L; |
449 const GURL scope("http://example.com/"); | 449 const GURL scope("http://example.com/"); |
450 const GURL url("http://example.com/worker.js"); | 450 const GURL url("http://example.com/worker.js"); |
451 | 451 |
452 std::unique_ptr<EmbeddedWorkerInstance> worker = | 452 std::unique_ptr<EmbeddedWorkerInstance> worker = |
453 embedded_worker_registry()->CreateWorker(); | 453 embedded_worker_registry()->CreateWorker(); |
454 worker->AddListener(this); | 454 worker->AddListener(this); |
455 | 455 |
456 // Stop the start worker sequence before a process is allocated. | 456 // Stop the start worker sequence before a process is allocated. |
457 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 457 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
458 | 458 |
459 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( | 459 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
460 CreateStartParams(version_id, scope, url)); | 460 CreateStartParams(version_id, scope, url)); |
461 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 461 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
462 base::Bind(&base::DoNothing))); | 462 base::Bind(&base::DoNothing))); |
463 worker->Stop(); | 463 worker->Stop(); |
464 base::RunLoop().RunUntilIdle(); | 464 base::RunLoop().RunUntilIdle(); |
465 | 465 |
466 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 466 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPED, worker->status()); |
467 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id()); | 467 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id()); |
468 | 468 |
469 // The start callback should not be aborted by stop (see a comment on the dtor | 469 // The start callback should not be aborted by stop (see a comment on the dtor |
470 // of EmbeddedWorkerInstance::StartTask). | 470 // of EmbeddedWorkerInstance::StartTask). |
471 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); | 471 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); |
472 | 472 |
473 // "PROCESS_ALLOCATED" event should not be recorded. | 473 // "PROCESS_ALLOCATED" event should not be recorded. |
474 ASSERT_EQ(1u, events_.size()); | 474 ASSERT_EQ(1u, events_.size()); |
475 EXPECT_EQ(DETACHED, events_[0].type); | 475 EXPECT_EQ(DETACHED, events_[0].type); |
476 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, events_[0].status); | 476 EXPECT_EQ(EmbeddedWorkerInstance::Status::STARTING, events_[0].status); |
477 events_.clear(); | 477 events_.clear(); |
478 | 478 |
479 // Restart the worker. | 479 // Restart the worker. |
480 status = SERVICE_WORKER_ERROR_MAX_VALUE; | 480 status = SERVICE_WORKER_ERROR_MAX_VALUE; |
481 std::unique_ptr<base::RunLoop> run_loop(new base::RunLoop); | 481 std::unique_ptr<base::RunLoop> run_loop(new base::RunLoop); |
482 params = CreateStartParams(version_id, scope, url); | 482 params = CreateStartParams(version_id, scope, url); |
483 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 483 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
484 run_loop->QuitClosure())); | 484 run_loop->QuitClosure())); |
485 run_loop->Run(); | 485 run_loop->Run(); |
486 | 486 |
(...skipping 26 matching lines...) Expand all Loading... |
513 base::Bind(&base::DoNothing))); | 513 base::Bind(&base::DoNothing))); |
514 base::RunLoop().RunUntilIdle(); | 514 base::RunLoop().RunUntilIdle(); |
515 | 515 |
516 // Make the worker stopping and attempt to send a resume after download | 516 // Make the worker stopping and attempt to send a resume after download |
517 // message. | 517 // message. |
518 worker->Stop(); | 518 worker->Stop(); |
519 worker->ResumeAfterDownload(); | 519 worker->ResumeAfterDownload(); |
520 base::RunLoop().RunUntilIdle(); | 520 base::RunLoop().RunUntilIdle(); |
521 | 521 |
522 // The resume after download message should not have been sent. | 522 // The resume after download message should not have been sent. |
523 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 523 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPED, worker->status()); |
524 EXPECT_FALSE(ipc_sink()->GetFirstMessageMatching( | 524 EXPECT_FALSE(ipc_sink()->GetFirstMessageMatching( |
525 EmbeddedWorkerMsg_ResumeAfterDownload::ID)); | 525 EmbeddedWorkerMsg_ResumeAfterDownload::ID)); |
526 } | 526 } |
527 | 527 |
528 TEST_F(EmbeddedWorkerInstanceTest, StopAfterSendingStartWorkerMessage) { | 528 TEST_F(EmbeddedWorkerInstanceTest, StopAfterSendingStartWorkerMessage) { |
529 const int64_t version_id = 55L; | 529 const int64_t version_id = 55L; |
530 const GURL scope("http://example.com/"); | 530 const GURL scope("http://example.com/"); |
531 const GURL url("http://example.com/worker.js"); | 531 const GURL url("http://example.com/worker.js"); |
532 | 532 |
533 helper_.reset(new StalledInStartWorkerHelper); | 533 helper_.reset(new StalledInStartWorkerHelper); |
(...skipping 10 matching lines...) Expand all Loading... |
544 base::RunLoop().RunUntilIdle(); | 544 base::RunLoop().RunUntilIdle(); |
545 | 545 |
546 ASSERT_EQ(2u, events_.size()); | 546 ASSERT_EQ(2u, events_.size()); |
547 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); | 547 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); |
548 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); | 548 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); |
549 events_.clear(); | 549 events_.clear(); |
550 | 550 |
551 worker->Stop(); | 551 worker->Stop(); |
552 base::RunLoop().RunUntilIdle(); | 552 base::RunLoop().RunUntilIdle(); |
553 | 553 |
554 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 554 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPED, worker->status()); |
555 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id()); | 555 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id()); |
556 | 556 |
557 // The start callback should not be aborted by stop (see a comment on the dtor | 557 // The start callback should not be aborted by stop (see a comment on the dtor |
558 // of EmbeddedWorkerInstance::StartTask). | 558 // of EmbeddedWorkerInstance::StartTask). |
559 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); | 559 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); |
560 | 560 |
561 // "STARTED" event should not be recorded. | 561 // "STARTED" event should not be recorded. |
562 ASSERT_EQ(1u, events_.size()); | 562 ASSERT_EQ(1u, events_.size()); |
563 EXPECT_EQ(STOPPED, events_[0].type); | 563 EXPECT_EQ(STOPPED, events_[0].type); |
564 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, events_[0].status); | 564 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPING, events_[0].status); |
565 events_.clear(); | 565 events_.clear(); |
566 | 566 |
567 // Restart the worker. | 567 // Restart the worker. |
568 static_cast<StalledInStartWorkerHelper*>(helper_.get()) | 568 static_cast<StalledInStartWorkerHelper*>(helper_.get()) |
569 ->set_force_stall_in_start(false); | 569 ->set_force_stall_in_start(false); |
570 status = SERVICE_WORKER_ERROR_MAX_VALUE; | 570 status = SERVICE_WORKER_ERROR_MAX_VALUE; |
571 std::unique_ptr<base::RunLoop> run_loop(new base::RunLoop); | 571 std::unique_ptr<base::RunLoop> run_loop(new base::RunLoop); |
572 | 572 |
573 params = CreateStartParams(version_id, scope, url); | 573 params = CreateStartParams(version_id, scope, url); |
574 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 574 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
(...skipping 26 matching lines...) Expand all Loading... |
601 base::RunLoop run_loop; | 601 base::RunLoop run_loop; |
602 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( | 602 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
603 CreateStartParams(version_id, pattern, url)); | 603 CreateStartParams(version_id, pattern, url)); |
604 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 604 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
605 run_loop.QuitClosure())); | 605 run_loop.QuitClosure())); |
606 run_loop.Run(); | 606 run_loop.Run(); |
607 | 607 |
608 // Detach. | 608 // Detach. |
609 int process_id = worker->process_id(); | 609 int process_id = worker->process_id(); |
610 worker->Detach(); | 610 worker->Detach(); |
611 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 611 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPED, worker->status()); |
612 | 612 |
613 // Send the registry a message from the detached worker. Nothing should | 613 // Send the registry a message from the detached worker. Nothing should |
614 // happen. | 614 // happen. |
615 embedded_worker_registry()->OnWorkerStarted(process_id, | 615 embedded_worker_registry()->OnWorkerStarted(process_id, |
616 worker->embedded_worker_id()); | 616 worker->embedded_worker_id()); |
617 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 617 EXPECT_EQ(EmbeddedWorkerInstance::Status::STOPPED, worker->status()); |
618 } | 618 } |
619 | 619 |
620 // Test for when sending the start IPC failed. | 620 // Test for when sending the start IPC failed. |
621 TEST_F(EmbeddedWorkerInstanceTest, FailToSendStartIPC) { | 621 TEST_F(EmbeddedWorkerInstanceTest, FailToSendStartIPC) { |
622 helper_.reset(new FailToSendIPCHelper()); | 622 helper_.reset(new FailToSendIPCHelper()); |
623 | 623 |
624 const int64_t version_id = 55L; | 624 const int64_t version_id = 55L; |
625 const GURL pattern("http://example.com/"); | 625 const GURL pattern("http://example.com/"); |
626 const GURL url("http://example.com/worker.js"); | 626 const GURL url("http://example.com/worker.js"); |
627 | 627 |
(...skipping 10 matching lines...) Expand all Loading... |
638 CreateStartParams(version_id, pattern, url)); | 638 CreateStartParams(version_id, pattern, url)); |
639 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | 639 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, |
640 run_loop.QuitClosure())); | 640 run_loop.QuitClosure())); |
641 run_loop.Run(); | 641 run_loop.Run(); |
642 | 642 |
643 // The callback should have run, and we should have got an OnStopped message. | 643 // The callback should have run, and we should have got an OnStopped message. |
644 EXPECT_EQ(SERVICE_WORKER_ERROR_IPC_FAILED, status); | 644 EXPECT_EQ(SERVICE_WORKER_ERROR_IPC_FAILED, status); |
645 ASSERT_EQ(2u, events_.size()); | 645 ASSERT_EQ(2u, events_.size()); |
646 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); | 646 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); |
647 EXPECT_EQ(STOPPED, events_[1].type); | 647 EXPECT_EQ(STOPPED, events_[1].type); |
648 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, events_[1].status); | 648 EXPECT_EQ(EmbeddedWorkerInstance::Status::STARTING, events_[1].status); |
649 } | 649 } |
650 | 650 |
651 } // namespace content | 651 } // namespace content |
OLD | NEW |