blob: 81ca5609e56b227c23193eea615658b1839f0406 [file] [log] [blame]
[email protected]522cc10d2012-01-11 22:39:541// Copyright (c) 2012 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 }
[email protected]952394af2011-11-16 01:06:46123 const std::string& channel_name() { return channel_name_; }
initial.commit09911bf2008-07-26 23:55:29124 Channel::Mode mode() { return mode_; }
[email protected]aa96ae772009-01-20 22:08:15125 WaitableEvent* done_event() { return done_.get(); }
[email protected]1e9499c2010-04-06 20:33:36126 WaitableEvent* shutdown_event() { return &shutdown_event_; }
[email protected]9eec2252009-12-01 02:34:18127 void ResetChannel() { channel_.reset(); }
initial.commit09911bf2008-07-26 23:55:29128 // Derived classes need to call this when they've completed their part of
129 // the test.
[email protected]aa96ae772009-01-20 22:08:15130 void Done() { done_->Signal(); }
[email protected]1e9499c2010-04-06 20:33:36131
132 protected:
[email protected]c9e0c7332011-05-13 22:42:41133 SyncChannel* channel() { return channel_.get(); }
initial.commit09911bf2008-07-26 23:55:29134 // Functions for dervied classes to implement if they wish.
135 virtual void Run() { }
initial.commit09911bf2008-07-26 23:55:29136 virtual void OnAnswer(int* answer) { NOTREACHED(); }
137 virtual void OnAnswerDelay(Message* reply_msg) {
138 // The message handler map below can only take one entry for
139 // SyncChannelTestMsg_AnswerToLife, so since some classes want
140 // the normal version while other want the delayed reply, we
141 // call the normal version if the derived class didn't override
142 // this function.
143 int answer;
144 OnAnswer(&answer);
145 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, answer);
146 Send(reply_msg);
147 }
[email protected]3cdb7af812008-10-24 19:21:13148 virtual void OnDouble(int in, int* out) { NOTREACHED(); }
149 virtual void OnDoubleDelay(int in, Message* reply_msg) {
150 int result;
151 OnDouble(in, &result);
152 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result);
153 Send(reply_msg);
154 }
initial.commit09911bf2008-07-26 23:55:29155
[email protected]ac0efda2009-10-14 16:22:02156 virtual void OnNestedTestMsg(Message* reply_msg) {
157 NOTREACHED();
158 }
159
[email protected]952394af2011-11-16 01:06:46160 virtual SyncChannel* CreateChannel() {
161 return new SyncChannel(
162 channel_name_, mode_, this, ipc_thread_.message_loop_proxy(), true,
163 &shutdown_event_);
164 }
165
[email protected]3cdb7af812008-10-24 19:21:13166 base::Thread* ListenerThread() {
167 return overrided_thread_ ? overrided_thread_ : &listener_thread_;
168 }
[email protected]87339f02010-09-02 21:45:50169
[email protected]54af05f2011-04-08 03:38:21170 const base::Thread& ipc_thread() const { return ipc_thread_; }
171
[email protected]87339f02010-09-02 21:45:50172 private:
initial.commit09911bf2008-07-26 23:55:29173 // Called on the listener thread to create the sync channel.
174 void OnStart() {
initial.commit09911bf2008-07-26 23:55:29175 // Link ipc_thread_, listener_thread_ and channel_ altogether.
[email protected]17b89142008-11-07 21:52:15176 StartThread(&ipc_thread_, MessageLoop::TYPE_IO);
[email protected]952394af2011-11-16 01:06:46177 channel_.reset(CreateChannel());
[email protected]aa96ae772009-01-20 22:08:15178 channel_created_->Signal();
initial.commit09911bf2008-07-26 23:55:29179 Run();
180 }
181
[email protected]9d4ff5ed2009-03-03 00:21:56182 void OnListenerThreadShutdown1(WaitableEvent* listener_event,
183 WaitableEvent* ipc_event) {
[email protected]3cdb7af812008-10-24 19:21:13184 // SyncChannel needs to be destructed on the thread that it was created on.
185 channel_.reset();
[email protected]9d4ff5ed2009-03-03 00:21:56186
187 MessageLoop::current()->RunAllPending();
[email protected]aa96ae772009-01-20 22:08:15188
[email protected]72b6f8e22011-11-12 21:16:41189 ipc_thread_.message_loop()->PostTask(
190 FROM_HERE, base::Bind(&Worker::OnIPCThreadShutdown, this,
191 listener_event, ipc_event));
[email protected]3cdb7af812008-10-24 19:21:13192 }
193
[email protected]9d4ff5ed2009-03-03 00:21:56194 void OnIPCThreadShutdown(WaitableEvent* listener_event,
195 WaitableEvent* ipc_event) {
196 MessageLoop::current()->RunAllPending();
[email protected]aa96ae772009-01-20 22:08:15197 ipc_event->Signal();
[email protected]9d4ff5ed2009-03-03 00:21:56198
[email protected]72b6f8e22011-11-12 21:16:41199 listener_thread_.message_loop()->PostTask(
200 FROM_HERE, base::Bind(&Worker::OnListenerThreadShutdown2, this,
201 listener_event));
[email protected]9d4ff5ed2009-03-03 00:21:56202 }
203
204 void OnListenerThreadShutdown2(WaitableEvent* listener_event) {
205 MessageLoop::current()->RunAllPending();
206 listener_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13207 }
208
[email protected]a95986a82010-12-24 06:19:28209 bool OnMessageReceived(const Message& message) {
initial.commit09911bf2008-07-26 23:55:29210 IPC_BEGIN_MESSAGE_MAP(Worker, message)
[email protected]3cdb7af812008-10-24 19:21:13211 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay)
initial.commit09911bf2008-07-26 23:55:29212 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife,
213 OnAnswerDelay)
[email protected]ac0efda2009-10-14 16:22:02214 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelNestedTestMsg_String,
215 OnNestedTestMsg)
initial.commit09911bf2008-07-26 23:55:29216 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28217 return true;
initial.commit09911bf2008-07-26 23:55:29218 }
219
[email protected]17b89142008-11-07 21:52:15220 void StartThread(base::Thread* thread, MessageLoop::Type type) {
[email protected]ab820df2008-08-26 05:55:10221 base::Thread::Options options;
[email protected]17b89142008-11-07 21:52:15222 options.message_loop_type = type;
[email protected]ab820df2008-08-26 05:55:10223 thread->StartWithOptions(options);
224 }
225
[email protected]aa96ae772009-01-20 22:08:15226 scoped_ptr<WaitableEvent> done_;
227 scoped_ptr<WaitableEvent> channel_created_;
[email protected]9a3a293b2009-06-04 22:28:16228 std::string channel_name_;
initial.commit09911bf2008-07-26 23:55:29229 Channel::Mode mode_;
230 scoped_ptr<SyncChannel> channel_;
[email protected]ab820df2008-08-26 05:55:10231 base::Thread ipc_thread_;
232 base::Thread listener_thread_;
233 base::Thread* overrided_thread_;
initial.commit09911bf2008-07-26 23:55:29234
[email protected]8930d472009-02-21 08:05:28235 base::WaitableEvent shutdown_event_;
236
[email protected]73a797fb2010-06-07 02:10:18237 DISALLOW_COPY_AND_ASSIGN(Worker);
initial.commit09911bf2008-07-26 23:55:29238};
239
240
241// Starts the test with the given workers. This function deletes the workers
242// when it's done.
243void RunTest(std::vector<Worker*> workers) {
initial.commit09911bf2008-07-26 23:55:29244 // First we create the workers that are channel servers, or else the other
245 // workers' channel initialization might fail because the pipe isn't created..
246 for (size_t i = 0; i < workers.size(); ++i) {
[email protected]1707726c2011-02-03 20:35:09247 if (workers[i]->mode() & Channel::MODE_SERVER_FLAG) {
initial.commit09911bf2008-07-26 23:55:29248 workers[i]->Start();
249 workers[i]->WaitForChannelCreation();
250 }
251 }
252
253 // now create the clients
254 for (size_t i = 0; i < workers.size(); ++i) {
[email protected]1707726c2011-02-03 20:35:09255 if (workers[i]->mode() & Channel::MODE_CLIENT_FLAG)
initial.commit09911bf2008-07-26 23:55:29256 workers[i]->Start();
257 }
258
259 // wait for all the workers to finish
initial.commit09911bf2008-07-26 23:55:29260 for (size_t i = 0; i < workers.size(); ++i)
[email protected]aa96ae772009-01-20 22:08:15261 workers[i]->done_event()->Wait();
initial.commit09911bf2008-07-26 23:55:29262
[email protected]82e5ee82009-04-03 02:29:45263 STLDeleteContainerPointers(workers.begin(), workers.end());
initial.commit09911bf2008-07-26 23:55:29264}
265
[email protected]dd3eac22008-08-26 07:28:34266} // namespace
267
[email protected]9629c0e2009-02-04 23:16:29268class IPCSyncChannelTest : public testing::Test {
269 private:
270 MessageLoop message_loop_;
271};
272
initial.commit09911bf2008-07-26 23:55:29273//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34274
275namespace {
276
initial.commit09911bf2008-07-26 23:55:29277class SimpleServer : public Worker {
278 public:
[email protected]b7243c42010-07-23 05:23:13279 explicit SimpleServer(bool pump_during_send)
[email protected]3cdb7af812008-10-24 19:21:13280 : Worker(Channel::MODE_SERVER, "simpler_server"),
281 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29282 void Run() {
[email protected]aa96ae772009-01-20 22:08:15283 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
initial.commit09911bf2008-07-26 23:55:29284 Done();
285 }
[email protected]3cdb7af812008-10-24 19:21:13286
287 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29288};
289
290class SimpleClient : public Worker {
291 public:
292 SimpleClient() : Worker(Channel::MODE_CLIENT, "simple_client") { }
293
294 void OnAnswer(int* answer) {
295 *answer = 42;
296 Done();
297 }
298};
299
[email protected]3cdb7af812008-10-24 19:21:13300void Simple(bool pump_during_send) {
initial.commit09911bf2008-07-26 23:55:29301 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13302 workers.push_back(new SimpleServer(pump_during_send));
initial.commit09911bf2008-07-26 23:55:29303 workers.push_back(new SimpleClient());
304 RunTest(workers);
305}
306
[email protected]3cdb7af812008-10-24 19:21:13307} // namespace
308
309// Tests basic synchronous call
310TEST_F(IPCSyncChannelTest, Simple) {
311 Simple(false);
312 Simple(true);
313}
initial.commit09911bf2008-07-26 23:55:29314
315//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34316
317namespace {
318
[email protected]952394af2011-11-16 01:06:46319// Worker classes which override how the sync channel is created to use the
320// two-step initialization (calling the lightweight constructor and then
321// ChannelProxy::Init separately) process.
322class TwoStepServer : public Worker {
323 public:
324 explicit TwoStepServer(bool create_pipe_now)
325 : Worker(Channel::MODE_SERVER, "simpler_server"),
326 create_pipe_now_(create_pipe_now) { }
327
328 void Run() {
329 SendAnswerToLife(false, base::kNoTimeout, true);
330 Done();
331 }
332
333 virtual SyncChannel* CreateChannel() {
334 SyncChannel* channel = new SyncChannel(
335 this, ipc_thread().message_loop_proxy(), shutdown_event());
336 channel->Init(channel_name(), mode(), create_pipe_now_);
337 return channel;
338 }
339
340 bool create_pipe_now_;
341};
342
343class TwoStepClient : public Worker {
344 public:
345 TwoStepClient(bool create_pipe_now)
346 : Worker(Channel::MODE_CLIENT, "simple_client"),
347 create_pipe_now_(create_pipe_now) { }
348
349 void OnAnswer(int* answer) {
350 *answer = 42;
351 Done();
352 }
353
354 virtual SyncChannel* CreateChannel() {
355 SyncChannel* channel = new SyncChannel(
356 this, ipc_thread().message_loop_proxy(), shutdown_event());
357 channel->Init(channel_name(), mode(), create_pipe_now_);
358 return channel;
359 }
360
361 bool create_pipe_now_;
362};
363
364void TwoStep(bool create_server_pipe_now, bool create_client_pipe_now) {
365 std::vector<Worker*> workers;
366 workers.push_back(new TwoStepServer(create_server_pipe_now));
367 workers.push_back(new TwoStepClient(create_client_pipe_now));
368 RunTest(workers);
369}
370
371} // namespace
372
373// Tests basic two-step initialization, where you call the lightweight
374// constructor then Init.
375TEST_F(IPCSyncChannelTest, TwoStepInitialization) {
376 TwoStep(false, false);
377 TwoStep(false, true);
378 TwoStep(true, false);
379 TwoStep(true, true);
380}
381
382
383//-----------------------------------------------------------------------------
384
385namespace {
386
initial.commit09911bf2008-07-26 23:55:29387class DelayClient : public Worker {
388 public:
389 DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { }
390
391 void OnAnswerDelay(Message* reply_msg) {
392 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
393 Send(reply_msg);
394 Done();
395 }
396};
397
[email protected]3cdb7af812008-10-24 19:21:13398void DelayReply(bool pump_during_send) {
initial.commit09911bf2008-07-26 23:55:29399 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13400 workers.push_back(new SimpleServer(pump_during_send));
initial.commit09911bf2008-07-26 23:55:29401 workers.push_back(new DelayClient());
402 RunTest(workers);
403}
404
[email protected]3cdb7af812008-10-24 19:21:13405} // namespace
406
407// Tests that asynchronous replies work
408TEST_F(IPCSyncChannelTest, DelayReply) {
409 DelayReply(false);
410 DelayReply(true);
411}
initial.commit09911bf2008-07-26 23:55:29412
413//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34414
415namespace {
416
initial.commit09911bf2008-07-26 23:55:29417class NoHangServer : public Worker {
418 public:
[email protected]e7e38032011-07-26 17:25:25419 NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send)
[email protected]3cdb7af812008-10-24 19:21:13420 : Worker(Channel::MODE_SERVER, "no_hang_server"),
421 got_first_reply_(got_first_reply),
422 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29423 void Run() {
[email protected]aa96ae772009-01-20 22:08:15424 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
425 got_first_reply_->Signal();
initial.commit09911bf2008-07-26 23:55:29426
[email protected]aa96ae772009-01-20 22:08:15427 SendAnswerToLife(pump_during_send_, base::kNoTimeout, false);
initial.commit09911bf2008-07-26 23:55:29428 Done();
429 }
430
[email protected]aa96ae772009-01-20 22:08:15431 WaitableEvent* got_first_reply_;
[email protected]3cdb7af812008-10-24 19:21:13432 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29433};
434
435class NoHangClient : public Worker {
436 public:
[email protected]aa96ae772009-01-20 22:08:15437 explicit NoHangClient(WaitableEvent* got_first_reply)
initial.commit09911bf2008-07-26 23:55:29438 : Worker(Channel::MODE_CLIENT, "no_hang_client"),
439 got_first_reply_(got_first_reply) { }
440
441 virtual void OnAnswerDelay(Message* reply_msg) {
442 // Use the DELAY_REPLY macro so that we can force the reply to be sent
443 // before this function returns (when the channel will be reset).
444 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
445 Send(reply_msg);
446 got_first_reply_->Wait();
447 CloseChannel();
448 Done();
449 }
450
[email protected]aa96ae772009-01-20 22:08:15451 WaitableEvent* got_first_reply_;
initial.commit09911bf2008-07-26 23:55:29452};
453
[email protected]3cdb7af812008-10-24 19:21:13454void NoHang(bool pump_during_send) {
[email protected]aa96ae772009-01-20 22:08:15455 WaitableEvent got_first_reply(false, false);
initial.commit09911bf2008-07-26 23:55:29456 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13457 workers.push_back(new NoHangServer(&got_first_reply, pump_during_send));
initial.commit09911bf2008-07-26 23:55:29458 workers.push_back(new NoHangClient(&got_first_reply));
459 RunTest(workers);
460}
461
[email protected]3cdb7af812008-10-24 19:21:13462} // namespace
463
464// Tests that caller doesn't hang if receiver dies
465TEST_F(IPCSyncChannelTest, NoHang) {
466 NoHang(false);
467 NoHang(true);
468}
initial.commit09911bf2008-07-26 23:55:29469
470//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34471
472namespace {
473
[email protected]3cdb7af812008-10-24 19:21:13474class UnblockServer : public Worker {
initial.commit09911bf2008-07-26 23:55:29475 public:
[email protected]9eec2252009-12-01 02:34:18476 UnblockServer(bool pump_during_send, bool delete_during_send)
[email protected]3cdb7af812008-10-24 19:21:13477 : Worker(Channel::MODE_SERVER, "unblock_server"),
[email protected]9eec2252009-12-01 02:34:18478 pump_during_send_(pump_during_send),
479 delete_during_send_(delete_during_send) { }
initial.commit09911bf2008-07-26 23:55:29480 void Run() {
[email protected]9eec2252009-12-01 02:34:18481 if (delete_during_send_) {
482 // Use custom code since race conditions mean the answer may or may not be
483 // available.
484 int answer = 0;
485 SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer);
486 if (pump_during_send_)
487 msg->EnableMessagePumping();
488 Send(msg);
489 } else {
490 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
491 }
initial.commit09911bf2008-07-26 23:55:29492 Done();
493 }
494
[email protected]9eec2252009-12-01 02:34:18495 void OnDoubleDelay(int in, Message* reply_msg) {
496 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
497 Send(reply_msg);
498 if (delete_during_send_)
499 ResetChannel();
initial.commit09911bf2008-07-26 23:55:29500 }
[email protected]3cdb7af812008-10-24 19:21:13501
502 bool pump_during_send_;
[email protected]9eec2252009-12-01 02:34:18503 bool delete_during_send_;
initial.commit09911bf2008-07-26 23:55:29504};
505
[email protected]3cdb7af812008-10-24 19:21:13506class UnblockClient : public Worker {
initial.commit09911bf2008-07-26 23:55:29507 public:
[email protected]b7243c42010-07-23 05:23:13508 explicit UnblockClient(bool pump_during_send)
[email protected]3cdb7af812008-10-24 19:21:13509 : Worker(Channel::MODE_CLIENT, "unblock_client"),
510 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29511
512 void OnAnswer(int* answer) {
[email protected]4df10d612008-11-12 00:38:26513 SendDouble(pump_during_send_, true);
514 *answer = 42;
initial.commit09911bf2008-07-26 23:55:29515 Done();
516 }
[email protected]3cdb7af812008-10-24 19:21:13517
518 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29519};
520
[email protected]9eec2252009-12-01 02:34:18521void Unblock(bool server_pump, bool client_pump, bool delete_during_send) {
[email protected]3cdb7af812008-10-24 19:21:13522 std::vector<Worker*> workers;
[email protected]9eec2252009-12-01 02:34:18523 workers.push_back(new UnblockServer(server_pump, delete_during_send));
[email protected]3cdb7af812008-10-24 19:21:13524 workers.push_back(new UnblockClient(client_pump));
525 RunTest(workers);
526}
527
[email protected]dd3eac22008-08-26 07:28:34528} // namespace
529
initial.commit09911bf2008-07-26 23:55:29530// Tests that the caller unblocks to answer a sync message from the receiver.
[email protected]3cdb7af812008-10-24 19:21:13531TEST_F(IPCSyncChannelTest, Unblock) {
[email protected]9eec2252009-12-01 02:34:18532 Unblock(false, false, false);
533 Unblock(false, true, false);
534 Unblock(true, false, false);
535 Unblock(true, true, false);
536}
537
538//-----------------------------------------------------------------------------
539
[email protected]c9e0c7332011-05-13 22:42:41540// Tests that the the SyncChannel object can be deleted during a Send.
[email protected]9eec2252009-12-01 02:34:18541TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) {
542 Unblock(false, false, true);
543 Unblock(false, true, true);
544 Unblock(true, false, true);
545 Unblock(true, true, true);
[email protected]3cdb7af812008-10-24 19:21:13546}
547
548//-----------------------------------------------------------------------------
549
550namespace {
551
552class RecursiveServer : public Worker {
553 public:
[email protected]e7e38032011-07-26 17:25:25554 RecursiveServer(bool expected_send_result, bool pump_first, bool pump_second)
555 : Worker(Channel::MODE_SERVER, "recursive_server"),
556 expected_send_result_(expected_send_result),
557 pump_first_(pump_first), pump_second_(pump_second) {}
[email protected]3cdb7af812008-10-24 19:21:13558 void Run() {
[email protected]4df10d612008-11-12 00:38:26559 SendDouble(pump_first_, expected_send_result_);
[email protected]3cdb7af812008-10-24 19:21:13560 Done();
561 }
562
563 void OnDouble(int in, int* out) {
[email protected]4df10d612008-11-12 00:38:26564 *out = in * 2;
[email protected]aa96ae772009-01-20 22:08:15565 SendAnswerToLife(pump_second_, base::kNoTimeout, expected_send_result_);
[email protected]3cdb7af812008-10-24 19:21:13566 }
567
568 bool expected_send_result_, pump_first_, pump_second_;
569};
570
571class RecursiveClient : public Worker {
572 public:
[email protected]e7e38032011-07-26 17:25:25573 RecursiveClient(bool pump_during_send, bool close_channel)
574 : Worker(Channel::MODE_CLIENT, "recursive_client"),
575 pump_during_send_(pump_during_send), close_channel_(close_channel) {}
[email protected]3cdb7af812008-10-24 19:21:13576
577 void OnDoubleDelay(int in, Message* reply_msg) {
[email protected]4df10d612008-11-12 00:38:26578 SendDouble(pump_during_send_, !close_channel_);
[email protected]c690a182008-10-24 23:10:32579 if (close_channel_) {
580 delete reply_msg;
581 } else {
[email protected]3cdb7af812008-10-24 19:21:13582 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
583 Send(reply_msg);
584 }
585 Done();
586 }
587
588 void OnAnswerDelay(Message* reply_msg) {
589 if (close_channel_) {
[email protected]c690a182008-10-24 23:10:32590 delete reply_msg;
[email protected]3cdb7af812008-10-24 19:21:13591 CloseChannel();
592 } else {
593 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
594 Send(reply_msg);
595 }
596 }
597
598 bool pump_during_send_, close_channel_;
599};
600
601void Recursive(
602 bool server_pump_first, bool server_pump_second, bool client_pump) {
initial.commit09911bf2008-07-26 23:55:29603 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13604 workers.push_back(
605 new RecursiveServer(true, server_pump_first, server_pump_second));
606 workers.push_back(new RecursiveClient(client_pump, false));
initial.commit09911bf2008-07-26 23:55:29607 RunTest(workers);
608}
609
[email protected]3cdb7af812008-10-24 19:21:13610} // namespace
611
612// Tests a server calling Send while another Send is pending.
613TEST_F(IPCSyncChannelTest, Recursive) {
614 Recursive(false, false, false);
615 Recursive(false, false, true);
616 Recursive(false, true, false);
617 Recursive(false, true, true);
618 Recursive(true, false, false);
619 Recursive(true, false, true);
620 Recursive(true, true, false);
621 Recursive(true, true, true);
622}
623
624//-----------------------------------------------------------------------------
625
626namespace {
627
628void RecursiveNoHang(
629 bool server_pump_first, bool server_pump_second, bool client_pump) {
630 std::vector<Worker*> workers;
631 workers.push_back(
632 new RecursiveServer(false, server_pump_first, server_pump_second));
633 workers.push_back(new RecursiveClient(client_pump, true));
634 RunTest(workers);
635}
636
637} // namespace
638
639// Tests that if a caller makes a sync call during an existing sync call and
640// the receiver dies, neither of the Send() calls hang.
641TEST_F(IPCSyncChannelTest, RecursiveNoHang) {
642 RecursiveNoHang(false, false, false);
643 RecursiveNoHang(false, false, true);
644 RecursiveNoHang(false, true, false);
645 RecursiveNoHang(false, true, true);
646 RecursiveNoHang(true, false, false);
647 RecursiveNoHang(true, false, true);
648 RecursiveNoHang(true, true, false);
649 RecursiveNoHang(true, true, true);
650}
initial.commit09911bf2008-07-26 23:55:29651
652//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34653
654namespace {
655
initial.commit09911bf2008-07-26 23:55:29656class MultipleServer1 : public Worker {
657 public:
[email protected]b7243c42010-07-23 05:23:13658 explicit MultipleServer1(bool pump_during_send)
[email protected]9a3a293b2009-06-04 22:28:16659 : Worker("test_channel1", Channel::MODE_SERVER),
[email protected]3cdb7af812008-10-24 19:21:13660 pump_during_send_(pump_during_send) { }
661
initial.commit09911bf2008-07-26 23:55:29662 void Run() {
[email protected]4df10d612008-11-12 00:38:26663 SendDouble(pump_during_send_, true);
initial.commit09911bf2008-07-26 23:55:29664 Done();
665 }
[email protected]3cdb7af812008-10-24 19:21:13666
667 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29668};
669
670class MultipleClient1 : public Worker {
671 public:
[email protected]aa96ae772009-01-20 22:08:15672 MultipleClient1(WaitableEvent* client1_msg_received,
673 WaitableEvent* client1_can_reply) :
[email protected]9a3a293b2009-06-04 22:28:16674 Worker("test_channel1", Channel::MODE_CLIENT),
initial.commit09911bf2008-07-26 23:55:29675 client1_msg_received_(client1_msg_received),
676 client1_can_reply_(client1_can_reply) { }
677
678 void OnDouble(int in, int* out) {
[email protected]aa96ae772009-01-20 22:08:15679 client1_msg_received_->Signal();
initial.commit09911bf2008-07-26 23:55:29680 *out = in * 2;
681 client1_can_reply_->Wait();
682 Done();
683 }
684
685 private:
[email protected]aa96ae772009-01-20 22:08:15686 WaitableEvent *client1_msg_received_, *client1_can_reply_;
initial.commit09911bf2008-07-26 23:55:29687};
688
689class MultipleServer2 : public Worker {
690 public:
[email protected]9a3a293b2009-06-04 22:28:16691 MultipleServer2() : Worker("test_channel2", Channel::MODE_SERVER) { }
initial.commit09911bf2008-07-26 23:55:29692
693 void OnAnswer(int* result) {
694 *result = 42;
695 Done();
696 }
697};
698
699class MultipleClient2 : public Worker {
700 public:
[email protected]3cdb7af812008-10-24 19:21:13701 MultipleClient2(
[email protected]aa96ae772009-01-20 22:08:15702 WaitableEvent* client1_msg_received, WaitableEvent* client1_can_reply,
[email protected]3cdb7af812008-10-24 19:21:13703 bool pump_during_send)
[email protected]9a3a293b2009-06-04 22:28:16704 : Worker("test_channel2", Channel::MODE_CLIENT),
initial.commit09911bf2008-07-26 23:55:29705 client1_msg_received_(client1_msg_received),
[email protected]3cdb7af812008-10-24 19:21:13706 client1_can_reply_(client1_can_reply),
707 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29708
709 void Run() {
initial.commit09911bf2008-07-26 23:55:29710 client1_msg_received_->Wait();
[email protected]aa96ae772009-01-20 22:08:15711 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
712 client1_can_reply_->Signal();
initial.commit09911bf2008-07-26 23:55:29713 Done();
714 }
715
716 private:
[email protected]aa96ae772009-01-20 22:08:15717 WaitableEvent *client1_msg_received_, *client1_can_reply_;
[email protected]3cdb7af812008-10-24 19:21:13718 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29719};
720
[email protected]3cdb7af812008-10-24 19:21:13721void Multiple(bool server_pump, bool client_pump) {
initial.commit09911bf2008-07-26 23:55:29722 std::vector<Worker*> workers;
723
724 // A shared worker thread so that server1 and server2 run on one thread.
[email protected]ab820df2008-08-26 05:55:10725 base::Thread worker_thread("Multiple");
[email protected]6314e6f62009-07-15 16:07:14726 ASSERT_TRUE(worker_thread.Start());
initial.commit09911bf2008-07-26 23:55:29727
728 // Server1 sends a sync msg to client1, which blocks the reply until
729 // server2 (which runs on the same worker thread as server1) responds
730 // to a sync msg from client2.
[email protected]aa96ae772009-01-20 22:08:15731 WaitableEvent client1_msg_received(false, false);
732 WaitableEvent client1_can_reply(false, false);
initial.commit09911bf2008-07-26 23:55:29733
734 Worker* worker;
735
736 worker = new MultipleServer2();
737 worker->OverrideThread(&worker_thread);
738 workers.push_back(worker);
739
740 worker = new MultipleClient2(
[email protected]3cdb7af812008-10-24 19:21:13741 &client1_msg_received, &client1_can_reply, client_pump);
initial.commit09911bf2008-07-26 23:55:29742 workers.push_back(worker);
743
[email protected]3cdb7af812008-10-24 19:21:13744 worker = new MultipleServer1(server_pump);
initial.commit09911bf2008-07-26 23:55:29745 worker->OverrideThread(&worker_thread);
746 workers.push_back(worker);
747
748 worker = new MultipleClient1(
749 &client1_msg_received, &client1_can_reply);
750 workers.push_back(worker);
751
752 RunTest(workers);
753}
754
[email protected]3cdb7af812008-10-24 19:21:13755} // namespace
756
757// Tests that multiple SyncObjects on the same listener thread can unblock each
758// other.
759TEST_F(IPCSyncChannelTest, Multiple) {
760 Multiple(false, false);
761 Multiple(false, true);
762 Multiple(true, false);
763 Multiple(true, true);
764}
initial.commit09911bf2008-07-26 23:55:29765
766//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34767
768namespace {
769
[email protected]ac0efda2009-10-14 16:22:02770// This class provides server side functionality to test the case where
771// multiple sync channels are in use on the same thread on the client and
772// nested calls are issued.
773class QueuedReplyServer : public Worker {
initial.commit09911bf2008-07-26 23:55:29774 public:
[email protected]b7243c42010-07-23 05:23:13775 QueuedReplyServer(base::Thread* listener_thread,
776 const std::string& channel_name,
777 const std::string& reply_text)
[email protected]ac0efda2009-10-14 16:22:02778 : Worker(channel_name, Channel::MODE_SERVER),
779 reply_text_(reply_text) {
780 Worker::OverrideThread(listener_thread);
initial.commit09911bf2008-07-26 23:55:29781 }
[email protected]3cdb7af812008-10-24 19:21:13782
[email protected]ac0efda2009-10-14 16:22:02783 virtual void OnNestedTestMsg(Message* reply_msg) {
[email protected]2a9d601b2010-10-19 23:50:00784 VLOG(1) << __FUNCTION__ << " Sending reply: " << reply_text_;
785 SyncChannelNestedTestMsg_String::WriteReplyParams(reply_msg, reply_text_);
[email protected]ac0efda2009-10-14 16:22:02786 Send(reply_msg);
initial.commit09911bf2008-07-26 23:55:29787 Done();
788 }
789
790 private:
[email protected]ac0efda2009-10-14 16:22:02791 std::string reply_text_;
initial.commit09911bf2008-07-26 23:55:29792};
793
[email protected]ac0efda2009-10-14 16:22:02794// The QueuedReplyClient class provides functionality to test the case where
795// multiple sync channels are in use on the same thread and they make nested
796// sync calls, i.e. while the first channel waits for a response it makes a
797// sync call on another channel.
798// The callstack should unwind correctly, i.e. the outermost call should
799// complete first, and so on.
800class QueuedReplyClient : public Worker {
initial.commit09911bf2008-07-26 23:55:29801 public:
[email protected]ac0efda2009-10-14 16:22:02802 QueuedReplyClient(base::Thread* listener_thread,
803 const std::string& channel_name,
804 const std::string& expected_text,
805 bool pump_during_send)
806 : Worker(channel_name, Channel::MODE_CLIENT),
[email protected]7ee1a44c2010-07-23 14:18:59807 pump_during_send_(pump_during_send),
808 expected_text_(expected_text) {
[email protected]ac0efda2009-10-14 16:22:02809 Worker::OverrideThread(listener_thread);
810 }
initial.commit09911bf2008-07-26 23:55:29811
[email protected]ac0efda2009-10-14 16:22:02812 virtual void Run() {
813 std::string response;
814 SyncMessage* msg = new SyncChannelNestedTestMsg_String(&response);
815 if (pump_during_send_)
816 msg->EnableMessagePumping();
817 bool result = Send(msg);
818 DCHECK(result);
[email protected]9eec2252009-12-01 02:34:18819 DCHECK_EQ(response, expected_text_);
initial.commit09911bf2008-07-26 23:55:29820
[email protected]2a9d601b2010-10-19 23:50:00821 VLOG(1) << __FUNCTION__ << " Received reply: " << response;
initial.commit09911bf2008-07-26 23:55:29822 Done();
823 }
824
[email protected]ac0efda2009-10-14 16:22:02825 private:
[email protected]3cdb7af812008-10-24 19:21:13826 bool pump_during_send_;
[email protected]ac0efda2009-10-14 16:22:02827 std::string expected_text_;
initial.commit09911bf2008-07-26 23:55:29828};
829
[email protected]ac0efda2009-10-14 16:22:02830void QueuedReply(bool client_pump) {
initial.commit09911bf2008-07-26 23:55:29831 std::vector<Worker*> workers;
832
[email protected]ac0efda2009-10-14 16:22:02833 // A shared worker thread for servers
834 base::Thread server_worker_thread("QueuedReply_ServerListener");
835 ASSERT_TRUE(server_worker_thread.Start());
initial.commit09911bf2008-07-26 23:55:29836
[email protected]ac0efda2009-10-14 16:22:02837 base::Thread client_worker_thread("QueuedReply_ClientListener");
838 ASSERT_TRUE(client_worker_thread.Start());
initial.commit09911bf2008-07-26 23:55:29839
840 Worker* worker;
841
[email protected]ac0efda2009-10-14 16:22:02842 worker = new QueuedReplyServer(&server_worker_thread,
843 "QueuedReply_Server1",
844 "Got first message");
initial.commit09911bf2008-07-26 23:55:29845 workers.push_back(worker);
846
[email protected]ac0efda2009-10-14 16:22:02847 worker = new QueuedReplyServer(&server_worker_thread,
848 "QueuedReply_Server2",
849 "Got second message");
initial.commit09911bf2008-07-26 23:55:29850 workers.push_back(worker);
851
[email protected]ac0efda2009-10-14 16:22:02852 worker = new QueuedReplyClient(&client_worker_thread,
853 "QueuedReply_Server1",
854 "Got first message",
855 client_pump);
initial.commit09911bf2008-07-26 23:55:29856 workers.push_back(worker);
857
[email protected]ac0efda2009-10-14 16:22:02858 worker = new QueuedReplyClient(&client_worker_thread,
859 "QueuedReply_Server2",
860 "Got second message",
861 client_pump);
initial.commit09911bf2008-07-26 23:55:29862 workers.push_back(worker);
863
864 RunTest(workers);
865}
866
[email protected]3cdb7af812008-10-24 19:21:13867} // namespace
868
869// While a blocking send is in progress, the listener thread might answer other
870// synchronous messages. This tests that if during the response to another
871// message the reply to the original messages comes, it is queued up correctly
872// and the original Send is unblocked later.
[email protected]ac0efda2009-10-14 16:22:02873// We also test that the send call stacks unwind correctly when the channel
874// pumps messages while waiting for a response.
[email protected]3cdb7af812008-10-24 19:21:13875TEST_F(IPCSyncChannelTest, QueuedReply) {
[email protected]ac0efda2009-10-14 16:22:02876 QueuedReply(false);
877 QueuedReply(true);
[email protected]3cdb7af812008-10-24 19:21:13878}
initial.commit09911bf2008-07-26 23:55:29879
880//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34881
882namespace {
883
[email protected]3cdb7af812008-10-24 19:21:13884class ChattyClient : public Worker {
initial.commit09911bf2008-07-26 23:55:29885 public:
[email protected]3cdb7af812008-10-24 19:21:13886 ChattyClient() :
887 Worker(Channel::MODE_CLIENT, "chatty_client") { }
initial.commit09911bf2008-07-26 23:55:29888
889 void OnAnswer(int* answer) {
890 // The PostMessage limit is 10k. Send 20% more than that.
891 const int kMessageLimit = 10000;
892 const int kMessagesToSend = kMessageLimit * 120 / 100;
893 for (int i = 0; i < kMessagesToSend; ++i) {
[email protected]4df10d612008-11-12 00:38:26894 if (!SendDouble(false, true))
initial.commit09911bf2008-07-26 23:55:29895 break;
896 }
[email protected]4df10d612008-11-12 00:38:26897 *answer = 42;
initial.commit09911bf2008-07-26 23:55:29898 Done();
899 }
900};
901
[email protected]3cdb7af812008-10-24 19:21:13902void ChattyServer(bool pump_during_send) {
903 std::vector<Worker*> workers;
[email protected]9eec2252009-12-01 02:34:18904 workers.push_back(new UnblockServer(pump_during_send, false));
[email protected]3cdb7af812008-10-24 19:21:13905 workers.push_back(new ChattyClient());
906 RunTest(workers);
907}
908
[email protected]dd3eac22008-08-26 07:28:34909} // namespace
910
[email protected]4df10d612008-11-12 00:38:26911// Tests http://b/1093251 - that sending lots of sync messages while
initial.commit09911bf2008-07-26 23:55:29912// the receiver is waiting for a sync reply does not overflow the PostMessage
913// queue.
[email protected]dd3eac22008-08-26 07:28:34914TEST_F(IPCSyncChannelTest, ChattyServer) {
[email protected]3cdb7af812008-10-24 19:21:13915 ChattyServer(false);
916 ChattyServer(true);
initial.commit09911bf2008-07-26 23:55:29917}
[email protected]d65cab7a2008-08-12 01:25:41918
[email protected]d65cab7a2008-08-12 01:25:41919//------------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34920
921namespace {
922
[email protected]d65cab7a2008-08-12 01:25:41923class TimeoutServer : public Worker {
924 public:
[email protected]b7243c42010-07-23 05:23:13925 TimeoutServer(int timeout_ms,
926 std::vector<bool> timeout_seq,
927 bool pump_during_send)
[email protected]d65cab7a2008-08-12 01:25:41928 : Worker(Channel::MODE_SERVER, "timeout_server"),
929 timeout_ms_(timeout_ms),
[email protected]3cdb7af812008-10-24 19:21:13930 timeout_seq_(timeout_seq),
931 pump_during_send_(pump_during_send) {
[email protected]d65cab7a2008-08-12 01:25:41932 }
933
934 void Run() {
935 for (std::vector<bool>::const_iterator iter = timeout_seq_.begin();
936 iter != timeout_seq_.end(); ++iter) {
[email protected]4df10d612008-11-12 00:38:26937 SendAnswerToLife(pump_during_send_, timeout_ms_, !*iter);
[email protected]d65cab7a2008-08-12 01:25:41938 }
939 Done();
940 }
941
942 private:
943 int timeout_ms_;
944 std::vector<bool> timeout_seq_;
[email protected]3cdb7af812008-10-24 19:21:13945 bool pump_during_send_;
[email protected]d65cab7a2008-08-12 01:25:41946};
947
948class UnresponsiveClient : public Worker {
949 public:
[email protected]b7243c42010-07-23 05:23:13950 explicit UnresponsiveClient(std::vector<bool> timeout_seq)
[email protected]d65cab7a2008-08-12 01:25:41951 : Worker(Channel::MODE_CLIENT, "unresponsive_client"),
952 timeout_seq_(timeout_seq) {
[email protected]b7243c42010-07-23 05:23:13953 }
[email protected]d65cab7a2008-08-12 01:25:41954
955 void OnAnswerDelay(Message* reply_msg) {
956 DCHECK(!timeout_seq_.empty());
957 if (!timeout_seq_[0]) {
958 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
959 Send(reply_msg);
960 } else {
961 // Don't reply.
[email protected]463667372008-08-20 20:20:52962 delete reply_msg;
[email protected]d65cab7a2008-08-12 01:25:41963 }
964 timeout_seq_.erase(timeout_seq_.begin());
965 if (timeout_seq_.empty())
966 Done();
967 }
968
969 private:
970 // Whether we should time-out or respond to the various messages we receive.
971 std::vector<bool> timeout_seq_;
972};
973
[email protected]3cdb7af812008-10-24 19:21:13974void SendWithTimeoutOK(bool pump_during_send) {
975 std::vector<Worker*> workers;
976 std::vector<bool> timeout_seq;
977 timeout_seq.push_back(false);
978 timeout_seq.push_back(false);
979 timeout_seq.push_back(false);
980 workers.push_back(new TimeoutServer(5000, timeout_seq, pump_during_send));
981 workers.push_back(new SimpleClient());
982 RunTest(workers);
983}
984
985void SendWithTimeoutTimeout(bool pump_during_send) {
986 std::vector<Worker*> workers;
987 std::vector<bool> timeout_seq;
988 timeout_seq.push_back(true);
989 timeout_seq.push_back(false);
990 timeout_seq.push_back(false);
991 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send));
992 workers.push_back(new UnresponsiveClient(timeout_seq));
993 RunTest(workers);
994}
995
996void SendWithTimeoutMixedOKAndTimeout(bool pump_during_send) {
997 std::vector<Worker*> workers;
998 std::vector<bool> timeout_seq;
999 timeout_seq.push_back(true);
1000 timeout_seq.push_back(false);
1001 timeout_seq.push_back(false);
1002 timeout_seq.push_back(true);
1003 timeout_seq.push_back(false);
1004 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send));
1005 workers.push_back(new UnresponsiveClient(timeout_seq));
1006 RunTest(workers);
1007}
1008
[email protected]dd3eac22008-08-26 07:28:341009} // namespace
1010
[email protected]d65cab7a2008-08-12 01:25:411011// Tests that SendWithTimeout does not time-out if the response comes back fast
1012// enough.
[email protected]dd3eac22008-08-26 07:28:341013TEST_F(IPCSyncChannelTest, SendWithTimeoutOK) {
[email protected]3cdb7af812008-10-24 19:21:131014 SendWithTimeoutOK(false);
1015 SendWithTimeoutOK(true);
[email protected]d65cab7a2008-08-12 01:25:411016}
1017
1018// Tests that SendWithTimeout does time-out.
[email protected]dd3eac22008-08-26 07:28:341019TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) {
[email protected]3cdb7af812008-10-24 19:21:131020 SendWithTimeoutTimeout(false);
1021 SendWithTimeoutTimeout(true);
[email protected]d65cab7a2008-08-12 01:25:411022}
1023
1024// Sends some message that time-out and some that succeed.
[email protected]711032e2011-01-19 08:11:561025// Crashes flakily, http://crbug.com/70075.
1026TEST_F(IPCSyncChannelTest, DISABLED_SendWithTimeoutMixedOKAndTimeout) {
[email protected]3cdb7af812008-10-24 19:21:131027 SendWithTimeoutMixedOKAndTimeout(false);
1028 SendWithTimeoutMixedOKAndTimeout(true);
1029}
[email protected]4df10d612008-11-12 00:38:261030
1031//------------------------------------------------------------------------------
1032
1033namespace {
1034
[email protected]0633e3152011-11-28 18:35:391035void NestedCallback(Worker* server) {
1036 // Sleep a bit so that we wake up after the reply has been received.
[email protected]b5393332012-01-13 00:11:011037 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(250));
[email protected]0633e3152011-11-28 18:35:391038 server->SendAnswerToLife(true, base::kNoTimeout, true);
1039}
[email protected]4df10d612008-11-12 00:38:261040
[email protected]0633e3152011-11-28 18:35:391041bool timeout_occurred = false;
[email protected]4df10d612008-11-12 00:38:261042
[email protected]0633e3152011-11-28 18:35:391043void TimeoutCallback() {
1044 timeout_occurred = true;
1045}
[email protected]4df10d612008-11-12 00:38:261046
1047class DoneEventRaceServer : public Worker {
1048 public:
1049 DoneEventRaceServer()
1050 : Worker(Channel::MODE_SERVER, "done_event_race_server") { }
1051
1052 void Run() {
[email protected]0633e3152011-11-28 18:35:391053 MessageLoop::current()->PostTask(FROM_HERE,
1054 base::Bind(&NestedCallback, this));
1055 MessageLoop::current()->PostDelayedTask(
1056 FROM_HERE, base::Bind(&TimeoutCallback), 9000);
[email protected]4df10d612008-11-12 00:38:261057 // Even though we have a timeout on the Send, it will succeed since for this
1058 // bug, the reply message comes back and is deserialized, however the done
1059 // event wasn't set. So we indirectly use the timeout task to notice if a
1060 // timeout occurred.
1061 SendAnswerToLife(true, 10000, true);
[email protected]0633e3152011-11-28 18:35:391062 DCHECK(!timeout_occurred);
[email protected]4df10d612008-11-12 00:38:261063 Done();
1064 }
1065};
1066
1067} // namespace
1068
1069// Tests http://b/1474092 - that if after the done_event is set but before
1070// OnObjectSignaled is called another message is sent out, then after its
1071// reply comes back OnObjectSignaled will be called for the first message.
1072TEST_F(IPCSyncChannelTest, DoneEventRace) {
1073 std::vector<Worker*> workers;
1074 workers.push_back(new DoneEventRaceServer());
1075 workers.push_back(new SimpleClient());
1076 RunTest(workers);
1077}
[email protected]1e9499c2010-04-06 20:33:361078
1079//-----------------------------------------------------------------------------
1080
1081namespace {
1082
[email protected]c9e0c7332011-05-13 22:42:411083class TestSyncMessageFilter : public SyncMessageFilter {
[email protected]1e9499c2010-04-06 20:33:361084 public:
1085 TestSyncMessageFilter(base::WaitableEvent* shutdown_event, Worker* worker)
1086 : SyncMessageFilter(shutdown_event),
1087 worker_(worker),
1088 thread_("helper_thread") {
1089 base::Thread::Options options;
1090 options.message_loop_type = MessageLoop::TYPE_DEFAULT;
1091 thread_.StartWithOptions(options);
1092 }
1093
1094 virtual void OnFilterAdded(Channel* channel) {
1095 SyncMessageFilter::OnFilterAdded(channel);
[email protected]72b6f8e22011-11-12 21:16:411096 thread_.message_loop()->PostTask(
1097 FROM_HERE,
1098 base::Bind(&TestSyncMessageFilter::SendMessageOnHelperThread, this));
[email protected]1e9499c2010-04-06 20:33:361099 }
1100
1101 void SendMessageOnHelperThread() {
1102 int answer = 0;
1103 bool result = Send(new SyncChannelTestMsg_AnswerToLife(&answer));
1104 DCHECK(result);
1105 DCHECK_EQ(answer, 42);
1106
1107 worker_->Done();
1108 }
1109
1110 Worker* worker_;
1111 base::Thread thread_;
1112};
1113
1114class SyncMessageFilterServer : public Worker {
1115 public:
1116 SyncMessageFilterServer()
1117 : Worker(Channel::MODE_SERVER, "sync_message_filter_server") {
1118 filter_ = new TestSyncMessageFilter(shutdown_event(), this);
1119 }
1120
1121 void Run() {
1122 channel()->AddFilter(filter_.get());
1123 }
1124
1125 scoped_refptr<TestSyncMessageFilter> filter_;
1126};
1127
[email protected]87339f02010-09-02 21:45:501128// This class provides functionality to test the case that a Send on the sync
1129// channel does not crash after the channel has been closed.
1130class ServerSendAfterClose : public Worker {
1131 public:
1132 ServerSendAfterClose()
1133 : Worker(Channel::MODE_SERVER, "simpler_server"),
1134 send_result_(true) {
1135 }
1136
1137 bool SendDummy() {
[email protected]72b6f8e22011-11-12 21:16:411138 ListenerThread()->message_loop()->PostTask(
[email protected]dcde7672012-01-06 02:37:171139 FROM_HERE, base::Bind(base::IgnoreResult(&ServerSendAfterClose::Send),
1140 this, new SyncChannelTestMsg_NoArgs));
[email protected]87339f02010-09-02 21:45:501141 return true;
1142 }
1143
1144 bool send_result() const {
1145 return send_result_;
1146 }
1147
1148 private:
1149 virtual void Run() {
1150 CloseChannel();
1151 Done();
1152 }
1153
1154 bool Send(Message* msg) {
1155 send_result_ = Worker::Send(msg);
1156 Done();
1157 return send_result_;
1158 }
1159
1160 bool send_result_;
1161};
1162
[email protected]1e9499c2010-04-06 20:33:361163} // namespace
1164
1165// Tests basic synchronous call
1166TEST_F(IPCSyncChannelTest, SyncMessageFilter) {
1167 std::vector<Worker*> workers;
1168 workers.push_back(new SyncMessageFilterServer());
1169 workers.push_back(new SimpleClient());
1170 RunTest(workers);
1171}
[email protected]87339f02010-09-02 21:45:501172
1173// Test the case when the channel is closed and a Send is attempted after that.
1174TEST_F(IPCSyncChannelTest, SendAfterClose) {
1175 ServerSendAfterClose server;
1176 server.Start();
1177
1178 server.done_event()->Wait();
1179 server.done_event()->Reset();
1180
1181 server.SendDummy();
1182 server.done_event()->Wait();
1183
1184 EXPECT_FALSE(server.send_result());
1185}
1186
[email protected]54af05f2011-04-08 03:38:211187//-----------------------------------------------------------------------------
[email protected]87339f02010-09-02 21:45:501188
[email protected]54af05f2011-04-08 03:38:211189namespace {
1190
1191class RestrictedDispatchServer : public Worker {
1192 public:
1193 RestrictedDispatchServer(WaitableEvent* sent_ping_event)
1194 : Worker("restricted_channel", Channel::MODE_SERVER),
1195 sent_ping_event_(sent_ping_event) { }
1196
1197 void OnDoPing(int ping) {
1198 // Send an asynchronous message that unblocks the caller.
[email protected]c9e0c7332011-05-13 22:42:411199 Message* msg = new SyncChannelTestMsg_Ping(ping);
[email protected]54af05f2011-04-08 03:38:211200 msg->set_unblock(true);
1201 Send(msg);
1202 // Signal the event after the message has been sent on the channel, on the
1203 // IPC thread.
[email protected]72b6f8e22011-11-12 21:16:411204 ipc_thread().message_loop()->PostTask(
1205 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnPingSent, this));
[email protected]54af05f2011-04-08 03:38:211206 }
1207
1208 base::Thread* ListenerThread() { return Worker::ListenerThread(); }
1209
1210 private:
1211 bool OnMessageReceived(const Message& message) {
1212 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchServer, message)
1213 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
1214 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done)
1215 IPC_END_MESSAGE_MAP()
1216 return true;
1217 }
1218
1219 void OnPingSent() {
1220 sent_ping_event_->Signal();
1221 }
1222
1223 void OnNoArgs() { }
1224 WaitableEvent* sent_ping_event_;
1225};
1226
1227class NonRestrictedDispatchServer : public Worker {
1228 public:
1229 NonRestrictedDispatchServer()
1230 : Worker("non_restricted_channel", Channel::MODE_SERVER) {}
1231
1232 private:
1233 bool OnMessageReceived(const Message& message) {
1234 IPC_BEGIN_MESSAGE_MAP(NonRestrictedDispatchServer, message)
1235 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
1236 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done)
1237 IPC_END_MESSAGE_MAP()
1238 return true;
1239 }
1240
1241 void OnNoArgs() { }
1242};
1243
1244class RestrictedDispatchClient : public Worker {
1245 public:
1246 RestrictedDispatchClient(WaitableEvent* sent_ping_event,
1247 RestrictedDispatchServer* server,
1248 int* success)
1249 : Worker("restricted_channel", Channel::MODE_CLIENT),
1250 ping_(0),
1251 server_(server),
1252 success_(success),
1253 sent_ping_event_(sent_ping_event) {}
1254
1255 void Run() {
1256 // Incoming messages from our channel should only be dispatched when we
1257 // send a message on that same channel.
1258 channel()->SetRestrictDispatchToSameChannel(true);
1259
[email protected]72b6f8e22011-11-12 21:16:411260 server_->ListenerThread()->message_loop()->PostTask(
1261 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 1));
[email protected]54af05f2011-04-08 03:38:211262 sent_ping_event_->Wait();
1263 Send(new SyncChannelTestMsg_NoArgs);
1264 if (ping_ == 1)
1265 ++*success_;
1266 else
1267 LOG(ERROR) << "Send failed to dispatch incoming message on same channel";
1268
1269 scoped_ptr<SyncChannel> non_restricted_channel(new SyncChannel(
1270 "non_restricted_channel", Channel::MODE_CLIENT, this,
[email protected]92bf9062011-05-02 18:00:491271 ipc_thread().message_loop_proxy(), true, shutdown_event()));
[email protected]54af05f2011-04-08 03:38:211272
[email protected]72b6f8e22011-11-12 21:16:411273 server_->ListenerThread()->message_loop()->PostTask(
1274 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 2));
[email protected]54af05f2011-04-08 03:38:211275 sent_ping_event_->Wait();
1276 // Check that the incoming message is *not* dispatched when sending on the
1277 // non restricted channel.
1278 // TODO(piman): there is a possibility of a false positive race condition
1279 // here, if the message that was posted on the server-side end of the pipe
1280 // is not visible yet on the client side, but I don't know how to solve this
1281 // without hooking into the internals of SyncChannel. I haven't seen it in
1282 // practice (i.e. not setting SetRestrictDispatchToSameChannel does cause
1283 // the following to fail).
1284 non_restricted_channel->Send(new SyncChannelTestMsg_NoArgs);
1285 if (ping_ == 1)
1286 ++*success_;
1287 else
1288 LOG(ERROR) << "Send dispatched message from restricted channel";
1289
1290 Send(new SyncChannelTestMsg_NoArgs);
1291 if (ping_ == 2)
1292 ++*success_;
1293 else
1294 LOG(ERROR) << "Send failed to dispatch incoming message on same channel";
1295
1296 non_restricted_channel->Send(new SyncChannelTestMsg_Done);
1297 non_restricted_channel.reset();
1298 Send(new SyncChannelTestMsg_Done);
1299 Done();
1300 }
1301
1302 private:
1303 bool OnMessageReceived(const Message& message) {
1304 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchClient, message)
1305 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Ping, OnPing)
1306 IPC_END_MESSAGE_MAP()
1307 return true;
1308 }
1309
1310 void OnPing(int ping) {
1311 ping_ = ping;
1312 }
1313
1314 int ping_;
1315 RestrictedDispatchServer* server_;
1316 int* success_;
1317 WaitableEvent* sent_ping_event_;
1318};
1319
1320} // namespace
1321
1322TEST_F(IPCSyncChannelTest, RestrictedDispatch) {
1323 WaitableEvent sent_ping_event(false, false);
1324
1325 RestrictedDispatchServer* server =
1326 new RestrictedDispatchServer(&sent_ping_event);
1327 int success = 0;
1328 std::vector<Worker*> workers;
1329 workers.push_back(new NonRestrictedDispatchServer);
1330 workers.push_back(server);
1331 workers.push_back(
1332 new RestrictedDispatchClient(&sent_ping_event, server, &success));
1333 RunTest(workers);
1334 EXPECT_EQ(3, success);
1335}
[email protected]c9e0c7332011-05-13 22:42:411336
[email protected]522cc10d2012-01-11 22:39:541337//-----------------------------------------------------------------------------
1338
1339// This test case inspired by crbug.com/108491
1340// We create two servers that use the same ListenerThread but have
1341// SetRestrictDispatchToSameChannel set to true.
1342// We create clients, then use some specific WaitableEvent wait/signalling to
1343// ensure that messages get dispatched in a way that causes a deadlock due to
1344// a nested dispatch and an eligible message in a higher-level dispatch's
1345// delayed_queue. Specifically, we start with client1 about so send an
1346// unblocking message to server1, while the shared listener thread for the
1347// servers server1 and server2 is about to send a non-unblocking message to
1348// client1. At the same time, client2 will be about to send an unblocking
1349// message to server2. Server1 will handle the client1->server1 message by
1350// telling server2 to send a non-unblocking message to client2.
1351// What should happen is that the send to server2 should find the pending,
1352// same-context client2->server2 message to dispatch, causing client2 to
1353// unblock then handle the server2->client2 message, so that the shared
1354// servers' listener thread can then respond to the client1->server1 message.
1355// Then client1 can handle the non-unblocking server1->client1 message.
1356// The old code would end up in a state where the server2->client2 message is
1357// sent, but the client2->server2 message (which is eligible for dispatch, and
1358// which is what client2 is waiting for) is stashed in a local delayed_queue
1359// that has server1's channel context, causing a deadlock.
1360// WaitableEvents in the events array are used to:
1361// event 0: indicate to client1 that server listener is in OnDoServerTask
1362// event 1: indicate to client1 that client2 listener is in OnDoClient2Task
1363// event 2: indicate to server1 that client2 listener is in OnDoClient2Task
1364// event 3: indicate to client2 that server listener is in OnDoServerTask
1365
1366namespace {
1367
1368class RestrictedDispatchDeadlockServer : public Worker {
1369 public:
1370 RestrictedDispatchDeadlockServer(int server_num,
1371 WaitableEvent* server_ready_event,
1372 WaitableEvent** events,
1373 RestrictedDispatchDeadlockServer* peer)
1374 : Worker(server_num == 1 ? "channel1" : "channel2", Channel::MODE_SERVER),
1375 server_num_(server_num),
1376 server_ready_event_(server_ready_event),
1377 events_(events),
1378 peer_(peer),
1379 client_kicked_(false) { }
1380
1381 void OnDoServerTask() {
1382 events_[3]->Signal();
1383 events_[2]->Wait();
1384 events_[0]->Signal();
1385 SendMessageToClient();
1386 }
1387
1388 void Run() {
1389 channel()->SetRestrictDispatchToSameChannel(true);
1390 server_ready_event_->Signal();
1391 }
1392
1393 base::Thread* ListenerThread() { return Worker::ListenerThread(); }
1394
1395 private:
1396 bool OnMessageReceived(const Message& message) {
1397 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchDeadlockServer, message)
1398 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
1399 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done)
1400 IPC_END_MESSAGE_MAP()
1401 return true;
1402 }
1403
1404 void OnNoArgs() {
1405 if (server_num_ == 1) {
1406 DCHECK(peer_ != NULL);
1407 peer_->SendMessageToClient();
1408 }
1409 }
1410
1411 void SendMessageToClient() {
1412 Message* msg = new SyncChannelTestMsg_NoArgs;
1413 msg->set_unblock(false);
1414 DCHECK(!msg->should_unblock());
1415 Send(msg);
1416 }
1417
1418 int server_num_;
1419 WaitableEvent* server_ready_event_;
1420 WaitableEvent** events_;
1421 RestrictedDispatchDeadlockServer* peer_;
1422 bool client_kicked_;
1423};
1424
1425class RestrictedDispatchDeadlockClient2 : public Worker {
1426 public:
1427 RestrictedDispatchDeadlockClient2(RestrictedDispatchDeadlockServer* server,
1428 WaitableEvent* server_ready_event,
1429 WaitableEvent** events)
1430 : Worker("channel2", Channel::MODE_CLIENT),
1431 server_(server),
1432 server_ready_event_(server_ready_event),
1433 events_(events),
1434 received_msg_(false),
1435 received_noarg_reply_(false),
1436 done_issued_(false) {}
1437
1438 void Run() {
1439 server_ready_event_->Wait();
1440 }
1441
1442 void OnDoClient2Task() {
1443 events_[3]->Wait();
1444 events_[1]->Signal();
1445 events_[2]->Signal();
1446 DCHECK(received_msg_ == false);
1447
1448 Message* message = new SyncChannelTestMsg_NoArgs;
1449 message->set_unblock(true);
1450 Send(message);
1451 received_noarg_reply_ = true;
1452 }
1453
1454 base::Thread* ListenerThread() { return Worker::ListenerThread(); }
1455 private:
1456 bool OnMessageReceived(const Message& message) {
1457 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchDeadlockClient2, message)
1458 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
1459 IPC_END_MESSAGE_MAP()
1460 return true;
1461 }
1462
1463 void OnNoArgs() {
1464 received_msg_ = true;
1465 PossiblyDone();
1466 }
1467
1468 void PossiblyDone() {
1469 if (received_noarg_reply_ && received_msg_) {
1470 DCHECK(done_issued_ == false);
1471 done_issued_ = true;
1472 Send(new SyncChannelTestMsg_Done);
1473 Done();
1474 }
1475 }
1476
1477 RestrictedDispatchDeadlockServer* server_;
1478 WaitableEvent* server_ready_event_;
1479 WaitableEvent** events_;
1480 bool received_msg_;
1481 bool received_noarg_reply_;
1482 bool done_issued_;
1483};
1484
1485class RestrictedDispatchDeadlockClient1 : public Worker {
1486 public:
1487 RestrictedDispatchDeadlockClient1(RestrictedDispatchDeadlockServer* server,
1488 RestrictedDispatchDeadlockClient2* peer,
1489 WaitableEvent* server_ready_event,
1490 WaitableEvent** events)
1491 : Worker("channel1", Channel::MODE_CLIENT),
1492 server_(server),
1493 peer_(peer),
1494 server_ready_event_(server_ready_event),
1495 events_(events),
1496 received_msg_(false),
1497 received_noarg_reply_(false),
1498 done_issued_(false) {}
1499
1500 void Run() {
1501 server_ready_event_->Wait();
1502 server_->ListenerThread()->message_loop()->PostTask(
1503 FROM_HERE,
1504 base::Bind(&RestrictedDispatchDeadlockServer::OnDoServerTask, server_));
1505 peer_->ListenerThread()->message_loop()->PostTask(
1506 FROM_HERE,
1507 base::Bind(&RestrictedDispatchDeadlockClient2::OnDoClient2Task, peer_));
1508 events_[0]->Wait();
1509 events_[1]->Wait();
1510 DCHECK(received_msg_ == false);
1511
1512 Message* message = new SyncChannelTestMsg_NoArgs;
1513 message->set_unblock(true);
1514 Send(message);
1515 received_noarg_reply_ = true;
1516 PossiblyDone();
1517 }
1518
1519 base::Thread* ListenerThread() { return Worker::ListenerThread(); }
1520 private:
1521 bool OnMessageReceived(const Message& message) {
1522 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchDeadlockClient1, message)
1523 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
1524 IPC_END_MESSAGE_MAP()
1525 return true;
1526 }
1527
1528 void OnNoArgs() {
1529 received_msg_ = true;
1530 PossiblyDone();
1531 }
1532
1533 void PossiblyDone() {
1534 if (received_noarg_reply_ && received_msg_) {
1535 DCHECK(done_issued_ == false);
1536 done_issued_ = true;
1537 Send(new SyncChannelTestMsg_Done);
1538 Done();
1539 }
1540 }
1541
1542 RestrictedDispatchDeadlockServer* server_;
1543 RestrictedDispatchDeadlockClient2* peer_;
1544 WaitableEvent* server_ready_event_;
1545 WaitableEvent** events_;
1546 bool received_msg_;
1547 bool received_noarg_reply_;
1548 bool done_issued_;
1549};
1550
1551} // namespace
1552
1553TEST_F(IPCSyncChannelTest, RestrictedDispatchDeadlock) {
1554 std::vector<Worker*> workers;
1555
1556 // A shared worker thread so that server1 and server2 run on one thread.
1557 base::Thread worker_thread("RestrictedDispatchDeadlock");
1558 ASSERT_TRUE(worker_thread.Start());
1559
1560 WaitableEvent server1_ready(false, false);
1561 WaitableEvent server2_ready(false, false);
1562
1563 WaitableEvent event0(false, false);
1564 WaitableEvent event1(false, false);
1565 WaitableEvent event2(false, false);
1566 WaitableEvent event3(false, false);
1567 WaitableEvent* events[4] = {&event0, &event1, &event2, &event3};
1568
1569 RestrictedDispatchDeadlockServer* server1;
1570 RestrictedDispatchDeadlockServer* server2;
1571 RestrictedDispatchDeadlockClient1* client1;
1572 RestrictedDispatchDeadlockClient2* client2;
1573
1574 server2 = new RestrictedDispatchDeadlockServer(2, &server2_ready, events,
1575 NULL);
1576 server2->OverrideThread(&worker_thread);
1577 workers.push_back(server2);
1578
1579 client2 = new RestrictedDispatchDeadlockClient2(server2, &server2_ready,
1580 events);
1581 workers.push_back(client2);
1582
1583 server1 = new RestrictedDispatchDeadlockServer(1, &server1_ready, events,
1584 server2);
1585 server1->OverrideThread(&worker_thread);
1586 workers.push_back(server1);
1587
1588 client1 = new RestrictedDispatchDeadlockClient1(server1, client2,
1589 &server1_ready, events);
1590 workers.push_back(client1);
1591
1592 RunTest(workers);
1593}
1594
[email protected]c9e0c7332011-05-13 22:42:411595} // namespace IPC