blob: e9ec02155a8d2ec97f444c288bea5e3cb24aa16c [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]0a6fc4b2012-04-05 02:38:3417#include "base/process_util.h"
[email protected]7286e3fc2011-07-19 22:13:2418#include "base/stl_util.h"
initial.commit09911bf2008-07-26 23:55:2919#include "base/string_util.h"
[email protected]ee857512010-05-14 08:24:4220#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
[email protected]f214f8792011-01-01 02:17:0821#include "base/threading/platform_thread.h"
[email protected]34b99632011-01-01 01:01:0622#include "base/threading/thread.h"
[email protected]44f9c952011-01-02 06:05:3923#include "base/synchronization/waitable_event.h"
[email protected]57319ce2012-06-11 22:35:2624#include "ipc/ipc_listener.h"
[email protected]946d1b22009-07-22 23:57:2125#include "ipc/ipc_message.h"
[email protected]57319ce2012-06-11 22:35:2626#include "ipc/ipc_sender.h"
[email protected]1e9499c2010-04-06 20:33:3627#include "ipc/ipc_sync_message_filter.h"
[email protected]21fa3a12010-12-08 23:34:1628#include "ipc/ipc_sync_message_unittest.h"
initial.commit09911bf2008-07-26 23:55:2929#include "testing/gtest/include/gtest/gtest.h"
30
[email protected]aa96ae772009-01-20 22:08:1531using base::WaitableEvent;
initial.commit09911bf2008-07-26 23:55:2932
[email protected]c9e0c7332011-05-13 22:42:4133namespace IPC {
34
[email protected]dd3eac22008-08-26 07:28:3435namespace {
36
initial.commit09911bf2008-07-26 23:55:2937// Base class for a "process" with listener and IPC threads.
[email protected]57319ce2012-06-11 22:35:2638class Worker : public Listener, public Sender {
initial.commit09911bf2008-07-26 23:55:2939 public:
40 // Will create a channel without a name.
41 Worker(Channel::Mode mode, const std::string& thread_name)
[email protected]aa96ae772009-01-20 22:08:1542 : done_(new WaitableEvent(false, false)),
43 channel_created_(new WaitableEvent(false, false)),
initial.commit09911bf2008-07-26 23:55:2944 mode_(mode),
45 ipc_thread_((thread_name + "_ipc").c_str()),
46 listener_thread_((thread_name + "_listener").c_str()),
[email protected]8930d472009-02-21 08:05:2847 overrided_thread_(NULL),
[email protected]8a734422009-10-27 11:28:5848 shutdown_event_(true, false) {
49 // The data race on vfptr is real but is very hard
50 // to suppress using standard Valgrind mechanism (suppressions).
51 // We have to use ANNOTATE_BENIGN_RACE to hide the reports and
52 // make ThreadSanitizer bots green.
53 ANNOTATE_BENIGN_RACE(this, "Race on vfptr, http://crbug.com/25841");
54 }
initial.commit09911bf2008-07-26 23:55:2955
56 // Will create a named channel and use this name for the threads' name.
[email protected]9a3a293b2009-06-04 22:28:1657 Worker(const std::string& channel_name, Channel::Mode mode)
[email protected]aa96ae772009-01-20 22:08:1558 : done_(new WaitableEvent(false, false)),
59 channel_created_(new WaitableEvent(false, false)),
60 channel_name_(channel_name),
initial.commit09911bf2008-07-26 23:55:2961 mode_(mode),
[email protected]9a3a293b2009-06-04 22:28:1662 ipc_thread_((channel_name + "_ipc").c_str()),
63 listener_thread_((channel_name + "_listener").c_str()),
[email protected]8930d472009-02-21 08:05:2864 overrided_thread_(NULL),
[email protected]8a734422009-10-27 11:28:5865 shutdown_event_(true, false) {
66 // The data race on vfptr is real but is very hard
67 // to suppress using standard Valgrind mechanism (suppressions).
68 // We have to use ANNOTATE_BENIGN_RACE to hide the reports and
69 // make ThreadSanitizer bots green.
70 ANNOTATE_BENIGN_RACE(this, "Race on vfptr, http://crbug.com/25841");
71 }
initial.commit09911bf2008-07-26 23:55:2972
73 // The IPC thread needs to outlive SyncChannel, so force the correct order of
74 // destruction.
75 virtual ~Worker() {
[email protected]aa96ae772009-01-20 22:08:1576 WaitableEvent listener_done(false, false), ipc_done(false, false);
[email protected]72b6f8e22011-11-12 21:16:4177 ListenerThread()->message_loop()->PostTask(
78 FROM_HERE, base::Bind(&Worker::OnListenerThreadShutdown1, this,
79 &listener_done, &ipc_done));
[email protected]aa96ae772009-01-20 22:08:1580 listener_done.Wait();
81 ipc_done.Wait();
initial.commit09911bf2008-07-26 23:55:2982 ipc_thread_.Stop();
83 listener_thread_.Stop();
initial.commit09911bf2008-07-26 23:55:2984 }
85 void AddRef() { }
86 void Release() { }
87 bool Send(Message* msg) { return channel_->Send(msg); }
[email protected]d65cab7a2008-08-12 01:25:4188 bool SendWithTimeout(Message* msg, int timeout_ms) {
89 return channel_->SendWithTimeout(msg, timeout_ms);
90 }
[email protected]aa96ae772009-01-20 22:08:1591 void WaitForChannelCreation() { channel_created_->Wait(); }
[email protected]3cdb7af812008-10-24 19:21:1392 void CloseChannel() {
93 DCHECK(MessageLoop::current() == ListenerThread()->message_loop());
94 channel_->Close();
95 }
initial.commit09911bf2008-07-26 23:55:2996 void Start() {
[email protected]17b89142008-11-07 21:52:1597 StartThread(&listener_thread_, MessageLoop::TYPE_DEFAULT);
[email protected]72b6f8e22011-11-12 21:16:4198 ListenerThread()->message_loop()->PostTask(
99 FROM_HERE, base::Bind(&Worker::OnStart, this));
initial.commit09911bf2008-07-26 23:55:29100 }
[email protected]ab820df2008-08-26 05:55:10101 void OverrideThread(base::Thread* overrided_thread) {
initial.commit09911bf2008-07-26 23:55:29102 DCHECK(overrided_thread_ == NULL);
103 overrided_thread_ = overrided_thread;
104 }
[email protected]4df10d612008-11-12 00:38:26105 bool SendAnswerToLife(bool pump, int timeout, bool succeed) {
106 int answer = 0;
107 SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer);
108 if (pump)
109 msg->EnableMessagePumping();
110 bool result = SendWithTimeout(msg, timeout);
[email protected]9eec2252009-12-01 02:34:18111 DCHECK_EQ(result, succeed);
112 DCHECK_EQ(answer, (succeed ? 42 : 0));
[email protected]4df10d612008-11-12 00:38:26113 return result;
114 }
115 bool SendDouble(bool pump, bool succeed) {
116 int answer = 0;
117 SyncMessage* msg = new SyncChannelTestMsg_Double(5, &answer);
118 if (pump)
119 msg->EnableMessagePumping();
120 bool result = Send(msg);
[email protected]9eec2252009-12-01 02:34:18121 DCHECK_EQ(result, succeed);
122 DCHECK_EQ(answer, (succeed ? 10 : 0));
[email protected]4df10d612008-11-12 00:38:26123 return result;
124 }
[email protected]952394af2011-11-16 01:06:46125 const std::string& channel_name() { return channel_name_; }
initial.commit09911bf2008-07-26 23:55:29126 Channel::Mode mode() { return mode_; }
[email protected]aa96ae772009-01-20 22:08:15127 WaitableEvent* done_event() { return done_.get(); }
[email protected]1e9499c2010-04-06 20:33:36128 WaitableEvent* shutdown_event() { return &shutdown_event_; }
[email protected]9eec2252009-12-01 02:34:18129 void ResetChannel() { channel_.reset(); }
initial.commit09911bf2008-07-26 23:55:29130 // Derived classes need to call this when they've completed their part of
131 // the test.
[email protected]aa96ae772009-01-20 22:08:15132 void Done() { done_->Signal(); }
[email protected]1e9499c2010-04-06 20:33:36133
134 protected:
[email protected]c9e0c7332011-05-13 22:42:41135 SyncChannel* channel() { return channel_.get(); }
initial.commit09911bf2008-07-26 23:55:29136 // Functions for dervied classes to implement if they wish.
137 virtual void Run() { }
initial.commit09911bf2008-07-26 23:55:29138 virtual void OnAnswer(int* answer) { NOTREACHED(); }
139 virtual void OnAnswerDelay(Message* reply_msg) {
140 // The message handler map below can only take one entry for
141 // SyncChannelTestMsg_AnswerToLife, so since some classes want
142 // the normal version while other want the delayed reply, we
143 // call the normal version if the derived class didn't override
144 // this function.
145 int answer;
146 OnAnswer(&answer);
147 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, answer);
148 Send(reply_msg);
149 }
[email protected]3cdb7af812008-10-24 19:21:13150 virtual void OnDouble(int in, int* out) { NOTREACHED(); }
151 virtual void OnDoubleDelay(int in, Message* reply_msg) {
152 int result;
153 OnDouble(in, &result);
154 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result);
155 Send(reply_msg);
156 }
initial.commit09911bf2008-07-26 23:55:29157
[email protected]ac0efda2009-10-14 16:22:02158 virtual void OnNestedTestMsg(Message* reply_msg) {
159 NOTREACHED();
160 }
161
[email protected]952394af2011-11-16 01:06:46162 virtual SyncChannel* CreateChannel() {
163 return new SyncChannel(
164 channel_name_, mode_, this, ipc_thread_.message_loop_proxy(), true,
165 &shutdown_event_);
166 }
167
[email protected]3cdb7af812008-10-24 19:21:13168 base::Thread* ListenerThread() {
169 return overrided_thread_ ? overrided_thread_ : &listener_thread_;
170 }
[email protected]87339f02010-09-02 21:45:50171
[email protected]54af05f2011-04-08 03:38:21172 const base::Thread& ipc_thread() const { return ipc_thread_; }
173
[email protected]87339f02010-09-02 21:45:50174 private:
initial.commit09911bf2008-07-26 23:55:29175 // Called on the listener thread to create the sync channel.
176 void OnStart() {
initial.commit09911bf2008-07-26 23:55:29177 // Link ipc_thread_, listener_thread_ and channel_ altogether.
[email protected]17b89142008-11-07 21:52:15178 StartThread(&ipc_thread_, MessageLoop::TYPE_IO);
[email protected]952394af2011-11-16 01:06:46179 channel_.reset(CreateChannel());
[email protected]aa96ae772009-01-20 22:08:15180 channel_created_->Signal();
initial.commit09911bf2008-07-26 23:55:29181 Run();
182 }
183
[email protected]9d4ff5ed2009-03-03 00:21:56184 void OnListenerThreadShutdown1(WaitableEvent* listener_event,
185 WaitableEvent* ipc_event) {
[email protected]3cdb7af812008-10-24 19:21:13186 // SyncChannel needs to be destructed on the thread that it was created on.
187 channel_.reset();
[email protected]9d4ff5ed2009-03-03 00:21:56188
189 MessageLoop::current()->RunAllPending();
[email protected]aa96ae772009-01-20 22:08:15190
[email protected]72b6f8e22011-11-12 21:16:41191 ipc_thread_.message_loop()->PostTask(
192 FROM_HERE, base::Bind(&Worker::OnIPCThreadShutdown, this,
193 listener_event, ipc_event));
[email protected]3cdb7af812008-10-24 19:21:13194 }
195
[email protected]9d4ff5ed2009-03-03 00:21:56196 void OnIPCThreadShutdown(WaitableEvent* listener_event,
197 WaitableEvent* ipc_event) {
198 MessageLoop::current()->RunAllPending();
[email protected]aa96ae772009-01-20 22:08:15199 ipc_event->Signal();
[email protected]9d4ff5ed2009-03-03 00:21:56200
[email protected]72b6f8e22011-11-12 21:16:41201 listener_thread_.message_loop()->PostTask(
202 FROM_HERE, base::Bind(&Worker::OnListenerThreadShutdown2, this,
203 listener_event));
[email protected]9d4ff5ed2009-03-03 00:21:56204 }
205
206 void OnListenerThreadShutdown2(WaitableEvent* listener_event) {
207 MessageLoop::current()->RunAllPending();
208 listener_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13209 }
210
[email protected]a95986a82010-12-24 06:19:28211 bool OnMessageReceived(const Message& message) {
initial.commit09911bf2008-07-26 23:55:29212 IPC_BEGIN_MESSAGE_MAP(Worker, message)
[email protected]3cdb7af812008-10-24 19:21:13213 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay)
initial.commit09911bf2008-07-26 23:55:29214 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife,
215 OnAnswerDelay)
[email protected]ac0efda2009-10-14 16:22:02216 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelNestedTestMsg_String,
217 OnNestedTestMsg)
initial.commit09911bf2008-07-26 23:55:29218 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28219 return true;
initial.commit09911bf2008-07-26 23:55:29220 }
221
[email protected]17b89142008-11-07 21:52:15222 void StartThread(base::Thread* thread, MessageLoop::Type type) {
[email protected]ab820df2008-08-26 05:55:10223 base::Thread::Options options;
[email protected]17b89142008-11-07 21:52:15224 options.message_loop_type = type;
[email protected]ab820df2008-08-26 05:55:10225 thread->StartWithOptions(options);
226 }
227
[email protected]aa96ae772009-01-20 22:08:15228 scoped_ptr<WaitableEvent> done_;
229 scoped_ptr<WaitableEvent> channel_created_;
[email protected]9a3a293b2009-06-04 22:28:16230 std::string channel_name_;
initial.commit09911bf2008-07-26 23:55:29231 Channel::Mode mode_;
232 scoped_ptr<SyncChannel> channel_;
[email protected]ab820df2008-08-26 05:55:10233 base::Thread ipc_thread_;
234 base::Thread listener_thread_;
235 base::Thread* overrided_thread_;
initial.commit09911bf2008-07-26 23:55:29236
[email protected]8930d472009-02-21 08:05:28237 base::WaitableEvent shutdown_event_;
238
[email protected]73a797fb2010-06-07 02:10:18239 DISALLOW_COPY_AND_ASSIGN(Worker);
initial.commit09911bf2008-07-26 23:55:29240};
241
242
243// Starts the test with the given workers. This function deletes the workers
244// when it's done.
245void RunTest(std::vector<Worker*> workers) {
initial.commit09911bf2008-07-26 23:55:29246 // First we create the workers that are channel servers, or else the other
247 // workers' channel initialization might fail because the pipe isn't created..
248 for (size_t i = 0; i < workers.size(); ++i) {
[email protected]1707726c2011-02-03 20:35:09249 if (workers[i]->mode() & Channel::MODE_SERVER_FLAG) {
initial.commit09911bf2008-07-26 23:55:29250 workers[i]->Start();
251 workers[i]->WaitForChannelCreation();
252 }
253 }
254
255 // now create the clients
256 for (size_t i = 0; i < workers.size(); ++i) {
[email protected]1707726c2011-02-03 20:35:09257 if (workers[i]->mode() & Channel::MODE_CLIENT_FLAG)
initial.commit09911bf2008-07-26 23:55:29258 workers[i]->Start();
259 }
260
261 // wait for all the workers to finish
initial.commit09911bf2008-07-26 23:55:29262 for (size_t i = 0; i < workers.size(); ++i)
[email protected]aa96ae772009-01-20 22:08:15263 workers[i]->done_event()->Wait();
initial.commit09911bf2008-07-26 23:55:29264
[email protected]82e5ee82009-04-03 02:29:45265 STLDeleteContainerPointers(workers.begin(), workers.end());
initial.commit09911bf2008-07-26 23:55:29266}
267
[email protected]dd3eac22008-08-26 07:28:34268} // namespace
269
[email protected]9629c0e2009-02-04 23:16:29270class IPCSyncChannelTest : public testing::Test {
271 private:
272 MessageLoop message_loop_;
273};
274
initial.commit09911bf2008-07-26 23:55:29275//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34276
277namespace {
278
initial.commit09911bf2008-07-26 23:55:29279class SimpleServer : public Worker {
280 public:
[email protected]b7243c42010-07-23 05:23:13281 explicit SimpleServer(bool pump_during_send)
[email protected]3cdb7af812008-10-24 19:21:13282 : Worker(Channel::MODE_SERVER, "simpler_server"),
283 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29284 void Run() {
[email protected]aa96ae772009-01-20 22:08:15285 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
initial.commit09911bf2008-07-26 23:55:29286 Done();
287 }
[email protected]3cdb7af812008-10-24 19:21:13288
289 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29290};
291
292class SimpleClient : public Worker {
293 public:
294 SimpleClient() : Worker(Channel::MODE_CLIENT, "simple_client") { }
295
296 void OnAnswer(int* answer) {
297 *answer = 42;
298 Done();
299 }
300};
301
[email protected]3cdb7af812008-10-24 19:21:13302void Simple(bool pump_during_send) {
initial.commit09911bf2008-07-26 23:55:29303 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13304 workers.push_back(new SimpleServer(pump_during_send));
initial.commit09911bf2008-07-26 23:55:29305 workers.push_back(new SimpleClient());
306 RunTest(workers);
307}
308
[email protected]3cdb7af812008-10-24 19:21:13309} // namespace
310
311// Tests basic synchronous call
312TEST_F(IPCSyncChannelTest, Simple) {
313 Simple(false);
314 Simple(true);
315}
initial.commit09911bf2008-07-26 23:55:29316
317//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34318
319namespace {
320
[email protected]952394af2011-11-16 01:06:46321// Worker classes which override how the sync channel is created to use the
322// two-step initialization (calling the lightweight constructor and then
323// ChannelProxy::Init separately) process.
324class TwoStepServer : public Worker {
325 public:
326 explicit TwoStepServer(bool create_pipe_now)
327 : Worker(Channel::MODE_SERVER, "simpler_server"),
328 create_pipe_now_(create_pipe_now) { }
329
330 void Run() {
331 SendAnswerToLife(false, base::kNoTimeout, true);
332 Done();
333 }
334
335 virtual SyncChannel* CreateChannel() {
336 SyncChannel* channel = new SyncChannel(
337 this, ipc_thread().message_loop_proxy(), shutdown_event());
338 channel->Init(channel_name(), mode(), create_pipe_now_);
339 return channel;
340 }
341
342 bool create_pipe_now_;
343};
344
345class TwoStepClient : public Worker {
346 public:
347 TwoStepClient(bool create_pipe_now)
348 : Worker(Channel::MODE_CLIENT, "simple_client"),
349 create_pipe_now_(create_pipe_now) { }
350
351 void OnAnswer(int* answer) {
352 *answer = 42;
353 Done();
354 }
355
356 virtual SyncChannel* CreateChannel() {
357 SyncChannel* channel = new SyncChannel(
358 this, ipc_thread().message_loop_proxy(), shutdown_event());
359 channel->Init(channel_name(), mode(), create_pipe_now_);
360 return channel;
361 }
362
363 bool create_pipe_now_;
364};
365
366void TwoStep(bool create_server_pipe_now, bool create_client_pipe_now) {
367 std::vector<Worker*> workers;
368 workers.push_back(new TwoStepServer(create_server_pipe_now));
369 workers.push_back(new TwoStepClient(create_client_pipe_now));
370 RunTest(workers);
371}
372
373} // namespace
374
375// Tests basic two-step initialization, where you call the lightweight
376// constructor then Init.
377TEST_F(IPCSyncChannelTest, TwoStepInitialization) {
378 TwoStep(false, false);
379 TwoStep(false, true);
380 TwoStep(true, false);
381 TwoStep(true, true);
382}
383
384
385//-----------------------------------------------------------------------------
386
387namespace {
388
initial.commit09911bf2008-07-26 23:55:29389class DelayClient : public Worker {
390 public:
391 DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { }
392
393 void OnAnswerDelay(Message* reply_msg) {
394 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
395 Send(reply_msg);
396 Done();
397 }
398};
399
[email protected]3cdb7af812008-10-24 19:21:13400void DelayReply(bool pump_during_send) {
initial.commit09911bf2008-07-26 23:55:29401 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13402 workers.push_back(new SimpleServer(pump_during_send));
initial.commit09911bf2008-07-26 23:55:29403 workers.push_back(new DelayClient());
404 RunTest(workers);
405}
406
[email protected]3cdb7af812008-10-24 19:21:13407} // namespace
408
409// Tests that asynchronous replies work
410TEST_F(IPCSyncChannelTest, DelayReply) {
411 DelayReply(false);
412 DelayReply(true);
413}
initial.commit09911bf2008-07-26 23:55:29414
415//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34416
417namespace {
418
initial.commit09911bf2008-07-26 23:55:29419class NoHangServer : public Worker {
420 public:
[email protected]e7e38032011-07-26 17:25:25421 NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send)
[email protected]3cdb7af812008-10-24 19:21:13422 : Worker(Channel::MODE_SERVER, "no_hang_server"),
423 got_first_reply_(got_first_reply),
424 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29425 void Run() {
[email protected]aa96ae772009-01-20 22:08:15426 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
427 got_first_reply_->Signal();
initial.commit09911bf2008-07-26 23:55:29428
[email protected]aa96ae772009-01-20 22:08:15429 SendAnswerToLife(pump_during_send_, base::kNoTimeout, false);
initial.commit09911bf2008-07-26 23:55:29430 Done();
431 }
432
[email protected]aa96ae772009-01-20 22:08:15433 WaitableEvent* got_first_reply_;
[email protected]3cdb7af812008-10-24 19:21:13434 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29435};
436
437class NoHangClient : public Worker {
438 public:
[email protected]aa96ae772009-01-20 22:08:15439 explicit NoHangClient(WaitableEvent* got_first_reply)
initial.commit09911bf2008-07-26 23:55:29440 : Worker(Channel::MODE_CLIENT, "no_hang_client"),
441 got_first_reply_(got_first_reply) { }
442
443 virtual void OnAnswerDelay(Message* reply_msg) {
444 // Use the DELAY_REPLY macro so that we can force the reply to be sent
445 // before this function returns (when the channel will be reset).
446 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
447 Send(reply_msg);
448 got_first_reply_->Wait();
449 CloseChannel();
450 Done();
451 }
452
[email protected]aa96ae772009-01-20 22:08:15453 WaitableEvent* got_first_reply_;
initial.commit09911bf2008-07-26 23:55:29454};
455
[email protected]3cdb7af812008-10-24 19:21:13456void NoHang(bool pump_during_send) {
[email protected]aa96ae772009-01-20 22:08:15457 WaitableEvent got_first_reply(false, false);
initial.commit09911bf2008-07-26 23:55:29458 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13459 workers.push_back(new NoHangServer(&got_first_reply, pump_during_send));
initial.commit09911bf2008-07-26 23:55:29460 workers.push_back(new NoHangClient(&got_first_reply));
461 RunTest(workers);
462}
463
[email protected]3cdb7af812008-10-24 19:21:13464} // namespace
465
466// Tests that caller doesn't hang if receiver dies
467TEST_F(IPCSyncChannelTest, NoHang) {
468 NoHang(false);
469 NoHang(true);
470}
initial.commit09911bf2008-07-26 23:55:29471
472//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34473
474namespace {
475
[email protected]3cdb7af812008-10-24 19:21:13476class UnblockServer : public Worker {
initial.commit09911bf2008-07-26 23:55:29477 public:
[email protected]9eec2252009-12-01 02:34:18478 UnblockServer(bool pump_during_send, bool delete_during_send)
[email protected]3cdb7af812008-10-24 19:21:13479 : Worker(Channel::MODE_SERVER, "unblock_server"),
[email protected]9eec2252009-12-01 02:34:18480 pump_during_send_(pump_during_send),
481 delete_during_send_(delete_during_send) { }
initial.commit09911bf2008-07-26 23:55:29482 void Run() {
[email protected]9eec2252009-12-01 02:34:18483 if (delete_during_send_) {
484 // Use custom code since race conditions mean the answer may or may not be
485 // available.
486 int answer = 0;
487 SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer);
488 if (pump_during_send_)
489 msg->EnableMessagePumping();
490 Send(msg);
491 } else {
492 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
493 }
initial.commit09911bf2008-07-26 23:55:29494 Done();
495 }
496
[email protected]9eec2252009-12-01 02:34:18497 void OnDoubleDelay(int in, Message* reply_msg) {
498 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
499 Send(reply_msg);
500 if (delete_during_send_)
501 ResetChannel();
initial.commit09911bf2008-07-26 23:55:29502 }
[email protected]3cdb7af812008-10-24 19:21:13503
504 bool pump_during_send_;
[email protected]9eec2252009-12-01 02:34:18505 bool delete_during_send_;
initial.commit09911bf2008-07-26 23:55:29506};
507
[email protected]3cdb7af812008-10-24 19:21:13508class UnblockClient : public Worker {
initial.commit09911bf2008-07-26 23:55:29509 public:
[email protected]b7243c42010-07-23 05:23:13510 explicit UnblockClient(bool pump_during_send)
[email protected]3cdb7af812008-10-24 19:21:13511 : Worker(Channel::MODE_CLIENT, "unblock_client"),
512 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29513
514 void OnAnswer(int* answer) {
[email protected]4df10d612008-11-12 00:38:26515 SendDouble(pump_during_send_, true);
516 *answer = 42;
initial.commit09911bf2008-07-26 23:55:29517 Done();
518 }
[email protected]3cdb7af812008-10-24 19:21:13519
520 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29521};
522
[email protected]9eec2252009-12-01 02:34:18523void Unblock(bool server_pump, bool client_pump, bool delete_during_send) {
[email protected]3cdb7af812008-10-24 19:21:13524 std::vector<Worker*> workers;
[email protected]9eec2252009-12-01 02:34:18525 workers.push_back(new UnblockServer(server_pump, delete_during_send));
[email protected]3cdb7af812008-10-24 19:21:13526 workers.push_back(new UnblockClient(client_pump));
527 RunTest(workers);
528}
529
[email protected]dd3eac22008-08-26 07:28:34530} // namespace
531
initial.commit09911bf2008-07-26 23:55:29532// Tests that the caller unblocks to answer a sync message from the receiver.
[email protected]3cdb7af812008-10-24 19:21:13533TEST_F(IPCSyncChannelTest, Unblock) {
[email protected]9eec2252009-12-01 02:34:18534 Unblock(false, false, false);
535 Unblock(false, true, false);
536 Unblock(true, false, false);
537 Unblock(true, true, false);
538}
539
540//-----------------------------------------------------------------------------
541
[email protected]c9e0c7332011-05-13 22:42:41542// Tests that the the SyncChannel object can be deleted during a Send.
[email protected]9eec2252009-12-01 02:34:18543TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) {
544 Unblock(false, false, true);
545 Unblock(false, true, true);
546 Unblock(true, false, true);
547 Unblock(true, true, true);
[email protected]3cdb7af812008-10-24 19:21:13548}
549
550//-----------------------------------------------------------------------------
551
552namespace {
553
554class RecursiveServer : public Worker {
555 public:
[email protected]e7e38032011-07-26 17:25:25556 RecursiveServer(bool expected_send_result, bool pump_first, bool pump_second)
557 : Worker(Channel::MODE_SERVER, "recursive_server"),
558 expected_send_result_(expected_send_result),
559 pump_first_(pump_first), pump_second_(pump_second) {}
[email protected]3cdb7af812008-10-24 19:21:13560 void Run() {
[email protected]4df10d612008-11-12 00:38:26561 SendDouble(pump_first_, expected_send_result_);
[email protected]3cdb7af812008-10-24 19:21:13562 Done();
563 }
564
565 void OnDouble(int in, int* out) {
[email protected]4df10d612008-11-12 00:38:26566 *out = in * 2;
[email protected]aa96ae772009-01-20 22:08:15567 SendAnswerToLife(pump_second_, base::kNoTimeout, expected_send_result_);
[email protected]3cdb7af812008-10-24 19:21:13568 }
569
570 bool expected_send_result_, pump_first_, pump_second_;
571};
572
573class RecursiveClient : public Worker {
574 public:
[email protected]e7e38032011-07-26 17:25:25575 RecursiveClient(bool pump_during_send, bool close_channel)
576 : Worker(Channel::MODE_CLIENT, "recursive_client"),
577 pump_during_send_(pump_during_send), close_channel_(close_channel) {}
[email protected]3cdb7af812008-10-24 19:21:13578
579 void OnDoubleDelay(int in, Message* reply_msg) {
[email protected]4df10d612008-11-12 00:38:26580 SendDouble(pump_during_send_, !close_channel_);
[email protected]c690a182008-10-24 23:10:32581 if (close_channel_) {
582 delete reply_msg;
583 } else {
[email protected]3cdb7af812008-10-24 19:21:13584 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
585 Send(reply_msg);
586 }
587 Done();
588 }
589
590 void OnAnswerDelay(Message* reply_msg) {
591 if (close_channel_) {
[email protected]c690a182008-10-24 23:10:32592 delete reply_msg;
[email protected]3cdb7af812008-10-24 19:21:13593 CloseChannel();
594 } else {
595 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
596 Send(reply_msg);
597 }
598 }
599
600 bool pump_during_send_, close_channel_;
601};
602
603void Recursive(
604 bool server_pump_first, bool server_pump_second, bool client_pump) {
initial.commit09911bf2008-07-26 23:55:29605 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13606 workers.push_back(
607 new RecursiveServer(true, server_pump_first, server_pump_second));
608 workers.push_back(new RecursiveClient(client_pump, false));
initial.commit09911bf2008-07-26 23:55:29609 RunTest(workers);
610}
611
[email protected]3cdb7af812008-10-24 19:21:13612} // namespace
613
614// Tests a server calling Send while another Send is pending.
615TEST_F(IPCSyncChannelTest, Recursive) {
616 Recursive(false, false, false);
617 Recursive(false, false, true);
618 Recursive(false, true, false);
619 Recursive(false, true, true);
620 Recursive(true, false, false);
621 Recursive(true, false, true);
622 Recursive(true, true, false);
623 Recursive(true, true, true);
624}
625
626//-----------------------------------------------------------------------------
627
628namespace {
629
630void RecursiveNoHang(
631 bool server_pump_first, bool server_pump_second, bool client_pump) {
632 std::vector<Worker*> workers;
633 workers.push_back(
634 new RecursiveServer(false, server_pump_first, server_pump_second));
635 workers.push_back(new RecursiveClient(client_pump, true));
636 RunTest(workers);
637}
638
639} // namespace
640
641// Tests that if a caller makes a sync call during an existing sync call and
642// the receiver dies, neither of the Send() calls hang.
643TEST_F(IPCSyncChannelTest, RecursiveNoHang) {
644 RecursiveNoHang(false, false, false);
645 RecursiveNoHang(false, false, true);
646 RecursiveNoHang(false, true, false);
647 RecursiveNoHang(false, true, true);
648 RecursiveNoHang(true, false, false);
649 RecursiveNoHang(true, false, true);
650 RecursiveNoHang(true, true, false);
651 RecursiveNoHang(true, true, true);
652}
initial.commit09911bf2008-07-26 23:55:29653
654//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34655
656namespace {
657
initial.commit09911bf2008-07-26 23:55:29658class MultipleServer1 : public Worker {
659 public:
[email protected]b7243c42010-07-23 05:23:13660 explicit MultipleServer1(bool pump_during_send)
[email protected]9a3a293b2009-06-04 22:28:16661 : Worker("test_channel1", Channel::MODE_SERVER),
[email protected]3cdb7af812008-10-24 19:21:13662 pump_during_send_(pump_during_send) { }
663
initial.commit09911bf2008-07-26 23:55:29664 void Run() {
[email protected]4df10d612008-11-12 00:38:26665 SendDouble(pump_during_send_, true);
initial.commit09911bf2008-07-26 23:55:29666 Done();
667 }
[email protected]3cdb7af812008-10-24 19:21:13668
669 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29670};
671
672class MultipleClient1 : public Worker {
673 public:
[email protected]aa96ae772009-01-20 22:08:15674 MultipleClient1(WaitableEvent* client1_msg_received,
675 WaitableEvent* client1_can_reply) :
[email protected]9a3a293b2009-06-04 22:28:16676 Worker("test_channel1", Channel::MODE_CLIENT),
initial.commit09911bf2008-07-26 23:55:29677 client1_msg_received_(client1_msg_received),
678 client1_can_reply_(client1_can_reply) { }
679
680 void OnDouble(int in, int* out) {
[email protected]aa96ae772009-01-20 22:08:15681 client1_msg_received_->Signal();
initial.commit09911bf2008-07-26 23:55:29682 *out = in * 2;
683 client1_can_reply_->Wait();
684 Done();
685 }
686
687 private:
[email protected]aa96ae772009-01-20 22:08:15688 WaitableEvent *client1_msg_received_, *client1_can_reply_;
initial.commit09911bf2008-07-26 23:55:29689};
690
691class MultipleServer2 : public Worker {
692 public:
[email protected]9a3a293b2009-06-04 22:28:16693 MultipleServer2() : Worker("test_channel2", Channel::MODE_SERVER) { }
initial.commit09911bf2008-07-26 23:55:29694
695 void OnAnswer(int* result) {
696 *result = 42;
697 Done();
698 }
699};
700
701class MultipleClient2 : public Worker {
702 public:
[email protected]3cdb7af812008-10-24 19:21:13703 MultipleClient2(
[email protected]aa96ae772009-01-20 22:08:15704 WaitableEvent* client1_msg_received, WaitableEvent* client1_can_reply,
[email protected]3cdb7af812008-10-24 19:21:13705 bool pump_during_send)
[email protected]9a3a293b2009-06-04 22:28:16706 : Worker("test_channel2", Channel::MODE_CLIENT),
initial.commit09911bf2008-07-26 23:55:29707 client1_msg_received_(client1_msg_received),
[email protected]3cdb7af812008-10-24 19:21:13708 client1_can_reply_(client1_can_reply),
709 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29710
711 void Run() {
initial.commit09911bf2008-07-26 23:55:29712 client1_msg_received_->Wait();
[email protected]aa96ae772009-01-20 22:08:15713 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
714 client1_can_reply_->Signal();
initial.commit09911bf2008-07-26 23:55:29715 Done();
716 }
717
718 private:
[email protected]aa96ae772009-01-20 22:08:15719 WaitableEvent *client1_msg_received_, *client1_can_reply_;
[email protected]3cdb7af812008-10-24 19:21:13720 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29721};
722
[email protected]3cdb7af812008-10-24 19:21:13723void Multiple(bool server_pump, bool client_pump) {
initial.commit09911bf2008-07-26 23:55:29724 std::vector<Worker*> workers;
725
726 // A shared worker thread so that server1 and server2 run on one thread.
[email protected]ab820df2008-08-26 05:55:10727 base::Thread worker_thread("Multiple");
[email protected]6314e6f62009-07-15 16:07:14728 ASSERT_TRUE(worker_thread.Start());
initial.commit09911bf2008-07-26 23:55:29729
730 // Server1 sends a sync msg to client1, which blocks the reply until
731 // server2 (which runs on the same worker thread as server1) responds
732 // to a sync msg from client2.
[email protected]aa96ae772009-01-20 22:08:15733 WaitableEvent client1_msg_received(false, false);
734 WaitableEvent client1_can_reply(false, false);
initial.commit09911bf2008-07-26 23:55:29735
736 Worker* worker;
737
738 worker = new MultipleServer2();
739 worker->OverrideThread(&worker_thread);
740 workers.push_back(worker);
741
742 worker = new MultipleClient2(
[email protected]3cdb7af812008-10-24 19:21:13743 &client1_msg_received, &client1_can_reply, client_pump);
initial.commit09911bf2008-07-26 23:55:29744 workers.push_back(worker);
745
[email protected]3cdb7af812008-10-24 19:21:13746 worker = new MultipleServer1(server_pump);
initial.commit09911bf2008-07-26 23:55:29747 worker->OverrideThread(&worker_thread);
748 workers.push_back(worker);
749
750 worker = new MultipleClient1(
751 &client1_msg_received, &client1_can_reply);
752 workers.push_back(worker);
753
754 RunTest(workers);
755}
756
[email protected]3cdb7af812008-10-24 19:21:13757} // namespace
758
759// Tests that multiple SyncObjects on the same listener thread can unblock each
760// other.
761TEST_F(IPCSyncChannelTest, Multiple) {
762 Multiple(false, false);
763 Multiple(false, true);
764 Multiple(true, false);
765 Multiple(true, true);
766}
initial.commit09911bf2008-07-26 23:55:29767
768//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34769
770namespace {
771
[email protected]ac0efda2009-10-14 16:22:02772// This class provides server side functionality to test the case where
773// multiple sync channels are in use on the same thread on the client and
774// nested calls are issued.
775class QueuedReplyServer : public Worker {
initial.commit09911bf2008-07-26 23:55:29776 public:
[email protected]b7243c42010-07-23 05:23:13777 QueuedReplyServer(base::Thread* listener_thread,
778 const std::string& channel_name,
779 const std::string& reply_text)
[email protected]ac0efda2009-10-14 16:22:02780 : Worker(channel_name, Channel::MODE_SERVER),
781 reply_text_(reply_text) {
782 Worker::OverrideThread(listener_thread);
initial.commit09911bf2008-07-26 23:55:29783 }
[email protected]3cdb7af812008-10-24 19:21:13784
[email protected]ac0efda2009-10-14 16:22:02785 virtual void OnNestedTestMsg(Message* reply_msg) {
[email protected]2a9d601b2010-10-19 23:50:00786 VLOG(1) << __FUNCTION__ << " Sending reply: " << reply_text_;
787 SyncChannelNestedTestMsg_String::WriteReplyParams(reply_msg, reply_text_);
[email protected]ac0efda2009-10-14 16:22:02788 Send(reply_msg);
initial.commit09911bf2008-07-26 23:55:29789 Done();
790 }
791
792 private:
[email protected]ac0efda2009-10-14 16:22:02793 std::string reply_text_;
initial.commit09911bf2008-07-26 23:55:29794};
795
[email protected]ac0efda2009-10-14 16:22:02796// The QueuedReplyClient class provides functionality to test the case where
797// multiple sync channels are in use on the same thread and they make nested
798// sync calls, i.e. while the first channel waits for a response it makes a
799// sync call on another channel.
800// The callstack should unwind correctly, i.e. the outermost call should
801// complete first, and so on.
802class QueuedReplyClient : public Worker {
initial.commit09911bf2008-07-26 23:55:29803 public:
[email protected]ac0efda2009-10-14 16:22:02804 QueuedReplyClient(base::Thread* listener_thread,
805 const std::string& channel_name,
806 const std::string& expected_text,
807 bool pump_during_send)
808 : Worker(channel_name, Channel::MODE_CLIENT),
[email protected]7ee1a44c2010-07-23 14:18:59809 pump_during_send_(pump_during_send),
810 expected_text_(expected_text) {
[email protected]ac0efda2009-10-14 16:22:02811 Worker::OverrideThread(listener_thread);
812 }
initial.commit09911bf2008-07-26 23:55:29813
[email protected]ac0efda2009-10-14 16:22:02814 virtual void Run() {
815 std::string response;
816 SyncMessage* msg = new SyncChannelNestedTestMsg_String(&response);
817 if (pump_during_send_)
818 msg->EnableMessagePumping();
819 bool result = Send(msg);
820 DCHECK(result);
[email protected]9eec2252009-12-01 02:34:18821 DCHECK_EQ(response, expected_text_);
initial.commit09911bf2008-07-26 23:55:29822
[email protected]2a9d601b2010-10-19 23:50:00823 VLOG(1) << __FUNCTION__ << " Received reply: " << response;
initial.commit09911bf2008-07-26 23:55:29824 Done();
825 }
826
[email protected]ac0efda2009-10-14 16:22:02827 private:
[email protected]3cdb7af812008-10-24 19:21:13828 bool pump_during_send_;
[email protected]ac0efda2009-10-14 16:22:02829 std::string expected_text_;
initial.commit09911bf2008-07-26 23:55:29830};
831
[email protected]ac0efda2009-10-14 16:22:02832void QueuedReply(bool client_pump) {
initial.commit09911bf2008-07-26 23:55:29833 std::vector<Worker*> workers;
834
[email protected]ac0efda2009-10-14 16:22:02835 // A shared worker thread for servers
836 base::Thread server_worker_thread("QueuedReply_ServerListener");
837 ASSERT_TRUE(server_worker_thread.Start());
initial.commit09911bf2008-07-26 23:55:29838
[email protected]ac0efda2009-10-14 16:22:02839 base::Thread client_worker_thread("QueuedReply_ClientListener");
840 ASSERT_TRUE(client_worker_thread.Start());
initial.commit09911bf2008-07-26 23:55:29841
842 Worker* worker;
843
[email protected]ac0efda2009-10-14 16:22:02844 worker = new QueuedReplyServer(&server_worker_thread,
845 "QueuedReply_Server1",
846 "Got first message");
initial.commit09911bf2008-07-26 23:55:29847 workers.push_back(worker);
848
[email protected]ac0efda2009-10-14 16:22:02849 worker = new QueuedReplyServer(&server_worker_thread,
850 "QueuedReply_Server2",
851 "Got second message");
initial.commit09911bf2008-07-26 23:55:29852 workers.push_back(worker);
853
[email protected]ac0efda2009-10-14 16:22:02854 worker = new QueuedReplyClient(&client_worker_thread,
855 "QueuedReply_Server1",
856 "Got first message",
857 client_pump);
initial.commit09911bf2008-07-26 23:55:29858 workers.push_back(worker);
859
[email protected]ac0efda2009-10-14 16:22:02860 worker = new QueuedReplyClient(&client_worker_thread,
861 "QueuedReply_Server2",
862 "Got second message",
863 client_pump);
initial.commit09911bf2008-07-26 23:55:29864 workers.push_back(worker);
865
866 RunTest(workers);
867}
868
[email protected]3cdb7af812008-10-24 19:21:13869} // namespace
870
871// While a blocking send is in progress, the listener thread might answer other
872// synchronous messages. This tests that if during the response to another
873// message the reply to the original messages comes, it is queued up correctly
874// and the original Send is unblocked later.
[email protected]ac0efda2009-10-14 16:22:02875// We also test that the send call stacks unwind correctly when the channel
876// pumps messages while waiting for a response.
[email protected]3cdb7af812008-10-24 19:21:13877TEST_F(IPCSyncChannelTest, QueuedReply) {
[email protected]ac0efda2009-10-14 16:22:02878 QueuedReply(false);
879 QueuedReply(true);
[email protected]3cdb7af812008-10-24 19:21:13880}
initial.commit09911bf2008-07-26 23:55:29881
882//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34883
884namespace {
885
[email protected]3cdb7af812008-10-24 19:21:13886class ChattyClient : public Worker {
initial.commit09911bf2008-07-26 23:55:29887 public:
[email protected]3cdb7af812008-10-24 19:21:13888 ChattyClient() :
889 Worker(Channel::MODE_CLIENT, "chatty_client") { }
initial.commit09911bf2008-07-26 23:55:29890
891 void OnAnswer(int* answer) {
892 // The PostMessage limit is 10k. Send 20% more than that.
893 const int kMessageLimit = 10000;
894 const int kMessagesToSend = kMessageLimit * 120 / 100;
895 for (int i = 0; i < kMessagesToSend; ++i) {
[email protected]4df10d612008-11-12 00:38:26896 if (!SendDouble(false, true))
initial.commit09911bf2008-07-26 23:55:29897 break;
898 }
[email protected]4df10d612008-11-12 00:38:26899 *answer = 42;
initial.commit09911bf2008-07-26 23:55:29900 Done();
901 }
902};
903
[email protected]3cdb7af812008-10-24 19:21:13904void ChattyServer(bool pump_during_send) {
905 std::vector<Worker*> workers;
[email protected]9eec2252009-12-01 02:34:18906 workers.push_back(new UnblockServer(pump_during_send, false));
[email protected]3cdb7af812008-10-24 19:21:13907 workers.push_back(new ChattyClient());
908 RunTest(workers);
909}
910
[email protected]dd3eac22008-08-26 07:28:34911} // namespace
912
[email protected]4df10d612008-11-12 00:38:26913// Tests http://b/1093251 - that sending lots of sync messages while
initial.commit09911bf2008-07-26 23:55:29914// the receiver is waiting for a sync reply does not overflow the PostMessage
915// queue.
[email protected]dd3eac22008-08-26 07:28:34916TEST_F(IPCSyncChannelTest, ChattyServer) {
[email protected]3cdb7af812008-10-24 19:21:13917 ChattyServer(false);
918 ChattyServer(true);
initial.commit09911bf2008-07-26 23:55:29919}
[email protected]d65cab7a2008-08-12 01:25:41920
[email protected]d65cab7a2008-08-12 01:25:41921//------------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34922
923namespace {
924
[email protected]d65cab7a2008-08-12 01:25:41925class TimeoutServer : public Worker {
926 public:
[email protected]b7243c42010-07-23 05:23:13927 TimeoutServer(int timeout_ms,
928 std::vector<bool> timeout_seq,
929 bool pump_during_send)
[email protected]d65cab7a2008-08-12 01:25:41930 : Worker(Channel::MODE_SERVER, "timeout_server"),
931 timeout_ms_(timeout_ms),
[email protected]3cdb7af812008-10-24 19:21:13932 timeout_seq_(timeout_seq),
933 pump_during_send_(pump_during_send) {
[email protected]d65cab7a2008-08-12 01:25:41934 }
935
936 void Run() {
937 for (std::vector<bool>::const_iterator iter = timeout_seq_.begin();
938 iter != timeout_seq_.end(); ++iter) {
[email protected]4df10d612008-11-12 00:38:26939 SendAnswerToLife(pump_during_send_, timeout_ms_, !*iter);
[email protected]d65cab7a2008-08-12 01:25:41940 }
941 Done();
942 }
943
944 private:
945 int timeout_ms_;
946 std::vector<bool> timeout_seq_;
[email protected]3cdb7af812008-10-24 19:21:13947 bool pump_during_send_;
[email protected]d65cab7a2008-08-12 01:25:41948};
949
950class UnresponsiveClient : public Worker {
951 public:
[email protected]b7243c42010-07-23 05:23:13952 explicit UnresponsiveClient(std::vector<bool> timeout_seq)
[email protected]d65cab7a2008-08-12 01:25:41953 : Worker(Channel::MODE_CLIENT, "unresponsive_client"),
954 timeout_seq_(timeout_seq) {
[email protected]b7243c42010-07-23 05:23:13955 }
[email protected]d65cab7a2008-08-12 01:25:41956
957 void OnAnswerDelay(Message* reply_msg) {
958 DCHECK(!timeout_seq_.empty());
959 if (!timeout_seq_[0]) {
960 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
961 Send(reply_msg);
962 } else {
963 // Don't reply.
[email protected]463667372008-08-20 20:20:52964 delete reply_msg;
[email protected]d65cab7a2008-08-12 01:25:41965 }
966 timeout_seq_.erase(timeout_seq_.begin());
967 if (timeout_seq_.empty())
968 Done();
969 }
970
971 private:
972 // Whether we should time-out or respond to the various messages we receive.
973 std::vector<bool> timeout_seq_;
974};
975
[email protected]3cdb7af812008-10-24 19:21:13976void SendWithTimeoutOK(bool pump_during_send) {
977 std::vector<Worker*> workers;
978 std::vector<bool> timeout_seq;
979 timeout_seq.push_back(false);
980 timeout_seq.push_back(false);
981 timeout_seq.push_back(false);
982 workers.push_back(new TimeoutServer(5000, timeout_seq, pump_during_send));
983 workers.push_back(new SimpleClient());
984 RunTest(workers);
985}
986
987void SendWithTimeoutTimeout(bool pump_during_send) {
988 std::vector<Worker*> workers;
989 std::vector<bool> timeout_seq;
990 timeout_seq.push_back(true);
991 timeout_seq.push_back(false);
992 timeout_seq.push_back(false);
993 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send));
994 workers.push_back(new UnresponsiveClient(timeout_seq));
995 RunTest(workers);
996}
997
998void SendWithTimeoutMixedOKAndTimeout(bool pump_during_send) {
999 std::vector<Worker*> workers;
1000 std::vector<bool> timeout_seq;
1001 timeout_seq.push_back(true);
1002 timeout_seq.push_back(false);
1003 timeout_seq.push_back(false);
1004 timeout_seq.push_back(true);
1005 timeout_seq.push_back(false);
1006 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send));
1007 workers.push_back(new UnresponsiveClient(timeout_seq));
1008 RunTest(workers);
1009}
1010
[email protected]dd3eac22008-08-26 07:28:341011} // namespace
1012
[email protected]d65cab7a2008-08-12 01:25:411013// Tests that SendWithTimeout does not time-out if the response comes back fast
1014// enough.
[email protected]dd3eac22008-08-26 07:28:341015TEST_F(IPCSyncChannelTest, SendWithTimeoutOK) {
[email protected]3cdb7af812008-10-24 19:21:131016 SendWithTimeoutOK(false);
1017 SendWithTimeoutOK(true);
[email protected]d65cab7a2008-08-12 01:25:411018}
1019
1020// Tests that SendWithTimeout does time-out.
[email protected]dd3eac22008-08-26 07:28:341021TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) {
[email protected]3cdb7af812008-10-24 19:21:131022 SendWithTimeoutTimeout(false);
1023 SendWithTimeoutTimeout(true);
[email protected]d65cab7a2008-08-12 01:25:411024}
1025
1026// Sends some message that time-out and some that succeed.
[email protected]711032e2011-01-19 08:11:561027// Crashes flakily, http://crbug.com/70075.
1028TEST_F(IPCSyncChannelTest, DISABLED_SendWithTimeoutMixedOKAndTimeout) {
[email protected]3cdb7af812008-10-24 19:21:131029 SendWithTimeoutMixedOKAndTimeout(false);
1030 SendWithTimeoutMixedOKAndTimeout(true);
1031}
[email protected]4df10d612008-11-12 00:38:261032
1033//------------------------------------------------------------------------------
1034
1035namespace {
1036
[email protected]0633e3152011-11-28 18:35:391037void NestedCallback(Worker* server) {
1038 // Sleep a bit so that we wake up after the reply has been received.
[email protected]b5393332012-01-13 00:11:011039 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(250));
[email protected]0633e3152011-11-28 18:35:391040 server->SendAnswerToLife(true, base::kNoTimeout, true);
1041}
[email protected]4df10d612008-11-12 00:38:261042
[email protected]0633e3152011-11-28 18:35:391043bool timeout_occurred = false;
[email protected]4df10d612008-11-12 00:38:261044
[email protected]0633e3152011-11-28 18:35:391045void TimeoutCallback() {
1046 timeout_occurred = true;
1047}
[email protected]4df10d612008-11-12 00:38:261048
1049class DoneEventRaceServer : public Worker {
1050 public:
1051 DoneEventRaceServer()
1052 : Worker(Channel::MODE_SERVER, "done_event_race_server") { }
1053
1054 void Run() {
[email protected]0633e3152011-11-28 18:35:391055 MessageLoop::current()->PostTask(FROM_HERE,
1056 base::Bind(&NestedCallback, this));
1057 MessageLoop::current()->PostDelayedTask(
[email protected]5896fa042012-03-07 04:41:401058 FROM_HERE,
1059 base::Bind(&TimeoutCallback),
1060 base::TimeDelta::FromSeconds(9));
[email protected]4df10d612008-11-12 00:38:261061 // Even though we have a timeout on the Send, it will succeed since for this
1062 // bug, the reply message comes back and is deserialized, however the done
1063 // event wasn't set. So we indirectly use the timeout task to notice if a
1064 // timeout occurred.
1065 SendAnswerToLife(true, 10000, true);
[email protected]0633e3152011-11-28 18:35:391066 DCHECK(!timeout_occurred);
[email protected]4df10d612008-11-12 00:38:261067 Done();
1068 }
1069};
1070
1071} // namespace
1072
1073// Tests http://b/1474092 - that if after the done_event is set but before
1074// OnObjectSignaled is called another message is sent out, then after its
1075// reply comes back OnObjectSignaled will be called for the first message.
1076TEST_F(IPCSyncChannelTest, DoneEventRace) {
1077 std::vector<Worker*> workers;
1078 workers.push_back(new DoneEventRaceServer());
1079 workers.push_back(new SimpleClient());
1080 RunTest(workers);
1081}
[email protected]1e9499c2010-04-06 20:33:361082
1083//-----------------------------------------------------------------------------
1084
1085namespace {
1086
[email protected]c9e0c7332011-05-13 22:42:411087class TestSyncMessageFilter : public SyncMessageFilter {
[email protected]1e9499c2010-04-06 20:33:361088 public:
[email protected]d4fc4d6a2012-09-08 01:24:491089 TestSyncMessageFilter(base::WaitableEvent* shutdown_event,
1090 Worker* worker,
1091 scoped_refptr<base::MessageLoopProxy> message_loop)
[email protected]1e9499c2010-04-06 20:33:361092 : SyncMessageFilter(shutdown_event),
1093 worker_(worker),
[email protected]d4fc4d6a2012-09-08 01:24:491094 message_loop_(message_loop) {
[email protected]1e9499c2010-04-06 20:33:361095 }
1096
1097 virtual void OnFilterAdded(Channel* channel) {
1098 SyncMessageFilter::OnFilterAdded(channel);
[email protected]d4fc4d6a2012-09-08 01:24:491099 message_loop_->PostTask(
[email protected]72b6f8e22011-11-12 21:16:411100 FROM_HERE,
1101 base::Bind(&TestSyncMessageFilter::SendMessageOnHelperThread, this));
[email protected]1e9499c2010-04-06 20:33:361102 }
1103
1104 void SendMessageOnHelperThread() {
1105 int answer = 0;
1106 bool result = Send(new SyncChannelTestMsg_AnswerToLife(&answer));
1107 DCHECK(result);
1108 DCHECK_EQ(answer, 42);
1109
1110 worker_->Done();
1111 }
1112
[email protected]f729d15a2012-04-28 02:12:001113 private:
1114 virtual ~TestSyncMessageFilter() {}
1115
[email protected]1e9499c2010-04-06 20:33:361116 Worker* worker_;
[email protected]d4fc4d6a2012-09-08 01:24:491117 scoped_refptr<base::MessageLoopProxy> message_loop_;
[email protected]1e9499c2010-04-06 20:33:361118};
1119
1120class SyncMessageFilterServer : public Worker {
1121 public:
1122 SyncMessageFilterServer()
[email protected]d4fc4d6a2012-09-08 01:24:491123 : Worker(Channel::MODE_SERVER, "sync_message_filter_server"),
1124 thread_("helper_thread") {
1125 base::Thread::Options options;
1126 options.message_loop_type = MessageLoop::TYPE_DEFAULT;
1127 thread_.StartWithOptions(options);
1128 filter_ = new TestSyncMessageFilter(shutdown_event(), this,
1129 thread_.message_loop_proxy());
[email protected]1e9499c2010-04-06 20:33:361130 }
1131
1132 void Run() {
1133 channel()->AddFilter(filter_.get());
1134 }
1135
[email protected]d4fc4d6a2012-09-08 01:24:491136 base::Thread thread_;
[email protected]1e9499c2010-04-06 20:33:361137 scoped_refptr<TestSyncMessageFilter> filter_;
1138};
1139
[email protected]87339f02010-09-02 21:45:501140// This class provides functionality to test the case that a Send on the sync
1141// channel does not crash after the channel has been closed.
1142class ServerSendAfterClose : public Worker {
1143 public:
1144 ServerSendAfterClose()
1145 : Worker(Channel::MODE_SERVER, "simpler_server"),
1146 send_result_(true) {
1147 }
1148
1149 bool SendDummy() {
[email protected]72b6f8e22011-11-12 21:16:411150 ListenerThread()->message_loop()->PostTask(
[email protected]dcde7672012-01-06 02:37:171151 FROM_HERE, base::Bind(base::IgnoreResult(&ServerSendAfterClose::Send),
1152 this, new SyncChannelTestMsg_NoArgs));
[email protected]87339f02010-09-02 21:45:501153 return true;
1154 }
1155
1156 bool send_result() const {
1157 return send_result_;
1158 }
1159
1160 private:
1161 virtual void Run() {
1162 CloseChannel();
1163 Done();
1164 }
1165
1166 bool Send(Message* msg) {
1167 send_result_ = Worker::Send(msg);
1168 Done();
1169 return send_result_;
1170 }
1171
1172 bool send_result_;
1173};
1174
[email protected]1e9499c2010-04-06 20:33:361175} // namespace
1176
1177// Tests basic synchronous call
1178TEST_F(IPCSyncChannelTest, SyncMessageFilter) {
1179 std::vector<Worker*> workers;
1180 workers.push_back(new SyncMessageFilterServer());
1181 workers.push_back(new SimpleClient());
1182 RunTest(workers);
1183}
[email protected]87339f02010-09-02 21:45:501184
1185// Test the case when the channel is closed and a Send is attempted after that.
1186TEST_F(IPCSyncChannelTest, SendAfterClose) {
1187 ServerSendAfterClose server;
1188 server.Start();
1189
1190 server.done_event()->Wait();
1191 server.done_event()->Reset();
1192
1193 server.SendDummy();
1194 server.done_event()->Wait();
1195
1196 EXPECT_FALSE(server.send_result());
1197}
1198
[email protected]54af05f2011-04-08 03:38:211199//-----------------------------------------------------------------------------
[email protected]87339f02010-09-02 21:45:501200
[email protected]54af05f2011-04-08 03:38:211201namespace {
1202
1203class RestrictedDispatchServer : public Worker {
1204 public:
[email protected]298ee7d2012-03-30 21:29:301205 RestrictedDispatchServer(WaitableEvent* sent_ping_event,
1206 WaitableEvent* wait_event)
[email protected]54af05f2011-04-08 03:38:211207 : Worker("restricted_channel", Channel::MODE_SERVER),
[email protected]298ee7d2012-03-30 21:29:301208 sent_ping_event_(sent_ping_event),
1209 wait_event_(wait_event) { }
[email protected]54af05f2011-04-08 03:38:211210
1211 void OnDoPing(int ping) {
1212 // Send an asynchronous message that unblocks the caller.
[email protected]c9e0c7332011-05-13 22:42:411213 Message* msg = new SyncChannelTestMsg_Ping(ping);
[email protected]54af05f2011-04-08 03:38:211214 msg->set_unblock(true);
1215 Send(msg);
1216 // Signal the event after the message has been sent on the channel, on the
1217 // IPC thread.
[email protected]72b6f8e22011-11-12 21:16:411218 ipc_thread().message_loop()->PostTask(
1219 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnPingSent, this));
[email protected]54af05f2011-04-08 03:38:211220 }
1221
[email protected]298ee7d2012-03-30 21:29:301222 void OnPingTTL(int ping, int* out) {
1223 *out = ping;
1224 wait_event_->Wait();
1225 }
1226
[email protected]54af05f2011-04-08 03:38:211227 base::Thread* ListenerThread() { return Worker::ListenerThread(); }
1228
1229 private:
1230 bool OnMessageReceived(const Message& message) {
1231 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchServer, message)
1232 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
[email protected]298ee7d2012-03-30 21:29:301233 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_PingTTL, OnPingTTL)
[email protected]54af05f2011-04-08 03:38:211234 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done)
1235 IPC_END_MESSAGE_MAP()
1236 return true;
1237 }
1238
1239 void OnPingSent() {
1240 sent_ping_event_->Signal();
1241 }
1242
1243 void OnNoArgs() { }
1244 WaitableEvent* sent_ping_event_;
[email protected]298ee7d2012-03-30 21:29:301245 WaitableEvent* wait_event_;
[email protected]54af05f2011-04-08 03:38:211246};
1247
1248class NonRestrictedDispatchServer : public Worker {
1249 public:
[email protected]298ee7d2012-03-30 21:29:301250 NonRestrictedDispatchServer(WaitableEvent* signal_event)
1251 : Worker("non_restricted_channel", Channel::MODE_SERVER),
1252 signal_event_(signal_event) {}
1253
1254 base::Thread* ListenerThread() { return Worker::ListenerThread(); }
1255
1256 void OnDoPingTTL(int ping) {
1257 int value = 0;
1258 Send(new SyncChannelTestMsg_PingTTL(ping, &value));
1259 signal_event_->Signal();
1260 }
[email protected]54af05f2011-04-08 03:38:211261
1262 private:
1263 bool OnMessageReceived(const Message& message) {
1264 IPC_BEGIN_MESSAGE_MAP(NonRestrictedDispatchServer, message)
1265 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
1266 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done)
1267 IPC_END_MESSAGE_MAP()
1268 return true;
1269 }
1270
1271 void OnNoArgs() { }
[email protected]298ee7d2012-03-30 21:29:301272 WaitableEvent* signal_event_;
[email protected]54af05f2011-04-08 03:38:211273};
1274
1275class RestrictedDispatchClient : public Worker {
1276 public:
1277 RestrictedDispatchClient(WaitableEvent* sent_ping_event,
1278 RestrictedDispatchServer* server,
[email protected]298ee7d2012-03-30 21:29:301279 NonRestrictedDispatchServer* server2,
[email protected]54af05f2011-04-08 03:38:211280 int* success)
1281 : Worker("restricted_channel", Channel::MODE_CLIENT),
1282 ping_(0),
1283 server_(server),
[email protected]298ee7d2012-03-30 21:29:301284 server2_(server2),
[email protected]54af05f2011-04-08 03:38:211285 success_(success),
1286 sent_ping_event_(sent_ping_event) {}
1287
1288 void Run() {
1289 // Incoming messages from our channel should only be dispatched when we
1290 // send a message on that same channel.
[email protected]298ee7d2012-03-30 21:29:301291 channel()->SetRestrictDispatchChannelGroup(1);
[email protected]54af05f2011-04-08 03:38:211292
[email protected]72b6f8e22011-11-12 21:16:411293 server_->ListenerThread()->message_loop()->PostTask(
1294 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 1));
[email protected]54af05f2011-04-08 03:38:211295 sent_ping_event_->Wait();
1296 Send(new SyncChannelTestMsg_NoArgs);
1297 if (ping_ == 1)
1298 ++*success_;
1299 else
1300 LOG(ERROR) << "Send failed to dispatch incoming message on same channel";
1301
[email protected]298ee7d2012-03-30 21:29:301302 non_restricted_channel_.reset(new SyncChannel(
[email protected]54af05f2011-04-08 03:38:211303 "non_restricted_channel", Channel::MODE_CLIENT, this,
[email protected]92bf9062011-05-02 18:00:491304 ipc_thread().message_loop_proxy(), true, shutdown_event()));
[email protected]54af05f2011-04-08 03:38:211305
[email protected]72b6f8e22011-11-12 21:16:411306 server_->ListenerThread()->message_loop()->PostTask(
1307 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 2));
[email protected]54af05f2011-04-08 03:38:211308 sent_ping_event_->Wait();
1309 // Check that the incoming message is *not* dispatched when sending on the
1310 // non restricted channel.
1311 // TODO(piman): there is a possibility of a false positive race condition
1312 // here, if the message that was posted on the server-side end of the pipe
1313 // is not visible yet on the client side, but I don't know how to solve this
1314 // without hooking into the internals of SyncChannel. I haven't seen it in
1315 // practice (i.e. not setting SetRestrictDispatchToSameChannel does cause
1316 // the following to fail).
[email protected]298ee7d2012-03-30 21:29:301317 non_restricted_channel_->Send(new SyncChannelTestMsg_NoArgs);
[email protected]54af05f2011-04-08 03:38:211318 if (ping_ == 1)
1319 ++*success_;
1320 else
1321 LOG(ERROR) << "Send dispatched message from restricted channel";
1322
1323 Send(new SyncChannelTestMsg_NoArgs);
1324 if (ping_ == 2)
1325 ++*success_;
1326 else
1327 LOG(ERROR) << "Send failed to dispatch incoming message on same channel";
1328
[email protected]298ee7d2012-03-30 21:29:301329 // Check that the incoming message on the non-restricted channel is
1330 // dispatched when sending on the restricted channel.
1331 server2_->ListenerThread()->message_loop()->PostTask(
1332 FROM_HERE,
1333 base::Bind(&NonRestrictedDispatchServer::OnDoPingTTL, server2_, 3));
1334 int value = 0;
1335 Send(new SyncChannelTestMsg_PingTTL(4, &value));
1336 if (ping_ == 3 && value == 4)
1337 ++*success_;
1338 else
1339 LOG(ERROR) << "Send failed to dispatch message from unrestricted channel";
1340
1341 non_restricted_channel_->Send(new SyncChannelTestMsg_Done);
1342 non_restricted_channel_.reset();
[email protected]54af05f2011-04-08 03:38:211343 Send(new SyncChannelTestMsg_Done);
1344 Done();
1345 }
1346
1347 private:
1348 bool OnMessageReceived(const Message& message) {
1349 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchClient, message)
1350 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Ping, OnPing)
[email protected]298ee7d2012-03-30 21:29:301351 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_PingTTL, OnPingTTL)
[email protected]54af05f2011-04-08 03:38:211352 IPC_END_MESSAGE_MAP()
1353 return true;
1354 }
1355
1356 void OnPing(int ping) {
1357 ping_ = ping;
1358 }
1359
[email protected]298ee7d2012-03-30 21:29:301360 void OnPingTTL(int ping, IPC::Message* reply) {
1361 ping_ = ping;
1362 // This message comes from the NonRestrictedDispatchServer, we have to send
1363 // the reply back manually.
1364 SyncChannelTestMsg_PingTTL::WriteReplyParams(reply, ping);
1365 non_restricted_channel_->Send(reply);
1366 }
1367
[email protected]54af05f2011-04-08 03:38:211368 int ping_;
1369 RestrictedDispatchServer* server_;
[email protected]298ee7d2012-03-30 21:29:301370 NonRestrictedDispatchServer* server2_;
[email protected]54af05f2011-04-08 03:38:211371 int* success_;
1372 WaitableEvent* sent_ping_event_;
[email protected]298ee7d2012-03-30 21:29:301373 scoped_ptr<SyncChannel> non_restricted_channel_;
[email protected]54af05f2011-04-08 03:38:211374};
1375
1376} // namespace
1377
1378TEST_F(IPCSyncChannelTest, RestrictedDispatch) {
1379 WaitableEvent sent_ping_event(false, false);
[email protected]298ee7d2012-03-30 21:29:301380 WaitableEvent wait_event(false, false);
[email protected]54af05f2011-04-08 03:38:211381 RestrictedDispatchServer* server =
[email protected]298ee7d2012-03-30 21:29:301382 new RestrictedDispatchServer(&sent_ping_event, &wait_event);
1383 NonRestrictedDispatchServer* server2 =
1384 new NonRestrictedDispatchServer(&wait_event);
1385
[email protected]54af05f2011-04-08 03:38:211386 int success = 0;
1387 std::vector<Worker*> workers;
[email protected]54af05f2011-04-08 03:38:211388 workers.push_back(server);
[email protected]298ee7d2012-03-30 21:29:301389 workers.push_back(server2);
1390 workers.push_back(new RestrictedDispatchClient(
1391 &sent_ping_event, server, server2, &success));
[email protected]54af05f2011-04-08 03:38:211392 RunTest(workers);
[email protected]298ee7d2012-03-30 21:29:301393 EXPECT_EQ(4, success);
[email protected]54af05f2011-04-08 03:38:211394}
[email protected]c9e0c7332011-05-13 22:42:411395
[email protected]522cc10d2012-01-11 22:39:541396//-----------------------------------------------------------------------------
1397
1398// This test case inspired by crbug.com/108491
1399// We create two servers that use the same ListenerThread but have
1400// SetRestrictDispatchToSameChannel set to true.
1401// We create clients, then use some specific WaitableEvent wait/signalling to
1402// ensure that messages get dispatched in a way that causes a deadlock due to
1403// a nested dispatch and an eligible message in a higher-level dispatch's
1404// delayed_queue. Specifically, we start with client1 about so send an
1405// unblocking message to server1, while the shared listener thread for the
1406// servers server1 and server2 is about to send a non-unblocking message to
1407// client1. At the same time, client2 will be about to send an unblocking
1408// message to server2. Server1 will handle the client1->server1 message by
1409// telling server2 to send a non-unblocking message to client2.
1410// What should happen is that the send to server2 should find the pending,
1411// same-context client2->server2 message to dispatch, causing client2 to
1412// unblock then handle the server2->client2 message, so that the shared
1413// servers' listener thread can then respond to the client1->server1 message.
1414// Then client1 can handle the non-unblocking server1->client1 message.
1415// The old code would end up in a state where the server2->client2 message is
1416// sent, but the client2->server2 message (which is eligible for dispatch, and
1417// which is what client2 is waiting for) is stashed in a local delayed_queue
1418// that has server1's channel context, causing a deadlock.
1419// WaitableEvents in the events array are used to:
1420// event 0: indicate to client1 that server listener is in OnDoServerTask
1421// event 1: indicate to client1 that client2 listener is in OnDoClient2Task
1422// event 2: indicate to server1 that client2 listener is in OnDoClient2Task
1423// event 3: indicate to client2 that server listener is in OnDoServerTask
1424
1425namespace {
1426
1427class RestrictedDispatchDeadlockServer : public Worker {
1428 public:
1429 RestrictedDispatchDeadlockServer(int server_num,
1430 WaitableEvent* server_ready_event,
1431 WaitableEvent** events,
1432 RestrictedDispatchDeadlockServer* peer)
1433 : Worker(server_num == 1 ? "channel1" : "channel2", Channel::MODE_SERVER),
1434 server_num_(server_num),
1435 server_ready_event_(server_ready_event),
1436 events_(events),
[email protected]629ddf852012-07-20 16:17:291437 peer_(peer) { }
[email protected]522cc10d2012-01-11 22:39:541438
1439 void OnDoServerTask() {
1440 events_[3]->Signal();
1441 events_[2]->Wait();
1442 events_[0]->Signal();
1443 SendMessageToClient();
1444 }
1445
1446 void Run() {
[email protected]298ee7d2012-03-30 21:29:301447 channel()->SetRestrictDispatchChannelGroup(1);
[email protected]522cc10d2012-01-11 22:39:541448 server_ready_event_->Signal();
1449 }
1450
1451 base::Thread* ListenerThread() { return Worker::ListenerThread(); }
1452
1453 private:
1454 bool OnMessageReceived(const Message& message) {
1455 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchDeadlockServer, message)
1456 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
1457 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done)
1458 IPC_END_MESSAGE_MAP()
1459 return true;
1460 }
1461
1462 void OnNoArgs() {
1463 if (server_num_ == 1) {
1464 DCHECK(peer_ != NULL);
1465 peer_->SendMessageToClient();
1466 }
1467 }
1468
1469 void SendMessageToClient() {
1470 Message* msg = new SyncChannelTestMsg_NoArgs;
1471 msg->set_unblock(false);
1472 DCHECK(!msg->should_unblock());
1473 Send(msg);
1474 }
1475
1476 int server_num_;
1477 WaitableEvent* server_ready_event_;
1478 WaitableEvent** events_;
1479 RestrictedDispatchDeadlockServer* peer_;
[email protected]522cc10d2012-01-11 22:39:541480};
1481
1482class RestrictedDispatchDeadlockClient2 : public Worker {
1483 public:
1484 RestrictedDispatchDeadlockClient2(RestrictedDispatchDeadlockServer* server,
1485 WaitableEvent* server_ready_event,
1486 WaitableEvent** events)
1487 : Worker("channel2", Channel::MODE_CLIENT),
[email protected]522cc10d2012-01-11 22:39:541488 server_ready_event_(server_ready_event),
1489 events_(events),
1490 received_msg_(false),
1491 received_noarg_reply_(false),
1492 done_issued_(false) {}
1493
1494 void Run() {
1495 server_ready_event_->Wait();
1496 }
1497
1498 void OnDoClient2Task() {
1499 events_[3]->Wait();
1500 events_[1]->Signal();
1501 events_[2]->Signal();
1502 DCHECK(received_msg_ == false);
1503
1504 Message* message = new SyncChannelTestMsg_NoArgs;
1505 message->set_unblock(true);
1506 Send(message);
1507 received_noarg_reply_ = true;
1508 }
1509
1510 base::Thread* ListenerThread() { return Worker::ListenerThread(); }
1511 private:
1512 bool OnMessageReceived(const Message& message) {
1513 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchDeadlockClient2, message)
1514 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
1515 IPC_END_MESSAGE_MAP()
1516 return true;
1517 }
1518
1519 void OnNoArgs() {
1520 received_msg_ = true;
1521 PossiblyDone();
1522 }
1523
1524 void PossiblyDone() {
1525 if (received_noarg_reply_ && received_msg_) {
1526 DCHECK(done_issued_ == false);
1527 done_issued_ = true;
1528 Send(new SyncChannelTestMsg_Done);
1529 Done();
1530 }
1531 }
1532
[email protected]522cc10d2012-01-11 22:39:541533 WaitableEvent* server_ready_event_;
1534 WaitableEvent** events_;
1535 bool received_msg_;
1536 bool received_noarg_reply_;
1537 bool done_issued_;
1538};
1539
1540class RestrictedDispatchDeadlockClient1 : public Worker {
1541 public:
1542 RestrictedDispatchDeadlockClient1(RestrictedDispatchDeadlockServer* server,
1543 RestrictedDispatchDeadlockClient2* peer,
1544 WaitableEvent* server_ready_event,
1545 WaitableEvent** events)
1546 : Worker("channel1", Channel::MODE_CLIENT),
1547 server_(server),
1548 peer_(peer),
1549 server_ready_event_(server_ready_event),
1550 events_(events),
1551 received_msg_(false),
1552 received_noarg_reply_(false),
1553 done_issued_(false) {}
1554
1555 void Run() {
1556 server_ready_event_->Wait();
1557 server_->ListenerThread()->message_loop()->PostTask(
1558 FROM_HERE,
1559 base::Bind(&RestrictedDispatchDeadlockServer::OnDoServerTask, server_));
1560 peer_->ListenerThread()->message_loop()->PostTask(
1561 FROM_HERE,
1562 base::Bind(&RestrictedDispatchDeadlockClient2::OnDoClient2Task, peer_));
1563 events_[0]->Wait();
1564 events_[1]->Wait();
1565 DCHECK(received_msg_ == false);
1566
1567 Message* message = new SyncChannelTestMsg_NoArgs;
1568 message->set_unblock(true);
1569 Send(message);
1570 received_noarg_reply_ = true;
1571 PossiblyDone();
1572 }
1573
1574 base::Thread* ListenerThread() { return Worker::ListenerThread(); }
1575 private:
1576 bool OnMessageReceived(const Message& message) {
1577 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchDeadlockClient1, message)
1578 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
1579 IPC_END_MESSAGE_MAP()
1580 return true;
1581 }
1582
1583 void OnNoArgs() {
1584 received_msg_ = true;
1585 PossiblyDone();
1586 }
1587
1588 void PossiblyDone() {
1589 if (received_noarg_reply_ && received_msg_) {
1590 DCHECK(done_issued_ == false);
1591 done_issued_ = true;
1592 Send(new SyncChannelTestMsg_Done);
1593 Done();
1594 }
1595 }
1596
1597 RestrictedDispatchDeadlockServer* server_;
1598 RestrictedDispatchDeadlockClient2* peer_;
1599 WaitableEvent* server_ready_event_;
1600 WaitableEvent** events_;
1601 bool received_msg_;
1602 bool received_noarg_reply_;
1603 bool done_issued_;
1604};
1605
1606} // namespace
1607
1608TEST_F(IPCSyncChannelTest, RestrictedDispatchDeadlock) {
1609 std::vector<Worker*> workers;
1610
1611 // A shared worker thread so that server1 and server2 run on one thread.
1612 base::Thread worker_thread("RestrictedDispatchDeadlock");
1613 ASSERT_TRUE(worker_thread.Start());
1614
1615 WaitableEvent server1_ready(false, false);
1616 WaitableEvent server2_ready(false, false);
1617
1618 WaitableEvent event0(false, false);
1619 WaitableEvent event1(false, false);
1620 WaitableEvent event2(false, false);
1621 WaitableEvent event3(false, false);
1622 WaitableEvent* events[4] = {&event0, &event1, &event2, &event3};
1623
1624 RestrictedDispatchDeadlockServer* server1;
1625 RestrictedDispatchDeadlockServer* server2;
1626 RestrictedDispatchDeadlockClient1* client1;
1627 RestrictedDispatchDeadlockClient2* client2;
1628
1629 server2 = new RestrictedDispatchDeadlockServer(2, &server2_ready, events,
1630 NULL);
1631 server2->OverrideThread(&worker_thread);
1632 workers.push_back(server2);
1633
1634 client2 = new RestrictedDispatchDeadlockClient2(server2, &server2_ready,
1635 events);
1636 workers.push_back(client2);
1637
1638 server1 = new RestrictedDispatchDeadlockServer(1, &server1_ready, events,
1639 server2);
1640 server1->OverrideThread(&worker_thread);
1641 workers.push_back(server1);
1642
1643 client1 = new RestrictedDispatchDeadlockClient1(server1, client2,
1644 &server1_ready, events);
1645 workers.push_back(client1);
1646
1647 RunTest(workers);
1648}
1649
[email protected]5c41e6e12012-03-17 02:20:461650//-----------------------------------------------------------------------------
1651
[email protected]298ee7d2012-03-30 21:29:301652// This test case inspired by crbug.com/120530
1653// We create 4 workers that pipe to each other W1->W2->W3->W4->W1 then we send a
1654// message that recurses through 3, 4 or 5 steps to make sure, say, W1 can
1655// re-enter when called from W4 while it's sending a message to W2.
1656// The first worker drives the whole test so it must be treated specially.
1657namespace {
1658
1659class RestrictedDispatchPipeWorker : public Worker {
1660 public:
1661 RestrictedDispatchPipeWorker(
1662 const std::string &channel1,
1663 WaitableEvent* event1,
1664 const std::string &channel2,
1665 WaitableEvent* event2,
1666 int group,
1667 int* success)
1668 : Worker(channel1, Channel::MODE_SERVER),
1669 event1_(event1),
1670 event2_(event2),
1671 other_channel_name_(channel2),
1672 group_(group),
1673 success_(success) {
1674 }
1675
1676 void OnPingTTL(int ping, int* ret) {
1677 *ret = 0;
1678 if (!ping)
1679 return;
1680 other_channel_->Send(new SyncChannelTestMsg_PingTTL(ping - 1, ret));
1681 ++*ret;
1682 }
1683
1684 void OnDone() {
1685 if (is_first())
1686 return;
1687 other_channel_->Send(new SyncChannelTestMsg_Done);
1688 other_channel_.reset();
1689 Done();
1690 }
1691
1692 void Run() {
1693 channel()->SetRestrictDispatchChannelGroup(group_);
1694 if (is_first())
1695 event1_->Signal();
1696 event2_->Wait();
1697 other_channel_.reset(new SyncChannel(
1698 other_channel_name_, Channel::MODE_CLIENT, this,
1699 ipc_thread().message_loop_proxy(), true, shutdown_event()));
1700 other_channel_->SetRestrictDispatchChannelGroup(group_);
1701 if (!is_first()) {
1702 event1_->Signal();
1703 return;
1704 }
1705 *success_ = 0;
1706 int value = 0;
1707 OnPingTTL(3, &value);
1708 *success_ += (value == 3);
1709 OnPingTTL(4, &value);
1710 *success_ += (value == 4);
1711 OnPingTTL(5, &value);
1712 *success_ += (value == 5);
1713 other_channel_->Send(new SyncChannelTestMsg_Done);
1714 other_channel_.reset();
1715 Done();
1716 }
1717
1718 bool is_first() { return !!success_; }
1719
1720 private:
1721 bool OnMessageReceived(const Message& message) {
1722 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchPipeWorker, message)
1723 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_PingTTL, OnPingTTL)
1724 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, OnDone)
1725 IPC_END_MESSAGE_MAP()
1726 return true;
1727 }
1728
1729 scoped_ptr<SyncChannel> other_channel_;
1730 WaitableEvent* event1_;
1731 WaitableEvent* event2_;
1732 std::string other_channel_name_;
1733 int group_;
1734 int* success_;
1735};
1736
1737} // namespace
1738
1739TEST_F(IPCSyncChannelTest, RestrictedDispatch4WayDeadlock) {
1740 int success = 0;
1741 std::vector<Worker*> workers;
1742 WaitableEvent event0(true, false);
1743 WaitableEvent event1(true, false);
1744 WaitableEvent event2(true, false);
1745 WaitableEvent event3(true, false);
1746 workers.push_back(new RestrictedDispatchPipeWorker(
1747 "channel0", &event0, "channel1", &event1, 1, &success));
1748 workers.push_back(new RestrictedDispatchPipeWorker(
1749 "channel1", &event1, "channel2", &event2, 2, NULL));
1750 workers.push_back(new RestrictedDispatchPipeWorker(
1751 "channel2", &event2, "channel3", &event3, 3, NULL));
1752 workers.push_back(new RestrictedDispatchPipeWorker(
1753 "channel3", &event3, "channel0", &event0, 4, NULL));
1754 RunTest(workers);
1755 EXPECT_EQ(3, success);
1756}
1757
[email protected]9134cce6d2012-04-10 20:07:531758
1759//-----------------------------------------------------------------------------
1760//
1761// This test case inspired by crbug.com/122443
1762// We want to make sure a reply message with the unblock flag set correctly
1763// behaves as a reply, not a regular message.
1764// We have 3 workers. Server1 will send a message to Server2 (which will block),
1765// during which it will dispatch a message comming from Client, at which point
1766// it will send another message to Server2. While sending that second message it
1767// will receive a reply from Server1 with the unblock flag.
1768
1769namespace {
1770
1771class ReentrantReplyServer1 : public Worker {
1772 public:
1773 ReentrantReplyServer1(WaitableEvent* server_ready)
1774 : Worker("reentrant_reply1", Channel::MODE_SERVER),
1775 server_ready_(server_ready) { }
1776
1777 void Run() {
1778 server2_channel_.reset(new SyncChannel(
1779 "reentrant_reply2", Channel::MODE_CLIENT, this,
1780 ipc_thread().message_loop_proxy(), true, shutdown_event()));
1781 server_ready_->Signal();
1782 Message* msg = new SyncChannelTestMsg_Reentrant1();
1783 server2_channel_->Send(msg);
1784 server2_channel_.reset();
1785 Done();
1786 }
1787
1788 private:
1789 bool OnMessageReceived(const Message& message) {
1790 IPC_BEGIN_MESSAGE_MAP(ReentrantReplyServer1, message)
1791 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Reentrant2, OnReentrant2)
1792 IPC_REPLY_HANDLER(OnReply)
1793 IPC_END_MESSAGE_MAP()
1794 return true;
1795 }
1796
1797 void OnReentrant2() {
1798 Message* msg = new SyncChannelTestMsg_Reentrant3();
1799 server2_channel_->Send(msg);
1800 }
1801
1802 void OnReply(const Message& message) {
1803 // If we get here, the Send() will never receive the reply (thus would
1804 // hang), so abort instead.
1805 LOG(FATAL) << "Reply message was dispatched";
1806 }
1807
1808 WaitableEvent* server_ready_;
1809 scoped_ptr<SyncChannel> server2_channel_;
1810};
1811
1812class ReentrantReplyServer2 : public Worker {
1813 public:
1814 ReentrantReplyServer2()
1815 : Worker("reentrant_reply2", Channel::MODE_SERVER),
1816 reply_(NULL) { }
1817
1818 private:
1819 bool OnMessageReceived(const Message& message) {
1820 IPC_BEGIN_MESSAGE_MAP(ReentrantReplyServer2, message)
1821 IPC_MESSAGE_HANDLER_DELAY_REPLY(
1822 SyncChannelTestMsg_Reentrant1, OnReentrant1)
1823 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Reentrant3, OnReentrant3)
1824 IPC_END_MESSAGE_MAP()
1825 return true;
1826 }
1827
1828 void OnReentrant1(Message* reply) {
1829 DCHECK(!reply_);
1830 reply_ = reply;
1831 }
1832
1833 void OnReentrant3() {
1834 DCHECK(reply_);
1835 Message* reply = reply_;
1836 reply_ = NULL;
1837 reply->set_unblock(true);
1838 Send(reply);
1839 Done();
1840 }
1841
1842 Message* reply_;
1843};
1844
1845class ReentrantReplyClient : public Worker {
1846 public:
1847 ReentrantReplyClient(WaitableEvent* server_ready)
1848 : Worker("reentrant_reply1", Channel::MODE_CLIENT),
1849 server_ready_(server_ready) { }
1850
1851 void Run() {
1852 server_ready_->Wait();
1853 Send(new SyncChannelTestMsg_Reentrant2());
1854 Done();
1855 }
1856
1857 private:
1858 WaitableEvent* server_ready_;
1859};
1860
1861} // namespace
1862
1863TEST_F(IPCSyncChannelTest, ReentrantReply) {
1864 std::vector<Worker*> workers;
1865 WaitableEvent server_ready(false, false);
1866 workers.push_back(new ReentrantReplyServer2());
1867 workers.push_back(new ReentrantReplyServer1(&server_ready));
1868 workers.push_back(new ReentrantReplyClient(&server_ready));
1869 RunTest(workers);
1870}
1871
[email protected]298ee7d2012-03-30 21:29:301872//-----------------------------------------------------------------------------
1873
[email protected]5c41e6e12012-03-17 02:20:461874// Generate a validated channel ID using Channel::GenerateVerifiedChannelID().
1875namespace {
1876
1877class VerifiedServer : public Worker {
1878 public:
1879 VerifiedServer(base::Thread* listener_thread,
1880 const std::string& channel_name,
1881 const std::string& reply_text)
1882 : Worker(channel_name, Channel::MODE_SERVER),
1883 reply_text_(reply_text) {
1884 Worker::OverrideThread(listener_thread);
1885 }
1886
1887 virtual void OnNestedTestMsg(Message* reply_msg) {
1888 VLOG(1) << __FUNCTION__ << " Sending reply: " << reply_text_;
1889 SyncChannelNestedTestMsg_String::WriteReplyParams(reply_msg, reply_text_);
1890 Send(reply_msg);
[email protected]0a6fc4b2012-04-05 02:38:341891 ASSERT_EQ(channel()->peer_pid(), base::GetCurrentProcId());
[email protected]5c41e6e12012-03-17 02:20:461892 Done();
1893 }
1894
1895 private:
1896 std::string reply_text_;
1897};
1898
1899class VerifiedClient : public Worker {
1900 public:
1901 VerifiedClient(base::Thread* listener_thread,
1902 const std::string& channel_name,
1903 const std::string& expected_text)
1904 : Worker(channel_name, Channel::MODE_CLIENT),
1905 expected_text_(expected_text) {
1906 Worker::OverrideThread(listener_thread);
1907 }
1908
1909 virtual void Run() {
1910 std::string response;
1911 SyncMessage* msg = new SyncChannelNestedTestMsg_String(&response);
1912 bool result = Send(msg);
1913 DCHECK(result);
1914 DCHECK_EQ(response, expected_text_);
[email protected]281bc5b2012-06-27 16:07:341915 // expected_text_ is only used in the above DCHECK. This line suppresses the
1916 // "unused private field" warning in release builds.
1917 (void)expected_text_;
[email protected]5c41e6e12012-03-17 02:20:461918
1919 VLOG(1) << __FUNCTION__ << " Received reply: " << response;
[email protected]0a6fc4b2012-04-05 02:38:341920 ASSERT_EQ(channel()->peer_pid(), base::GetCurrentProcId());
[email protected]5c41e6e12012-03-17 02:20:461921 Done();
1922 }
1923
1924 private:
[email protected]5c41e6e12012-03-17 02:20:461925 std::string expected_text_;
1926};
1927
1928void Verified() {
1929 std::vector<Worker*> workers;
1930
1931 // A shared worker thread for servers
1932 base::Thread server_worker_thread("Verified_ServerListener");
1933 ASSERT_TRUE(server_worker_thread.Start());
1934
1935 base::Thread client_worker_thread("Verified_ClientListener");
1936 ASSERT_TRUE(client_worker_thread.Start());
1937
1938 std::string channel_id = Channel::GenerateVerifiedChannelID("Verified");
1939 Worker* worker;
1940
1941 worker = new VerifiedServer(&server_worker_thread,
1942 channel_id,
1943 "Got first message");
1944 workers.push_back(worker);
1945
1946 worker = new VerifiedClient(&client_worker_thread,
1947 channel_id,
1948 "Got first message");
1949 workers.push_back(worker);
1950
1951 RunTest(workers);
1952
1953#if defined(OS_WIN)
1954#endif
1955}
1956
1957} // namespace
1958
1959// Windows needs to send an out-of-band secret to verify the client end of the
1960// channel. Test that we still connect correctly in that case.
1961TEST_F(IPCSyncChannelTest, Verified) {
1962 Verified();
1963}
1964
[email protected]c9e0c7332011-05-13 22:42:411965} // namespace IPC