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