diff options
author | satorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-20 01:07:17 +0000 |
---|---|---|
committer | satorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-20 01:07:17 +0000 |
commit | 12f9766f28712f8ec3755db4558dba4db5bd8a70 (patch) | |
tree | 6ab911b0b85b1851bef201f25825911d4a66849e /dbus/test_service.cc | |
parent | 940895b5308ace929396a87bd13cf0764f0c16e2 (diff) | |
download | chromium_src-12f9766f28712f8ec3755db4558dba4db5bd8a70.zip chromium_src-12f9766f28712f8ec3755db4558dba4db5bd8a70.tar.gz chromium_src-12f9766f28712f8ec3755db4558dba4db5bd8a70.tar.bz2 |
Rework TestService using asynchronos API of ExportedObject.
This change is to to exercise asynchronos API of ExportedObject.
The asynchronos API is implemented on top the synchronos API,
hence the synchronos API code is also covered.
Along the way, change EndToEndAsyncTest to use the D-Bus thread.
Simplified the test code per phajdan.jr's comments as well.
TEST=dbus_unittests
BUG=90036
Review URL: http://codereview.chromium.org/7671028
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@97540 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'dbus/test_service.cc')
-rw-r--r-- | dbus/test_service.cc | 113 |
1 files changed, 87 insertions, 26 deletions
diff --git a/dbus/test_service.cc b/dbus/test_service.cc index c25f803..a3bd001 100644 --- a/dbus/test_service.cc +++ b/dbus/test_service.cc @@ -5,6 +5,7 @@ #include "dbus/test_service.h" #include "base/bind.h" +#include "base/test/test_timeouts.h" #include "base/threading/platform_thread.h" #include "dbus/bus.h" #include "dbus/exported_object.h" @@ -12,71 +13,131 @@ namespace dbus { -const int TestService::kSlowEchoSleepMs = 100; // In milliseconds. +// Echo, SlowEcho, BrokenMethod. +const int TestService::kNumMethodsToExport = 3; -TestService::TestService() +TestService::Options::Options() + : dbus_thread(NULL) { +} + +TestService::Options::~Options() { +} + +TestService::TestService(const Options& options) : base::Thread("TestService"), - service_started_(false), - on_service_started_(&service_started_lock_) { + dbus_thread_(options.dbus_thread), + on_shutdown_(false /* manual_reset */, false /* initially_signaled */), + on_all_methods_exported_(false, false), + num_exported_methods_(0) { } TestService::~TestService() { } -void TestService::StartService() { +bool TestService::StartService() { base::Thread::Options thread_options; thread_options.message_loop_type = MessageLoop::TYPE_IO; - StartWithOptions(thread_options); + return StartWithOptions(thread_options); +} + +bool TestService::WaitUntilServiceIsStarted() { + const base::TimeDelta timeout( + base::TimeDelta::FromMilliseconds( + TestTimeouts::action_max_timeout_ms())); + // Wait until all methods are exported. + return on_all_methods_exported_.TimedWait(timeout); } -void TestService::WaitUntilServiceIsStarted() { +void TestService::Shutdown() { message_loop()->PostTask( FROM_HERE, - base::Bind(&TestService::OnServiceStarted, + base::Bind(&TestService::ShutdownInternal, base::Unretained(this))); - base::AutoLock auto_lock(service_started_lock_); - while (!service_started_) - on_service_started_.Wait(); } -void TestService::OnServiceStarted() { - base::AutoLock auto_lock(service_started_lock_); - service_started_ = true; - on_service_started_.Signal(); +bool TestService::WaitUntilServiceIsShutdown() { + const base::TimeDelta timeout( + base::TimeDelta::FromMilliseconds( + TestTimeouts::action_max_timeout_ms())); + return on_shutdown_.TimedWait(timeout); +} + +bool TestService::HasDBusThread() { + return bus_->HasDBusThread(); +} + +void TestService::ShutdownInternal() { + bus_->Shutdown(base::Bind(&TestService::OnShutdown, + base::Unretained(this))); +} + +void TestService::OnExported(const std::string& interface_name, + const std::string& method_name, + bool success) { + if (!success) { + LOG(ERROR) << "Failed to export: " << interface_name << "." + << method_name; + // Returning here will make WaitUntilServiceIsStarted() to time out + // and return false. + return; + } + + ++num_exported_methods_; + if (num_exported_methods_ == kNumMethodsToExport) + on_all_methods_exported_.Signal(); +} + +void TestService::OnShutdown() { + on_shutdown_.Signal(); } void TestService::Run(MessageLoop* message_loop) { Bus::Options bus_options; bus_options.bus_type = Bus::SESSION; bus_options.connection_type = Bus::PRIVATE; + bus_options.dbus_thread = dbus_thread_; bus_ = new Bus(bus_options); exported_object_ = bus_->GetExportedObject( "org.chromium.TestService", "/org/chromium/TestObject"); - CHECK(exported_object_->ExportMethodAndBlock( + + int num_methods = 0; + exported_object_->ExportMethod( "org.chromium.TestInterface", "Echo", base::Bind(&TestService::Echo, - base::Unretained(this)))); - CHECK(exported_object_->ExportMethodAndBlock( + base::Unretained(this)), + base::Bind(&TestService::OnExported, + base::Unretained(this))); + ++num_methods; + + exported_object_->ExportMethod( "org.chromium.TestInterface", "SlowEcho", base::Bind(&TestService::SlowEcho, - base::Unretained(this)))); - CHECK(exported_object_->ExportMethodAndBlock( + base::Unretained(this)), + base::Bind(&TestService::OnExported, + base::Unretained(this))); + ++num_methods; + + exported_object_->ExportMethod( "org.chromium.TestInterface", "BrokenMethod", base::Bind(&TestService::BrokenMethod, - base::Unretained(this)))); + base::Unretained(this)), + base::Bind(&TestService::OnExported, + base::Unretained(this))); + ++num_methods; + // Just print an error message as we don't want to crash tests. + // Tests will fail at a call to WaitUntilServiceIsStarted(). + if (num_methods != kNumMethodsToExport) { + LOG(ERROR) << "The number of methods does not match"; + } message_loop->Run(); } -void TestService::CleanUp() { - bus_->ShutdownAndBlock(); -} - Response* TestService::Echo(MethodCall* method_call) { MessageReader reader(method_call); std::string text_message; @@ -90,7 +151,7 @@ Response* TestService::Echo(MethodCall* method_call) { } Response* TestService::SlowEcho(MethodCall* method_call) { - base::PlatformThread::Sleep(kSlowEchoSleepMs); + base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms()); return Echo(method_call); } |