blob: 029e49c667c09e2d0cff09e4092c090db556f388 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294//
5// Unit test for SyncChannel.
6
[email protected]22b42c52010-12-20 06:59:237#include "ipc/ipc_sync_channel.h"
8
initial.commit09911bf2008-07-26 23:55:299#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
[email protected]72b6f8e22011-11-12 21:16:4113#include "base/bind.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1515#include "base/memory/scoped_ptr.h"
initial.commit09911bf2008-07-26 23:55:2916#include "base/message_loop.h"
[email protected]7286e3fc2011-07-19 22:13:2417#include "base/stl_util.h"
initial.commit09911bf2008-07-26 23:55:2918#include "base/string_util.h"
[email protected]ee857512010-05-14 08:24:4219#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
[email protected]f214f8792011-01-01 02:17:0820#include "base/threading/platform_thread.h"
[email protected]34b99632011-01-01 01:01:0621#include "base/threading/thread.h"
[email protected]44f9c952011-01-02 06:05:3922#include "base/synchronization/waitable_event.h"
[email protected]946d1b22009-07-22 23:57:2123#include "ipc/ipc_message.h"
[email protected]1e9499c2010-04-06 20:33:3624#include "ipc/ipc_sync_message_filter.h"
[email protected]21fa3a12010-12-08 23:34:1625#include "ipc/ipc_sync_message_unittest.h"
initial.commit09911bf2008-07-26 23:55:2926#include "testing/gtest/include/gtest/gtest.h"
27
[email protected]aa96ae772009-01-20 22:08:1528using base::WaitableEvent;
initial.commit09911bf2008-07-26 23:55:2929
[email protected]c9e0c7332011-05-13 22:42:4130namespace IPC {
31
[email protected]dd3eac22008-08-26 07:28:3432namespace {
33
initial.commit09911bf2008-07-26 23:55:2934// Base class for a "process" with listener and IPC threads.
35class Worker : public Channel::Listener, public Message::Sender {
36 public:
37 // Will create a channel without a name.
38 Worker(Channel::Mode mode, const std::string& thread_name)
[email protected]aa96ae772009-01-20 22:08:1539 : done_(new WaitableEvent(false, false)),
40 channel_created_(new WaitableEvent(false, false)),
initial.commit09911bf2008-07-26 23:55:2941 mode_(mode),
42 ipc_thread_((thread_name + "_ipc").c_str()),
43 listener_thread_((thread_name + "_listener").c_str()),
[email protected]8930d472009-02-21 08:05:2844 overrided_thread_(NULL),
[email protected]8a734422009-10-27 11:28:5845 shutdown_event_(true, false) {
46 // The data race on vfptr is real but is very hard
47 // to suppress using standard Valgrind mechanism (suppressions).
48 // We have to use ANNOTATE_BENIGN_RACE to hide the reports and
49 // make ThreadSanitizer bots green.
50 ANNOTATE_BENIGN_RACE(this, "Race on vfptr, http://crbug.com/25841");
51 }
initial.commit09911bf2008-07-26 23:55:2952
53 // Will create a named channel and use this name for the threads' name.
[email protected]9a3a293b2009-06-04 22:28:1654 Worker(const std::string& channel_name, Channel::Mode mode)
[email protected]aa96ae772009-01-20 22:08:1555 : done_(new WaitableEvent(false, false)),
56 channel_created_(new WaitableEvent(false, false)),
57 channel_name_(channel_name),
initial.commit09911bf2008-07-26 23:55:2958 mode_(mode),
[email protected]9a3a293b2009-06-04 22:28:1659 ipc_thread_((channel_name + "_ipc").c_str()),
60 listener_thread_((channel_name + "_listener").c_str()),
[email protected]8930d472009-02-21 08:05:2861 overrided_thread_(NULL),
[email protected]8a734422009-10-27 11:28:5862 shutdown_event_(true, false) {
63 // The data race on vfptr is real but is very hard
64 // to suppress using standard Valgrind mechanism (suppressions).
65 // We have to use ANNOTATE_BENIGN_RACE to hide the reports and
66 // make ThreadSanitizer bots green.
67 ANNOTATE_BENIGN_RACE(this, "Race on vfptr, http://crbug.com/25841");
68 }
initial.commit09911bf2008-07-26 23:55:2969
70 // The IPC thread needs to outlive SyncChannel, so force the correct order of
71 // destruction.
72 virtual ~Worker() {
[email protected]aa96ae772009-01-20 22:08:1573 WaitableEvent listener_done(false, false), ipc_done(false, false);
[email protected]72b6f8e22011-11-12 21:16:4174 ListenerThread()->message_loop()->PostTask(
75 FROM_HERE, base::Bind(&Worker::OnListenerThreadShutdown1, this,
76 &listener_done, &ipc_done));
[email protected]aa96ae772009-01-20 22:08:1577 listener_done.Wait();
78 ipc_done.Wait();
initial.commit09911bf2008-07-26 23:55:2979 ipc_thread_.Stop();
80 listener_thread_.Stop();
initial.commit09911bf2008-07-26 23:55:2981 }
82 void AddRef() { }
83 void Release() { }
[email protected]39fe32a2009-09-30 04:29:2084 static bool ImplementsThreadSafeReferenceCounting() { return true; }
initial.commit09911bf2008-07-26 23:55:2985 bool Send(Message* msg) { return channel_->Send(msg); }
[email protected]d65cab7a2008-08-12 01:25:4186 bool SendWithTimeout(Message* msg, int timeout_ms) {
87 return channel_->SendWithTimeout(msg, timeout_ms);
88 }
[email protected]aa96ae772009-01-20 22:08:1589 void WaitForChannelCreation() { channel_created_->Wait(); }
[email protected]3cdb7af812008-10-24 19:21:1390 void CloseChannel() {
91 DCHECK(MessageLoop::current() == ListenerThread()->message_loop());
92 channel_->Close();
93 }
initial.commit09911bf2008-07-26 23:55:2994 void Start() {
[email protected]17b89142008-11-07 21:52:1595 StartThread(&listener_thread_, MessageLoop::TYPE_DEFAULT);
[email protected]72b6f8e22011-11-12 21:16:4196 ListenerThread()->message_loop()->PostTask(
97 FROM_HERE, base::Bind(&Worker::OnStart, this));
initial.commit09911bf2008-07-26 23:55:2998 }
[email protected]ab820df2008-08-26 05:55:1099 void OverrideThread(base::Thread* overrided_thread) {
initial.commit09911bf2008-07-26 23:55:29100 DCHECK(overrided_thread_ == NULL);
101 overrided_thread_ = overrided_thread;
102 }
[email protected]4df10d612008-11-12 00:38:26103 bool SendAnswerToLife(bool pump, int timeout, bool succeed) {
104 int answer = 0;
105 SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer);
106 if (pump)
107 msg->EnableMessagePumping();
108 bool result = SendWithTimeout(msg, timeout);
[email protected]9eec2252009-12-01 02:34:18109 DCHECK_EQ(result, succeed);
110 DCHECK_EQ(answer, (succeed ? 42 : 0));
[email protected]4df10d612008-11-12 00:38:26111 return result;
112 }
113 bool SendDouble(bool pump, bool succeed) {
114 int answer = 0;
115 SyncMessage* msg = new SyncChannelTestMsg_Double(5, &answer);
116 if (pump)
117 msg->EnableMessagePumping();
118 bool result = Send(msg);
[email protected]9eec2252009-12-01 02:34:18119 DCHECK_EQ(result, succeed);
120 DCHECK_EQ(answer, (succeed ? 10 : 0));
[email protected]4df10d612008-11-12 00:38:26121 return result;
122 }
initial.commit09911bf2008-07-26 23:55:29123 Channel::Mode mode() { return mode_; }
[email protected]aa96ae772009-01-20 22:08:15124 WaitableEvent* done_event() { return done_.get(); }
[email protected]1e9499c2010-04-06 20:33:36125 WaitableEvent* shutdown_event() { return &shutdown_event_; }
[email protected]9eec2252009-12-01 02:34:18126 void ResetChannel() { channel_.reset(); }
initial.commit09911bf2008-07-26 23:55:29127 // Derived classes need to call this when they've completed their part of
128 // the test.
[email protected]aa96ae772009-01-20 22:08:15129 void Done() { done_->Signal(); }
[email protected]1e9499c2010-04-06 20:33:36130
131 protected:
[email protected]c9e0c7332011-05-13 22:42:41132 SyncChannel* channel() { return channel_.get(); }
initial.commit09911bf2008-07-26 23:55:29133 // Functions for dervied classes to implement if they wish.
134 virtual void Run() { }
initial.commit09911bf2008-07-26 23:55:29135 virtual void OnAnswer(int* answer) { NOTREACHED(); }
136 virtual void OnAnswerDelay(Message* reply_msg) {
137 // The message handler map below can only take one entry for
138 // SyncChannelTestMsg_AnswerToLife, so since some classes want
139 // the normal version while other want the delayed reply, we
140 // call the normal version if the derived class didn't override
141 // this function.
142 int answer;
143 OnAnswer(&answer);
144 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, answer);
145 Send(reply_msg);
146 }
[email protected]3cdb7af812008-10-24 19:21:13147 virtual void OnDouble(int in, int* out) { NOTREACHED(); }
148 virtual void OnDoubleDelay(int in, Message* reply_msg) {
149 int result;
150 OnDouble(in, &result);
151 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result);
152 Send(reply_msg);
153 }
initial.commit09911bf2008-07-26 23:55:29154
[email protected]ac0efda2009-10-14 16:22:02155 virtual void OnNestedTestMsg(Message* reply_msg) {
156 NOTREACHED();
157 }
158
[email protected]3cdb7af812008-10-24 19:21:13159 base::Thread* ListenerThread() {
160 return overrided_thread_ ? overrided_thread_ : &listener_thread_;
161 }
[email protected]87339f02010-09-02 21:45:50162
[email protected]54af05f2011-04-08 03:38:21163 const base::Thread& ipc_thread() const { return ipc_thread_; }
164
[email protected]87339f02010-09-02 21:45:50165 private:
initial.commit09911bf2008-07-26 23:55:29166 // Called on the listener thread to create the sync channel.
167 void OnStart() {
initial.commit09911bf2008-07-26 23:55:29168 // Link ipc_thread_, listener_thread_ and channel_ altogether.
[email protected]17b89142008-11-07 21:52:15169 StartThread(&ipc_thread_, MessageLoop::TYPE_IO);
initial.commit09911bf2008-07-26 23:55:29170 channel_.reset(new SyncChannel(
[email protected]92bf9062011-05-02 18:00:49171 channel_name_, mode_, this, ipc_thread_.message_loop_proxy(), true,
[email protected]8930d472009-02-21 08:05:28172 &shutdown_event_));
[email protected]aa96ae772009-01-20 22:08:15173 channel_created_->Signal();
initial.commit09911bf2008-07-26 23:55:29174 Run();
175 }
176
[email protected]9d4ff5ed2009-03-03 00:21:56177 void OnListenerThreadShutdown1(WaitableEvent* listener_event,
178 WaitableEvent* ipc_event) {
[email protected]3cdb7af812008-10-24 19:21:13179 // SyncChannel needs to be destructed on the thread that it was created on.
180 channel_.reset();
[email protected]9d4ff5ed2009-03-03 00:21:56181
182 MessageLoop::current()->RunAllPending();
[email protected]aa96ae772009-01-20 22:08:15183
[email protected]72b6f8e22011-11-12 21:16:41184 ipc_thread_.message_loop()->PostTask(
185 FROM_HERE, base::Bind(&Worker::OnIPCThreadShutdown, this,
186 listener_event, ipc_event));
[email protected]3cdb7af812008-10-24 19:21:13187 }
188
[email protected]9d4ff5ed2009-03-03 00:21:56189 void OnIPCThreadShutdown(WaitableEvent* listener_event,
190 WaitableEvent* ipc_event) {
191 MessageLoop::current()->RunAllPending();
[email protected]aa96ae772009-01-20 22:08:15192 ipc_event->Signal();
[email protected]9d4ff5ed2009-03-03 00:21:56193
[email protected]72b6f8e22011-11-12 21:16:41194 listener_thread_.message_loop()->PostTask(
195 FROM_HERE, base::Bind(&Worker::OnListenerThreadShutdown2, this,
196 listener_event));
[email protected]9d4ff5ed2009-03-03 00:21:56197 }
198
199 void OnListenerThreadShutdown2(WaitableEvent* listener_event) {
200 MessageLoop::current()->RunAllPending();
201 listener_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13202 }
203
[email protected]a95986a82010-12-24 06:19:28204 bool OnMessageReceived(const Message& message) {
initial.commit09911bf2008-07-26 23:55:29205 IPC_BEGIN_MESSAGE_MAP(Worker, message)
[email protected]3cdb7af812008-10-24 19:21:13206 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay)
initial.commit09911bf2008-07-26 23:55:29207 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife,
208 OnAnswerDelay)
[email protected]ac0efda2009-10-14 16:22:02209 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelNestedTestMsg_String,
210 OnNestedTestMsg)
initial.commit09911bf2008-07-26 23:55:29211 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28212 return true;
initial.commit09911bf2008-07-26 23:55:29213 }
214
[email protected]17b89142008-11-07 21:52:15215 void StartThread(base::Thread* thread, MessageLoop::Type type) {
[email protected]ab820df2008-08-26 05:55:10216 base::Thread::Options options;
[email protected]17b89142008-11-07 21:52:15217 options.message_loop_type = type;
[email protected]ab820df2008-08-26 05:55:10218 thread->StartWithOptions(options);
219 }
220
[email protected]aa96ae772009-01-20 22:08:15221 scoped_ptr<WaitableEvent> done_;
222 scoped_ptr<WaitableEvent> channel_created_;
[email protected]9a3a293b2009-06-04 22:28:16223 std::string channel_name_;
initial.commit09911bf2008-07-26 23:55:29224 Channel::Mode mode_;
225 scoped_ptr<SyncChannel> channel_;
[email protected]ab820df2008-08-26 05:55:10226 base::Thread ipc_thread_;
227 base::Thread listener_thread_;
228 base::Thread* overrided_thread_;
initial.commit09911bf2008-07-26 23:55:29229
[email protected]8930d472009-02-21 08:05:28230 base::WaitableEvent shutdown_event_;
231
[email protected]73a797fb2010-06-07 02:10:18232 DISALLOW_COPY_AND_ASSIGN(Worker);
initial.commit09911bf2008-07-26 23:55:29233};
234
235
236// Starts the test with the given workers. This function deletes the workers
237// when it's done.
238void RunTest(std::vector<Worker*> workers) {
initial.commit09911bf2008-07-26 23:55:29239 // First we create the workers that are channel servers, or else the other
240 // workers' channel initialization might fail because the pipe isn't created..
241 for (size_t i = 0; i < workers.size(); ++i) {
[email protected]1707726c2011-02-03 20:35:09242 if (workers[i]->mode() & Channel::MODE_SERVER_FLAG) {
initial.commit09911bf2008-07-26 23:55:29243 workers[i]->Start();
244 workers[i]->WaitForChannelCreation();
245 }
246 }
247
248 // now create the clients
249 for (size_t i = 0; i < workers.size(); ++i) {
[email protected]1707726c2011-02-03 20:35:09250 if (workers[i]->mode() & Channel::MODE_CLIENT_FLAG)
initial.commit09911bf2008-07-26 23:55:29251 workers[i]->Start();
252 }
253
254 // wait for all the workers to finish
initial.commit09911bf2008-07-26 23:55:29255 for (size_t i = 0; i < workers.size(); ++i)
[email protected]aa96ae772009-01-20 22:08:15256 workers[i]->done_event()->Wait();
initial.commit09911bf2008-07-26 23:55:29257
[email protected]82e5ee82009-04-03 02:29:45258 STLDeleteContainerPointers(workers.begin(), workers.end());
initial.commit09911bf2008-07-26 23:55:29259}
260
[email protected]dd3eac22008-08-26 07:28:34261} // namespace
262
[email protected]9629c0e2009-02-04 23:16:29263class IPCSyncChannelTest : public testing::Test {
264 private:
265 MessageLoop message_loop_;
266};
267
initial.commit09911bf2008-07-26 23:55:29268//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34269
270namespace {
271
initial.commit09911bf2008-07-26 23:55:29272class SimpleServer : public Worker {
273 public:
[email protected]b7243c42010-07-23 05:23:13274 explicit SimpleServer(bool pump_during_send)
[email protected]3cdb7af812008-10-24 19:21:13275 : Worker(Channel::MODE_SERVER, "simpler_server"),
276 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29277 void Run() {
[email protected]aa96ae772009-01-20 22:08:15278 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
initial.commit09911bf2008-07-26 23:55:29279 Done();
280 }
[email protected]3cdb7af812008-10-24 19:21:13281
282 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29283};
284
285class SimpleClient : public Worker {
286 public:
287 SimpleClient() : Worker(Channel::MODE_CLIENT, "simple_client") { }
288
289 void OnAnswer(int* answer) {
290 *answer = 42;
291 Done();
292 }
293};
294
[email protected]3cdb7af812008-10-24 19:21:13295void Simple(bool pump_during_send) {
initial.commit09911bf2008-07-26 23:55:29296 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13297 workers.push_back(new SimpleServer(pump_during_send));
initial.commit09911bf2008-07-26 23:55:29298 workers.push_back(new SimpleClient());
299 RunTest(workers);
300}
301
[email protected]3cdb7af812008-10-24 19:21:13302} // namespace
303
304// Tests basic synchronous call
305TEST_F(IPCSyncChannelTest, Simple) {
306 Simple(false);
307 Simple(true);
308}
initial.commit09911bf2008-07-26 23:55:29309
310//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34311
312namespace {
313
initial.commit09911bf2008-07-26 23:55:29314class DelayClient : public Worker {
315 public:
316 DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { }
317
318 void OnAnswerDelay(Message* reply_msg) {
319 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
320 Send(reply_msg);
321 Done();
322 }
323};
324
[email protected]3cdb7af812008-10-24 19:21:13325void DelayReply(bool pump_during_send) {
initial.commit09911bf2008-07-26 23:55:29326 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13327 workers.push_back(new SimpleServer(pump_during_send));
initial.commit09911bf2008-07-26 23:55:29328 workers.push_back(new DelayClient());
329 RunTest(workers);
330}
331
[email protected]3cdb7af812008-10-24 19:21:13332} // namespace
333
334// Tests that asynchronous replies work
335TEST_F(IPCSyncChannelTest, DelayReply) {
336 DelayReply(false);
337 DelayReply(true);
338}
initial.commit09911bf2008-07-26 23:55:29339
340//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34341
342namespace {
343
initial.commit09911bf2008-07-26 23:55:29344class NoHangServer : public Worker {
345 public:
[email protected]e7e38032011-07-26 17:25:25346 NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send)
[email protected]3cdb7af812008-10-24 19:21:13347 : Worker(Channel::MODE_SERVER, "no_hang_server"),
348 got_first_reply_(got_first_reply),
349 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29350 void Run() {
[email protected]aa96ae772009-01-20 22:08:15351 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
352 got_first_reply_->Signal();
initial.commit09911bf2008-07-26 23:55:29353
[email protected]aa96ae772009-01-20 22:08:15354 SendAnswerToLife(pump_during_send_, base::kNoTimeout, false);
initial.commit09911bf2008-07-26 23:55:29355 Done();
356 }
357
[email protected]aa96ae772009-01-20 22:08:15358 WaitableEvent* got_first_reply_;
[email protected]3cdb7af812008-10-24 19:21:13359 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29360};
361
362class NoHangClient : public Worker {
363 public:
[email protected]aa96ae772009-01-20 22:08:15364 explicit NoHangClient(WaitableEvent* got_first_reply)
initial.commit09911bf2008-07-26 23:55:29365 : Worker(Channel::MODE_CLIENT, "no_hang_client"),
366 got_first_reply_(got_first_reply) { }
367
368 virtual void OnAnswerDelay(Message* reply_msg) {
369 // Use the DELAY_REPLY macro so that we can force the reply to be sent
370 // before this function returns (when the channel will be reset).
371 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
372 Send(reply_msg);
373 got_first_reply_->Wait();
374 CloseChannel();
375 Done();
376 }
377
[email protected]aa96ae772009-01-20 22:08:15378 WaitableEvent* got_first_reply_;
initial.commit09911bf2008-07-26 23:55:29379};
380
[email protected]3cdb7af812008-10-24 19:21:13381void NoHang(bool pump_during_send) {
[email protected]aa96ae772009-01-20 22:08:15382 WaitableEvent got_first_reply(false, false);
initial.commit09911bf2008-07-26 23:55:29383 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13384 workers.push_back(new NoHangServer(&got_first_reply, pump_during_send));
initial.commit09911bf2008-07-26 23:55:29385 workers.push_back(new NoHangClient(&got_first_reply));
386 RunTest(workers);
387}
388
[email protected]3cdb7af812008-10-24 19:21:13389} // namespace
390
391// Tests that caller doesn't hang if receiver dies
392TEST_F(IPCSyncChannelTest, NoHang) {
393 NoHang(false);
394 NoHang(true);
395}
initial.commit09911bf2008-07-26 23:55:29396
397//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34398
399namespace {
400
[email protected]3cdb7af812008-10-24 19:21:13401class UnblockServer : public Worker {
initial.commit09911bf2008-07-26 23:55:29402 public:
[email protected]9eec2252009-12-01 02:34:18403 UnblockServer(bool pump_during_send, bool delete_during_send)
[email protected]3cdb7af812008-10-24 19:21:13404 : Worker(Channel::MODE_SERVER, "unblock_server"),
[email protected]9eec2252009-12-01 02:34:18405 pump_during_send_(pump_during_send),
406 delete_during_send_(delete_during_send) { }
initial.commit09911bf2008-07-26 23:55:29407 void Run() {
[email protected]9eec2252009-12-01 02:34:18408 if (delete_during_send_) {
409 // Use custom code since race conditions mean the answer may or may not be
410 // available.
411 int answer = 0;
412 SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer);
413 if (pump_during_send_)
414 msg->EnableMessagePumping();
415 Send(msg);
416 } else {
417 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
418 }
initial.commit09911bf2008-07-26 23:55:29419 Done();
420 }
421
[email protected]9eec2252009-12-01 02:34:18422 void OnDoubleDelay(int in, Message* reply_msg) {
423 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
424 Send(reply_msg);
425 if (delete_during_send_)
426 ResetChannel();
initial.commit09911bf2008-07-26 23:55:29427 }
[email protected]3cdb7af812008-10-24 19:21:13428
429 bool pump_during_send_;
[email protected]9eec2252009-12-01 02:34:18430 bool delete_during_send_;
initial.commit09911bf2008-07-26 23:55:29431};
432
[email protected]3cdb7af812008-10-24 19:21:13433class UnblockClient : public Worker {
initial.commit09911bf2008-07-26 23:55:29434 public:
[email protected]b7243c42010-07-23 05:23:13435 explicit UnblockClient(bool pump_during_send)
[email protected]3cdb7af812008-10-24 19:21:13436 : Worker(Channel::MODE_CLIENT, "unblock_client"),
437 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29438
439 void OnAnswer(int* answer) {
[email protected]4df10d612008-11-12 00:38:26440 SendDouble(pump_during_send_, true);
441 *answer = 42;
initial.commit09911bf2008-07-26 23:55:29442 Done();
443 }
[email protected]3cdb7af812008-10-24 19:21:13444
445 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29446};
447
[email protected]9eec2252009-12-01 02:34:18448void Unblock(bool server_pump, bool client_pump, bool delete_during_send) {
[email protected]3cdb7af812008-10-24 19:21:13449 std::vector<Worker*> workers;
[email protected]9eec2252009-12-01 02:34:18450 workers.push_back(new UnblockServer(server_pump, delete_during_send));
[email protected]3cdb7af812008-10-24 19:21:13451 workers.push_back(new UnblockClient(client_pump));
452 RunTest(workers);
453}
454
[email protected]dd3eac22008-08-26 07:28:34455} // namespace
456
initial.commit09911bf2008-07-26 23:55:29457// Tests that the caller unblocks to answer a sync message from the receiver.
[email protected]3cdb7af812008-10-24 19:21:13458TEST_F(IPCSyncChannelTest, Unblock) {
[email protected]9eec2252009-12-01 02:34:18459 Unblock(false, false, false);
460 Unblock(false, true, false);
461 Unblock(true, false, false);
462 Unblock(true, true, false);
463}
464
465//-----------------------------------------------------------------------------
466
[email protected]c9e0c7332011-05-13 22:42:41467// Tests that the the SyncChannel object can be deleted during a Send.
[email protected]9eec2252009-12-01 02:34:18468TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) {
469 Unblock(false, false, true);
470 Unblock(false, true, true);
471 Unblock(true, false, true);
472 Unblock(true, true, true);
[email protected]3cdb7af812008-10-24 19:21:13473}
474
475//-----------------------------------------------------------------------------
476
477namespace {
478
479class RecursiveServer : public Worker {
480 public:
[email protected]e7e38032011-07-26 17:25:25481 RecursiveServer(bool expected_send_result, bool pump_first, bool pump_second)
482 : Worker(Channel::MODE_SERVER, "recursive_server"),
483 expected_send_result_(expected_send_result),
484 pump_first_(pump_first), pump_second_(pump_second) {}
[email protected]3cdb7af812008-10-24 19:21:13485 void Run() {
[email protected]4df10d612008-11-12 00:38:26486 SendDouble(pump_first_, expected_send_result_);
[email protected]3cdb7af812008-10-24 19:21:13487 Done();
488 }
489
490 void OnDouble(int in, int* out) {
[email protected]4df10d612008-11-12 00:38:26491 *out = in * 2;
[email protected]aa96ae772009-01-20 22:08:15492 SendAnswerToLife(pump_second_, base::kNoTimeout, expected_send_result_);
[email protected]3cdb7af812008-10-24 19:21:13493 }
494
495 bool expected_send_result_, pump_first_, pump_second_;
496};
497
498class RecursiveClient : public Worker {
499 public:
[email protected]e7e38032011-07-26 17:25:25500 RecursiveClient(bool pump_during_send, bool close_channel)
501 : Worker(Channel::MODE_CLIENT, "recursive_client"),
502 pump_during_send_(pump_during_send), close_channel_(close_channel) {}
[email protected]3cdb7af812008-10-24 19:21:13503
504 void OnDoubleDelay(int in, Message* reply_msg) {
[email protected]4df10d612008-11-12 00:38:26505 SendDouble(pump_during_send_, !close_channel_);
[email protected]c690a182008-10-24 23:10:32506 if (close_channel_) {
507 delete reply_msg;
508 } else {
[email protected]3cdb7af812008-10-24 19:21:13509 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
510 Send(reply_msg);
511 }
512 Done();
513 }
514
515 void OnAnswerDelay(Message* reply_msg) {
516 if (close_channel_) {
[email protected]c690a182008-10-24 23:10:32517 delete reply_msg;
[email protected]3cdb7af812008-10-24 19:21:13518 CloseChannel();
519 } else {
520 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
521 Send(reply_msg);
522 }
523 }
524
525 bool pump_during_send_, close_channel_;
526};
527
528void Recursive(
529 bool server_pump_first, bool server_pump_second, bool client_pump) {
initial.commit09911bf2008-07-26 23:55:29530 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13531 workers.push_back(
532 new RecursiveServer(true, server_pump_first, server_pump_second));
533 workers.push_back(new RecursiveClient(client_pump, false));
initial.commit09911bf2008-07-26 23:55:29534 RunTest(workers);
535}
536
[email protected]3cdb7af812008-10-24 19:21:13537} // namespace
538
539// Tests a server calling Send while another Send is pending.
540TEST_F(IPCSyncChannelTest, Recursive) {
541 Recursive(false, false, false);
542 Recursive(false, false, true);
543 Recursive(false, true, false);
544 Recursive(false, true, true);
545 Recursive(true, false, false);
546 Recursive(true, false, true);
547 Recursive(true, true, false);
548 Recursive(true, true, true);
549}
550
551//-----------------------------------------------------------------------------
552
553namespace {
554
555void RecursiveNoHang(
556 bool server_pump_first, bool server_pump_second, bool client_pump) {
557 std::vector<Worker*> workers;
558 workers.push_back(
559 new RecursiveServer(false, server_pump_first, server_pump_second));
560 workers.push_back(new RecursiveClient(client_pump, true));
561 RunTest(workers);
562}
563
564} // namespace
565
566// Tests that if a caller makes a sync call during an existing sync call and
567// the receiver dies, neither of the Send() calls hang.
568TEST_F(IPCSyncChannelTest, RecursiveNoHang) {
569 RecursiveNoHang(false, false, false);
570 RecursiveNoHang(false, false, true);
571 RecursiveNoHang(false, true, false);
572 RecursiveNoHang(false, true, true);
573 RecursiveNoHang(true, false, false);
574 RecursiveNoHang(true, false, true);
575 RecursiveNoHang(true, true, false);
576 RecursiveNoHang(true, true, true);
577}
initial.commit09911bf2008-07-26 23:55:29578
579//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34580
581namespace {
582
initial.commit09911bf2008-07-26 23:55:29583class MultipleServer1 : public Worker {
584 public:
[email protected]b7243c42010-07-23 05:23:13585 explicit MultipleServer1(bool pump_during_send)
[email protected]9a3a293b2009-06-04 22:28:16586 : Worker("test_channel1", Channel::MODE_SERVER),
[email protected]3cdb7af812008-10-24 19:21:13587 pump_during_send_(pump_during_send) { }
588
initial.commit09911bf2008-07-26 23:55:29589 void Run() {
[email protected]4df10d612008-11-12 00:38:26590 SendDouble(pump_during_send_, true);
initial.commit09911bf2008-07-26 23:55:29591 Done();
592 }
[email protected]3cdb7af812008-10-24 19:21:13593
594 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29595};
596
597class MultipleClient1 : public Worker {
598 public:
[email protected]aa96ae772009-01-20 22:08:15599 MultipleClient1(WaitableEvent* client1_msg_received,
600 WaitableEvent* client1_can_reply) :
[email protected]9a3a293b2009-06-04 22:28:16601 Worker("test_channel1", Channel::MODE_CLIENT),
initial.commit09911bf2008-07-26 23:55:29602 client1_msg_received_(client1_msg_received),
603 client1_can_reply_(client1_can_reply) { }
604
605 void OnDouble(int in, int* out) {
[email protected]aa96ae772009-01-20 22:08:15606 client1_msg_received_->Signal();
initial.commit09911bf2008-07-26 23:55:29607 *out = in * 2;
608 client1_can_reply_->Wait();
609 Done();
610 }
611
612 private:
[email protected]aa96ae772009-01-20 22:08:15613 WaitableEvent *client1_msg_received_, *client1_can_reply_;
initial.commit09911bf2008-07-26 23:55:29614};
615
616class MultipleServer2 : public Worker {
617 public:
[email protected]9a3a293b2009-06-04 22:28:16618 MultipleServer2() : Worker("test_channel2", Channel::MODE_SERVER) { }
initial.commit09911bf2008-07-26 23:55:29619
620 void OnAnswer(int* result) {
621 *result = 42;
622 Done();
623 }
624};
625
626class MultipleClient2 : public Worker {
627 public:
[email protected]3cdb7af812008-10-24 19:21:13628 MultipleClient2(
[email protected]aa96ae772009-01-20 22:08:15629 WaitableEvent* client1_msg_received, WaitableEvent* client1_can_reply,
[email protected]3cdb7af812008-10-24 19:21:13630 bool pump_during_send)
[email protected]9a3a293b2009-06-04 22:28:16631 : Worker("test_channel2", Channel::MODE_CLIENT),
initial.commit09911bf2008-07-26 23:55:29632 client1_msg_received_(client1_msg_received),
[email protected]3cdb7af812008-10-24 19:21:13633 client1_can_reply_(client1_can_reply),
634 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29635
636 void Run() {
initial.commit09911bf2008-07-26 23:55:29637 client1_msg_received_->Wait();
[email protected]aa96ae772009-01-20 22:08:15638 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
639 client1_can_reply_->Signal();
initial.commit09911bf2008-07-26 23:55:29640 Done();
641 }
642
643 private:
[email protected]aa96ae772009-01-20 22:08:15644 WaitableEvent *client1_msg_received_, *client1_can_reply_;
[email protected]3cdb7af812008-10-24 19:21:13645 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29646};
647
[email protected]3cdb7af812008-10-24 19:21:13648void Multiple(bool server_pump, bool client_pump) {
initial.commit09911bf2008-07-26 23:55:29649 std::vector<Worker*> workers;
650
651 // A shared worker thread so that server1 and server2 run on one thread.
[email protected]ab820df2008-08-26 05:55:10652 base::Thread worker_thread("Multiple");
[email protected]6314e6f62009-07-15 16:07:14653 ASSERT_TRUE(worker_thread.Start());
initial.commit09911bf2008-07-26 23:55:29654
655 // Server1 sends a sync msg to client1, which blocks the reply until
656 // server2 (which runs on the same worker thread as server1) responds
657 // to a sync msg from client2.
[email protected]aa96ae772009-01-20 22:08:15658 WaitableEvent client1_msg_received(false, false);
659 WaitableEvent client1_can_reply(false, false);
initial.commit09911bf2008-07-26 23:55:29660
661 Worker* worker;
662
663 worker = new MultipleServer2();
664 worker->OverrideThread(&worker_thread);
665 workers.push_back(worker);
666
667 worker = new MultipleClient2(
[email protected]3cdb7af812008-10-24 19:21:13668 &client1_msg_received, &client1_can_reply, client_pump);
initial.commit09911bf2008-07-26 23:55:29669 workers.push_back(worker);
670
[email protected]3cdb7af812008-10-24 19:21:13671 worker = new MultipleServer1(server_pump);
initial.commit09911bf2008-07-26 23:55:29672 worker->OverrideThread(&worker_thread);
673 workers.push_back(worker);
674
675 worker = new MultipleClient1(
676 &client1_msg_received, &client1_can_reply);
677 workers.push_back(worker);
678
679 RunTest(workers);
680}
681
[email protected]3cdb7af812008-10-24 19:21:13682} // namespace
683
684// Tests that multiple SyncObjects on the same listener thread can unblock each
685// other.
686TEST_F(IPCSyncChannelTest, Multiple) {
687 Multiple(false, false);
688 Multiple(false, true);
689 Multiple(true, false);
690 Multiple(true, true);
691}
initial.commit09911bf2008-07-26 23:55:29692
693//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34694
695namespace {
696
[email protected]ac0efda2009-10-14 16:22:02697// This class provides server side functionality to test the case where
698// multiple sync channels are in use on the same thread on the client and
699// nested calls are issued.
700class QueuedReplyServer : public Worker {
initial.commit09911bf2008-07-26 23:55:29701 public:
[email protected]b7243c42010-07-23 05:23:13702 QueuedReplyServer(base::Thread* listener_thread,
703 const std::string& channel_name,
704 const std::string& reply_text)
[email protected]ac0efda2009-10-14 16:22:02705 : Worker(channel_name, Channel::MODE_SERVER),
706 reply_text_(reply_text) {
707 Worker::OverrideThread(listener_thread);
initial.commit09911bf2008-07-26 23:55:29708 }
[email protected]3cdb7af812008-10-24 19:21:13709
[email protected]ac0efda2009-10-14 16:22:02710 virtual void OnNestedTestMsg(Message* reply_msg) {
[email protected]2a9d601b2010-10-19 23:50:00711 VLOG(1) << __FUNCTION__ << " Sending reply: " << reply_text_;
712 SyncChannelNestedTestMsg_String::WriteReplyParams(reply_msg, reply_text_);
[email protected]ac0efda2009-10-14 16:22:02713 Send(reply_msg);
initial.commit09911bf2008-07-26 23:55:29714 Done();
715 }
716
717 private:
[email protected]ac0efda2009-10-14 16:22:02718 std::string reply_text_;
initial.commit09911bf2008-07-26 23:55:29719};
720
[email protected]ac0efda2009-10-14 16:22:02721// The QueuedReplyClient class provides functionality to test the case where
722// multiple sync channels are in use on the same thread and they make nested
723// sync calls, i.e. while the first channel waits for a response it makes a
724// sync call on another channel.
725// The callstack should unwind correctly, i.e. the outermost call should
726// complete first, and so on.
727class QueuedReplyClient : public Worker {
initial.commit09911bf2008-07-26 23:55:29728 public:
[email protected]ac0efda2009-10-14 16:22:02729 QueuedReplyClient(base::Thread* listener_thread,
730 const std::string& channel_name,
731 const std::string& expected_text,
732 bool pump_during_send)
733 : Worker(channel_name, Channel::MODE_CLIENT),
[email protected]7ee1a44c2010-07-23 14:18:59734 pump_during_send_(pump_during_send),
735 expected_text_(expected_text) {
[email protected]ac0efda2009-10-14 16:22:02736 Worker::OverrideThread(listener_thread);
737 }
initial.commit09911bf2008-07-26 23:55:29738
[email protected]ac0efda2009-10-14 16:22:02739 virtual void Run() {
740 std::string response;
741 SyncMessage* msg = new SyncChannelNestedTestMsg_String(&response);
742 if (pump_during_send_)
743 msg->EnableMessagePumping();
744 bool result = Send(msg);
745 DCHECK(result);
[email protected]9eec2252009-12-01 02:34:18746 DCHECK_EQ(response, expected_text_);
initial.commit09911bf2008-07-26 23:55:29747
[email protected]2a9d601b2010-10-19 23:50:00748 VLOG(1) << __FUNCTION__ << " Received reply: " << response;
initial.commit09911bf2008-07-26 23:55:29749 Done();
750 }
751
[email protected]ac0efda2009-10-14 16:22:02752 private:
[email protected]3cdb7af812008-10-24 19:21:13753 bool pump_during_send_;
[email protected]ac0efda2009-10-14 16:22:02754 std::string expected_text_;
initial.commit09911bf2008-07-26 23:55:29755};
756
[email protected]ac0efda2009-10-14 16:22:02757void QueuedReply(bool client_pump) {
initial.commit09911bf2008-07-26 23:55:29758 std::vector<Worker*> workers;
759
[email protected]ac0efda2009-10-14 16:22:02760 // A shared worker thread for servers
761 base::Thread server_worker_thread("QueuedReply_ServerListener");
762 ASSERT_TRUE(server_worker_thread.Start());
initial.commit09911bf2008-07-26 23:55:29763
[email protected]ac0efda2009-10-14 16:22:02764 base::Thread client_worker_thread("QueuedReply_ClientListener");
765 ASSERT_TRUE(client_worker_thread.Start());
initial.commit09911bf2008-07-26 23:55:29766
767 Worker* worker;
768
[email protected]ac0efda2009-10-14 16:22:02769 worker = new QueuedReplyServer(&server_worker_thread,
770 "QueuedReply_Server1",
771 "Got first message");
initial.commit09911bf2008-07-26 23:55:29772 workers.push_back(worker);
773
[email protected]ac0efda2009-10-14 16:22:02774 worker = new QueuedReplyServer(&server_worker_thread,
775 "QueuedReply_Server2",
776 "Got second message");
initial.commit09911bf2008-07-26 23:55:29777 workers.push_back(worker);
778
[email protected]ac0efda2009-10-14 16:22:02779 worker = new QueuedReplyClient(&client_worker_thread,
780 "QueuedReply_Server1",
781 "Got first message",
782 client_pump);
initial.commit09911bf2008-07-26 23:55:29783 workers.push_back(worker);
784
[email protected]ac0efda2009-10-14 16:22:02785 worker = new QueuedReplyClient(&client_worker_thread,
786 "QueuedReply_Server2",
787 "Got second message",
788 client_pump);
initial.commit09911bf2008-07-26 23:55:29789 workers.push_back(worker);
790
791 RunTest(workers);
792}
793
[email protected]3cdb7af812008-10-24 19:21:13794} // namespace
795
796// While a blocking send is in progress, the listener thread might answer other
797// synchronous messages. This tests that if during the response to another
798// message the reply to the original messages comes, it is queued up correctly
799// and the original Send is unblocked later.
[email protected]ac0efda2009-10-14 16:22:02800// We also test that the send call stacks unwind correctly when the channel
801// pumps messages while waiting for a response.
[email protected]3cdb7af812008-10-24 19:21:13802TEST_F(IPCSyncChannelTest, QueuedReply) {
[email protected]ac0efda2009-10-14 16:22:02803 QueuedReply(false);
804 QueuedReply(true);
[email protected]3cdb7af812008-10-24 19:21:13805}
initial.commit09911bf2008-07-26 23:55:29806
807//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34808
809namespace {
810
[email protected]3cdb7af812008-10-24 19:21:13811class ChattyClient : public Worker {
initial.commit09911bf2008-07-26 23:55:29812 public:
[email protected]3cdb7af812008-10-24 19:21:13813 ChattyClient() :
814 Worker(Channel::MODE_CLIENT, "chatty_client") { }
initial.commit09911bf2008-07-26 23:55:29815
816 void OnAnswer(int* answer) {
817 // The PostMessage limit is 10k. Send 20% more than that.
818 const int kMessageLimit = 10000;
819 const int kMessagesToSend = kMessageLimit * 120 / 100;
820 for (int i = 0; i < kMessagesToSend; ++i) {
[email protected]4df10d612008-11-12 00:38:26821 if (!SendDouble(false, true))
initial.commit09911bf2008-07-26 23:55:29822 break;
823 }
[email protected]4df10d612008-11-12 00:38:26824 *answer = 42;
initial.commit09911bf2008-07-26 23:55:29825 Done();
826 }
827};
828
[email protected]3cdb7af812008-10-24 19:21:13829void ChattyServer(bool pump_during_send) {
830 std::vector<Worker*> workers;
[email protected]9eec2252009-12-01 02:34:18831 workers.push_back(new UnblockServer(pump_during_send, false));
[email protected]3cdb7af812008-10-24 19:21:13832 workers.push_back(new ChattyClient());
833 RunTest(workers);
834}
835
[email protected]dd3eac22008-08-26 07:28:34836} // namespace
837
[email protected]4df10d612008-11-12 00:38:26838// Tests http://b/1093251 - that sending lots of sync messages while
initial.commit09911bf2008-07-26 23:55:29839// the receiver is waiting for a sync reply does not overflow the PostMessage
840// queue.
[email protected]dd3eac22008-08-26 07:28:34841TEST_F(IPCSyncChannelTest, ChattyServer) {
[email protected]3cdb7af812008-10-24 19:21:13842 ChattyServer(false);
843 ChattyServer(true);
initial.commit09911bf2008-07-26 23:55:29844}
[email protected]d65cab7a2008-08-12 01:25:41845
[email protected]d65cab7a2008-08-12 01:25:41846//------------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34847
848namespace {
849
[email protected]d65cab7a2008-08-12 01:25:41850class TimeoutServer : public Worker {
851 public:
[email protected]b7243c42010-07-23 05:23:13852 TimeoutServer(int timeout_ms,
853 std::vector<bool> timeout_seq,
854 bool pump_during_send)
[email protected]d65cab7a2008-08-12 01:25:41855 : Worker(Channel::MODE_SERVER, "timeout_server"),
856 timeout_ms_(timeout_ms),
[email protected]3cdb7af812008-10-24 19:21:13857 timeout_seq_(timeout_seq),
858 pump_during_send_(pump_during_send) {
[email protected]d65cab7a2008-08-12 01:25:41859 }
860
861 void Run() {
862 for (std::vector<bool>::const_iterator iter = timeout_seq_.begin();
863 iter != timeout_seq_.end(); ++iter) {
[email protected]4df10d612008-11-12 00:38:26864 SendAnswerToLife(pump_during_send_, timeout_ms_, !*iter);
[email protected]d65cab7a2008-08-12 01:25:41865 }
866 Done();
867 }
868
869 private:
870 int timeout_ms_;
871 std::vector<bool> timeout_seq_;
[email protected]3cdb7af812008-10-24 19:21:13872 bool pump_during_send_;
[email protected]d65cab7a2008-08-12 01:25:41873};
874
875class UnresponsiveClient : public Worker {
876 public:
[email protected]b7243c42010-07-23 05:23:13877 explicit UnresponsiveClient(std::vector<bool> timeout_seq)
[email protected]d65cab7a2008-08-12 01:25:41878 : Worker(Channel::MODE_CLIENT, "unresponsive_client"),
879 timeout_seq_(timeout_seq) {
[email protected]b7243c42010-07-23 05:23:13880 }
[email protected]d65cab7a2008-08-12 01:25:41881
882 void OnAnswerDelay(Message* reply_msg) {
883 DCHECK(!timeout_seq_.empty());
884 if (!timeout_seq_[0]) {
885 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
886 Send(reply_msg);
887 } else {
888 // Don't reply.
[email protected]463667372008-08-20 20:20:52889 delete reply_msg;
[email protected]d65cab7a2008-08-12 01:25:41890 }
891 timeout_seq_.erase(timeout_seq_.begin());
892 if (timeout_seq_.empty())
893 Done();
894 }
895
896 private:
897 // Whether we should time-out or respond to the various messages we receive.
898 std::vector<bool> timeout_seq_;
899};
900
[email protected]3cdb7af812008-10-24 19:21:13901void SendWithTimeoutOK(bool pump_during_send) {
902 std::vector<Worker*> workers;
903 std::vector<bool> timeout_seq;
904 timeout_seq.push_back(false);
905 timeout_seq.push_back(false);
906 timeout_seq.push_back(false);
907 workers.push_back(new TimeoutServer(5000, timeout_seq, pump_during_send));
908 workers.push_back(new SimpleClient());
909 RunTest(workers);
910}
911
912void SendWithTimeoutTimeout(bool pump_during_send) {
913 std::vector<Worker*> workers;
914 std::vector<bool> timeout_seq;
915 timeout_seq.push_back(true);
916 timeout_seq.push_back(false);
917 timeout_seq.push_back(false);
918 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send));
919 workers.push_back(new UnresponsiveClient(timeout_seq));
920 RunTest(workers);
921}
922
923void SendWithTimeoutMixedOKAndTimeout(bool pump_during_send) {
924 std::vector<Worker*> workers;
925 std::vector<bool> timeout_seq;
926 timeout_seq.push_back(true);
927 timeout_seq.push_back(false);
928 timeout_seq.push_back(false);
929 timeout_seq.push_back(true);
930 timeout_seq.push_back(false);
931 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send));
932 workers.push_back(new UnresponsiveClient(timeout_seq));
933 RunTest(workers);
934}
935
[email protected]dd3eac22008-08-26 07:28:34936} // namespace
937
[email protected]d65cab7a2008-08-12 01:25:41938// Tests that SendWithTimeout does not time-out if the response comes back fast
939// enough.
[email protected]dd3eac22008-08-26 07:28:34940TEST_F(IPCSyncChannelTest, SendWithTimeoutOK) {
[email protected]3cdb7af812008-10-24 19:21:13941 SendWithTimeoutOK(false);
942 SendWithTimeoutOK(true);
[email protected]d65cab7a2008-08-12 01:25:41943}
944
945// Tests that SendWithTimeout does time-out.
[email protected]dd3eac22008-08-26 07:28:34946TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) {
[email protected]3cdb7af812008-10-24 19:21:13947 SendWithTimeoutTimeout(false);
948 SendWithTimeoutTimeout(true);
[email protected]d65cab7a2008-08-12 01:25:41949}
950
951// Sends some message that time-out and some that succeed.
[email protected]711032e2011-01-19 08:11:56952// Crashes flakily, http://crbug.com/70075.
953TEST_F(IPCSyncChannelTest, DISABLED_SendWithTimeoutMixedOKAndTimeout) {
[email protected]3cdb7af812008-10-24 19:21:13954 SendWithTimeoutMixedOKAndTimeout(false);
955 SendWithTimeoutMixedOKAndTimeout(true);
956}
[email protected]4df10d612008-11-12 00:38:26957
958//------------------------------------------------------------------------------
959
960namespace {
961
962class NestedTask : public Task {
963 public:
[email protected]e7e38032011-07-26 17:25:25964 explicit NestedTask(Worker* server) : server_(server) {}
[email protected]4df10d612008-11-12 00:38:26965 void Run() {
966 // Sleep a bit so that we wake up after the reply has been received.
[email protected]f214f8792011-01-01 02:17:08967 base::PlatformThread::Sleep(250);
[email protected]aa96ae772009-01-20 22:08:15968 server_->SendAnswerToLife(true, base::kNoTimeout, true);
[email protected]4df10d612008-11-12 00:38:26969 }
970
971 Worker* server_;
972};
973
974static bool timeout_occured = false;
975
976class TimeoutTask : public Task {
977 public:
978 void Run() {
979 timeout_occured = true;
980 }
981};
982
983class DoneEventRaceServer : public Worker {
984 public:
985 DoneEventRaceServer()
986 : Worker(Channel::MODE_SERVER, "done_event_race_server") { }
987
988 void Run() {
989 MessageLoop::current()->PostTask(FROM_HERE, new NestedTask(this));
990 MessageLoop::current()->PostDelayedTask(FROM_HERE, new TimeoutTask(), 9000);
991 // Even though we have a timeout on the Send, it will succeed since for this
992 // bug, the reply message comes back and is deserialized, however the done
993 // event wasn't set. So we indirectly use the timeout task to notice if a
994 // timeout occurred.
995 SendAnswerToLife(true, 10000, true);
996 DCHECK(!timeout_occured);
997 Done();
998 }
999};
1000
1001} // namespace
1002
1003// Tests http://b/1474092 - that if after the done_event is set but before
1004// OnObjectSignaled is called another message is sent out, then after its
1005// reply comes back OnObjectSignaled will be called for the first message.
1006TEST_F(IPCSyncChannelTest, DoneEventRace) {
1007 std::vector<Worker*> workers;
1008 workers.push_back(new DoneEventRaceServer());
1009 workers.push_back(new SimpleClient());
1010 RunTest(workers);
1011}
[email protected]1e9499c2010-04-06 20:33:361012
1013//-----------------------------------------------------------------------------
1014
1015namespace {
1016
[email protected]c9e0c7332011-05-13 22:42:411017class TestSyncMessageFilter : public SyncMessageFilter {
[email protected]1e9499c2010-04-06 20:33:361018 public:
1019 TestSyncMessageFilter(base::WaitableEvent* shutdown_event, Worker* worker)
1020 : SyncMessageFilter(shutdown_event),
1021 worker_(worker),
1022 thread_("helper_thread") {
1023 base::Thread::Options options;
1024 options.message_loop_type = MessageLoop::TYPE_DEFAULT;
1025 thread_.StartWithOptions(options);
1026 }
1027
1028 virtual void OnFilterAdded(Channel* channel) {
1029 SyncMessageFilter::OnFilterAdded(channel);
[email protected]72b6f8e22011-11-12 21:16:411030 thread_.message_loop()->PostTask(
1031 FROM_HERE,
1032 base::Bind(&TestSyncMessageFilter::SendMessageOnHelperThread, this));
[email protected]1e9499c2010-04-06 20:33:361033 }
1034
1035 void SendMessageOnHelperThread() {
1036 int answer = 0;
1037 bool result = Send(new SyncChannelTestMsg_AnswerToLife(&answer));
1038 DCHECK(result);
1039 DCHECK_EQ(answer, 42);
1040
1041 worker_->Done();
1042 }
1043
1044 Worker* worker_;
1045 base::Thread thread_;
1046};
1047
1048class SyncMessageFilterServer : public Worker {
1049 public:
1050 SyncMessageFilterServer()
1051 : Worker(Channel::MODE_SERVER, "sync_message_filter_server") {
1052 filter_ = new TestSyncMessageFilter(shutdown_event(), this);
1053 }
1054
1055 void Run() {
1056 channel()->AddFilter(filter_.get());
1057 }
1058
1059 scoped_refptr<TestSyncMessageFilter> filter_;
1060};
1061
[email protected]87339f02010-09-02 21:45:501062// This class provides functionality to test the case that a Send on the sync
1063// channel does not crash after the channel has been closed.
1064class ServerSendAfterClose : public Worker {
1065 public:
1066 ServerSendAfterClose()
1067 : Worker(Channel::MODE_SERVER, "simpler_server"),
1068 send_result_(true) {
1069 }
1070
1071 bool SendDummy() {
[email protected]72b6f8e22011-11-12 21:16:411072 ListenerThread()->message_loop()->PostTask(
1073 FROM_HERE, base::IgnoreReturn<bool>(
1074 base::Bind(&ServerSendAfterClose::Send, this,
1075 new SyncChannelTestMsg_NoArgs)));
[email protected]87339f02010-09-02 21:45:501076 return true;
1077 }
1078
1079 bool send_result() const {
1080 return send_result_;
1081 }
1082
1083 private:
1084 virtual void Run() {
1085 CloseChannel();
1086 Done();
1087 }
1088
1089 bool Send(Message* msg) {
1090 send_result_ = Worker::Send(msg);
1091 Done();
1092 return send_result_;
1093 }
1094
1095 bool send_result_;
1096};
1097
[email protected]1e9499c2010-04-06 20:33:361098} // namespace
1099
1100// Tests basic synchronous call
1101TEST_F(IPCSyncChannelTest, SyncMessageFilter) {
1102 std::vector<Worker*> workers;
1103 workers.push_back(new SyncMessageFilterServer());
1104 workers.push_back(new SimpleClient());
1105 RunTest(workers);
1106}
[email protected]87339f02010-09-02 21:45:501107
1108// Test the case when the channel is closed and a Send is attempted after that.
1109TEST_F(IPCSyncChannelTest, SendAfterClose) {
1110 ServerSendAfterClose server;
1111 server.Start();
1112
1113 server.done_event()->Wait();
1114 server.done_event()->Reset();
1115
1116 server.SendDummy();
1117 server.done_event()->Wait();
1118
1119 EXPECT_FALSE(server.send_result());
1120}
1121
[email protected]54af05f2011-04-08 03:38:211122//-----------------------------------------------------------------------------
[email protected]87339f02010-09-02 21:45:501123
[email protected]54af05f2011-04-08 03:38:211124namespace {
1125
1126class RestrictedDispatchServer : public Worker {
1127 public:
1128 RestrictedDispatchServer(WaitableEvent* sent_ping_event)
1129 : Worker("restricted_channel", Channel::MODE_SERVER),
1130 sent_ping_event_(sent_ping_event) { }
1131
1132 void OnDoPing(int ping) {
1133 // Send an asynchronous message that unblocks the caller.
[email protected]c9e0c7332011-05-13 22:42:411134 Message* msg = new SyncChannelTestMsg_Ping(ping);
[email protected]54af05f2011-04-08 03:38:211135 msg->set_unblock(true);
1136 Send(msg);
1137 // Signal the event after the message has been sent on the channel, on the
1138 // IPC thread.
[email protected]72b6f8e22011-11-12 21:16:411139 ipc_thread().message_loop()->PostTask(
1140 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnPingSent, this));
[email protected]54af05f2011-04-08 03:38:211141 }
1142
1143 base::Thread* ListenerThread() { return Worker::ListenerThread(); }
1144
1145 private:
1146 bool OnMessageReceived(const Message& message) {
1147 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchServer, message)
1148 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
1149 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done)
1150 IPC_END_MESSAGE_MAP()
1151 return true;
1152 }
1153
1154 void OnPingSent() {
1155 sent_ping_event_->Signal();
1156 }
1157
1158 void OnNoArgs() { }
1159 WaitableEvent* sent_ping_event_;
1160};
1161
1162class NonRestrictedDispatchServer : public Worker {
1163 public:
1164 NonRestrictedDispatchServer()
1165 : Worker("non_restricted_channel", Channel::MODE_SERVER) {}
1166
1167 private:
1168 bool OnMessageReceived(const Message& message) {
1169 IPC_BEGIN_MESSAGE_MAP(NonRestrictedDispatchServer, message)
1170 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
1171 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done)
1172 IPC_END_MESSAGE_MAP()
1173 return true;
1174 }
1175
1176 void OnNoArgs() { }
1177};
1178
1179class RestrictedDispatchClient : public Worker {
1180 public:
1181 RestrictedDispatchClient(WaitableEvent* sent_ping_event,
1182 RestrictedDispatchServer* server,
1183 int* success)
1184 : Worker("restricted_channel", Channel::MODE_CLIENT),
1185 ping_(0),
1186 server_(server),
1187 success_(success),
1188 sent_ping_event_(sent_ping_event) {}
1189
1190 void Run() {
1191 // Incoming messages from our channel should only be dispatched when we
1192 // send a message on that same channel.
1193 channel()->SetRestrictDispatchToSameChannel(true);
1194
[email protected]72b6f8e22011-11-12 21:16:411195 server_->ListenerThread()->message_loop()->PostTask(
1196 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 1));
[email protected]54af05f2011-04-08 03:38:211197 sent_ping_event_->Wait();
1198 Send(new SyncChannelTestMsg_NoArgs);
1199 if (ping_ == 1)
1200 ++*success_;
1201 else
1202 LOG(ERROR) << "Send failed to dispatch incoming message on same channel";
1203
1204 scoped_ptr<SyncChannel> non_restricted_channel(new SyncChannel(
1205 "non_restricted_channel", Channel::MODE_CLIENT, this,
[email protected]92bf9062011-05-02 18:00:491206 ipc_thread().message_loop_proxy(), true, shutdown_event()));
[email protected]54af05f2011-04-08 03:38:211207
[email protected]72b6f8e22011-11-12 21:16:411208 server_->ListenerThread()->message_loop()->PostTask(
1209 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 2));
[email protected]54af05f2011-04-08 03:38:211210 sent_ping_event_->Wait();
1211 // Check that the incoming message is *not* dispatched when sending on the
1212 // non restricted channel.
1213 // TODO(piman): there is a possibility of a false positive race condition
1214 // here, if the message that was posted on the server-side end of the pipe
1215 // is not visible yet on the client side, but I don't know how to solve this
1216 // without hooking into the internals of SyncChannel. I haven't seen it in
1217 // practice (i.e. not setting SetRestrictDispatchToSameChannel does cause
1218 // the following to fail).
1219 non_restricted_channel->Send(new SyncChannelTestMsg_NoArgs);
1220 if (ping_ == 1)
1221 ++*success_;
1222 else
1223 LOG(ERROR) << "Send dispatched message from restricted channel";
1224
1225 Send(new SyncChannelTestMsg_NoArgs);
1226 if (ping_ == 2)
1227 ++*success_;
1228 else
1229 LOG(ERROR) << "Send failed to dispatch incoming message on same channel";
1230
1231 non_restricted_channel->Send(new SyncChannelTestMsg_Done);
1232 non_restricted_channel.reset();
1233 Send(new SyncChannelTestMsg_Done);
1234 Done();
1235 }
1236
1237 private:
1238 bool OnMessageReceived(const Message& message) {
1239 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchClient, message)
1240 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Ping, OnPing)
1241 IPC_END_MESSAGE_MAP()
1242 return true;
1243 }
1244
1245 void OnPing(int ping) {
1246 ping_ = ping;
1247 }
1248
1249 int ping_;
1250 RestrictedDispatchServer* server_;
1251 int* success_;
1252 WaitableEvent* sent_ping_event_;
1253};
1254
1255} // namespace
1256
1257TEST_F(IPCSyncChannelTest, RestrictedDispatch) {
1258 WaitableEvent sent_ping_event(false, false);
1259
1260 RestrictedDispatchServer* server =
1261 new RestrictedDispatchServer(&sent_ping_event);
1262 int success = 0;
1263 std::vector<Worker*> workers;
1264 workers.push_back(new NonRestrictedDispatchServer);
1265 workers.push_back(server);
1266 workers.push_back(
1267 new RestrictedDispatchClient(&sent_ping_event, server, &success));
1268 RunTest(workers);
1269 EXPECT_EQ(3, success);
1270}
[email protected]c9e0c7332011-05-13 22:42:411271
1272} // namespace IPC