From 067d84f52f2ec47c2614108be6bf0e11552eb34b Mon Sep 17 00:00:00 2001 From: hashimoto Date: Tue, 5 Jan 2016 00:48:03 -0800 Subject: dbus: Use randomly generated string as a TestService's service name To run tests parallelly. BUG=571274 TEST=dbus_unittests Review URL: https://codereview.chromium.org/1559873005 Cr-Commit-Position: refs/heads/master@{#367499} --- dbus/end_to_end_async_unittest.cc | 10 +++++----- dbus/end_to_end_sync_unittest.cc | 4 ++-- dbus/object_manager_unittest.cc | 12 ++++++------ dbus/object_proxy_unittest.cc | 15 +++++++-------- dbus/property_unittest.cc | 2 +- dbus/signal_sender_verification_unittest.cc | 16 ++++++++++------ dbus/test_service.cc | 13 +++++++++---- dbus/test_service.h | 9 +++++++++ 8 files changed, 49 insertions(+), 32 deletions(-) (limited to 'dbus') diff --git a/dbus/end_to_end_async_unittest.cc b/dbus/end_to_end_async_unittest.cc index 121c660..8f62864 100644 --- a/dbus/end_to_end_async_unittest.cc +++ b/dbus/end_to_end_async_unittest.cc @@ -63,7 +63,7 @@ class EndToEndAsyncTest : public testing::Test { bus_options.dbus_task_runner = dbus_thread_->task_runner(); bus_ = new Bus(bus_options); object_proxy_ = bus_->GetObjectProxy( - "org.chromium.TestService", + test_service_->service_name(), ObjectPath("/org/chromium/TestObject")); ASSERT_TRUE(bus_->HasDBusThread()); @@ -97,7 +97,7 @@ class EndToEndAsyncTest : public testing::Test { run_loop_->Run(); // Create a second object proxy for the root object. - root_object_proxy_ = bus_->GetObjectProxy("org.chromium.TestService", + root_object_proxy_ = bus_->GetObjectProxy(test_service_->service_name(), ObjectPath("/")); ASSERT_TRUE(bus_->HasDBusThread()); @@ -147,7 +147,7 @@ class EndToEndAsyncTest : public testing::Test { // Create new object proxy. object_proxy_ = bus_->GetObjectProxy( - "org.chromium.TestService", + test_service_->service_name(), ObjectPath("/org/chromium/TestObject")); } @@ -435,7 +435,7 @@ TEST_F(EndToEndAsyncTest, CancelPendingCalls) { // Remove the object proxy before receiving the result. // This results in cancelling the pending method call. - bus_->RemoveObjectProxy("org.chromium.TestService", + bus_->RemoveObjectProxy(test_service_->service_name(), ObjectPath("/org/chromium/TestObject"), base::Bind(&base::DoNothing)); @@ -517,7 +517,7 @@ TEST_F(EndToEndAsyncTest, InvalidObjectPath) { const ObjectPath invalid_object_path("/org/chromium/TestObject/"); // Replace object proxy with new one. - object_proxy_ = bus_->GetObjectProxy("org.chromium.TestService", + object_proxy_ = bus_->GetObjectProxy(test_service_->service_name(), invalid_object_path); MethodCall method_call("org.chromium.TestInterface", "Echo"); diff --git a/dbus/end_to_end_sync_unittest.cc b/dbus/end_to_end_sync_unittest.cc index 1167d96..47dc9b1 100644 --- a/dbus/end_to_end_sync_unittest.cc +++ b/dbus/end_to_end_sync_unittest.cc @@ -35,7 +35,7 @@ class EndToEndSyncTest : public testing::Test { client_bus_options.connection_type = Bus::PRIVATE; client_bus_ = new Bus(client_bus_options); object_proxy_ = client_bus_->GetObjectProxy( - "org.chromium.TestService", + test_service_->service_name(), ObjectPath("/org/chromium/TestObject")); ASSERT_FALSE(client_bus_->HasDBusThread()); } @@ -112,7 +112,7 @@ TEST_F(EndToEndSyncTest, InvalidObjectPath) { const ObjectPath invalid_object_path("/org/chromium/TestObject/"); // Replace object proxy with new one. - object_proxy_ = client_bus_->GetObjectProxy("org.chromium.TestService", + object_proxy_ = client_bus_->GetObjectProxy(test_service_->service_name(), invalid_object_path); MethodCall method_call("org.chromium.TestInterface", "Echo"); diff --git a/dbus/object_manager_unittest.cc b/dbus/object_manager_unittest.cc index fa83034..443210c 100644 --- a/dbus/object_manager_unittest.cc +++ b/dbus/object_manager_unittest.cc @@ -87,7 +87,7 @@ class ObjectManagerTest ASSERT_TRUE(bus_->HasDBusThread()); object_manager_ = bus_->GetObjectManager( - "org.chromium.TestService", + test_service_->service_name(), ObjectPath("/org/chromium/TestService")); object_manager_->RegisterInterface("org.chromium.TestInterface", this); @@ -189,7 +189,7 @@ class ObjectManagerTest void PerformAction(const std::string& action, const ObjectPath& object_path) { ObjectProxy* object_proxy = bus_->GetObjectProxy( - "org.chromium.TestService", + test_service_->service_name(), ObjectPath("/org/chromium/TestObject")); MethodCall method_call("org.chromium.TestInterface", "PerformAction"); @@ -288,7 +288,7 @@ TEST_F(ObjectManagerTest, GetObjectsWithUnknownInterface) { TEST_F(ObjectManagerTest, SameObject) { ObjectManager* object_manager = bus_->GetObjectManager( - "org.chromium.TestService", + test_service_->service_name(), ObjectPath("/org/chromium/TestService")); EXPECT_EQ(object_manager_, object_manager); } @@ -302,7 +302,7 @@ TEST_F(ObjectManagerTest, DifferentObjectForService) { TEST_F(ObjectManagerTest, DifferentObjectForPath) { ObjectManager* object_manager = bus_->GetObjectManager( - "org.chromium.TestService", + test_service_->service_name(), ObjectPath("/org/chromium/DifferentService")); EXPECT_NE(object_manager_, object_manager); } @@ -387,7 +387,7 @@ TEST_F(ObjectManagerTest, PropertiesChangedAsObjectsReceived) { object_manager_->UnregisterInterface("org.chromium.TestInterface"); run_loop_.reset(new base::RunLoop); EXPECT_TRUE(bus_->RemoveObjectManager( - "org.chromium.TestService", + test_service_->service_name(), ObjectPath("/org/chromium/TestService"), run_loop_->QuitClosure())); run_loop_->Run(); @@ -396,7 +396,7 @@ TEST_F(ObjectManagerTest, PropertiesChangedAsObjectsReceived) { ObjectPath("/org/chromium/TestService")); object_manager_ = bus_->GetObjectManager( - "org.chromium.TestService", + test_service_->service_name(), ObjectPath("/org/chromium/TestService")); object_manager_->RegisterInterface("org.chromium.TestInterface", this); diff --git a/dbus/object_proxy_unittest.cc b/dbus/object_proxy_unittest.cc index 22130b6..05c1294 100644 --- a/dbus/object_proxy_unittest.cc +++ b/dbus/object_proxy_unittest.cc @@ -20,16 +20,12 @@ class ObjectProxyTest : public testing::Test { bus_options.bus_type = Bus::SESSION; bus_options.connection_type = Bus::PRIVATE; bus_ = new Bus(bus_options); - - object_proxy_ = bus_->GetObjectProxy( - "org.chromium.TestService", ObjectPath("/org/chromium/TestObject")); } void TearDown() override { bus_->ShutdownAndBlock(); } base::MessageLoopForIO message_loop_; scoped_refptr bus_; - ObjectProxy* object_proxy_; }; // Used as a WaitForServiceToBeAvailableCallback. @@ -43,14 +39,17 @@ void OnServiceIsAvailable(scoped_ptr* run_loop, TEST_F(ObjectProxyTest, WaitForServiceToBeAvailable) { scoped_ptr run_loop; + TestService::Options options; + TestService test_service(options); + // Callback is not yet called because the service is not available. - object_proxy_->WaitForServiceToBeAvailable( + ObjectProxy* object_proxy = bus_->GetObjectProxy( + test_service.service_name(), ObjectPath("/org/chromium/TestObject")); + object_proxy->WaitForServiceToBeAvailable( base::Bind(&OnServiceIsAvailable, &run_loop)); base::RunLoop().RunUntilIdle(); // Start the service. - TestService::Options options; - TestService test_service(options); ASSERT_TRUE(test_service.StartService()); ASSERT_TRUE(test_service.WaitUntilServiceIsStarted()); ASSERT_TRUE(test_service.has_ownership()); @@ -61,7 +60,7 @@ TEST_F(ObjectProxyTest, WaitForServiceToBeAvailable) { // Callback is called because the service is already available. run_loop.reset(new base::RunLoop); - object_proxy_->WaitForServiceToBeAvailable( + object_proxy->WaitForServiceToBeAvailable( base::Bind(&OnServiceIsAvailable, &run_loop)); run_loop->Run(); diff --git a/dbus/property_unittest.cc b/dbus/property_unittest.cc index b6bbb18..f159284 100644 --- a/dbus/property_unittest.cc +++ b/dbus/property_unittest.cc @@ -77,7 +77,7 @@ class PropertyTest : public testing::Test { bus_options.dbus_task_runner = dbus_thread_->task_runner(); bus_ = new Bus(bus_options); object_proxy_ = bus_->GetObjectProxy( - "org.chromium.TestService", + test_service_->service_name(), ObjectPath("/org/chromium/TestObject")); ASSERT_TRUE(bus_->HasDBusThread()); diff --git a/dbus/signal_sender_verification_unittest.cc b/dbus/signal_sender_verification_unittest.cc index 0cedda7..785948a 100644 --- a/dbus/signal_sender_verification_unittest.cc +++ b/dbus/signal_sender_verification_unittest.cc @@ -40,6 +40,11 @@ class SignalSenderVerificationTest : public testing::Test { thread_options.message_loop_type = base::MessageLoop::TYPE_IO; ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); + // Create the test service, using the D-Bus thread. + TestService::Options options; + options.dbus_task_runner = dbus_thread_->task_runner(); + test_service_.reset(new TestService(options)); + // Create the client, using the D-Bus thread. Bus::Options bus_options; bus_options.bus_type = Bus::SESSION; @@ -47,7 +52,7 @@ class SignalSenderVerificationTest : public testing::Test { bus_options.dbus_task_runner = dbus_thread_->task_runner(); bus_ = new Bus(bus_options); object_proxy_ = bus_->GetObjectProxy( - "org.chromium.TestService", + test_service_->service_name(), ObjectPath("/org/chromium/TestObject")); ASSERT_TRUE(bus_->HasDBusThread()); @@ -69,10 +74,7 @@ class SignalSenderVerificationTest : public testing::Test { run_loop_.reset(new base::RunLoop); run_loop_->Run(); - // Start the test service, using the D-Bus thread. - TestService::Options options; - options.dbus_task_runner = dbus_thread_->task_runner(); - test_service_.reset(new TestService(options)); + // Start the test service. ASSERT_TRUE(test_service_->StartService()); ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); ASSERT_TRUE(test_service_->HasDBusThread()); @@ -80,6 +82,7 @@ class SignalSenderVerificationTest : public testing::Test { // Same setup for the second TestService. This service should not have the // ownership of the name at this point. + options.service_name = test_service_->service_name(); test_service2_.reset(new TestService(options)); ASSERT_TRUE(test_service2_->StartService()); ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted()); @@ -280,6 +283,7 @@ TEST_F(SignalSenderVerificationTest, TestOwnerStealing) { TestService::Options options; options.dbus_task_runner = dbus_thread_->task_runner(); options.request_ownership_options = Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT; + options.service_name = test_service_->service_name(); TestService stealable_test_service(options); ASSERT_TRUE(stealable_test_service.StartService()); ASSERT_TRUE(stealable_test_service.WaitUntilServiceIsStarted()); @@ -331,7 +335,7 @@ TEST_F(SignalSenderVerificationTest, DISABLED_TestMultipleObjects) { const char kMessage[] = "hello, world"; ObjectProxy* object_proxy2 = bus_->GetObjectProxy( - "org.chromium.TestService", + test_service_->service_name(), ObjectPath("/org/chromium/DifferentObject")); bool second_name_owner_changed_called = false; diff --git a/dbus/test_service.cc b/dbus/test_service.cc index a4e152a..7b36082 100644 --- a/dbus/test_service.cc +++ b/dbus/test_service.cc @@ -10,6 +10,7 @@ #include #include "base/bind.h" +#include "base/guid.h" #include "base/test/test_timeouts.h" #include "base/threading/platform_thread.h" #include "dbus/bus.h" @@ -41,6 +42,7 @@ TestService::Options::~Options() { TestService::TestService(const Options& options) : base::Thread("TestService"), + service_name_(options.service_name), request_ownership_options_(options.request_ownership_options), dbus_task_runner_(options.dbus_task_runner), on_name_obtained_(false, false), @@ -49,6 +51,9 @@ TestService::TestService(const Options& options) has_ownership_(false), exported_object_(NULL), exported_object_manager_(NULL) { + if (service_name_.empty()) { + service_name_ = "org.chromium.TestService-" + base::GenerateGUID(); + } } TestService::~TestService() { @@ -113,7 +118,7 @@ void TestService::SendTestSignalFromRootInternal(const std::string& message) { MessageWriter writer(&signal); writer.AppendString(message); - bus_->RequestOwnership("org.chromium.TestService", + bus_->RequestOwnership(service_name_, request_ownership_options_, base::Bind(&TestService::OnOwnership, base::Unretained(this), @@ -134,7 +139,7 @@ void TestService::RequestOwnership(base::Callback callback) { void TestService::RequestOwnershipInternal( base::Callback callback) { - bus_->RequestOwnership("org.chromium.TestService", + bus_->RequestOwnership(service_name_, request_ownership_options_, base::Bind(&TestService::OnOwnership, base::Unretained(this), @@ -161,7 +166,7 @@ void TestService::ReleaseOwnership(base::Closure callback) { void TestService::ReleaseOwnershipInternal( base::Closure callback) { - bus_->ReleaseOwnership("org.chromium.TestService"); + bus_->ReleaseOwnership(service_name_); has_ownership_ = false; bus_->GetOriginTaskRunner()->PostTask( @@ -188,7 +193,7 @@ void TestService::OnExported(const std::string& interface_name, if (num_exported_methods_ == kNumMethodsToExport) { // As documented in exported_object.h, the service name should be // requested after all methods are exposed. - bus_->RequestOwnership("org.chromium.TestService", + bus_->RequestOwnership(service_name_, request_ownership_options_, base::Bind(&TestService::OnOwnership, base::Unretained(this), diff --git a/dbus/test_service.h b/dbus/test_service.h index de4aa72..e2ed480 100644 --- a/dbus/test_service.h +++ b/dbus/test_service.h @@ -40,6 +40,9 @@ class TestService : public base::Thread { // Flags governing parameters of service ownership request. Bus::ServiceOwnershipOptions request_ownership_options; + + // Name of this service (randomly generated name will be used if empty). + std::string service_name; }; // The number of methods we'll export. @@ -78,6 +81,9 @@ class TestService : public base::Thread { // |callback| will be called when the ownership has been released. void ReleaseOwnership(base::Closure callback); + // Returns the name of this service. + const std::string& service_name() const { return service_name_; } + // Returns whether this instance has the name ownership or not. bool has_ownership() const { return has_ownership_; } @@ -199,6 +205,9 @@ class TestService : public base::Thread { dbus::ExportedObject::ResponseSender response_sender, bool success); + // Name of this service. + std::string service_name_; + // Options to use when requesting service ownership. Bus::ServiceOwnershipOptions request_ownership_options_; -- cgit v1.1