blob: 772df0b7165e4686586f95ad2c68944da50ab4a7 [file] [log] [blame]
[email protected]73a797fb2010-06-07 02:10:181// Copyright (c) 2010 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"
13#include "base/logging.h"
14#include "base/message_loop.h"
[email protected]aa96ae772009-01-20 22:08:1515#include "base/platform_thread.h"
[email protected]5097dc82010-07-15 17:23:2316#include "base/scoped_ptr.h"
[email protected]807204142009-05-05 03:31:4417#include "base/stl_util-inl.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"
initial.commit09911bf2008-07-26 23:55:2920#include "base/thread.h"
[email protected]aa96ae772009-01-20 22:08:1521#include "base/waitable_event.h"
[email protected]946d1b22009-07-22 23:57:2122#include "ipc/ipc_message.h"
[email protected]1e9499c2010-04-06 20:33:3623#include "ipc/ipc_sync_message_filter.h"
[email protected]21fa3a12010-12-08 23:34:1624#include "ipc/ipc_sync_message_unittest.h"
initial.commit09911bf2008-07-26 23:55:2925#include "testing/gtest/include/gtest/gtest.h"
26
initial.commit09911bf2008-07-26 23:55:2927using namespace IPC;
[email protected]aa96ae772009-01-20 22:08:1528using base::WaitableEvent;
initial.commit09911bf2008-07-26 23:55:2929
[email protected]dd3eac22008-08-26 07:28:3430namespace {
31
initial.commit09911bf2008-07-26 23:55:2932// Base class for a "process" with listener and IPC threads.
33class Worker : public Channel::Listener, public Message::Sender {
34 public:
35 // Will create a channel without a name.
36 Worker(Channel::Mode mode, const std::string& thread_name)
[email protected]aa96ae772009-01-20 22:08:1537 : done_(new WaitableEvent(false, false)),
38 channel_created_(new WaitableEvent(false, false)),
initial.commit09911bf2008-07-26 23:55:2939 mode_(mode),
40 ipc_thread_((thread_name + "_ipc").c_str()),
41 listener_thread_((thread_name + "_listener").c_str()),
[email protected]8930d472009-02-21 08:05:2842 overrided_thread_(NULL),
[email protected]8a734422009-10-27 11:28:5843 shutdown_event_(true, false) {
44 // The data race on vfptr is real but is very hard
45 // to suppress using standard Valgrind mechanism (suppressions).
46 // We have to use ANNOTATE_BENIGN_RACE to hide the reports and
47 // make ThreadSanitizer bots green.
48 ANNOTATE_BENIGN_RACE(this, "Race on vfptr, http://crbug.com/25841");
49 }
initial.commit09911bf2008-07-26 23:55:2950
51 // Will create a named channel and use this name for the threads' name.
[email protected]9a3a293b2009-06-04 22:28:1652 Worker(const std::string& channel_name, Channel::Mode mode)
[email protected]aa96ae772009-01-20 22:08:1553 : done_(new WaitableEvent(false, false)),
54 channel_created_(new WaitableEvent(false, false)),
55 channel_name_(channel_name),
initial.commit09911bf2008-07-26 23:55:2956 mode_(mode),
[email protected]9a3a293b2009-06-04 22:28:1657 ipc_thread_((channel_name + "_ipc").c_str()),
58 listener_thread_((channel_name + "_listener").c_str()),
[email protected]8930d472009-02-21 08:05:2859 overrided_thread_(NULL),
[email protected]8a734422009-10-27 11:28:5860 shutdown_event_(true, false) {
61 // The data race on vfptr is real but is very hard
62 // to suppress using standard Valgrind mechanism (suppressions).
63 // We have to use ANNOTATE_BENIGN_RACE to hide the reports and
64 // make ThreadSanitizer bots green.
65 ANNOTATE_BENIGN_RACE(this, "Race on vfptr, http://crbug.com/25841");
66 }
initial.commit09911bf2008-07-26 23:55:2967
68 // The IPC thread needs to outlive SyncChannel, so force the correct order of
69 // destruction.
70 virtual ~Worker() {
[email protected]aa96ae772009-01-20 22:08:1571 WaitableEvent listener_done(false, false), ipc_done(false, false);
[email protected]3cdb7af812008-10-24 19:21:1372 ListenerThread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]9d4ff5ed2009-03-03 00:21:5673 this, &Worker::OnListenerThreadShutdown1, &listener_done,
[email protected]aa96ae772009-01-20 22:08:1574 &ipc_done));
75 listener_done.Wait();
76 ipc_done.Wait();
initial.commit09911bf2008-07-26 23:55:2977 ipc_thread_.Stop();
78 listener_thread_.Stop();
initial.commit09911bf2008-07-26 23:55:2979 }
80 void AddRef() { }
81 void Release() { }
[email protected]39fe32a2009-09-30 04:29:2082 static bool ImplementsThreadSafeReferenceCounting() { return true; }
initial.commit09911bf2008-07-26 23:55:2983 bool Send(Message* msg) { return channel_->Send(msg); }
[email protected]d65cab7a2008-08-12 01:25:4184 bool SendWithTimeout(Message* msg, int timeout_ms) {
85 return channel_->SendWithTimeout(msg, timeout_ms);
86 }
[email protected]aa96ae772009-01-20 22:08:1587 void WaitForChannelCreation() { channel_created_->Wait(); }
[email protected]3cdb7af812008-10-24 19:21:1388 void CloseChannel() {
89 DCHECK(MessageLoop::current() == ListenerThread()->message_loop());
90 channel_->Close();
91 }
initial.commit09911bf2008-07-26 23:55:2992 void Start() {
[email protected]17b89142008-11-07 21:52:1593 StartThread(&listener_thread_, MessageLoop::TYPE_DEFAULT);
[email protected]3cdb7af812008-10-24 19:21:1394 ListenerThread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
initial.commit09911bf2008-07-26 23:55:2995 this, &Worker::OnStart));
96 }
[email protected]ab820df2008-08-26 05:55:1097 void OverrideThread(base::Thread* overrided_thread) {
initial.commit09911bf2008-07-26 23:55:2998 DCHECK(overrided_thread_ == NULL);
99 overrided_thread_ = overrided_thread;
100 }
[email protected]4df10d612008-11-12 00:38:26101 bool SendAnswerToLife(bool pump, int timeout, bool succeed) {
102 int answer = 0;
103 SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer);
104 if (pump)
105 msg->EnableMessagePumping();
106 bool result = SendWithTimeout(msg, timeout);
[email protected]9eec2252009-12-01 02:34:18107 DCHECK_EQ(result, succeed);
108 DCHECK_EQ(answer, (succeed ? 42 : 0));
[email protected]4df10d612008-11-12 00:38:26109 return result;
110 }
111 bool SendDouble(bool pump, bool succeed) {
112 int answer = 0;
113 SyncMessage* msg = new SyncChannelTestMsg_Double(5, &answer);
114 if (pump)
115 msg->EnableMessagePumping();
116 bool result = Send(msg);
[email protected]9eec2252009-12-01 02:34:18117 DCHECK_EQ(result, succeed);
118 DCHECK_EQ(answer, (succeed ? 10 : 0));
[email protected]4df10d612008-11-12 00:38:26119 return result;
120 }
initial.commit09911bf2008-07-26 23:55:29121 Channel::Mode mode() { return mode_; }
[email protected]aa96ae772009-01-20 22:08:15122 WaitableEvent* done_event() { return done_.get(); }
[email protected]1e9499c2010-04-06 20:33:36123 WaitableEvent* shutdown_event() { return &shutdown_event_; }
[email protected]9eec2252009-12-01 02:34:18124 void ResetChannel() { channel_.reset(); }
initial.commit09911bf2008-07-26 23:55:29125 // Derived classes need to call this when they've completed their part of
126 // the test.
[email protected]aa96ae772009-01-20 22:08:15127 void Done() { done_->Signal(); }
[email protected]1e9499c2010-04-06 20:33:36128
129 protected:
130 IPC::SyncChannel* channel() { return channel_.get(); }
initial.commit09911bf2008-07-26 23:55:29131 // Functions for dervied classes to implement if they wish.
132 virtual void Run() { }
initial.commit09911bf2008-07-26 23:55:29133 virtual void OnAnswer(int* answer) { NOTREACHED(); }
134 virtual void OnAnswerDelay(Message* reply_msg) {
135 // The message handler map below can only take one entry for
136 // SyncChannelTestMsg_AnswerToLife, so since some classes want
137 // the normal version while other want the delayed reply, we
138 // call the normal version if the derived class didn't override
139 // this function.
140 int answer;
141 OnAnswer(&answer);
142 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, answer);
143 Send(reply_msg);
144 }
[email protected]3cdb7af812008-10-24 19:21:13145 virtual void OnDouble(int in, int* out) { NOTREACHED(); }
146 virtual void OnDoubleDelay(int in, Message* reply_msg) {
147 int result;
148 OnDouble(in, &result);
149 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result);
150 Send(reply_msg);
151 }
initial.commit09911bf2008-07-26 23:55:29152
[email protected]ac0efda2009-10-14 16:22:02153 virtual void OnNestedTestMsg(Message* reply_msg) {
154 NOTREACHED();
155 }
156
[email protected]3cdb7af812008-10-24 19:21:13157 base::Thread* ListenerThread() {
158 return overrided_thread_ ? overrided_thread_ : &listener_thread_;
159 }
[email protected]87339f02010-09-02 21:45:50160
161 private:
initial.commit09911bf2008-07-26 23:55:29162 // Called on the listener thread to create the sync channel.
163 void OnStart() {
initial.commit09911bf2008-07-26 23:55:29164 // Link ipc_thread_, listener_thread_ and channel_ altogether.
[email protected]17b89142008-11-07 21:52:15165 StartThread(&ipc_thread_, MessageLoop::TYPE_IO);
initial.commit09911bf2008-07-26 23:55:29166 channel_.reset(new SyncChannel(
[email protected]4b580bf2010-12-02 19:16:07167 channel_name_, mode_, this, ipc_thread_.message_loop(), true,
[email protected]8930d472009-02-21 08:05:28168 &shutdown_event_));
[email protected]aa96ae772009-01-20 22:08:15169 channel_created_->Signal();
initial.commit09911bf2008-07-26 23:55:29170 Run();
171 }
172
[email protected]9d4ff5ed2009-03-03 00:21:56173 void OnListenerThreadShutdown1(WaitableEvent* listener_event,
174 WaitableEvent* ipc_event) {
[email protected]3cdb7af812008-10-24 19:21:13175 // SyncChannel needs to be destructed on the thread that it was created on.
176 channel_.reset();
[email protected]9d4ff5ed2009-03-03 00:21:56177
178 MessageLoop::current()->RunAllPending();
[email protected]aa96ae772009-01-20 22:08:15179
[email protected]3cdb7af812008-10-24 19:21:13180 ipc_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]9d4ff5ed2009-03-03 00:21:56181 this, &Worker::OnIPCThreadShutdown, listener_event, ipc_event));
[email protected]3cdb7af812008-10-24 19:21:13182 }
183
[email protected]9d4ff5ed2009-03-03 00:21:56184 void OnIPCThreadShutdown(WaitableEvent* listener_event,
185 WaitableEvent* ipc_event) {
186 MessageLoop::current()->RunAllPending();
[email protected]aa96ae772009-01-20 22:08:15187 ipc_event->Signal();
[email protected]9d4ff5ed2009-03-03 00:21:56188
189 listener_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
190 this, &Worker::OnListenerThreadShutdown2, listener_event));
191 }
192
193 void OnListenerThreadShutdown2(WaitableEvent* listener_event) {
194 MessageLoop::current()->RunAllPending();
195 listener_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13196 }
197
initial.commit09911bf2008-07-26 23:55:29198 void OnMessageReceived(const Message& message) {
199 IPC_BEGIN_MESSAGE_MAP(Worker, message)
[email protected]3cdb7af812008-10-24 19:21:13200 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay)
initial.commit09911bf2008-07-26 23:55:29201 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife,
202 OnAnswerDelay)
[email protected]ac0efda2009-10-14 16:22:02203 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelNestedTestMsg_String,
204 OnNestedTestMsg)
initial.commit09911bf2008-07-26 23:55:29205 IPC_END_MESSAGE_MAP()
206 }
207
[email protected]17b89142008-11-07 21:52:15208 void StartThread(base::Thread* thread, MessageLoop::Type type) {
[email protected]ab820df2008-08-26 05:55:10209 base::Thread::Options options;
[email protected]17b89142008-11-07 21:52:15210 options.message_loop_type = type;
[email protected]ab820df2008-08-26 05:55:10211 thread->StartWithOptions(options);
212 }
213
[email protected]aa96ae772009-01-20 22:08:15214 scoped_ptr<WaitableEvent> done_;
215 scoped_ptr<WaitableEvent> channel_created_;
[email protected]9a3a293b2009-06-04 22:28:16216 std::string channel_name_;
initial.commit09911bf2008-07-26 23:55:29217 Channel::Mode mode_;
218 scoped_ptr<SyncChannel> channel_;
[email protected]ab820df2008-08-26 05:55:10219 base::Thread ipc_thread_;
220 base::Thread listener_thread_;
221 base::Thread* overrided_thread_;
initial.commit09911bf2008-07-26 23:55:29222
[email protected]8930d472009-02-21 08:05:28223 base::WaitableEvent shutdown_event_;
224
[email protected]73a797fb2010-06-07 02:10:18225 DISALLOW_COPY_AND_ASSIGN(Worker);
initial.commit09911bf2008-07-26 23:55:29226};
227
228
229// Starts the test with the given workers. This function deletes the workers
230// when it's done.
231void RunTest(std::vector<Worker*> workers) {
initial.commit09911bf2008-07-26 23:55:29232 // First we create the workers that are channel servers, or else the other
233 // workers' channel initialization might fail because the pipe isn't created..
234 for (size_t i = 0; i < workers.size(); ++i) {
235 if (workers[i]->mode() == Channel::MODE_SERVER) {
236 workers[i]->Start();
237 workers[i]->WaitForChannelCreation();
238 }
239 }
240
241 // now create the clients
242 for (size_t i = 0; i < workers.size(); ++i) {
243 if (workers[i]->mode() == Channel::MODE_CLIENT)
244 workers[i]->Start();
245 }
246
247 // wait for all the workers to finish
initial.commit09911bf2008-07-26 23:55:29248 for (size_t i = 0; i < workers.size(); ++i)
[email protected]aa96ae772009-01-20 22:08:15249 workers[i]->done_event()->Wait();
initial.commit09911bf2008-07-26 23:55:29250
[email protected]82e5ee82009-04-03 02:29:45251 STLDeleteContainerPointers(workers.begin(), workers.end());
initial.commit09911bf2008-07-26 23:55:29252}
253
[email protected]dd3eac22008-08-26 07:28:34254} // namespace
255
[email protected]9629c0e2009-02-04 23:16:29256class IPCSyncChannelTest : public testing::Test {
257 private:
258 MessageLoop message_loop_;
259};
260
initial.commit09911bf2008-07-26 23:55:29261//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34262
263namespace {
264
initial.commit09911bf2008-07-26 23:55:29265class SimpleServer : public Worker {
266 public:
[email protected]b7243c42010-07-23 05:23:13267 explicit SimpleServer(bool pump_during_send)
[email protected]3cdb7af812008-10-24 19:21:13268 : Worker(Channel::MODE_SERVER, "simpler_server"),
269 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29270 void Run() {
[email protected]aa96ae772009-01-20 22:08:15271 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
initial.commit09911bf2008-07-26 23:55:29272 Done();
273 }
[email protected]3cdb7af812008-10-24 19:21:13274
275 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29276};
277
278class SimpleClient : public Worker {
279 public:
280 SimpleClient() : Worker(Channel::MODE_CLIENT, "simple_client") { }
281
282 void OnAnswer(int* answer) {
283 *answer = 42;
284 Done();
285 }
286};
287
[email protected]3cdb7af812008-10-24 19:21:13288void Simple(bool pump_during_send) {
initial.commit09911bf2008-07-26 23:55:29289 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13290 workers.push_back(new SimpleServer(pump_during_send));
initial.commit09911bf2008-07-26 23:55:29291 workers.push_back(new SimpleClient());
292 RunTest(workers);
293}
294
[email protected]3cdb7af812008-10-24 19:21:13295} // namespace
296
297// Tests basic synchronous call
298TEST_F(IPCSyncChannelTest, Simple) {
299 Simple(false);
300 Simple(true);
301}
initial.commit09911bf2008-07-26 23:55:29302
303//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34304
305namespace {
306
initial.commit09911bf2008-07-26 23:55:29307class DelayClient : public Worker {
308 public:
309 DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { }
310
311 void OnAnswerDelay(Message* reply_msg) {
312 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
313 Send(reply_msg);
314 Done();
315 }
316};
317
[email protected]3cdb7af812008-10-24 19:21:13318void DelayReply(bool pump_during_send) {
initial.commit09911bf2008-07-26 23:55:29319 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13320 workers.push_back(new SimpleServer(pump_during_send));
initial.commit09911bf2008-07-26 23:55:29321 workers.push_back(new DelayClient());
322 RunTest(workers);
323}
324
[email protected]3cdb7af812008-10-24 19:21:13325} // namespace
326
327// Tests that asynchronous replies work
328TEST_F(IPCSyncChannelTest, DelayReply) {
329 DelayReply(false);
330 DelayReply(true);
331}
initial.commit09911bf2008-07-26 23:55:29332
333//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34334
335namespace {
336
initial.commit09911bf2008-07-26 23:55:29337class NoHangServer : public Worker {
338 public:
[email protected]aa96ae772009-01-20 22:08:15339 explicit NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send)
[email protected]3cdb7af812008-10-24 19:21:13340 : Worker(Channel::MODE_SERVER, "no_hang_server"),
341 got_first_reply_(got_first_reply),
342 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29343 void Run() {
[email protected]aa96ae772009-01-20 22:08:15344 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
345 got_first_reply_->Signal();
initial.commit09911bf2008-07-26 23:55:29346
[email protected]aa96ae772009-01-20 22:08:15347 SendAnswerToLife(pump_during_send_, base::kNoTimeout, false);
initial.commit09911bf2008-07-26 23:55:29348 Done();
349 }
350
[email protected]aa96ae772009-01-20 22:08:15351 WaitableEvent* got_first_reply_;
[email protected]3cdb7af812008-10-24 19:21:13352 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29353};
354
355class NoHangClient : public Worker {
356 public:
[email protected]aa96ae772009-01-20 22:08:15357 explicit NoHangClient(WaitableEvent* got_first_reply)
initial.commit09911bf2008-07-26 23:55:29358 : Worker(Channel::MODE_CLIENT, "no_hang_client"),
359 got_first_reply_(got_first_reply) { }
360
361 virtual void OnAnswerDelay(Message* reply_msg) {
362 // Use the DELAY_REPLY macro so that we can force the reply to be sent
363 // before this function returns (when the channel will be reset).
364 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
365 Send(reply_msg);
366 got_first_reply_->Wait();
367 CloseChannel();
368 Done();
369 }
370
[email protected]aa96ae772009-01-20 22:08:15371 WaitableEvent* got_first_reply_;
initial.commit09911bf2008-07-26 23:55:29372};
373
[email protected]3cdb7af812008-10-24 19:21:13374void NoHang(bool pump_during_send) {
[email protected]aa96ae772009-01-20 22:08:15375 WaitableEvent got_first_reply(false, false);
initial.commit09911bf2008-07-26 23:55:29376 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13377 workers.push_back(new NoHangServer(&got_first_reply, pump_during_send));
initial.commit09911bf2008-07-26 23:55:29378 workers.push_back(new NoHangClient(&got_first_reply));
379 RunTest(workers);
380}
381
[email protected]3cdb7af812008-10-24 19:21:13382} // namespace
383
384// Tests that caller doesn't hang if receiver dies
385TEST_F(IPCSyncChannelTest, NoHang) {
386 NoHang(false);
387 NoHang(true);
388}
initial.commit09911bf2008-07-26 23:55:29389
390//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34391
392namespace {
393
[email protected]3cdb7af812008-10-24 19:21:13394class UnblockServer : public Worker {
initial.commit09911bf2008-07-26 23:55:29395 public:
[email protected]9eec2252009-12-01 02:34:18396 UnblockServer(bool pump_during_send, bool delete_during_send)
[email protected]3cdb7af812008-10-24 19:21:13397 : Worker(Channel::MODE_SERVER, "unblock_server"),
[email protected]9eec2252009-12-01 02:34:18398 pump_during_send_(pump_during_send),
399 delete_during_send_(delete_during_send) { }
initial.commit09911bf2008-07-26 23:55:29400 void Run() {
[email protected]9eec2252009-12-01 02:34:18401 if (delete_during_send_) {
402 // Use custom code since race conditions mean the answer may or may not be
403 // available.
404 int answer = 0;
405 SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer);
406 if (pump_during_send_)
407 msg->EnableMessagePumping();
408 Send(msg);
409 } else {
410 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
411 }
initial.commit09911bf2008-07-26 23:55:29412 Done();
413 }
414
[email protected]9eec2252009-12-01 02:34:18415 void OnDoubleDelay(int in, Message* reply_msg) {
416 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
417 Send(reply_msg);
418 if (delete_during_send_)
419 ResetChannel();
initial.commit09911bf2008-07-26 23:55:29420 }
[email protected]3cdb7af812008-10-24 19:21:13421
422 bool pump_during_send_;
[email protected]9eec2252009-12-01 02:34:18423 bool delete_during_send_;
initial.commit09911bf2008-07-26 23:55:29424};
425
[email protected]3cdb7af812008-10-24 19:21:13426class UnblockClient : public Worker {
initial.commit09911bf2008-07-26 23:55:29427 public:
[email protected]b7243c42010-07-23 05:23:13428 explicit UnblockClient(bool pump_during_send)
[email protected]3cdb7af812008-10-24 19:21:13429 : Worker(Channel::MODE_CLIENT, "unblock_client"),
430 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29431
432 void OnAnswer(int* answer) {
[email protected]4df10d612008-11-12 00:38:26433 SendDouble(pump_during_send_, true);
434 *answer = 42;
initial.commit09911bf2008-07-26 23:55:29435 Done();
436 }
[email protected]3cdb7af812008-10-24 19:21:13437
438 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29439};
440
[email protected]9eec2252009-12-01 02:34:18441void Unblock(bool server_pump, bool client_pump, bool delete_during_send) {
[email protected]3cdb7af812008-10-24 19:21:13442 std::vector<Worker*> workers;
[email protected]9eec2252009-12-01 02:34:18443 workers.push_back(new UnblockServer(server_pump, delete_during_send));
[email protected]3cdb7af812008-10-24 19:21:13444 workers.push_back(new UnblockClient(client_pump));
445 RunTest(workers);
446}
447
[email protected]dd3eac22008-08-26 07:28:34448} // namespace
449
initial.commit09911bf2008-07-26 23:55:29450// Tests that the caller unblocks to answer a sync message from the receiver.
[email protected]3cdb7af812008-10-24 19:21:13451TEST_F(IPCSyncChannelTest, Unblock) {
[email protected]9eec2252009-12-01 02:34:18452 Unblock(false, false, false);
453 Unblock(false, true, false);
454 Unblock(true, false, false);
455 Unblock(true, true, false);
456}
457
458//-----------------------------------------------------------------------------
459
460// Tests that the the IPC::SyncChannel object can be deleted during a Send.
461TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) {
462 Unblock(false, false, true);
463 Unblock(false, true, true);
464 Unblock(true, false, true);
465 Unblock(true, true, true);
[email protected]3cdb7af812008-10-24 19:21:13466}
467
468//-----------------------------------------------------------------------------
469
470namespace {
471
472class RecursiveServer : public Worker {
473 public:
474 explicit RecursiveServer(
475 bool expected_send_result, bool pump_first, bool pump_second)
476 : Worker(Channel::MODE_SERVER, "recursive_server"),
477 expected_send_result_(expected_send_result),
478 pump_first_(pump_first), pump_second_(pump_second) { }
479 void Run() {
[email protected]4df10d612008-11-12 00:38:26480 SendDouble(pump_first_, expected_send_result_);
[email protected]3cdb7af812008-10-24 19:21:13481 Done();
482 }
483
484 void OnDouble(int in, int* out) {
[email protected]4df10d612008-11-12 00:38:26485 *out = in * 2;
[email protected]aa96ae772009-01-20 22:08:15486 SendAnswerToLife(pump_second_, base::kNoTimeout, expected_send_result_);
[email protected]3cdb7af812008-10-24 19:21:13487 }
488
489 bool expected_send_result_, pump_first_, pump_second_;
490};
491
492class RecursiveClient : public Worker {
493 public:
494 explicit RecursiveClient(bool pump_during_send, bool close_channel)
495 : Worker(Channel::MODE_CLIENT, "recursive_client"),
496 pump_during_send_(pump_during_send), close_channel_(close_channel) { }
497
498 void OnDoubleDelay(int in, Message* reply_msg) {
[email protected]4df10d612008-11-12 00:38:26499 SendDouble(pump_during_send_, !close_channel_);
[email protected]c690a182008-10-24 23:10:32500 if (close_channel_) {
501 delete reply_msg;
502 } else {
[email protected]3cdb7af812008-10-24 19:21:13503 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
504 Send(reply_msg);
505 }
506 Done();
507 }
508
509 void OnAnswerDelay(Message* reply_msg) {
510 if (close_channel_) {
[email protected]c690a182008-10-24 23:10:32511 delete reply_msg;
[email protected]3cdb7af812008-10-24 19:21:13512 CloseChannel();
513 } else {
514 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
515 Send(reply_msg);
516 }
517 }
518
519 bool pump_during_send_, close_channel_;
520};
521
522void Recursive(
523 bool server_pump_first, bool server_pump_second, bool client_pump) {
initial.commit09911bf2008-07-26 23:55:29524 std::vector<Worker*> workers;
[email protected]3cdb7af812008-10-24 19:21:13525 workers.push_back(
526 new RecursiveServer(true, server_pump_first, server_pump_second));
527 workers.push_back(new RecursiveClient(client_pump, false));
initial.commit09911bf2008-07-26 23:55:29528 RunTest(workers);
529}
530
[email protected]3cdb7af812008-10-24 19:21:13531} // namespace
532
533// Tests a server calling Send while another Send is pending.
534TEST_F(IPCSyncChannelTest, Recursive) {
535 Recursive(false, false, false);
536 Recursive(false, false, true);
537 Recursive(false, true, false);
538 Recursive(false, true, true);
539 Recursive(true, false, false);
540 Recursive(true, false, true);
541 Recursive(true, true, false);
542 Recursive(true, true, true);
543}
544
545//-----------------------------------------------------------------------------
546
547namespace {
548
549void RecursiveNoHang(
550 bool server_pump_first, bool server_pump_second, bool client_pump) {
551 std::vector<Worker*> workers;
552 workers.push_back(
553 new RecursiveServer(false, server_pump_first, server_pump_second));
554 workers.push_back(new RecursiveClient(client_pump, true));
555 RunTest(workers);
556}
557
558} // namespace
559
560// Tests that if a caller makes a sync call during an existing sync call and
561// the receiver dies, neither of the Send() calls hang.
562TEST_F(IPCSyncChannelTest, RecursiveNoHang) {
563 RecursiveNoHang(false, false, false);
564 RecursiveNoHang(false, false, true);
565 RecursiveNoHang(false, true, false);
566 RecursiveNoHang(false, true, true);
567 RecursiveNoHang(true, false, false);
568 RecursiveNoHang(true, false, true);
569 RecursiveNoHang(true, true, false);
570 RecursiveNoHang(true, true, true);
571}
initial.commit09911bf2008-07-26 23:55:29572
573//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34574
575namespace {
576
initial.commit09911bf2008-07-26 23:55:29577class MultipleServer1 : public Worker {
578 public:
[email protected]b7243c42010-07-23 05:23:13579 explicit MultipleServer1(bool pump_during_send)
[email protected]9a3a293b2009-06-04 22:28:16580 : Worker("test_channel1", Channel::MODE_SERVER),
[email protected]3cdb7af812008-10-24 19:21:13581 pump_during_send_(pump_during_send) { }
582
initial.commit09911bf2008-07-26 23:55:29583 void Run() {
[email protected]4df10d612008-11-12 00:38:26584 SendDouble(pump_during_send_, true);
initial.commit09911bf2008-07-26 23:55:29585 Done();
586 }
[email protected]3cdb7af812008-10-24 19:21:13587
588 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29589};
590
591class MultipleClient1 : public Worker {
592 public:
[email protected]aa96ae772009-01-20 22:08:15593 MultipleClient1(WaitableEvent* client1_msg_received,
594 WaitableEvent* client1_can_reply) :
[email protected]9a3a293b2009-06-04 22:28:16595 Worker("test_channel1", Channel::MODE_CLIENT),
initial.commit09911bf2008-07-26 23:55:29596 client1_msg_received_(client1_msg_received),
597 client1_can_reply_(client1_can_reply) { }
598
599 void OnDouble(int in, int* out) {
[email protected]aa96ae772009-01-20 22:08:15600 client1_msg_received_->Signal();
initial.commit09911bf2008-07-26 23:55:29601 *out = in * 2;
602 client1_can_reply_->Wait();
603 Done();
604 }
605
606 private:
[email protected]aa96ae772009-01-20 22:08:15607 WaitableEvent *client1_msg_received_, *client1_can_reply_;
initial.commit09911bf2008-07-26 23:55:29608};
609
610class MultipleServer2 : public Worker {
611 public:
[email protected]9a3a293b2009-06-04 22:28:16612 MultipleServer2() : Worker("test_channel2", Channel::MODE_SERVER) { }
initial.commit09911bf2008-07-26 23:55:29613
614 void OnAnswer(int* result) {
615 *result = 42;
616 Done();
617 }
618};
619
620class MultipleClient2 : public Worker {
621 public:
[email protected]3cdb7af812008-10-24 19:21:13622 MultipleClient2(
[email protected]aa96ae772009-01-20 22:08:15623 WaitableEvent* client1_msg_received, WaitableEvent* client1_can_reply,
[email protected]3cdb7af812008-10-24 19:21:13624 bool pump_during_send)
[email protected]9a3a293b2009-06-04 22:28:16625 : Worker("test_channel2", Channel::MODE_CLIENT),
initial.commit09911bf2008-07-26 23:55:29626 client1_msg_received_(client1_msg_received),
[email protected]3cdb7af812008-10-24 19:21:13627 client1_can_reply_(client1_can_reply),
628 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29629
630 void Run() {
initial.commit09911bf2008-07-26 23:55:29631 client1_msg_received_->Wait();
[email protected]aa96ae772009-01-20 22:08:15632 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
633 client1_can_reply_->Signal();
initial.commit09911bf2008-07-26 23:55:29634 Done();
635 }
636
637 private:
[email protected]aa96ae772009-01-20 22:08:15638 WaitableEvent *client1_msg_received_, *client1_can_reply_;
[email protected]3cdb7af812008-10-24 19:21:13639 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29640};
641
[email protected]3cdb7af812008-10-24 19:21:13642void Multiple(bool server_pump, bool client_pump) {
initial.commit09911bf2008-07-26 23:55:29643 std::vector<Worker*> workers;
644
645 // A shared worker thread so that server1 and server2 run on one thread.
[email protected]ab820df2008-08-26 05:55:10646 base::Thread worker_thread("Multiple");
[email protected]6314e6f62009-07-15 16:07:14647 ASSERT_TRUE(worker_thread.Start());
initial.commit09911bf2008-07-26 23:55:29648
649 // Server1 sends a sync msg to client1, which blocks the reply until
650 // server2 (which runs on the same worker thread as server1) responds
651 // to a sync msg from client2.
[email protected]aa96ae772009-01-20 22:08:15652 WaitableEvent client1_msg_received(false, false);
653 WaitableEvent client1_can_reply(false, false);
initial.commit09911bf2008-07-26 23:55:29654
655 Worker* worker;
656
657 worker = new MultipleServer2();
658 worker->OverrideThread(&worker_thread);
659 workers.push_back(worker);
660
661 worker = new MultipleClient2(
[email protected]3cdb7af812008-10-24 19:21:13662 &client1_msg_received, &client1_can_reply, client_pump);
initial.commit09911bf2008-07-26 23:55:29663 workers.push_back(worker);
664
[email protected]3cdb7af812008-10-24 19:21:13665 worker = new MultipleServer1(server_pump);
initial.commit09911bf2008-07-26 23:55:29666 worker->OverrideThread(&worker_thread);
667 workers.push_back(worker);
668
669 worker = new MultipleClient1(
670 &client1_msg_received, &client1_can_reply);
671 workers.push_back(worker);
672
673 RunTest(workers);
674}
675
[email protected]3cdb7af812008-10-24 19:21:13676} // namespace
677
678// Tests that multiple SyncObjects on the same listener thread can unblock each
679// other.
680TEST_F(IPCSyncChannelTest, Multiple) {
681 Multiple(false, false);
682 Multiple(false, true);
683 Multiple(true, false);
684 Multiple(true, true);
685}
initial.commit09911bf2008-07-26 23:55:29686
687//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34688
689namespace {
690
[email protected]ac0efda2009-10-14 16:22:02691// This class provides server side functionality to test the case where
692// multiple sync channels are in use on the same thread on the client and
693// nested calls are issued.
694class QueuedReplyServer : public Worker {
initial.commit09911bf2008-07-26 23:55:29695 public:
[email protected]b7243c42010-07-23 05:23:13696 QueuedReplyServer(base::Thread* listener_thread,
697 const std::string& channel_name,
698 const std::string& reply_text)
[email protected]ac0efda2009-10-14 16:22:02699 : Worker(channel_name, Channel::MODE_SERVER),
700 reply_text_(reply_text) {
701 Worker::OverrideThread(listener_thread);
initial.commit09911bf2008-07-26 23:55:29702 }
[email protected]3cdb7af812008-10-24 19:21:13703
[email protected]ac0efda2009-10-14 16:22:02704 virtual void OnNestedTestMsg(Message* reply_msg) {
[email protected]2a9d601b2010-10-19 23:50:00705 VLOG(1) << __FUNCTION__ << " Sending reply: " << reply_text_;
706 SyncChannelNestedTestMsg_String::WriteReplyParams(reply_msg, reply_text_);
[email protected]ac0efda2009-10-14 16:22:02707 Send(reply_msg);
initial.commit09911bf2008-07-26 23:55:29708 Done();
709 }
710
711 private:
[email protected]ac0efda2009-10-14 16:22:02712 std::string reply_text_;
initial.commit09911bf2008-07-26 23:55:29713};
714
[email protected]ac0efda2009-10-14 16:22:02715// The QueuedReplyClient class provides functionality to test the case where
716// multiple sync channels are in use on the same thread and they make nested
717// sync calls, i.e. while the first channel waits for a response it makes a
718// sync call on another channel.
719// The callstack should unwind correctly, i.e. the outermost call should
720// complete first, and so on.
721class QueuedReplyClient : public Worker {
initial.commit09911bf2008-07-26 23:55:29722 public:
[email protected]ac0efda2009-10-14 16:22:02723 QueuedReplyClient(base::Thread* listener_thread,
724 const std::string& channel_name,
725 const std::string& expected_text,
726 bool pump_during_send)
727 : Worker(channel_name, Channel::MODE_CLIENT),
[email protected]7ee1a44c2010-07-23 14:18:59728 pump_during_send_(pump_during_send),
729 expected_text_(expected_text) {
[email protected]ac0efda2009-10-14 16:22:02730 Worker::OverrideThread(listener_thread);
731 }
initial.commit09911bf2008-07-26 23:55:29732
[email protected]ac0efda2009-10-14 16:22:02733 virtual void Run() {
734 std::string response;
735 SyncMessage* msg = new SyncChannelNestedTestMsg_String(&response);
736 if (pump_during_send_)
737 msg->EnableMessagePumping();
738 bool result = Send(msg);
739 DCHECK(result);
[email protected]9eec2252009-12-01 02:34:18740 DCHECK_EQ(response, expected_text_);
initial.commit09911bf2008-07-26 23:55:29741
[email protected]2a9d601b2010-10-19 23:50:00742 VLOG(1) << __FUNCTION__ << " Received reply: " << response;
initial.commit09911bf2008-07-26 23:55:29743 Done();
744 }
745
[email protected]ac0efda2009-10-14 16:22:02746 private:
[email protected]3cdb7af812008-10-24 19:21:13747 bool pump_during_send_;
[email protected]ac0efda2009-10-14 16:22:02748 std::string expected_text_;
initial.commit09911bf2008-07-26 23:55:29749};
750
[email protected]ac0efda2009-10-14 16:22:02751void QueuedReply(bool client_pump) {
initial.commit09911bf2008-07-26 23:55:29752 std::vector<Worker*> workers;
753
[email protected]ac0efda2009-10-14 16:22:02754 // A shared worker thread for servers
755 base::Thread server_worker_thread("QueuedReply_ServerListener");
756 ASSERT_TRUE(server_worker_thread.Start());
initial.commit09911bf2008-07-26 23:55:29757
[email protected]ac0efda2009-10-14 16:22:02758 base::Thread client_worker_thread("QueuedReply_ClientListener");
759 ASSERT_TRUE(client_worker_thread.Start());
initial.commit09911bf2008-07-26 23:55:29760
761 Worker* worker;
762
[email protected]ac0efda2009-10-14 16:22:02763 worker = new QueuedReplyServer(&server_worker_thread,
764 "QueuedReply_Server1",
765 "Got first message");
initial.commit09911bf2008-07-26 23:55:29766 workers.push_back(worker);
767
[email protected]ac0efda2009-10-14 16:22:02768 worker = new QueuedReplyServer(&server_worker_thread,
769 "QueuedReply_Server2",
770 "Got second message");
initial.commit09911bf2008-07-26 23:55:29771 workers.push_back(worker);
772
[email protected]ac0efda2009-10-14 16:22:02773 worker = new QueuedReplyClient(&client_worker_thread,
774 "QueuedReply_Server1",
775 "Got first message",
776 client_pump);
initial.commit09911bf2008-07-26 23:55:29777 workers.push_back(worker);
778
[email protected]ac0efda2009-10-14 16:22:02779 worker = new QueuedReplyClient(&client_worker_thread,
780 "QueuedReply_Server2",
781 "Got second message",
782 client_pump);
initial.commit09911bf2008-07-26 23:55:29783 workers.push_back(worker);
784
785 RunTest(workers);
786}
787
[email protected]3cdb7af812008-10-24 19:21:13788} // namespace
789
790// While a blocking send is in progress, the listener thread might answer other
791// synchronous messages. This tests that if during the response to another
792// message the reply to the original messages comes, it is queued up correctly
793// and the original Send is unblocked later.
[email protected]ac0efda2009-10-14 16:22:02794// We also test that the send call stacks unwind correctly when the channel
795// pumps messages while waiting for a response.
[email protected]3cdb7af812008-10-24 19:21:13796TEST_F(IPCSyncChannelTest, QueuedReply) {
[email protected]ac0efda2009-10-14 16:22:02797 QueuedReply(false);
798 QueuedReply(true);
[email protected]3cdb7af812008-10-24 19:21:13799}
initial.commit09911bf2008-07-26 23:55:29800
801//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34802
803namespace {
804
[email protected]deba0ff2010-11-03 05:30:14805void DropAssert(const std::string&) {}
806
initial.commit09911bf2008-07-26 23:55:29807class BadServer : public Worker {
808 public:
[email protected]b7243c42010-07-23 05:23:13809 explicit BadServer(bool pump_during_send)
[email protected]3cdb7af812008-10-24 19:21:13810 : Worker(Channel::MODE_SERVER, "simpler_server"),
811 pump_during_send_(pump_during_send) { }
initial.commit09911bf2008-07-26 23:55:29812 void Run() {
813 int answer = 0;
814
[email protected]4df10d612008-11-12 00:38:26815 SyncMessage* msg = new SyncMessage(
[email protected]3cdb7af812008-10-24 19:21:13816 MSG_ROUTING_CONTROL, SyncChannelTestMsg_Double::ID,
817 Message::PRIORITY_NORMAL, NULL);
818 if (pump_during_send_)
819 msg->EnableMessagePumping();
820
[email protected]deba0ff2010-11-03 05:30:14821 // Temporarily ignore asserts so that the assertion in
822 // ipc_message_utils doesn't cause termination.
823 logging::SetLogAssertHandler(&DropAssert);
initial.commit09911bf2008-07-26 23:55:29824 bool result = Send(msg);
[email protected]deba0ff2010-11-03 05:30:14825 logging::SetLogAssertHandler(NULL);
initial.commit09911bf2008-07-26 23:55:29826 DCHECK(!result);
827
828 // Need to send another message to get the client to call Done().
829 result = Send(new SyncChannelTestMsg_AnswerToLife(&answer));
830 DCHECK(result);
[email protected]9eec2252009-12-01 02:34:18831 DCHECK_EQ(answer, 42);
initial.commit09911bf2008-07-26 23:55:29832
833 Done();
834 }
[email protected]3cdb7af812008-10-24 19:21:13835
836 bool pump_during_send_;
initial.commit09911bf2008-07-26 23:55:29837};
838
[email protected]3cdb7af812008-10-24 19:21:13839void BadMessage(bool pump_during_send) {
840 std::vector<Worker*> workers;
841 workers.push_back(new BadServer(pump_during_send));
842 workers.push_back(new SimpleClient());
843 RunTest(workers);
844}
845
[email protected]dd3eac22008-08-26 07:28:34846} // namespace
847
initial.commit09911bf2008-07-26 23:55:29848// Tests that if a message is not serialized correctly, the Send() will fail.
[email protected]dd3eac22008-08-26 07:28:34849TEST_F(IPCSyncChannelTest, BadMessage) {
[email protected]3cdb7af812008-10-24 19:21:13850 BadMessage(false);
851 BadMessage(true);
initial.commit09911bf2008-07-26 23:55:29852}
853
initial.commit09911bf2008-07-26 23:55:29854//-----------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34855
856namespace {
857
[email protected]3cdb7af812008-10-24 19:21:13858class ChattyClient : public Worker {
initial.commit09911bf2008-07-26 23:55:29859 public:
[email protected]3cdb7af812008-10-24 19:21:13860 ChattyClient() :
861 Worker(Channel::MODE_CLIENT, "chatty_client") { }
initial.commit09911bf2008-07-26 23:55:29862
863 void OnAnswer(int* answer) {
864 // The PostMessage limit is 10k. Send 20% more than that.
865 const int kMessageLimit = 10000;
866 const int kMessagesToSend = kMessageLimit * 120 / 100;
867 for (int i = 0; i < kMessagesToSend; ++i) {
[email protected]4df10d612008-11-12 00:38:26868 if (!SendDouble(false, true))
initial.commit09911bf2008-07-26 23:55:29869 break;
870 }
[email protected]4df10d612008-11-12 00:38:26871 *answer = 42;
initial.commit09911bf2008-07-26 23:55:29872 Done();
873 }
874};
875
[email protected]3cdb7af812008-10-24 19:21:13876void ChattyServer(bool pump_during_send) {
877 std::vector<Worker*> workers;
[email protected]9eec2252009-12-01 02:34:18878 workers.push_back(new UnblockServer(pump_during_send, false));
[email protected]3cdb7af812008-10-24 19:21:13879 workers.push_back(new ChattyClient());
880 RunTest(workers);
881}
882
[email protected]dd3eac22008-08-26 07:28:34883} // namespace
884
[email protected]4df10d612008-11-12 00:38:26885// Tests http://b/1093251 - that sending lots of sync messages while
initial.commit09911bf2008-07-26 23:55:29886// the receiver is waiting for a sync reply does not overflow the PostMessage
887// queue.
[email protected]dd3eac22008-08-26 07:28:34888TEST_F(IPCSyncChannelTest, ChattyServer) {
[email protected]3cdb7af812008-10-24 19:21:13889 ChattyServer(false);
890 ChattyServer(true);
initial.commit09911bf2008-07-26 23:55:29891}
[email protected]d65cab7a2008-08-12 01:25:41892
[email protected]d65cab7a2008-08-12 01:25:41893//------------------------------------------------------------------------------
[email protected]dd3eac22008-08-26 07:28:34894
895namespace {
896
[email protected]d65cab7a2008-08-12 01:25:41897class TimeoutServer : public Worker {
898 public:
[email protected]b7243c42010-07-23 05:23:13899 TimeoutServer(int timeout_ms,
900 std::vector<bool> timeout_seq,
901 bool pump_during_send)
[email protected]d65cab7a2008-08-12 01:25:41902 : Worker(Channel::MODE_SERVER, "timeout_server"),
903 timeout_ms_(timeout_ms),
[email protected]3cdb7af812008-10-24 19:21:13904 timeout_seq_(timeout_seq),
905 pump_during_send_(pump_during_send) {
[email protected]d65cab7a2008-08-12 01:25:41906 }
907
908 void Run() {
909 for (std::vector<bool>::const_iterator iter = timeout_seq_.begin();
910 iter != timeout_seq_.end(); ++iter) {
[email protected]4df10d612008-11-12 00:38:26911 SendAnswerToLife(pump_during_send_, timeout_ms_, !*iter);
[email protected]d65cab7a2008-08-12 01:25:41912 }
913 Done();
914 }
915
916 private:
917 int timeout_ms_;
918 std::vector<bool> timeout_seq_;
[email protected]3cdb7af812008-10-24 19:21:13919 bool pump_during_send_;
[email protected]d65cab7a2008-08-12 01:25:41920};
921
922class UnresponsiveClient : public Worker {
923 public:
[email protected]b7243c42010-07-23 05:23:13924 explicit UnresponsiveClient(std::vector<bool> timeout_seq)
[email protected]d65cab7a2008-08-12 01:25:41925 : Worker(Channel::MODE_CLIENT, "unresponsive_client"),
926 timeout_seq_(timeout_seq) {
[email protected]b7243c42010-07-23 05:23:13927 }
[email protected]d65cab7a2008-08-12 01:25:41928
929 void OnAnswerDelay(Message* reply_msg) {
930 DCHECK(!timeout_seq_.empty());
931 if (!timeout_seq_[0]) {
932 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
933 Send(reply_msg);
934 } else {
935 // Don't reply.
[email protected]463667372008-08-20 20:20:52936 delete reply_msg;
[email protected]d65cab7a2008-08-12 01:25:41937 }
938 timeout_seq_.erase(timeout_seq_.begin());
939 if (timeout_seq_.empty())
940 Done();
941 }
942
943 private:
944 // Whether we should time-out or respond to the various messages we receive.
945 std::vector<bool> timeout_seq_;
946};
947
[email protected]3cdb7af812008-10-24 19:21:13948void SendWithTimeoutOK(bool pump_during_send) {
949 std::vector<Worker*> workers;
950 std::vector<bool> timeout_seq;
951 timeout_seq.push_back(false);
952 timeout_seq.push_back(false);
953 timeout_seq.push_back(false);
954 workers.push_back(new TimeoutServer(5000, timeout_seq, pump_during_send));
955 workers.push_back(new SimpleClient());
956 RunTest(workers);
957}
958
959void SendWithTimeoutTimeout(bool pump_during_send) {
960 std::vector<Worker*> workers;
961 std::vector<bool> timeout_seq;
962 timeout_seq.push_back(true);
963 timeout_seq.push_back(false);
964 timeout_seq.push_back(false);
965 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send));
966 workers.push_back(new UnresponsiveClient(timeout_seq));
967 RunTest(workers);
968}
969
970void SendWithTimeoutMixedOKAndTimeout(bool pump_during_send) {
971 std::vector<Worker*> workers;
972 std::vector<bool> timeout_seq;
973 timeout_seq.push_back(true);
974 timeout_seq.push_back(false);
975 timeout_seq.push_back(false);
976 timeout_seq.push_back(true);
977 timeout_seq.push_back(false);
978 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send));
979 workers.push_back(new UnresponsiveClient(timeout_seq));
980 RunTest(workers);
981}
982
[email protected]dd3eac22008-08-26 07:28:34983} // namespace
984
[email protected]d65cab7a2008-08-12 01:25:41985// Tests that SendWithTimeout does not time-out if the response comes back fast
986// enough.
[email protected]dd3eac22008-08-26 07:28:34987TEST_F(IPCSyncChannelTest, SendWithTimeoutOK) {
[email protected]3cdb7af812008-10-24 19:21:13988 SendWithTimeoutOK(false);
989 SendWithTimeoutOK(true);
[email protected]d65cab7a2008-08-12 01:25:41990}
991
992// Tests that SendWithTimeout does time-out.
[email protected]dd3eac22008-08-26 07:28:34993TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) {
[email protected]3cdb7af812008-10-24 19:21:13994 SendWithTimeoutTimeout(false);
995 SendWithTimeoutTimeout(true);
[email protected]d65cab7a2008-08-12 01:25:41996}
997
998// Sends some message that time-out and some that succeed.
[email protected]dd3eac22008-08-26 07:28:34999TEST_F(IPCSyncChannelTest, SendWithTimeoutMixedOKAndTimeout) {
[email protected]3cdb7af812008-10-24 19:21:131000 SendWithTimeoutMixedOKAndTimeout(false);
1001 SendWithTimeoutMixedOKAndTimeout(true);
1002}
[email protected]4df10d612008-11-12 00:38:261003
1004//------------------------------------------------------------------------------
1005
1006namespace {
1007
1008class NestedTask : public Task {
1009 public:
[email protected]b7243c42010-07-23 05:23:131010 explicit NestedTask(Worker* server) : server_(server) { }
[email protected]4df10d612008-11-12 00:38:261011 void Run() {
1012 // Sleep a bit so that we wake up after the reply has been received.
[email protected]aa96ae772009-01-20 22:08:151013 PlatformThread::Sleep(250);
1014 server_->SendAnswerToLife(true, base::kNoTimeout, true);
[email protected]4df10d612008-11-12 00:38:261015 }
1016
1017 Worker* server_;
1018};
1019
1020static bool timeout_occured = false;
1021
1022class TimeoutTask : public Task {
1023 public:
1024 void Run() {
1025 timeout_occured = true;
1026 }
1027};
1028
1029class DoneEventRaceServer : public Worker {
1030 public:
1031 DoneEventRaceServer()
1032 : Worker(Channel::MODE_SERVER, "done_event_race_server") { }
1033
1034 void Run() {
1035 MessageLoop::current()->PostTask(FROM_HERE, new NestedTask(this));
1036 MessageLoop::current()->PostDelayedTask(FROM_HERE, new TimeoutTask(), 9000);
1037 // Even though we have a timeout on the Send, it will succeed since for this
1038 // bug, the reply message comes back and is deserialized, however the done
1039 // event wasn't set. So we indirectly use the timeout task to notice if a
1040 // timeout occurred.
1041 SendAnswerToLife(true, 10000, true);
1042 DCHECK(!timeout_occured);
1043 Done();
1044 }
1045};
1046
1047} // namespace
1048
1049// Tests http://b/1474092 - that if after the done_event is set but before
1050// OnObjectSignaled is called another message is sent out, then after its
1051// reply comes back OnObjectSignaled will be called for the first message.
1052TEST_F(IPCSyncChannelTest, DoneEventRace) {
1053 std::vector<Worker*> workers;
1054 workers.push_back(new DoneEventRaceServer());
1055 workers.push_back(new SimpleClient());
1056 RunTest(workers);
1057}
[email protected]1e9499c2010-04-06 20:33:361058
1059//-----------------------------------------------------------------------------
1060
1061namespace {
1062
1063class TestSyncMessageFilter : public IPC::SyncMessageFilter {
1064 public:
1065 TestSyncMessageFilter(base::WaitableEvent* shutdown_event, Worker* worker)
1066 : SyncMessageFilter(shutdown_event),
1067 worker_(worker),
1068 thread_("helper_thread") {
1069 base::Thread::Options options;
1070 options.message_loop_type = MessageLoop::TYPE_DEFAULT;
1071 thread_.StartWithOptions(options);
1072 }
1073
1074 virtual void OnFilterAdded(Channel* channel) {
1075 SyncMessageFilter::OnFilterAdded(channel);
1076 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
1077 this, &TestSyncMessageFilter::SendMessageOnHelperThread));
1078 }
1079
1080 void SendMessageOnHelperThread() {
1081 int answer = 0;
1082 bool result = Send(new SyncChannelTestMsg_AnswerToLife(&answer));
1083 DCHECK(result);
1084 DCHECK_EQ(answer, 42);
1085
1086 worker_->Done();
1087 }
1088
1089 Worker* worker_;
1090 base::Thread thread_;
1091};
1092
1093class SyncMessageFilterServer : public Worker {
1094 public:
1095 SyncMessageFilterServer()
1096 : Worker(Channel::MODE_SERVER, "sync_message_filter_server") {
1097 filter_ = new TestSyncMessageFilter(shutdown_event(), this);
1098 }
1099
1100 void Run() {
1101 channel()->AddFilter(filter_.get());
1102 }
1103
1104 scoped_refptr<TestSyncMessageFilter> filter_;
1105};
1106
[email protected]87339f02010-09-02 21:45:501107// This class provides functionality to test the case that a Send on the sync
1108// channel does not crash after the channel has been closed.
1109class ServerSendAfterClose : public Worker {
1110 public:
1111 ServerSendAfterClose()
1112 : Worker(Channel::MODE_SERVER, "simpler_server"),
1113 send_result_(true) {
1114 }
1115
1116 bool SendDummy() {
1117 ListenerThread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
1118 this, &ServerSendAfterClose::Send, new SyncChannelTestMsg_NoArgs));
1119 return true;
1120 }
1121
1122 bool send_result() const {
1123 return send_result_;
1124 }
1125
1126 private:
1127 virtual void Run() {
1128 CloseChannel();
1129 Done();
1130 }
1131
1132 bool Send(Message* msg) {
1133 send_result_ = Worker::Send(msg);
1134 Done();
1135 return send_result_;
1136 }
1137
1138 bool send_result_;
1139};
1140
[email protected]1e9499c2010-04-06 20:33:361141} // namespace
1142
1143// Tests basic synchronous call
1144TEST_F(IPCSyncChannelTest, SyncMessageFilter) {
1145 std::vector<Worker*> workers;
1146 workers.push_back(new SyncMessageFilterServer());
1147 workers.push_back(new SimpleClient());
1148 RunTest(workers);
1149}
[email protected]87339f02010-09-02 21:45:501150
1151// Test the case when the channel is closed and a Send is attempted after that.
1152TEST_F(IPCSyncChannelTest, SendAfterClose) {
1153 ServerSendAfterClose server;
1154 server.Start();
1155
1156 server.done_event()->Wait();
1157 server.done_event()->Reset();
1158
1159 server.SendDummy();
1160 server.done_event()->Wait();
1161
1162 EXPECT_FALSE(server.send_result());
1163}
1164
1165