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

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

Issue 2027583002: service worker: Avoid starting up for activation during shutdown (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add unittest Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | content/browser/service_worker/service_worker_metrics.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/test/test_simple_task_runner.h"
11 #include "content/browser/browser_thread_impl.h" 12 #include "content/browser/browser_thread_impl.h"
12 #include "content/browser/service_worker/embedded_worker_registry.h" 13 #include "content/browser/service_worker/embedded_worker_registry.h"
13 #include "content/browser/service_worker/embedded_worker_test_helper.h" 14 #include "content/browser/service_worker/embedded_worker_test_helper.h"
14 #include "content/browser/service_worker/service_worker_context_core.h" 15 #include "content/browser/service_worker/service_worker_context_core.h"
16 #include "content/browser/service_worker/service_worker_context_wrapper.h"
15 #include "content/browser/service_worker/service_worker_disk_cache.h" 17 #include "content/browser/service_worker/service_worker_disk_cache.h"
16 #include "content/browser/service_worker/service_worker_job_coordinator.h" 18 #include "content/browser/service_worker/service_worker_job_coordinator.h"
17 #include "content/browser/service_worker/service_worker_registration.h" 19 #include "content/browser/service_worker/service_worker_registration.h"
18 #include "content/browser/service_worker/service_worker_registration_status.h" 20 #include "content/browser/service_worker/service_worker_registration_status.h"
19 #include "content/browser/service_worker/service_worker_test_utils.h" 21 #include "content/browser/service_worker/service_worker_test_utils.h"
20 #include "content/common/service_worker/embedded_worker_messages.h" 22 #include "content/common/service_worker/embedded_worker_messages.h"
21 #include "content/common/service_worker/service_worker_messages.h" 23 #include "content/common/service_worker/service_worker_messages.h"
24 #include "content/common/service_worker/service_worker_utils.h"
22 #include "content/public/test/test_browser_thread_bundle.h" 25 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "ipc/ipc_test_sink.h" 26 #include "ipc/ipc_test_sink.h"
24 #include "net/base/io_buffer.h" 27 #include "net/base/io_buffer.h"
25 #include "net/base/net_errors.h" 28 #include "net/base/net_errors.h"
26 #include "net/base/test_completion_callback.h" 29 #include "net/base/test_completion_callback.h"
27 #include "net/http/http_response_headers.h" 30 #include "net/http/http_response_headers.h"
28 #include "testing/gtest/include/gtest/gtest.h" 31 #include "testing/gtest/include/gtest/gtest.h"
29 32
30 using net::IOBuffer; 33 using net::IOBuffer;
31 using net::TestCompletionCallback; 34 using net::TestCompletionCallback;
(...skipping 1186 matching lines...) Expand 10 before | Expand all | Expand 10 after
1218 EXPECT_EQ(NULL, registration->installing_version()); 1221 EXPECT_EQ(NULL, registration->installing_version());
1219 } 1222 }
1220 1223
1221 TEST_F(ServiceWorkerJobTest, RegisterWhileUninstalling) { 1224 TEST_F(ServiceWorkerJobTest, RegisterWhileUninstalling) {
1222 GURL pattern("http://www.example.com/one/"); 1225 GURL pattern("http://www.example.com/one/");
1223 GURL script1("http://www.example.com/service_worker.js"); 1226 GURL script1("http://www.example.com/service_worker.js");
1224 GURL script2("http://www.example.com/service_worker.js?new"); 1227 GURL script2("http://www.example.com/service_worker.js?new");
1225 1228
1226 scoped_refptr<ServiceWorkerRegistration> registration = 1229 scoped_refptr<ServiceWorkerRegistration> registration =
1227 RunRegisterJob(pattern, script1); 1230 RunRegisterJob(pattern, script1);
1231 scoped_refptr<base::TestSimpleTaskRunner> runner(
1232 new base::TestSimpleTaskRunner());
1233 registration->SetTaskRunnerForTest(runner);
1228 1234
1229 // Add a controllee and queue an unregister to force the uninstalling state. 1235 // Add a controllee and queue an unregister to force the uninstalling state.
1230 std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee(); 1236 std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee();
1231 scoped_refptr<ServiceWorkerVersion> old_version = 1237 scoped_refptr<ServiceWorkerVersion> old_version =
1232 registration->active_version(); 1238 registration->active_version();
1233 old_version->AddControllee(host.get()); 1239 old_version->AddControllee(host.get());
1234 RunUnregisterJob(pattern); 1240 RunUnregisterJob(pattern);
1235 1241
1236 // Register another script. 1242 // Register another script.
1237 EXPECT_EQ(registration, RunRegisterJob(pattern, script2)); 1243 EXPECT_EQ(registration, RunRegisterJob(pattern, script2));
(...skipping 13 matching lines...) Expand all
1251 old_version->RemoveControllee(host.get()); 1257 old_version->RemoveControllee(host.get());
1252 base::RunLoop().RunUntilIdle(); 1258 base::RunLoop().RunUntilIdle();
1253 1259
1254 EXPECT_FALSE(registration->is_uninstalling()); 1260 EXPECT_FALSE(registration->is_uninstalling());
1255 EXPECT_FALSE(registration->is_uninstalled()); 1261 EXPECT_FALSE(registration->is_uninstalled());
1256 1262
1257 // Verify the new version is activated. 1263 // Verify the new version is activated.
1258 EXPECT_EQ(NULL, registration->installing_version()); 1264 EXPECT_EQ(NULL, registration->installing_version());
1259 EXPECT_EQ(NULL, registration->waiting_version()); 1265 EXPECT_EQ(NULL, registration->waiting_version());
1260 EXPECT_EQ(new_version, registration->active_version()); 1266 EXPECT_EQ(new_version, registration->active_version());
1267 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status());
1268
1269 runner->RunUntilIdle();
1270 base::RunLoop().RunUntilIdle();
1261 EXPECT_EQ(ServiceWorkerVersion::RUNNING, new_version->running_status()); 1271 EXPECT_EQ(ServiceWorkerVersion::RUNNING, new_version->running_status());
1262 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, new_version->status()); 1272 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, new_version->status());
1263 } 1273 }
1264 1274
1265 TEST_F(ServiceWorkerJobTest, RegisterAndUnregisterWhileUninstalling) { 1275 TEST_F(ServiceWorkerJobTest, RegisterAndUnregisterWhileUninstalling) {
1266 GURL pattern("http://www.example.com/one/"); 1276 GURL pattern("http://www.example.com/one/");
1267 GURL script1("http://www.example.com/service_worker.js"); 1277 GURL script1("http://www.example.com/service_worker.js");
1268 GURL script2("http://www.example.com/service_worker.js?new"); 1278 GURL script2("http://www.example.com/service_worker.js?new");
1269 1279
1270 scoped_refptr<ServiceWorkerRegistration> registration = 1280 scoped_refptr<ServiceWorkerRegistration> registration =
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1310 EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, new_version->status()); 1320 EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, new_version->status());
1311 } 1321 }
1312 1322
1313 TEST_F(ServiceWorkerJobTest, RegisterSameScriptMultipleTimesWhileUninstalling) { 1323 TEST_F(ServiceWorkerJobTest, RegisterSameScriptMultipleTimesWhileUninstalling) {
1314 GURL pattern("http://www.example.com/one/"); 1324 GURL pattern("http://www.example.com/one/");
1315 GURL script1("http://www.example.com/service_worker.js"); 1325 GURL script1("http://www.example.com/service_worker.js");
1316 GURL script2("http://www.example.com/service_worker.js?new"); 1326 GURL script2("http://www.example.com/service_worker.js?new");
1317 1327
1318 scoped_refptr<ServiceWorkerRegistration> registration = 1328 scoped_refptr<ServiceWorkerRegistration> registration =
1319 RunRegisterJob(pattern, script1); 1329 RunRegisterJob(pattern, script1);
1330 scoped_refptr<base::TestSimpleTaskRunner> runner(
1331 new base::TestSimpleTaskRunner());
1332 registration->SetTaskRunnerForTest(runner);
1320 1333
1321 // Add a controllee and queue an unregister to force the uninstalling state. 1334 // Add a controllee and queue an unregister to force the uninstalling state.
1322 std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee(); 1335 std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee();
1323 scoped_refptr<ServiceWorkerVersion> old_version = 1336 scoped_refptr<ServiceWorkerVersion> old_version =
1324 registration->active_version(); 1337 registration->active_version();
1325 old_version->AddControllee(host.get()); 1338 old_version->AddControllee(host.get());
1326 RunUnregisterJob(pattern); 1339 RunUnregisterJob(pattern);
1327 1340
1328 EXPECT_EQ(registration, RunRegisterJob(pattern, script2)); 1341 EXPECT_EQ(registration, RunRegisterJob(pattern, script2));
1329 1342
(...skipping 13 matching lines...) Expand all
1343 old_version->RemoveControllee(host.get()); 1356 old_version->RemoveControllee(host.get());
1344 base::RunLoop().RunUntilIdle(); 1357 base::RunLoop().RunUntilIdle();
1345 1358
1346 EXPECT_FALSE(registration->is_uninstalling()); 1359 EXPECT_FALSE(registration->is_uninstalling());
1347 EXPECT_FALSE(registration->is_uninstalled()); 1360 EXPECT_FALSE(registration->is_uninstalled());
1348 1361
1349 // Verify the new version is activated. 1362 // Verify the new version is activated.
1350 EXPECT_EQ(NULL, registration->installing_version()); 1363 EXPECT_EQ(NULL, registration->installing_version());
1351 EXPECT_EQ(NULL, registration->waiting_version()); 1364 EXPECT_EQ(NULL, registration->waiting_version());
1352 EXPECT_EQ(new_version, registration->active_version()); 1365 EXPECT_EQ(new_version, registration->active_version());
1366 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status());
1367
1368 runner->RunUntilIdle();
1369 base::RunLoop().RunUntilIdle();
1353 EXPECT_EQ(ServiceWorkerVersion::RUNNING, new_version->running_status()); 1370 EXPECT_EQ(ServiceWorkerVersion::RUNNING, new_version->running_status());
1354 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, new_version->status()); 1371 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, new_version->status());
1355 } 1372 }
1356 1373
1357 TEST_F(ServiceWorkerJobTest, RegisterMultipleTimesWhileUninstalling) { 1374 TEST_F(ServiceWorkerJobTest, RegisterMultipleTimesWhileUninstalling) {
1358 GURL pattern("http://www.example.com/one/"); 1375 GURL pattern("http://www.example.com/one/");
1359 GURL script1("http://www.example.com/service_worker.js?first"); 1376 GURL script1("http://www.example.com/service_worker.js?first");
1360 GURL script2("http://www.example.com/service_worker.js?second"); 1377 GURL script2("http://www.example.com/service_worker.js?second");
1361 GURL script3("http://www.example.com/service_worker.js?third"); 1378 GURL script3("http://www.example.com/service_worker.js?third");
1362 1379
1363 scoped_refptr<ServiceWorkerRegistration> registration = 1380 scoped_refptr<ServiceWorkerRegistration> registration =
1364 RunRegisterJob(pattern, script1); 1381 RunRegisterJob(pattern, script1);
1382 scoped_refptr<base::TestSimpleTaskRunner> runner(
1383 new base::TestSimpleTaskRunner());
1384 registration->SetTaskRunnerForTest(runner);
1365 1385
1366 // Add a controllee and queue an unregister to force the uninstalling state. 1386 // Add a controllee and queue an unregister to force the uninstalling state.
1367 std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee(); 1387 std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee();
1368 scoped_refptr<ServiceWorkerVersion> first_version = 1388 scoped_refptr<ServiceWorkerVersion> first_version =
1369 registration->active_version(); 1389 registration->active_version();
1370 first_version->AddControllee(host.get()); 1390 first_version->AddControllee(host.get());
1371 RunUnregisterJob(pattern); 1391 RunUnregisterJob(pattern);
1372 1392
1373 EXPECT_EQ(registration, RunRegisterJob(pattern, script2)); 1393 EXPECT_EQ(registration, RunRegisterJob(pattern, script2));
1374 1394
(...skipping 17 matching lines...) Expand all
1392 first_version->RemoveControllee(host.get()); 1412 first_version->RemoveControllee(host.get());
1393 base::RunLoop().RunUntilIdle(); 1413 base::RunLoop().RunUntilIdle();
1394 1414
1395 EXPECT_FALSE(registration->is_uninstalling()); 1415 EXPECT_FALSE(registration->is_uninstalling());
1396 EXPECT_FALSE(registration->is_uninstalled()); 1416 EXPECT_FALSE(registration->is_uninstalled());
1397 1417
1398 // Verify the new version is activated. 1418 // Verify the new version is activated.
1399 EXPECT_EQ(NULL, registration->installing_version()); 1419 EXPECT_EQ(NULL, registration->installing_version());
1400 EXPECT_EQ(NULL, registration->waiting_version()); 1420 EXPECT_EQ(NULL, registration->waiting_version());
1401 EXPECT_EQ(third_version, registration->active_version()); 1421 EXPECT_EQ(third_version, registration->active_version());
1422 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, third_version->status());
1423
1424 runner->RunUntilIdle();
1425 base::RunLoop().RunUntilIdle();
1402 EXPECT_EQ(ServiceWorkerVersion::RUNNING, third_version->running_status()); 1426 EXPECT_EQ(ServiceWorkerVersion::RUNNING, third_version->running_status());
1403 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, third_version->status()); 1427 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, third_version->status());
1404 } 1428 }
1405 1429
1406 class EventCallbackHelper : public EmbeddedWorkerTestHelper { 1430 class EventCallbackHelper : public EmbeddedWorkerTestHelper {
1407 public: 1431 public:
1408 EventCallbackHelper() 1432 EventCallbackHelper()
1409 : EmbeddedWorkerTestHelper(base::FilePath()), 1433 : EmbeddedWorkerTestHelper(base::FilePath()),
1410 install_event_result_(blink::WebServiceWorkerEventResultCompleted), 1434 install_event_result_(blink::WebServiceWorkerEventResultCompleted),
1411 activate_event_result_(blink::WebServiceWorkerEventResultCompleted) {} 1435 activate_event_result_(blink::WebServiceWorkerEventResultCompleted) {}
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
1605 sink->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID); 1629 sink->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID);
1606 ASSERT_TRUE(start_msg); 1630 ASSERT_TRUE(start_msg);
1607 EmbeddedWorkerMsg_StartWorker::Param param; 1631 EmbeddedWorkerMsg_StartWorker::Param param;
1608 EmbeddedWorkerMsg_StartWorker::Read(start_msg, &param); 1632 EmbeddedWorkerMsg_StartWorker::Read(start_msg, &param);
1609 EmbeddedWorkerMsg_StartWorker_Params start_params = base::get<0>(param); 1633 EmbeddedWorkerMsg_StartWorker_Params start_params = base::get<0>(param);
1610 EXPECT_TRUE(start_params.pause_after_download); 1634 EXPECT_TRUE(start_params.pause_after_download);
1611 sink->ClearMessages(); 1635 sink->ClearMessages();
1612 } 1636 }
1613 } 1637 }
1614 1638
1639 // Test that activation doesn't complete if it's triggered by removing a
1640 // controllee and starting the worker failed due to shutdown.
1641 TEST_F(ServiceWorkerJobTest, ActivateCancelsOnShutdown) {
1642 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper;
1643 helper_.reset(update_helper);
1644 GURL pattern("http://www.example.com/");
1645 GURL script("http://www.example.com/service_worker.js");
1646
1647 scoped_refptr<ServiceWorkerRegistration> registration =
1648 RunRegisterJob(pattern, script);
1649 scoped_refptr<base::TestSimpleTaskRunner> runner(
1650 new base::TestSimpleTaskRunner());
1651 registration->SetTaskRunnerForTest(runner);
1652
1653 // Add a controllee.
1654 std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee();
1655 scoped_refptr<ServiceWorkerVersion> first_version =
1656 registration->active_version();
1657 first_version->AddControllee(host.get());
1658
1659 // Update. The new version should be waiting.
1660 registration->AddListener(update_helper);
1661 first_version->StartUpdate();
1662 base::RunLoop().RunUntilIdle();
1663 scoped_refptr<ServiceWorkerVersion> new_version =
1664 registration->waiting_version();
1665 ASSERT_TRUE(new_version);
1666 EXPECT_EQ(ServiceWorkerVersion::INSTALLED, new_version->status());
1667
1668 // Stop the worker so that it must start again when activation is attempted.
1669 // (This is not strictly necessary to exercise the codepath, but it makes it
1670 // easy to cause a failure with set_force_start_worker_failure after
1671 // shutdown is simulated. Otherwise our test helper often fails on
1672 // DCHECK(context)).
1673 new_version->StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
1674
1675 // Remove the controllee. The new version should be activating, and delayed
1676 // until the runner runs again.
1677 first_version->RemoveControllee(host.get());
1678 base::RunLoop().RunUntilIdle();
1679 EXPECT_EQ(new_version.get(), registration->active_version());
1680 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status());
1681
1682 // Shutdown.
1683 update_helper->context()->wrapper()->Shutdown();
1684 update_helper->set_force_start_worker_failure(true);
1685
1686 // Allow the activation to continue. It will fail, and the worker
1687 // should not be promoted to ACTIVATED because failure occur
1688 // during shutdown.
1689 runner->RunUntilIdle();
1690 base::RunLoop().RunUntilIdle();
1691 EXPECT_EQ(new_version.get(), registration->active_version());
1692 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status());
1693 registration->RemoveListener(update_helper);
1694 }
1695
1615 } // namespace content 1696 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/browser/service_worker/service_worker_metrics.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698