Add tests for fetch() in a service/shared worker after NetworkService crashes
Bug: 884007, 848256
Change-Id: I3dbeab15a52fc296058b1962364edb19c95d74a0
Reviewed-on: https://chromium-review.googlesource.com/1235261
Commit-Queue: Makoto Shimazu <[email protected]>
Reviewed-by: Matt Falkenhagen <[email protected]>
Reviewed-by: Kinuko Yasuda <[email protected]>
Cr-Commit-Position: refs/heads/master@{#593129}
diff --git a/content/browser/network_service_restart_browsertest.cc b/content/browser/network_service_restart_browsertest.cc
index 6848f52c..1eefed3 100644
--- a/content/browser/network_service_restart_browsertest.cc
+++ b/content/browser/network_service_restart_browsertest.cc
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/callback.h"
+#include "base/run_loop.h"
#include "base/test/bind_test_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_timeouts.h"
@@ -10,6 +12,9 @@
#include "content/browser/frame_host/render_frame_host_impl.h"
#include "content/browser/frame_host/render_frame_message_filter.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
+#include "content/browser/service_worker/embedded_worker_instance.h"
+#include "content/browser/service_worker/embedded_worker_status.h"
+#include "content/browser/service_worker/service_worker_context_core_observer.h"
#include "content/browser/storage_partition_impl.h"
#include "content/browser/url_loader_factory_getter.h"
#include "content/public/browser/browser_context.h"
@@ -100,6 +105,33 @@
}
}
+class ServiceWorkerStatusObserver : public ServiceWorkerContextCoreObserver {
+ public:
+ void WaitForState(EmbeddedWorkerStatus expected_status) {
+ if (latest_status_ == expected_status)
+ return;
+
+ expected_status_ = expected_status;
+ base::RunLoop loop;
+ callback_ = loop.QuitClosure();
+ loop.Run();
+ }
+
+ private:
+ void OnRunningStateChanged(int64_t version_id,
+ EmbeddedWorkerStatus running_status) override {
+ latest_status_ = running_status;
+ if (expected_status_.has_value() &&
+ running_status == expected_status_.value()) {
+ std::move(callback_).Run();
+ }
+ }
+
+ base::Optional<EmbeddedWorkerStatus> expected_status_;
+ EmbeddedWorkerStatus latest_status_;
+ base::OnceClosure callback_;
+};
+
} // namespace
// This test source has been excluded from Android as Android doesn't have
@@ -592,6 +624,78 @@
EXPECT_EQ(last_request_relative_url(), "/title2.html");
}
+// Make sure fetch from service worker context works after crash.
+//
+// Disabled since service workers don't support recovery from a NS crash:
+// https://crbug.com/884007.
+IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest,
+ DISABLED_ServiceWorkerFetch) {
+ StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
+ BrowserContext::GetDefaultStoragePartition(browser_context()));
+ ServiceWorkerStatusObserver observer;
+ ServiceWorkerContextWrapper* service_worker_context =
+ partition->GetServiceWorkerContext();
+ service_worker_context->AddObserver(&observer);
+
+ const GURL page_url = embedded_test_server()->GetURL(
+ "/service_worker/fetch_from_service_worker.html");
+ const GURL fetch_url = embedded_test_server()->GetURL("/echo");
+
+ // Navigate to the page and register a service worker.
+ EXPECT_TRUE(NavigateToURL(shell(), page_url));
+ EXPECT_EQ("ready", EvalJs(shell(), "setup();"));
+
+ // Fetch from the service worker.
+ const std::string script =
+ "fetch_from_service_worker('" + fetch_url.spec() + "');";
+ EXPECT_EQ("Echo", EvalJs(shell(), script));
+
+ // Crash the NetworkService process. Existing interfaces should receive error
+ // notifications at some point.
+ SimulateNetworkServiceCrash();
+ // Flush the interface to make sure the error notification was received.
+ partition->FlushNetworkInterfaceForTesting();
+
+ // Service worker should be stopped when network service crashes.
+ observer.WaitForState(EmbeddedWorkerStatus::STOPPED);
+
+ // Fetch from the service worker again.
+ EXPECT_EQ("Echo", EvalJs(shell(), script));
+
+ service_worker_context->RemoveObserver(&observer);
+}
+
+// Make sure fetch from shared worker context works after crash.
+//
+// Disabled since shared workers don't support recovery from a NS crash:
+// https://crbug.com/848256.
+IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest,
+ DISABLED_SharedWorkerFetch) {
+ StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
+ BrowserContext::GetDefaultStoragePartition(browser_context()));
+
+ const GURL page_url =
+ embedded_test_server()->GetURL("/workers/fetch_from_shared_worker.html");
+ const GURL fetch_url = embedded_test_server()->GetURL("/echo");
+
+ // Navigate to the page and prepare a shared worker.
+ EXPECT_TRUE(NavigateToURL(shell(), page_url));
+
+ // Fetch from the shared worker.
+ const std::string script =
+ "fetch_from_shared_worker('" + fetch_url.spec() + "');";
+ EXPECT_EQ("Echo", EvalJs(shell(), script));
+
+ // Crash the NetworkService process. Existing interfaces should receive error
+ // notifications at some point.
+ SimulateNetworkServiceCrash();
+ // Flush the interface to make sure the error notification was received.
+ partition->FlushNetworkInterfaceForTesting();
+
+ // Fetch from the shared worker again.
+ EXPECT_EQ("Echo", EvalJs(shell(), script));
+}
+
// Make sure the entry in |NetworkService::GetTotalNetworkUsages()| was cleared
// after process closed.
IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest,