| 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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 std::vector<mojom::ServiceWorkerEventDispatcherPtr> dispatchers_; | 139 std::vector<mojom::ServiceWorkerEventDispatcherPtr> dispatchers_; |
| 140 | 140 |
| 141 TestBrowserThreadBundle thread_bundle_; | 141 TestBrowserThreadBundle thread_bundle_; |
| 142 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 142 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
| 143 std::vector<EventLog> events_; | 143 std::vector<EventLog> events_; |
| 144 | 144 |
| 145 private: | 145 private: |
| 146 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); | 146 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); |
| 147 }; | 147 }; |
| 148 | 148 |
| 149 class EmbeddedWorkerInstanceTestP | |
| 150 : public MojoServiceWorkerTestP<EmbeddedWorkerInstanceTest> {}; | |
| 151 | |
| 152 // A helper to simulate the start worker sequence is stalled in a worker | 149 // A helper to simulate the start worker sequence is stalled in a worker |
| 153 // process. | 150 // process. |
| 154 class StalledInStartWorkerHelper : public EmbeddedWorkerTestHelper { | 151 class StalledInStartWorkerHelper : public EmbeddedWorkerTestHelper { |
| 155 public: | 152 public: |
| 156 StalledInStartWorkerHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} | 153 StalledInStartWorkerHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} |
| 157 ~StalledInStartWorkerHelper() override{}; | 154 ~StalledInStartWorkerHelper() override{}; |
| 158 | 155 |
| 159 void OnStartWorker(int embedded_worker_id, | 156 void OnStartWorker(int embedded_worker_id, |
| 160 int64_t service_worker_version_id, | 157 int64_t service_worker_version_id, |
| 161 const GURL& scope, | 158 const GURL& scope, |
| 162 const GURL& script_url, | 159 const GURL& script_url, |
| 163 bool pause_after_download) override { | 160 bool pause_after_download) override { |
| 164 if (force_stall_in_start_) { | 161 if (force_stall_in_start_) { |
| 165 // Do nothing to simulate a stall in the worker process. | 162 // Do nothing to simulate a stall in the worker process. |
| 166 return; | 163 return; |
| 167 } | 164 } |
| 168 EmbeddedWorkerTestHelper::OnStartWorker(embedded_worker_id, | 165 EmbeddedWorkerTestHelper::OnStartWorker(embedded_worker_id, |
| 169 service_worker_version_id, scope, | 166 service_worker_version_id, scope, |
| 170 script_url, pause_after_download); | 167 script_url, pause_after_download); |
| 171 } | 168 } |
| 172 | 169 |
| 173 void set_force_stall_in_start(bool force_stall_in_start) { | 170 void set_force_stall_in_start(bool force_stall_in_start) { |
| 174 force_stall_in_start_ = force_stall_in_start; | 171 force_stall_in_start_ = force_stall_in_start; |
| 175 } | 172 } |
| 176 | 173 |
| 177 private: | 174 private: |
| 178 bool force_stall_in_start_ = true; | 175 bool force_stall_in_start_ = true; |
| 179 }; | 176 }; |
| 180 | 177 |
| 181 class FailToSendIPCHelper : public EmbeddedWorkerTestHelper { | 178 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { |
| 182 public: | |
| 183 FailToSendIPCHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} | |
| 184 ~FailToSendIPCHelper() override {} | |
| 185 | |
| 186 bool Send(IPC::Message* message) override { | |
| 187 delete message; | |
| 188 return false; | |
| 189 } | |
| 190 }; | |
| 191 | |
| 192 TEST_P(EmbeddedWorkerInstanceTestP, StartAndStop) { | |
| 193 std::unique_ptr<EmbeddedWorkerInstance> worker = | 179 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 194 embedded_worker_registry()->CreateWorker(); | 180 embedded_worker_registry()->CreateWorker(); |
| 195 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 181 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 196 worker->AddListener(this); | 182 worker->AddListener(this); |
| 197 | 183 |
| 198 const int64_t service_worker_version_id = 55L; | 184 const int64_t service_worker_version_id = 55L; |
| 199 const GURL pattern("http://example.com/"); | 185 const GURL pattern("http://example.com/"); |
| 200 const GURL url("http://example.com/worker.js"); | 186 const GURL url("http://example.com/worker.js"); |
| 201 | 187 |
| 202 // Simulate adding one process to the pattern. | 188 // Simulate adding one process to the pattern. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 222 | 208 |
| 223 // Stop the worker. | 209 // Stop the worker. |
| 224 EXPECT_TRUE(worker->Stop()); | 210 EXPECT_TRUE(worker->Stop()); |
| 225 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, worker->status()); | 211 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, worker->status()); |
| 226 base::RunLoop().RunUntilIdle(); | 212 base::RunLoop().RunUntilIdle(); |
| 227 | 213 |
| 228 // The 'WorkerStopped' message should have been sent by | 214 // The 'WorkerStopped' message should have been sent by |
| 229 // EmbeddedWorkerTestHelper. | 215 // EmbeddedWorkerTestHelper. |
| 230 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 216 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 231 | 217 |
| 232 if (!is_mojo_enabled()) { | |
| 233 // Verify that we've sent two messages via chromium IPC to start and | |
| 234 // terminate the worker. | |
| 235 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( | |
| 236 EmbeddedWorkerMsg_StartWorker::ID)); | |
| 237 ASSERT_TRUE( | |
| 238 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StopWorker::ID)); | |
| 239 } | |
| 240 | |
| 241 // Check if the IPCs are fired in expected order. | 218 // Check if the IPCs are fired in expected order. |
| 242 ASSERT_EQ(4u, events_.size()); | 219 ASSERT_EQ(4u, events_.size()); |
| 243 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); | 220 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); |
| 244 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); | 221 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); |
| 245 EXPECT_EQ(STARTED, events_[2].type); | 222 EXPECT_EQ(STARTED, events_[2].type); |
| 246 EXPECT_EQ(STOPPED, events_[3].type); | 223 EXPECT_EQ(STOPPED, events_[3].type); |
| 247 } | 224 } |
| 248 | 225 |
| 249 // Test that a worker that failed twice will use a new render process | 226 // Test that a worker that failed twice will use a new render process |
| 250 // on the next attempt. | 227 // on the next attempt. |
| 251 TEST_P(EmbeddedWorkerInstanceTestP, ForceNewProcess) { | 228 TEST_F(EmbeddedWorkerInstanceTest, ForceNewProcess) { |
| 252 std::unique_ptr<EmbeddedWorkerInstance> worker = | 229 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 253 embedded_worker_registry()->CreateWorker(); | 230 embedded_worker_registry()->CreateWorker(); |
| 254 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 231 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 255 | 232 |
| 256 const int64_t service_worker_version_id = 55L; | 233 const int64_t service_worker_version_id = 55L; |
| 257 const GURL pattern("http://example.com/"); | 234 const GURL pattern("http://example.com/"); |
| 258 const GURL url("http://example.com/worker.js"); | 235 const GURL url("http://example.com/worker.js"); |
| 259 | 236 |
| 260 // Simulate adding one process to the pattern. | 237 // Simulate adding one process to the pattern. |
| 261 helper_->SimulateAddProcessToPattern(pattern, | 238 helper_->SimulateAddProcessToPattern(pattern, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 EXPECT_EQ(SERVICE_WORKER_OK, status); | 284 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 308 | 285 |
| 309 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status()); | 286 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status()); |
| 310 // The worker should be using the new render process. | 287 // The worker should be using the new render process. |
| 311 EXPECT_EQ(helper_->new_render_process_id(), worker->process_id()); | 288 EXPECT_EQ(helper_->new_render_process_id(), worker->process_id()); |
| 312 EXPECT_TRUE(worker->Stop()); | 289 EXPECT_TRUE(worker->Stop()); |
| 313 base::RunLoop().RunUntilIdle(); | 290 base::RunLoop().RunUntilIdle(); |
| 314 } | 291 } |
| 315 } | 292 } |
| 316 | 293 |
| 317 TEST_P(EmbeddedWorkerInstanceTestP, StopWhenDevToolsAttached) { | 294 TEST_F(EmbeddedWorkerInstanceTest, StopWhenDevToolsAttached) { |
| 318 std::unique_ptr<EmbeddedWorkerInstance> worker = | 295 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 319 embedded_worker_registry()->CreateWorker(); | 296 embedded_worker_registry()->CreateWorker(); |
| 320 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 297 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 321 | 298 |
| 322 const int64_t service_worker_version_id = 55L; | 299 const int64_t service_worker_version_id = 55L; |
| 323 const GURL pattern("http://example.com/"); | 300 const GURL pattern("http://example.com/"); |
| 324 const GURL url("http://example.com/worker.js"); | 301 const GURL url("http://example.com/worker.js"); |
| 325 | 302 |
| 326 // Simulate adding one process to the pattern. | 303 // Simulate adding one process to the pattern. |
| 327 helper_->SimulateAddProcessToPattern(pattern, | 304 helper_->SimulateAddProcessToPattern(pattern, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 354 | 331 |
| 355 // Calling Stop() actually stops the worker regardless of whether devtools | 332 // Calling Stop() actually stops the worker regardless of whether devtools |
| 356 // is attached or not. | 333 // is attached or not. |
| 357 EXPECT_TRUE(worker->Stop()); | 334 EXPECT_TRUE(worker->Stop()); |
| 358 base::RunLoop().RunUntilIdle(); | 335 base::RunLoop().RunUntilIdle(); |
| 359 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 336 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 360 } | 337 } |
| 361 | 338 |
| 362 // Test that the removal of a worker from the registry doesn't remove | 339 // Test that the removal of a worker from the registry doesn't remove |
| 363 // other workers in the same process. | 340 // other workers in the same process. |
| 364 TEST_P(EmbeddedWorkerInstanceTestP, RemoveWorkerInSharedProcess) { | 341 TEST_F(EmbeddedWorkerInstanceTest, RemoveWorkerInSharedProcess) { |
| 365 std::unique_ptr<EmbeddedWorkerInstance> worker1 = | 342 std::unique_ptr<EmbeddedWorkerInstance> worker1 = |
| 366 embedded_worker_registry()->CreateWorker(); | 343 embedded_worker_registry()->CreateWorker(); |
| 367 std::unique_ptr<EmbeddedWorkerInstance> worker2 = | 344 std::unique_ptr<EmbeddedWorkerInstance> worker2 = |
| 368 embedded_worker_registry()->CreateWorker(); | 345 embedded_worker_registry()->CreateWorker(); |
| 369 | 346 |
| 370 const int64_t version_id1 = 55L; | 347 const int64_t version_id1 = 55L; |
| 371 const int64_t version_id2 = 56L; | 348 const int64_t version_id2 = 56L; |
| 372 const GURL pattern("http://example.com/"); | 349 const GURL pattern("http://example.com/"); |
| 373 const GURL url("http://example.com/worker.js"); | 350 const GURL url("http://example.com/worker.js"); |
| 374 int process_id = helper_->mock_render_process_id(); | 351 int process_id = helper_->mock_render_process_id(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 // Only worker1 should be removed from the registry's process_map. | 388 // Only worker1 should be removed from the registry's process_map. |
| 412 EmbeddedWorkerRegistry* registry = | 389 EmbeddedWorkerRegistry* registry = |
| 413 helper_->context()->embedded_worker_registry(); | 390 helper_->context()->embedded_worker_registry(); |
| 414 EXPECT_EQ(0UL, registry->worker_process_map_[process_id].count(worker1_id)); | 391 EXPECT_EQ(0UL, registry->worker_process_map_[process_id].count(worker1_id)); |
| 415 EXPECT_EQ(1UL, registry->worker_process_map_[process_id].count( | 392 EXPECT_EQ(1UL, registry->worker_process_map_[process_id].count( |
| 416 worker2->embedded_worker_id())); | 393 worker2->embedded_worker_id())); |
| 417 | 394 |
| 418 worker2->Stop(); | 395 worker2->Stop(); |
| 419 } | 396 } |
| 420 | 397 |
| 421 TEST_P(EmbeddedWorkerInstanceTestP, DetachDuringProcessAllocation) { | 398 TEST_F(EmbeddedWorkerInstanceTest, DetachDuringProcessAllocation) { |
| 422 const int64_t version_id = 55L; | 399 const int64_t version_id = 55L; |
| 423 const GURL scope("http://example.com/"); | 400 const GURL scope("http://example.com/"); |
| 424 const GURL url("http://example.com/worker.js"); | 401 const GURL url("http://example.com/worker.js"); |
| 425 | 402 |
| 426 std::unique_ptr<EmbeddedWorkerInstance> worker = | 403 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 427 embedded_worker_registry()->CreateWorker(); | 404 embedded_worker_registry()->CreateWorker(); |
| 428 worker->AddListener(this); | 405 worker->AddListener(this); |
| 429 | 406 |
| 430 // Run the start worker sequence and detach during process allocation. | 407 // Run the start worker sequence and detach during process allocation. |
| 431 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 408 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 443 // The start callback should not be aborted by detach (see a comment on the | 420 // The start callback should not be aborted by detach (see a comment on the |
| 444 // dtor of EmbeddedWorkerInstance::StartTask). | 421 // dtor of EmbeddedWorkerInstance::StartTask). |
| 445 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); | 422 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); |
| 446 | 423 |
| 447 // "PROCESS_ALLOCATED" event should not be recorded. | 424 // "PROCESS_ALLOCATED" event should not be recorded. |
| 448 ASSERT_EQ(1u, events_.size()); | 425 ASSERT_EQ(1u, events_.size()); |
| 449 EXPECT_EQ(DETACHED, events_[0].type); | 426 EXPECT_EQ(DETACHED, events_[0].type); |
| 450 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[0].status); | 427 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[0].status); |
| 451 } | 428 } |
| 452 | 429 |
| 453 TEST_P(EmbeddedWorkerInstanceTestP, DetachAfterSendingStartWorkerMessage) { | 430 TEST_F(EmbeddedWorkerInstanceTest, DetachAfterSendingStartWorkerMessage) { |
| 454 const int64_t version_id = 55L; | 431 const int64_t version_id = 55L; |
| 455 const GURL scope("http://example.com/"); | 432 const GURL scope("http://example.com/"); |
| 456 const GURL url("http://example.com/worker.js"); | 433 const GURL url("http://example.com/worker.js"); |
| 457 | 434 |
| 458 helper_.reset(new StalledInStartWorkerHelper()); | 435 helper_.reset(new StalledInStartWorkerHelper()); |
| 459 std::unique_ptr<EmbeddedWorkerInstance> worker = | 436 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 460 embedded_worker_registry()->CreateWorker(); | 437 embedded_worker_registry()->CreateWorker(); |
| 461 worker->AddListener(this); | 438 worker->AddListener(this); |
| 462 | 439 |
| 463 // Run the start worker sequence until a start worker message is sent. | 440 // Run the start worker sequence until a start worker message is sent. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 483 // The start callback should not be aborted by detach (see a comment on the | 460 // The start callback should not be aborted by detach (see a comment on the |
| 484 // dtor of EmbeddedWorkerInstance::StartTask). | 461 // dtor of EmbeddedWorkerInstance::StartTask). |
| 485 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); | 462 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); |
| 486 | 463 |
| 487 // "STARTED" event should not be recorded. | 464 // "STARTED" event should not be recorded. |
| 488 ASSERT_EQ(1u, events_.size()); | 465 ASSERT_EQ(1u, events_.size()); |
| 489 EXPECT_EQ(DETACHED, events_[0].type); | 466 EXPECT_EQ(DETACHED, events_[0].type); |
| 490 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[0].status); | 467 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[0].status); |
| 491 } | 468 } |
| 492 | 469 |
| 493 TEST_P(EmbeddedWorkerInstanceTestP, StopDuringProcessAllocation) { | 470 TEST_F(EmbeddedWorkerInstanceTest, StopDuringProcessAllocation) { |
| 494 const int64_t version_id = 55L; | 471 const int64_t version_id = 55L; |
| 495 const GURL scope("http://example.com/"); | 472 const GURL scope("http://example.com/"); |
| 496 const GURL url("http://example.com/worker.js"); | 473 const GURL url("http://example.com/worker.js"); |
| 497 | 474 |
| 498 std::unique_ptr<EmbeddedWorkerInstance> worker = | 475 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 499 embedded_worker_registry()->CreateWorker(); | 476 embedded_worker_registry()->CreateWorker(); |
| 500 worker->AddListener(this); | 477 worker->AddListener(this); |
| 501 | 478 |
| 502 // Stop the start worker sequence before a process is allocated. | 479 // Stop the start worker sequence before a process is allocated. |
| 503 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 480 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 535 EXPECT_EQ(SERVICE_WORKER_OK, status); | 512 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 536 ASSERT_EQ(3u, events_.size()); | 513 ASSERT_EQ(3u, events_.size()); |
| 537 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); | 514 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); |
| 538 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); | 515 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); |
| 539 EXPECT_EQ(STARTED, events_[2].type); | 516 EXPECT_EQ(STARTED, events_[2].type); |
| 540 | 517 |
| 541 // Tear down the worker. | 518 // Tear down the worker. |
| 542 worker->Stop(); | 519 worker->Stop(); |
| 543 } | 520 } |
| 544 | 521 |
| 545 TEST_P(EmbeddedWorkerInstanceTestP, StopDuringPausedAfterDownload) { | 522 TEST_F(EmbeddedWorkerInstanceTest, StopDuringPausedAfterDownload) { |
| 546 const int64_t version_id = 55L; | 523 const int64_t version_id = 55L; |
| 547 const GURL scope("http://example.com/"); | 524 const GURL scope("http://example.com/"); |
| 548 const GURL url("http://example.com/worker.js"); | 525 const GURL url("http://example.com/worker.js"); |
| 549 | 526 |
| 550 std::unique_ptr<EmbeddedWorkerInstance> worker = | 527 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 551 embedded_worker_registry()->CreateWorker(); | 528 embedded_worker_registry()->CreateWorker(); |
| 552 worker->AddListener(this); | 529 worker->AddListener(this); |
| 553 | 530 |
| 554 // Run the start worker sequence until pause after download. | 531 // Run the start worker sequence until pause after download. |
| 555 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 532 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 567 worker->Stop(); | 544 worker->Stop(); |
| 568 worker->ResumeAfterDownload(); | 545 worker->ResumeAfterDownload(); |
| 569 base::RunLoop().RunUntilIdle(); | 546 base::RunLoop().RunUntilIdle(); |
| 570 | 547 |
| 571 // The resume after download message should not have been sent. | 548 // The resume after download message should not have been sent. |
| 572 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 549 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 573 EXPECT_FALSE(ipc_sink()->GetFirstMessageMatching( | 550 EXPECT_FALSE(ipc_sink()->GetFirstMessageMatching( |
| 574 EmbeddedWorkerMsg_ResumeAfterDownload::ID)); | 551 EmbeddedWorkerMsg_ResumeAfterDownload::ID)); |
| 575 } | 552 } |
| 576 | 553 |
| 577 TEST_P(EmbeddedWorkerInstanceTestP, StopAfterSendingStartWorkerMessage) { | 554 TEST_F(EmbeddedWorkerInstanceTest, StopAfterSendingStartWorkerMessage) { |
| 578 const int64_t version_id = 55L; | 555 const int64_t version_id = 55L; |
| 579 const GURL scope("http://example.com/"); | 556 const GURL scope("http://example.com/"); |
| 580 const GURL url("http://example.com/worker.js"); | 557 const GURL url("http://example.com/worker.js"); |
| 581 | 558 |
| 582 helper_.reset(new StalledInStartWorkerHelper); | 559 helper_.reset(new StalledInStartWorkerHelper); |
| 583 std::unique_ptr<EmbeddedWorkerInstance> worker = | 560 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 584 embedded_worker_registry()->CreateWorker(); | 561 embedded_worker_registry()->CreateWorker(); |
| 585 worker->AddListener(this); | 562 worker->AddListener(this); |
| 586 | 563 |
| 587 // Run the start worker sequence until a start worker message is sent. | 564 // Run the start worker sequence until a start worker message is sent. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 EXPECT_EQ(SERVICE_WORKER_OK, status); | 607 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 631 ASSERT_EQ(3u, events_.size()); | 608 ASSERT_EQ(3u, events_.size()); |
| 632 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); | 609 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); |
| 633 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); | 610 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); |
| 634 EXPECT_EQ(STARTED, events_[2].type); | 611 EXPECT_EQ(STARTED, events_[2].type); |
| 635 | 612 |
| 636 // Tear down the worker. | 613 // Tear down the worker. |
| 637 worker->Stop(); | 614 worker->Stop(); |
| 638 } | 615 } |
| 639 | 616 |
| 640 TEST_P(EmbeddedWorkerInstanceTestP, Detach) { | 617 TEST_F(EmbeddedWorkerInstanceTest, Detach) { |
| 641 const int64_t version_id = 55L; | 618 const int64_t version_id = 55L; |
| 642 const GURL pattern("http://example.com/"); | 619 const GURL pattern("http://example.com/"); |
| 643 const GURL url("http://example.com/worker.js"); | 620 const GURL url("http://example.com/worker.js"); |
| 644 std::unique_ptr<EmbeddedWorkerInstance> worker = | 621 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 645 embedded_worker_registry()->CreateWorker(); | 622 embedded_worker_registry()->CreateWorker(); |
| 646 helper_->SimulateAddProcessToPattern(pattern, | 623 helper_->SimulateAddProcessToPattern(pattern, |
| 647 helper_->mock_render_process_id()); | 624 helper_->mock_render_process_id()); |
| 648 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | 625 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
| 649 worker->AddListener(this); | 626 worker->AddListener(this); |
| 650 | 627 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 663 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 640 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 664 | 641 |
| 665 // Send the registry a message from the detached worker. Nothing should | 642 // Send the registry a message from the detached worker. Nothing should |
| 666 // happen. | 643 // happen. |
| 667 embedded_worker_registry()->OnWorkerStarted(process_id, | 644 embedded_worker_registry()->OnWorkerStarted(process_id, |
| 668 worker->embedded_worker_id()); | 645 worker->embedded_worker_id()); |
| 669 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 646 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 670 } | 647 } |
| 671 | 648 |
| 672 // Test for when sending the start IPC failed. | 649 // Test for when sending the start IPC failed. |
| 673 TEST_P(EmbeddedWorkerInstanceTestP, FailToSendStartIPC) { | 650 TEST_F(EmbeddedWorkerInstanceTest, FailToSendStartIPC) { |
| 674 const int64_t version_id = 55L; | 651 const int64_t version_id = 55L; |
| 675 const GURL pattern("http://example.com/"); | 652 const GURL pattern("http://example.com/"); |
| 676 const GURL url("http://example.com/worker.js"); | 653 const GURL url("http://example.com/worker.js"); |
| 677 | 654 |
| 678 if (is_mojo_enabled()) { | 655 // Let StartWorker fail; mojo IPC fails to connect to a remote interface. |
| 679 // Let StartWorker fail; mojo IPC fails to connect to a remote interface. | 656 helper_->RegisterMockInstanceClient(nullptr); |
| 680 helper_->RegisterMockInstanceClient(nullptr); | |
| 681 } else { | |
| 682 helper_.reset(new FailToSendIPCHelper()); | |
| 683 } | |
| 684 | 657 |
| 685 std::unique_ptr<EmbeddedWorkerInstance> worker = | 658 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 686 embedded_worker_registry()->CreateWorker(); | 659 embedded_worker_registry()->CreateWorker(); |
| 687 helper_->SimulateAddProcessToPattern(pattern, | 660 helper_->SimulateAddProcessToPattern(pattern, |
| 688 helper_->mock_render_process_id()); | 661 helper_->mock_render_process_id()); |
| 689 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | |
| 690 worker->AddListener(this); | 662 worker->AddListener(this); |
| 691 | 663 |
| 692 // Attempt to start the worker. | 664 // Attempt to start the worker. |
| 693 std::unique_ptr<EmbeddedWorkerStartParams> params = | 665 std::unique_ptr<EmbeddedWorkerStartParams> params = |
| 694 CreateStartParams(version_id, pattern, url); | 666 CreateStartParams(version_id, pattern, url); |
| 695 if (is_mojo_enabled()) { | 667 worker->Start(std::move(params), CreateEventDispatcher(), |
| 696 worker->Start(std::move(params), CreateEventDispatcher(), | 668 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 697 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 669 base::RunLoop().RunUntilIdle(); |
| 698 base::RunLoop().RunUntilIdle(); | |
| 699 } else { | |
| 700 base::RunLoop run_loop; | |
| 701 worker->Start( | |
| 702 std::move(params), CreateEventDispatcher(), | |
| 703 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); | |
| 704 run_loop.Run(); | |
| 705 } | |
| 706 | 670 |
| 707 if (is_mojo_enabled()) { | 671 // Worker should handle the failure of binding as detach. |
| 708 // Worker should handle the failure of binding as detach. | 672 ASSERT_EQ(3u, events_.size()); |
| 709 ASSERT_EQ(3u, events_.size()); | 673 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); |
| 710 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); | 674 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); |
| 711 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); | 675 EXPECT_EQ(DETACHED, events_[2].type); |
| 712 EXPECT_EQ(DETACHED, events_[2].type); | 676 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[2].status); |
| 713 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[2].status); | |
| 714 } else { | |
| 715 // The callback should have run, and we should have got an OnStopped | |
| 716 // message. | |
| 717 EXPECT_EQ(SERVICE_WORKER_ERROR_IPC_FAILED, status); | |
| 718 ASSERT_EQ(2u, events_.size()); | |
| 719 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); | |
| 720 EXPECT_EQ(STOPPED, events_[1].type); | |
| 721 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[1].status); | |
| 722 } | |
| 723 } | 677 } |
| 724 | 678 |
| 725 class FailEmbeddedWorkerInstanceClientImpl | 679 class FailEmbeddedWorkerInstanceClientImpl |
| 726 : public EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient { | 680 : public EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient { |
| 727 public: | 681 public: |
| 728 explicit FailEmbeddedWorkerInstanceClientImpl( | 682 explicit FailEmbeddedWorkerInstanceClientImpl( |
| 729 base::WeakPtr<EmbeddedWorkerTestHelper> helper) | 683 base::WeakPtr<EmbeddedWorkerTestHelper> helper) |
| 730 : EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient(helper) {} | 684 : EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient(helper) {} |
| 731 | 685 |
| 732 private: | 686 private: |
| 733 void StartWorker( | 687 void StartWorker( |
| 734 const EmbeddedWorkerStartParams& /* unused */, | 688 const EmbeddedWorkerStartParams& /* unused */, |
| 735 mojom::ServiceWorkerEventDispatcherRequest /* unused */) override { | 689 mojom::ServiceWorkerEventDispatcherRequest /* unused */) override { |
| 736 helper_->mock_instance_clients()->clear(); | 690 helper_->mock_instance_clients()->clear(); |
| 737 } | 691 } |
| 738 }; | 692 }; |
| 739 | 693 |
| 740 TEST_P(EmbeddedWorkerInstanceTestP, RemoveRemoteInterface) { | 694 TEST_F(EmbeddedWorkerInstanceTest, RemoveRemoteInterface) { |
| 741 if (!is_mojo_enabled()) | |
| 742 return; | |
| 743 | |
| 744 const int64_t version_id = 55L; | 695 const int64_t version_id = 55L; |
| 745 const GURL pattern("http://example.com/"); | 696 const GURL pattern("http://example.com/"); |
| 746 const GURL url("http://example.com/worker.js"); | 697 const GURL url("http://example.com/worker.js"); |
| 747 | 698 |
| 748 // Let StartWorker fail; binding is discarded in the middle of IPC | 699 // Let StartWorker fail; binding is discarded in the middle of IPC |
| 749 helper_->RegisterMockInstanceClient( | 700 helper_->RegisterMockInstanceClient( |
| 750 base::MakeUnique<FailEmbeddedWorkerInstanceClientImpl>( | 701 base::MakeUnique<FailEmbeddedWorkerInstanceClientImpl>( |
| 751 helper_->AsWeakPtr())); | 702 helper_->AsWeakPtr())); |
| 752 ASSERT_EQ(mock_instance_clients()->size(), 1UL); | 703 ASSERT_EQ(mock_instance_clients()->size(), 1UL); |
| 753 | 704 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 765 base::RunLoop().RunUntilIdle(); | 716 base::RunLoop().RunUntilIdle(); |
| 766 | 717 |
| 767 // Worker should handle the sudden shutdown as detach. | 718 // Worker should handle the sudden shutdown as detach. |
| 768 ASSERT_EQ(3u, events_.size()); | 719 ASSERT_EQ(3u, events_.size()); |
| 769 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); | 720 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); |
| 770 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); | 721 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); |
| 771 EXPECT_EQ(DETACHED, events_[2].type); | 722 EXPECT_EQ(DETACHED, events_[2].type); |
| 772 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[2].status); | 723 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[2].status); |
| 773 } | 724 } |
| 774 | 725 |
| 775 INSTANTIATE_TEST_CASE_P(EmbeddedWorkerInstanceTest, | |
| 776 EmbeddedWorkerInstanceTestP, | |
| 777 ::testing::Values(false, true)); | |
| 778 | |
| 779 } // namespace content | 726 } // namespace content |
| OLD | NEW |