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

Unified Diff: content/browser/service_worker/embedded_worker_instance_mojo_unittest.cc

Issue 2307543002: ServiceWorker: Mojofication of EWInstance::StopWorker (Closed)
Patch Set: Added a unittest file Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/service_worker/embedded_worker_instance_mojo_unittest.cc
diff --git a/content/browser/service_worker/embedded_worker_instance_unittest.cc b/content/browser/service_worker/embedded_worker_instance_mojo_unittest.cc
similarity index 86%
copy from content/browser/service_worker/embedded_worker_instance_unittest.cc
copy to content/browser/service_worker/embedded_worker_instance_mojo_unittest.cc
index 09dfb22b9d35162918b4dbe5403e23683cc6c181..02ab1d33134879dfb675fe340e43380e0698edbe 100644
--- a/content/browser/service_worker/embedded_worker_instance_unittest.cc
+++ b/content/browser/service_worker/embedded_worker_instance_mojo_unittest.cc
@@ -26,14 +26,19 @@
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
+using ::testing::_;
using ::testing::Eq;
using ::testing::Field;
+using ::testing::Invoke;
using ::testing::Pointee;
+using ::testing::Return;
namespace content {
namespace {
+void NoOpStatusCallback(ServiceWorkerStatusCode code) {}
+
void SaveStatusAndCall(ServiceWorkerStatusCode* out,
const base::Closure& callback,
ServiceWorkerStatusCode status) {
@@ -59,10 +64,10 @@ bool IsMojoForServiceWorkerEnabled() {
} // namespace
-class EmbeddedWorkerInstanceTest : public testing::Test,
- public EmbeddedWorkerInstance::Listener {
+class MojoEmbeddedWorkerInstanceTest : public testing::Test,
horo 2016/09/05 08:05:57 You can use TEST_P instead of duplicating tests. S
shimazu 2016/09/06 08:53:59 I didn't know that. Tried it.
+ public EmbeddedWorkerInstance::Listener {
protected:
- EmbeddedWorkerInstanceTest()
+ MojoEmbeddedWorkerInstanceTest()
: thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
enum EventType {
@@ -100,13 +105,16 @@ class EmbeddedWorkerInstanceTest : public testing::Test,
bool OnMessageReceived(const IPC::Message& message) override { return false; }
void SetUp() override {
+ base::CommandLine::ForCurrentProcess()->AppendSwitch(
+ switches::kMojoServiceWorker);
helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath()));
}
void TearDown() override { helper_.reset(); }
ServiceWorkerStatusCode StartWorker(EmbeddedWorkerInstance* worker,
- int id, const GURL& pattern,
+ int id,
+ const GURL& pattern,
const GURL& url) {
ServiceWorkerStatusCode status;
base::RunLoop run_loop;
@@ -127,27 +135,18 @@ class EmbeddedWorkerInstanceTest : public testing::Test,
IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); }
- TestBrowserThreadBundle thread_bundle_;
- std::unique_ptr<EmbeddedWorkerTestHelper> helper_;
- std::vector<EventLog> events_;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest);
-};
-
-class MojoEmbeddedWorkerInstanceTest : public EmbeddedWorkerInstanceTest {
- protected:
- void SetUp() override {
- base::CommandLine::ForCurrentProcess()->AppendSwitch(
- switches::kMojoServiceWorker);
- EmbeddedWorkerInstanceTest::SetUp();
- }
-
std::vector<std::unique_ptr<
EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient>>*
mock_instance_clients() {
return helper_->mock_instance_clients();
}
+
+ TestBrowserThreadBundle thread_bundle_;
+ std::unique_ptr<EmbeddedWorkerTestHelper> helper_;
+ std::vector<EventLog> events_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MojoEmbeddedWorkerInstanceTest);
};
// A helper to simulate the start worker sequence is stalled in a worker
@@ -179,62 +178,6 @@ class StalledInStartWorkerHelper : public EmbeddedWorkerTestHelper {
bool force_stall_in_start_ = true;
};
-class FailToSendIPCHelper : public EmbeddedWorkerTestHelper {
- public:
- FailToSendIPCHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {}
- ~FailToSendIPCHelper() override {}
-
- bool Send(IPC::Message* message) override {
- delete message;
- return false;
- }
-};
-
-TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) {
- std::unique_ptr<EmbeddedWorkerInstance> worker =
- embedded_worker_registry()->CreateWorker();
- EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
-
- const int64_t service_worker_version_id = 55L;
- const GURL pattern("http://example.com/");
- const GURL url("http://example.com/worker.js");
-
- // Simulate adding one process to the pattern.
- helper_->SimulateAddProcessToPattern(pattern,
- helper_->mock_render_process_id());
-
- // Start should succeed.
- ServiceWorkerStatusCode status;
- base::RunLoop run_loop;
- std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params =
- CreateStartParams(service_worker_version_id, pattern, url);
- worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status,
- run_loop.QuitClosure()));
- EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status());
- run_loop.Run();
- EXPECT_EQ(SERVICE_WORKER_OK, status);
-
- // The 'WorkerStarted' message should have been sent by
- // EmbeddedWorkerTestHelper.
- EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status());
- EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id());
-
- // Stop the worker.
- EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop());
- EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, worker->status());
- base::RunLoop().RunUntilIdle();
-
- // The 'WorkerStopped' message should have been sent by
- // EmbeddedWorkerTestHelper.
- EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
-
- // Verify that we've sent two messages to start and terminate the worker.
- ASSERT_TRUE(
- ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID));
- ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching(
- EmbeddedWorkerMsg_StopWorker::ID));
-}
-
TEST_F(MojoEmbeddedWorkerInstanceTest, StartAndStop) {
using mojom::EmbeddedWorkerStartWorkerParams;
@@ -252,7 +195,7 @@ TEST_F(MojoEmbeddedWorkerInstanceTest, StartAndStop) {
helper_->SimulateAddProcessToPattern(pattern,
helper_->mock_render_process_id());
- // Check if StartWorker will be called via mojo IPC.
+ // Check if StartWorker/StopWorker will be called via mojo IPC.
helper_->PrepareMockInstanceClients(1);
ASSERT_EQ(mock_instance_clients()->size(), 1UL);
EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient* instance_client =
@@ -267,6 +210,7 @@ TEST_F(MojoEmbeddedWorkerInstanceTest, StartAndStop) {
Pointee(
Field(&EmbeddedWorkerStartWorkerParams::script_url, Eq(url))))))
.Times(1);
+ EXPECT_CALL(*instance_client, MockStopWorker(_)).Times(1);
// Start should succeed.
ServiceWorkerStatusCode status;
@@ -289,18 +233,16 @@ TEST_F(MojoEmbeddedWorkerInstanceTest, StartAndStop) {
EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, worker->status());
base::RunLoop().RunUntilIdle();
- // The 'WorkerStopped' message should have been sent by
+ // Callback passed by mojo IPC should have been called by
// EmbeddedWorkerTestHelper.
EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
-
- // Ckeck if StopWorker has been sent via chromium IPC.
- ASSERT_TRUE(
- ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StopWorker::ID));
}
// Test that a worker that failed twice will use a new render process
// on the next attempt.
-TEST_F(EmbeddedWorkerInstanceTest, ForceNewProcess) {
+TEST_F(MojoEmbeddedWorkerInstanceTest, ForceNewProcess) {
+ ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
+
std::unique_ptr<EmbeddedWorkerInstance> worker =
embedded_worker_registry()->CreateWorker();
EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
@@ -364,7 +306,9 @@ TEST_F(EmbeddedWorkerInstanceTest, ForceNewProcess) {
}
}
-TEST_F(EmbeddedWorkerInstanceTest, StopWhenDevToolsAttached) {
+TEST_F(MojoEmbeddedWorkerInstanceTest, StopWhenDevToolsAttached) {
+ ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
+
std::unique_ptr<EmbeddedWorkerInstance> worker =
embedded_worker_registry()->CreateWorker();
EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
@@ -411,7 +355,9 @@ TEST_F(EmbeddedWorkerInstanceTest, StopWhenDevToolsAttached) {
// Test that the removal of a worker from the registry doesn't remove
// other workers in the same process.
-TEST_F(EmbeddedWorkerInstanceTest, RemoveWorkerInSharedProcess) {
+TEST_F(MojoEmbeddedWorkerInstanceTest, RemoveWorkerInSharedProcess) {
+ ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
+
std::unique_ptr<EmbeddedWorkerInstance> worker1 =
embedded_worker_registry()->CreateWorker();
std::unique_ptr<EmbeddedWorkerInstance> worker2 =
@@ -466,7 +412,9 @@ TEST_F(EmbeddedWorkerInstanceTest, RemoveWorkerInSharedProcess) {
worker2->Stop();
}
-TEST_F(EmbeddedWorkerInstanceTest, DetachDuringProcessAllocation) {
+TEST_F(MojoEmbeddedWorkerInstanceTest, DetachDuringProcessAllocation) {
+ ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
+
const int64_t version_id = 55L;
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
@@ -497,7 +445,9 @@ TEST_F(EmbeddedWorkerInstanceTest, DetachDuringProcessAllocation) {
EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[0].status);
}
-TEST_F(EmbeddedWorkerInstanceTest, DetachAfterSendingStartWorkerMessage) {
+TEST_F(MojoEmbeddedWorkerInstanceTest, DetachAfterSendingStartWorkerMessage) {
+ ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
+
const int64_t version_id = 55L;
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
@@ -536,7 +486,9 @@ TEST_F(EmbeddedWorkerInstanceTest, DetachAfterSendingStartWorkerMessage) {
EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[0].status);
}
-TEST_F(EmbeddedWorkerInstanceTest, StopDuringProcessAllocation) {
+TEST_F(MojoEmbeddedWorkerInstanceTest, StopDuringProcessAllocation) {
+ ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
+
const int64_t version_id = 55L;
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
@@ -565,7 +517,6 @@ TEST_F(EmbeddedWorkerInstanceTest, StopDuringProcessAllocation) {
// "PROCESS_ALLOCATED" event should not be recorded.
ASSERT_EQ(1u, events_.size());
EXPECT_EQ(DETACHED, events_[0].type);
- EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[0].status);
events_.clear();
// Restart the worker.
@@ -586,7 +537,9 @@ TEST_F(EmbeddedWorkerInstanceTest, StopDuringProcessAllocation) {
worker->Stop();
}
-TEST_F(EmbeddedWorkerInstanceTest, StopDuringPausedAfterDownload) {
+TEST_F(MojoEmbeddedWorkerInstanceTest, StopDuringPausedAfterDownload) {
+ ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
+
const int64_t version_id = 55L;
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
@@ -617,7 +570,9 @@ TEST_F(EmbeddedWorkerInstanceTest, StopDuringPausedAfterDownload) {
EmbeddedWorkerMsg_ResumeAfterDownload::ID));
}
-TEST_F(EmbeddedWorkerInstanceTest, StopAfterSendingStartWorkerMessage) {
+TEST_F(MojoEmbeddedWorkerInstanceTest, StopAfterSendingStartWorkerMessage) {
+ ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
+
const int64_t version_id = 55L;
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
@@ -678,7 +633,9 @@ TEST_F(EmbeddedWorkerInstanceTest, StopAfterSendingStartWorkerMessage) {
worker->Stop();
}
-TEST_F(EmbeddedWorkerInstanceTest, Detach) {
+TEST_F(MojoEmbeddedWorkerInstanceTest, Detach) {
+ ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
+
const int64_t version_id = 55L;
const GURL pattern("http://example.com/");
const GURL url("http://example.com/worker.js");
@@ -709,35 +666,74 @@ TEST_F(EmbeddedWorkerInstanceTest, Detach) {
EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
}
-// Test for when sending the start IPC failed.
-TEST_F(EmbeddedWorkerInstanceTest, FailToSendStartIPC) {
- helper_.reset(new FailToSendIPCHelper());
+// Test for when sending the start IPC failed: sudden shutdown of remote process
+TEST_F(MojoEmbeddedWorkerInstanceTest, RemoveRemoteInterface) {
+ ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
const int64_t version_id = 55L;
const GURL pattern("http://example.com/");
const GURL url("http://example.com/worker.js");
+ // Let StartWorker fail; binding is discarded in the middle of IPC
+ helper_->PrepareMockInstanceClients(1);
+ ASSERT_EQ(mock_instance_clients()->size(), 1UL);
+ EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient* instance_client =
+ mock_instance_clients()->at(0).get();
+ EXPECT_CALL(*instance_client, MockStartWorker(_))
+ .WillOnce(WithoutArgs(
+ Invoke([this]() { helper_->mock_instance_clients()->clear(); })));
+
std::unique_ptr<EmbeddedWorkerInstance> worker =
embedded_worker_registry()->CreateWorker();
helper_->SimulateAddProcessToPattern(pattern,
helper_->mock_render_process_id());
- ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
worker->AddListener(this);
// Attempt to start the worker.
- base::RunLoop run_loop;
std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params(
CreateStartParams(version_id, pattern, url));
- worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status,
- run_loop.QuitClosure()));
- run_loop.Run();
+ worker->Start(std::move(params), base::Bind(&NoOpStatusCallback));
+ base::RunLoop().RunUntilIdle();
- // The callback should have run, and we should have got an OnStopped message.
- EXPECT_EQ(SERVICE_WORKER_ERROR_IPC_FAILED, status);
- ASSERT_EQ(2u, events_.size());
+ // Worker should handle the sudden shutdown as detach.
+ ASSERT_EQ(3u, events_.size());
EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type);
- EXPECT_EQ(STOPPED, events_[1].type);
- EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[1].status);
+ EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type);
+ EXPECT_EQ(DETACHED, events_[2].type);
+ EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[2].status);
+}
+
+// Test for when sending the start IPC failed: connection failure
+TEST_F(MojoEmbeddedWorkerInstanceTest, FailToSendStartIPC) {
+ ASSERT_TRUE(IsMojoForServiceWorkerEnabled());
+
+ const int64_t version_id = 55L;
+ const GURL pattern("http://example.com/");
+ const GURL url("http://example.com/worker.js");
+
+ // Let StartWorker fail; mojo IPC fails to connect to a remote interface.
+ helper_->PrepareMockInstanceClients(1);
+ ASSERT_EQ(mock_instance_clients()->size(), 1UL);
+ mock_instance_clients()->at(0).reset();
+
+ std::unique_ptr<EmbeddedWorkerInstance> worker =
+ embedded_worker_registry()->CreateWorker();
+ helper_->SimulateAddProcessToPattern(pattern,
+ helper_->mock_render_process_id());
+ worker->AddListener(this);
+
+ // Attempt to start the worker.
+ std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params(
+ CreateStartParams(version_id, pattern, url));
+ worker->Start(std::move(params), base::Bind(&NoOpStatusCallback));
+ base::RunLoop().RunUntilIdle();
+
+ // Worker should handle the failure of binding as detach.
+ ASSERT_EQ(3u, events_.size());
+ EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type);
+ EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type);
+ EXPECT_EQ(DETACHED, events_[2].type);
+ EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[2].status);
}
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698