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

Side by Side Diff: dbus/signal_sender_verification_unittest.cc

Issue 11358111: Make SignalSenderVerificationTest more robust (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « no previous file | dbus/test_service.h » ('j') | dbus/test_service.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/memory/scoped_ptr.h" 6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/metrics/histogram.h" 8 #include "base/metrics/histogram.h"
9 #include "base/metrics/histogram_samples.h" 9 #include "base/metrics/histogram_samples.h"
10 #include "base/metrics/statistics_recorder.h" 10 #include "base/metrics/statistics_recorder.h"
(...skipping 24 matching lines...) Expand all
35 thread_options.message_loop_type = MessageLoop::TYPE_IO; 35 thread_options.message_loop_type = MessageLoop::TYPE_IO;
36 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); 36 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
37 37
38 // Start the test service, using the D-Bus thread. 38 // Start the test service, using the D-Bus thread.
39 dbus::TestService::Options options; 39 dbus::TestService::Options options;
40 options.dbus_thread_message_loop_proxy = dbus_thread_->message_loop_proxy(); 40 options.dbus_thread_message_loop_proxy = dbus_thread_->message_loop_proxy();
41 test_service_.reset(new dbus::TestService(options)); 41 test_service_.reset(new dbus::TestService(options));
42 ASSERT_TRUE(test_service_->StartService()); 42 ASSERT_TRUE(test_service_->StartService());
43 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); 43 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
44 ASSERT_TRUE(test_service_->HasDBusThread()); 44 ASSERT_TRUE(test_service_->HasDBusThread());
45 ASSERT_TRUE(test_service_->HasOwnership());
45 46
46 // Same setup for the second TestService. This service should not have the 47 // Same setup for the second TestService. This service should not have the
47 // ownership of the name at this point. 48 // ownership of the name at this point.
48 test_service2_.reset(new dbus::TestService(options)); 49 test_service2_.reset(new dbus::TestService(options));
49 ASSERT_TRUE(test_service2_->StartService()); 50 ASSERT_TRUE(test_service2_->StartService());
50 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted()); 51 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted());
51 ASSERT_TRUE(test_service2_->HasDBusThread()); 52 ASSERT_TRUE(test_service2_->HasDBusThread());
53 ASSERT_FALSE(test_service2_->HasOwnership());
52 54
53 // Create the client, using the D-Bus thread. 55 // Create the client, using the D-Bus thread.
54 dbus::Bus::Options bus_options; 56 dbus::Bus::Options bus_options;
55 bus_options.bus_type = dbus::Bus::SESSION; 57 bus_options.bus_type = dbus::Bus::SESSION;
56 bus_options.connection_type = dbus::Bus::PRIVATE; 58 bus_options.connection_type = dbus::Bus::PRIVATE;
57 bus_options.dbus_thread_message_loop_proxy = 59 bus_options.dbus_thread_message_loop_proxy =
58 dbus_thread_->message_loop_proxy(); 60 dbus_thread_->message_loop_proxy();
59 bus_ = new dbus::Bus(bus_options); 61 bus_ = new dbus::Bus(bus_options);
60 object_proxy_ = bus_->GetObjectProxy( 62 object_proxy_ = bus_->GetObjectProxy(
61 "org.chromium.TestService", 63 "org.chromium.TestService",
62 dbus::ObjectPath("/org/chromium/TestObject")); 64 dbus::ObjectPath("/org/chromium/TestObject"));
63 ASSERT_TRUE(bus_->HasDBusThread()); 65 ASSERT_TRUE(bus_->HasDBusThread());
64 66
65 // Connect to the "Test" signal of "org.chromium.TestInterface" from 67 // Connect to the "Test" signal of "org.chromium.TestInterface" from
66 // the remote object. 68 // the remote object.
67 object_proxy_->ConnectToSignal( 69 object_proxy_->ConnectToSignal(
68 "org.chromium.TestInterface", 70 "org.chromium.TestInterface",
69 "Test", 71 "Test",
70 base::Bind(&SignalSenderVerificationTest::OnTestSignal, 72 base::Bind(&SignalSenderVerificationTest::OnTestSignal,
71 base::Unretained(this)), 73 base::Unretained(this)),
72 base::Bind(&SignalSenderVerificationTest::OnConnected, 74 base::Bind(&SignalSenderVerificationTest::OnConnected,
73 base::Unretained(this))); 75 base::Unretained(this)));
74 // Wait until the object proxy is connected to the signal. 76 // Wait until the object proxy is connected to the signal.
75 message_loop_.Run(); 77 message_loop_.Run();
76 } 78 }
77 79
80 void OnOwnership(bool expected, bool success) {
81 ASSERT_EQ(expected, success);
82 message_loop_.PostTask(
83 FROM_HERE,
84 base::Bind(&SignalSenderVerificationTest::QuitMessageLoop,
85 base::Unretained(this)));
86 }
87
78 virtual void TearDown() { 88 virtual void TearDown() {
79 bus_->ShutdownOnDBusThreadAndBlock(); 89 bus_->ShutdownOnDBusThreadAndBlock();
80 90
81 // Shut down the service. 91 // Shut down the service.
82 test_service_->ShutdownAndBlock(); 92 test_service_->ShutdownAndBlock();
83 test_service2_->ShutdownAndBlock(); 93 test_service2_->ShutdownAndBlock();
84 94
85 // Reset to the default. 95 // Reset to the default.
86 base::ThreadRestrictions::SetIOAllowed(true); 96 base::ThreadRestrictions::SetIOAllowed(true);
87 97
(...skipping 20 matching lines...) Expand all
108 ASSERT_TRUE(success); 118 ASSERT_TRUE(success);
109 message_loop_.Quit(); 119 message_loop_.Quit();
110 } 120 }
111 121
112 // Wait for the hey signal to be received. 122 // Wait for the hey signal to be received.
113 void WaitForTestSignal() { 123 void WaitForTestSignal() {
114 // OnTestSignal() will quit the message loop. 124 // OnTestSignal() will quit the message loop.
115 message_loop_.Run(); 125 message_loop_.Run();
116 } 126 }
117 127
128 void QuitMessageLoop() {
129 message_loop_.Quit();
130 }
131
118 MessageLoop message_loop_; 132 MessageLoop message_loop_;
119 scoped_ptr<base::Thread> dbus_thread_; 133 scoped_ptr<base::Thread> dbus_thread_;
120 scoped_refptr<dbus::Bus> bus_; 134 scoped_refptr<dbus::Bus> bus_;
121 dbus::ObjectProxy* object_proxy_; 135 dbus::ObjectProxy* object_proxy_;
122 scoped_ptr<dbus::TestService> test_service_; 136 scoped_ptr<dbus::TestService> test_service_;
123 scoped_ptr<dbus::TestService> test_service2_; 137 scoped_ptr<dbus::TestService> test_service2_;
124 // Text message from "Test" signal. 138 // Text message from "Test" signal.
125 std::string test_signal_string_; 139 std::string test_signal_string_;
126 }; 140 };
127 141
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 176
163 // Send the test signal from the exported object. 177 // Send the test signal from the exported object.
164 test_service_->SendTestSignal(kMessage); 178 test_service_->SendTestSignal(kMessage);
165 // Receive the signal with the object proxy. The signal is handled in 179 // Receive the signal with the object proxy. The signal is handled in
166 // SignalSenderVerificationTest::OnTestSignal() in the main thread. 180 // SignalSenderVerificationTest::OnTestSignal() in the main thread.
167 WaitForTestSignal(); 181 WaitForTestSignal();
168 ASSERT_EQ(kMessage, test_signal_string_); 182 ASSERT_EQ(kMessage, test_signal_string_);
169 183
170 // Release and aquire the name ownership. 184 // Release and aquire the name ownership.
171 test_service_->ShutdownAndBlock(); 185 test_service_->ShutdownAndBlock();
172 test_service2_->RequestOwnership(); 186 // It takes some time for D-Bus to free the ownership of "TestService".
187 base::PlatformThread::Sleep(TestTimeouts::action_timeout());
satorux1 2012/11/07 01:42:25 Sleep should be avoided. Can we monitor the owners
Haruki Sato 2012/11/12 04:59:43 Thank you for the advice! Replaced with NameOwnerC
188 test_service2_->RequestOwnership(
189 base::Bind(&SignalSenderVerificationTest::OnOwnership,
190 base::Unretained(this),
191 true));
192 // OnOwnership will PostTask to quit the message loop.
193 message_loop_.Run();
194 ASSERT_TRUE(test_service2_->HasOwnership());
173 195
174 // Now the second service owns the name. 196 // Now the second service owns the name.
175 const char kNewMessage[] = "hello, new world"; 197 const char kNewMessage[] = "hello, new world";
176 198
177 test_service2_->SendTestSignal(kNewMessage); 199 test_service2_->SendTestSignal(kNewMessage);
178 WaitForTestSignal(); 200 WaitForTestSignal();
179 ASSERT_EQ(kNewMessage, test_signal_string_); 201 ASSERT_EQ(kNewMessage, test_signal_string_);
180 } 202 }
OLDNEW
« no previous file with comments | « no previous file | dbus/test_service.h » ('j') | dbus/test_service.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698