| OLD | NEW |
| 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 "content/browser/service_worker/service_worker_version.h" | 5 #include "content/browser/service_worker/service_worker_version.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <tuple> | 8 #include <tuple> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
| 14 #include "base/test/simple_test_tick_clock.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "content/browser/service_worker/embedded_worker_registry.h" | 16 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 16 #include "content/browser/service_worker/embedded_worker_status.h" | 17 #include "content/browser/service_worker/embedded_worker_status.h" |
| 17 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 18 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 18 #include "content/browser/service_worker/service_worker_context_core.h" | 19 #include "content/browser/service_worker/service_worker_context_core.h" |
| 19 #include "content/browser/service_worker/service_worker_registration.h" | 20 #include "content/browser/service_worker/service_worker_registration.h" |
| 20 #include "content/browser/service_worker/service_worker_test_utils.h" | 21 #include "content/browser/service_worker/service_worker_test_utils.h" |
| 21 #include "content/common/service_worker/service_worker_utils.h" | 22 #include "content/common/service_worker/service_worker_utils.h" |
| 22 #include "content/public/test/mock_render_process_host.h" | 23 #include "content/public/test/mock_render_process_host.h" |
| 23 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 event_type, CreateReceiverOnCurrentThread(&status)); | 275 event_type, CreateReceiverOnCurrentThread(&status)); |
| 275 base::RunLoop().RunUntilIdle(); | 276 base::RunLoop().RunUntilIdle(); |
| 276 | 277 |
| 277 // And finish request, as if a response to the event was received. | 278 // And finish request, as if a response to the event was received. |
| 278 EXPECT_TRUE(version_->FinishRequest(request_id, true /* was_handled */, | 279 EXPECT_TRUE(version_->FinishRequest(request_id, true /* was_handled */, |
| 279 base::Time::Now())); | 280 base::Time::Now())); |
| 280 base::RunLoop().RunUntilIdle(); | 281 base::RunLoop().RunUntilIdle(); |
| 281 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); | 282 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, status); |
| 282 } | 283 } |
| 283 | 284 |
| 285 base::SimpleTestTickClock* SetTickClockForTesting() { |
| 286 base::SimpleTestTickClock* tick_clock = new base::SimpleTestTickClock(); |
| 287 version_->SetTickClockForTesting(base::WrapUnique(tick_clock)); |
| 288 return tick_clock; |
| 289 } |
| 290 |
| 284 TestBrowserThreadBundle thread_bundle_; | 291 TestBrowserThreadBundle thread_bundle_; |
| 285 std::unique_ptr<MessageReceiver> helper_; | 292 std::unique_ptr<MessageReceiver> helper_; |
| 286 scoped_refptr<ServiceWorkerRegistration> registration_; | 293 scoped_refptr<ServiceWorkerRegistration> registration_; |
| 287 scoped_refptr<ServiceWorkerVersion> version_; | 294 scoped_refptr<ServiceWorkerVersion> version_; |
| 288 GURL pattern_; | 295 GURL pattern_; |
| 289 | 296 |
| 290 private: | 297 private: |
| 291 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersionTest); | 298 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersionTest); |
| 292 }; | 299 }; |
| 293 | 300 |
| (...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 852 EXPECT_TRUE(version_->timeout_timer_.IsRunning()); | 859 EXPECT_TRUE(version_->timeout_timer_.IsRunning()); |
| 853 version_->SetAllRequestExpirations(base::TimeTicks::Now()); | 860 version_->SetAllRequestExpirations(base::TimeTicks::Now()); |
| 854 version_->timeout_timer_.user_task().Run(); | 861 version_->timeout_timer_.user_task().Run(); |
| 855 base::RunLoop().RunUntilIdle(); | 862 base::RunLoop().RunUntilIdle(); |
| 856 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, status); | 863 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, status); |
| 857 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, version_->running_status()); | 864 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, version_->running_status()); |
| 858 EXPECT_FALSE(version_->FinishRequest(request_id, true /* was_handled */, | 865 EXPECT_FALSE(version_->FinishRequest(request_id, true /* was_handled */, |
| 859 base::Time::Now())); | 866 base::Time::Now())); |
| 860 } | 867 } |
| 861 | 868 |
| 862 TEST_P(ServiceWorkerVersionTestP, RequestCustomizedTimeout) { | 869 TEST_P(ServiceWorkerVersionTestP, RequestNowTimeout) { |
| 863 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value | 870 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value |
| 864 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | 871 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 865 | 872 |
| 866 version_->StartWorker(ServiceWorkerMetrics::EventType::SYNC, | 873 version_->StartWorker(ServiceWorkerMetrics::EventType::SYNC, |
| 867 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 874 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 868 base::RunLoop().RunUntilIdle(); | 875 base::RunLoop().RunUntilIdle(); |
| 869 | 876 |
| 870 // Create a request that should expire Now(). | 877 // Create a request that should expire Now(). |
| 871 int request_id = version_->StartRequestWithCustomTimeout( | 878 int request_id = version_->StartRequestWithCustomTimeout( |
| 872 ServiceWorkerMetrics::EventType::SYNC, | 879 ServiceWorkerMetrics::EventType::SYNC, |
| 873 CreateReceiverOnCurrentThread(&status), base::TimeDelta(), | 880 CreateReceiverOnCurrentThread(&status), base::TimeDelta(), |
| 874 ServiceWorkerVersion::CONTINUE_ON_TIMEOUT); | 881 ServiceWorkerVersion::CONTINUE_ON_TIMEOUT); |
| 875 | 882 |
| 876 base::RunLoop().RunUntilIdle(); | 883 base::RunLoop().RunUntilIdle(); |
| 877 EXPECT_TRUE(version_->timeout_timer_.IsRunning()); | 884 EXPECT_TRUE(version_->timeout_timer_.IsRunning()); |
| 878 version_->timeout_timer_.user_task().Run(); | 885 version_->timeout_timer_.user_task().Run(); |
| 879 base::RunLoop().RunUntilIdle(); | 886 base::RunLoop().RunUntilIdle(); |
| 880 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, status); | 887 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, status); |
| 881 | 888 |
| 882 EXPECT_FALSE(version_->FinishRequest(request_id, true /* was_handled */, | 889 EXPECT_FALSE(version_->FinishRequest(request_id, true /* was_handled */, |
| 883 base::Time::Now())); | 890 base::Time::Now())); |
| 884 | 891 |
| 885 // CONTINUE_ON_TIMEOUT timeouts don't stop the service worker. | 892 // CONTINUE_ON_TIMEOUT timeouts don't stop the service worker. |
| 886 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version_->running_status()); | 893 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version_->running_status()); |
| 887 } | 894 } |
| 888 | 895 |
| 889 TEST_P(ServiceWorkerVersionTestP, RequestCustomizedTimeoutKill) { | 896 TEST_P(ServiceWorkerVersionTestP, RequestNowTimeoutKill) { |
| 890 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value | 897 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value |
| 891 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | 898 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 892 | 899 |
| 893 version_->StartWorker(ServiceWorkerMetrics::EventType::SYNC, | 900 version_->StartWorker(ServiceWorkerMetrics::EventType::SYNC, |
| 894 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 901 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 895 base::RunLoop().RunUntilIdle(); | 902 base::RunLoop().RunUntilIdle(); |
| 896 | 903 |
| 897 // Create a request that should expire Now(). | 904 // Create a request that should expire Now(). |
| 898 int request_id = version_->StartRequestWithCustomTimeout( | 905 int request_id = version_->StartRequestWithCustomTimeout( |
| 899 ServiceWorkerMetrics::EventType::SYNC, | 906 ServiceWorkerMetrics::EventType::SYNC, |
| 900 CreateReceiverOnCurrentThread(&status), base::TimeDelta(), | 907 CreateReceiverOnCurrentThread(&status), base::TimeDelta(), |
| 901 ServiceWorkerVersion::KILL_ON_TIMEOUT); | 908 ServiceWorkerVersion::KILL_ON_TIMEOUT); |
| 902 | 909 |
| 903 base::RunLoop().RunUntilIdle(); | 910 base::RunLoop().RunUntilIdle(); |
| 904 EXPECT_TRUE(version_->timeout_timer_.IsRunning()); | 911 EXPECT_TRUE(version_->timeout_timer_.IsRunning()); |
| 905 version_->timeout_timer_.user_task().Run(); | 912 version_->timeout_timer_.user_task().Run(); |
| 906 base::RunLoop().RunUntilIdle(); | 913 base::RunLoop().RunUntilIdle(); |
| 907 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, status); | 914 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, status); |
| 908 | 915 |
| 909 EXPECT_FALSE(version_->FinishRequest(request_id, true /* was_handled */, | 916 EXPECT_FALSE(version_->FinishRequest(request_id, true /* was_handled */, |
| 910 base::Time::Now())); | 917 base::Time::Now())); |
| 911 | 918 |
| 912 // KILL_ON_TIMEOUT timeouts should stop the service worker. | 919 // KILL_ON_TIMEOUT timeouts should stop the service worker. |
| 913 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, version_->running_status()); | 920 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, version_->running_status()); |
| 914 } | 921 } |
| 915 | 922 |
| 923 TEST_P(ServiceWorkerVersionTestP, RequestCustomizedTimeout) { |
| 924 ServiceWorkerStatusCode first_status = |
| 925 SERVICE_WORKER_ERROR_MAX_VALUE; // dummy value |
| 926 ServiceWorkerStatusCode second_status = |
| 927 SERVICE_WORKER_ERROR_MAX_VALUE; // dummy value |
| 928 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 929 |
| 930 version_->StartWorker(ServiceWorkerMetrics::EventType::SYNC, |
| 931 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 932 base::RunLoop().RunUntilIdle(); |
| 933 |
| 934 base::SimpleTestTickClock* tick_clock = SetTickClockForTesting(); |
| 935 |
| 936 // Create two requests. One which times out in 10 seconds, one in 20 seconds. |
| 937 int timeout_seconds = 10; |
| 938 int first_request_id = version_->StartRequestWithCustomTimeout( |
| 939 ServiceWorkerMetrics::EventType::SYNC, |
| 940 CreateReceiverOnCurrentThread(&first_status), |
| 941 base::TimeDelta::FromSeconds(2 * timeout_seconds), |
| 942 ServiceWorkerVersion::KILL_ON_TIMEOUT); |
| 943 |
| 944 int second_request_id = version_->StartRequestWithCustomTimeout( |
| 945 ServiceWorkerMetrics::EventType::SYNC, |
| 946 CreateReceiverOnCurrentThread(&second_status), |
| 947 base::TimeDelta::FromSeconds(timeout_seconds), |
| 948 ServiceWorkerVersion::CONTINUE_ON_TIMEOUT); |
| 949 |
| 950 // The status should not have changed since neither task has timed out yet. |
| 951 base::RunLoop().RunUntilIdle(); |
| 952 EXPECT_TRUE(version_->timeout_timer_.IsRunning()); |
| 953 version_->timeout_timer_.user_task().Run(); |
| 954 base::RunLoop().RunUntilIdle(); |
| 955 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, first_status); |
| 956 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, second_status); |
| 957 |
| 958 // Now advance time until the second task timeout should expire. |
| 959 tick_clock->Advance(base::TimeDelta::FromSeconds(timeout_seconds + 1)); |
| 960 version_->timeout_timer_.user_task().Run(); |
| 961 base::RunLoop().RunUntilIdle(); |
| 962 EXPECT_EQ(SERVICE_WORKER_ERROR_MAX_VALUE, first_status); |
| 963 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, second_status); |
| 964 |
| 965 // CONTINUE_ON_TIMEOUT timeouts don't stop the service worker. |
| 966 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version_->running_status()); |
| 967 |
| 968 // Now advance time until both tasks should be expired. |
| 969 tick_clock->Advance(base::TimeDelta::FromSeconds(timeout_seconds + 1)); |
| 970 version_->timeout_timer_.user_task().Run(); |
| 971 base::RunLoop().RunUntilIdle(); |
| 972 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, first_status); |
| 973 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, second_status); |
| 974 |
| 975 EXPECT_FALSE(version_->FinishRequest(first_request_id, true /* was_handled */, |
| 976 base::Time::Now())); |
| 977 |
| 978 EXPECT_FALSE(version_->FinishRequest( |
| 979 second_request_id, true /* was_handled */, base::Time::Now())); |
| 980 |
| 981 // KILL_ON_TIMEOUT timeouts should stop the service worker. |
| 982 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, version_->running_status()); |
| 983 } |
| 984 |
| 916 TEST_P(ServiceWorkerVersionTestP, MixedRequestTimeouts) { | 985 TEST_P(ServiceWorkerVersionTestP, MixedRequestTimeouts) { |
| 917 ServiceWorkerStatusCode sync_status = | 986 ServiceWorkerStatusCode sync_status = |
| 918 SERVICE_WORKER_ERROR_NETWORK; // dummy value | 987 SERVICE_WORKER_ERROR_NETWORK; // dummy value |
| 919 ServiceWorkerStatusCode fetch_status = | 988 ServiceWorkerStatusCode fetch_status = |
| 920 SERVICE_WORKER_ERROR_NETWORK; // dummy value | 989 SERVICE_WORKER_ERROR_NETWORK; // dummy value |
| 921 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | 990 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 922 | 991 |
| 923 version_->StartWorker(ServiceWorkerMetrics::EventType::FETCH_MAIN_FRAME, | 992 version_->StartWorker(ServiceWorkerMetrics::EventType::FETCH_MAIN_FRAME, |
| 924 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 993 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 925 base::RunLoop().RunUntilIdle(); | 994 base::RunLoop().RunUntilIdle(); |
| (...skipping 787 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1713 INSTANTIATE_TEST_CASE_P(ServiceWorkerFailToStartTest, | 1782 INSTANTIATE_TEST_CASE_P(ServiceWorkerFailToStartTest, |
| 1714 ServiceWorkerFailToStartTest, | 1783 ServiceWorkerFailToStartTest, |
| 1715 testing::Bool()); | 1784 testing::Bool()); |
| 1716 INSTANTIATE_TEST_CASE_P(ServiceWorkerNavigationHintUMATest, | 1785 INSTANTIATE_TEST_CASE_P(ServiceWorkerNavigationHintUMATest, |
| 1717 ServiceWorkerNavigationHintUMATest, | 1786 ServiceWorkerNavigationHintUMATest, |
| 1718 testing::Bool()); | 1787 testing::Bool()); |
| 1719 INSTANTIATE_TEST_CASE_P(ServiceWorkerStallInStoppingTest, | 1788 INSTANTIATE_TEST_CASE_P(ServiceWorkerStallInStoppingTest, |
| 1720 ServiceWorkerStallInStoppingTest, | 1789 ServiceWorkerStallInStoppingTest, |
| 1721 testing::Bool()); | 1790 testing::Bool()); |
| 1722 } // namespace content | 1791 } // namespace content |
| OLD | NEW |