levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | // |
| 5 | // Unit test for SyncChannel. |
| 6 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 7 | #include "ipc/ipc_sync_channel.h" |
| 8 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/basictypes.h" |
| 13 | #include "base/logging.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 15 | #include "base/message_loop.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 16 | #include "base/stl_util-inl.h" |
| 17 | #include "base/string_util.h" |
timurrrr@chromium.org | f39c3ff | 2010-05-14 17:24:42 +0900 | [diff] [blame] | 18 | #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
brettw@google.com | 7c5cc67 | 2011-01-01 11:17:08 +0900 | [diff] [blame] | 19 | #include "base/threading/platform_thread.h" |
brettw@chromium.org | 5b5f5e0 | 2011-01-01 10:01:06 +0900 | [diff] [blame] | 20 | #include "base/threading/thread.h" |
brettw@chromium.org | 5238c7d | 2011-01-02 15:05:39 +0900 | [diff] [blame] | 21 | #include "base/synchronization/waitable_event.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 22 | #include "ipc/ipc_message.h" |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 23 | #include "ipc/ipc_sync_message_filter.h" |
jam@chromium.org | 86a8de1 | 2010-12-09 08:34:16 +0900 | [diff] [blame] | 24 | #include "ipc/ipc_sync_message_unittest.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 25 | #include "testing/gtest/include/gtest/gtest.h" |
| 26 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 27 | using namespace IPC; |
| 28 | using base::WaitableEvent; |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | // Base class for a "process" with listener and IPC threads. |
| 33 | class 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) |
| 37 | : done_(new WaitableEvent(false, false)), |
| 38 | channel_created_(new WaitableEvent(false, false)), |
| 39 | mode_(mode), |
| 40 | ipc_thread_((thread_name + "_ipc").c_str()), |
| 41 | listener_thread_((thread_name + "_listener").c_str()), |
| 42 | overrided_thread_(NULL), |
timurrrr@chromium.org | 03100a8 | 2009-10-27 20:28:58 +0900 | [diff] [blame] | 43 | 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 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 50 | |
| 51 | // Will create a named channel and use this name for the threads' name. |
| 52 | Worker(const std::string& channel_name, Channel::Mode mode) |
| 53 | : done_(new WaitableEvent(false, false)), |
| 54 | channel_created_(new WaitableEvent(false, false)), |
| 55 | channel_name_(channel_name), |
| 56 | mode_(mode), |
| 57 | ipc_thread_((channel_name + "_ipc").c_str()), |
| 58 | listener_thread_((channel_name + "_listener").c_str()), |
| 59 | overrided_thread_(NULL), |
timurrrr@chromium.org | 03100a8 | 2009-10-27 20:28:58 +0900 | [diff] [blame] | 60 | 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 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 67 | |
| 68 | // The IPC thread needs to outlive SyncChannel, so force the correct order of |
| 69 | // destruction. |
| 70 | virtual ~Worker() { |
| 71 | WaitableEvent listener_done(false, false), ipc_done(false, false); |
| 72 | ListenerThread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 73 | this, &Worker::OnListenerThreadShutdown1, &listener_done, |
| 74 | &ipc_done)); |
| 75 | listener_done.Wait(); |
| 76 | ipc_done.Wait(); |
| 77 | ipc_thread_.Stop(); |
| 78 | listener_thread_.Stop(); |
| 79 | } |
| 80 | void AddRef() { } |
| 81 | void Release() { } |
darin@chromium.org | 5cb996e | 2009-09-30 13:29:20 +0900 | [diff] [blame] | 82 | static bool ImplementsThreadSafeReferenceCounting() { return true; } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 83 | bool Send(Message* msg) { return channel_->Send(msg); } |
| 84 | bool SendWithTimeout(Message* msg, int timeout_ms) { |
| 85 | return channel_->SendWithTimeout(msg, timeout_ms); |
| 86 | } |
| 87 | void WaitForChannelCreation() { channel_created_->Wait(); } |
| 88 | void CloseChannel() { |
| 89 | DCHECK(MessageLoop::current() == ListenerThread()->message_loop()); |
| 90 | channel_->Close(); |
| 91 | } |
| 92 | void Start() { |
| 93 | StartThread(&listener_thread_, MessageLoop::TYPE_DEFAULT); |
| 94 | ListenerThread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 95 | this, &Worker::OnStart)); |
| 96 | } |
| 97 | void OverrideThread(base::Thread* overrided_thread) { |
| 98 | DCHECK(overrided_thread_ == NULL); |
| 99 | overrided_thread_ = overrided_thread; |
| 100 | } |
| 101 | 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); |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 107 | DCHECK_EQ(result, succeed); |
| 108 | DCHECK_EQ(answer, (succeed ? 42 : 0)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 109 | 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); |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 117 | DCHECK_EQ(result, succeed); |
| 118 | DCHECK_EQ(answer, (succeed ? 10 : 0)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 119 | return result; |
| 120 | } |
| 121 | Channel::Mode mode() { return mode_; } |
| 122 | WaitableEvent* done_event() { return done_.get(); } |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 123 | WaitableEvent* shutdown_event() { return &shutdown_event_; } |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 124 | void ResetChannel() { channel_.reset(); } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 125 | // Derived classes need to call this when they've completed their part of |
| 126 | // the test. |
| 127 | void Done() { done_->Signal(); } |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 128 | |
| 129 | protected: |
| 130 | IPC::SyncChannel* channel() { return channel_.get(); } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 131 | // Functions for dervied classes to implement if they wish. |
| 132 | virtual void Run() { } |
| 133 | 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 | } |
| 145 | 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 | } |
| 152 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 153 | virtual void OnNestedTestMsg(Message* reply_msg) { |
| 154 | NOTREACHED(); |
| 155 | } |
| 156 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 157 | base::Thread* ListenerThread() { |
| 158 | return overrided_thread_ ? overrided_thread_ : &listener_thread_; |
| 159 | } |
ananta@chromium.org | 999f297 | 2010-09-03 06:45:50 +0900 | [diff] [blame] | 160 | |
piman@google.com | 0cbefaa | 2011-04-08 12:38:21 +0900 | [diff] [blame^] | 161 | const base::Thread& ipc_thread() const { return ipc_thread_; } |
| 162 | |
ananta@chromium.org | 999f297 | 2010-09-03 06:45:50 +0900 | [diff] [blame] | 163 | private: |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 164 | // Called on the listener thread to create the sync channel. |
| 165 | void OnStart() { |
| 166 | // Link ipc_thread_, listener_thread_ and channel_ altogether. |
| 167 | StartThread(&ipc_thread_, MessageLoop::TYPE_IO); |
| 168 | channel_.reset(new SyncChannel( |
jam@chromium.org | e57135c | 2010-12-03 04:16:07 +0900 | [diff] [blame] | 169 | channel_name_, mode_, this, ipc_thread_.message_loop(), true, |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 170 | &shutdown_event_)); |
| 171 | channel_created_->Signal(); |
| 172 | Run(); |
| 173 | } |
| 174 | |
| 175 | void OnListenerThreadShutdown1(WaitableEvent* listener_event, |
| 176 | WaitableEvent* ipc_event) { |
| 177 | // SyncChannel needs to be destructed on the thread that it was created on. |
| 178 | channel_.reset(); |
| 179 | |
| 180 | MessageLoop::current()->RunAllPending(); |
| 181 | |
| 182 | ipc_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 183 | this, &Worker::OnIPCThreadShutdown, listener_event, ipc_event)); |
| 184 | } |
| 185 | |
| 186 | void OnIPCThreadShutdown(WaitableEvent* listener_event, |
| 187 | WaitableEvent* ipc_event) { |
| 188 | MessageLoop::current()->RunAllPending(); |
| 189 | ipc_event->Signal(); |
| 190 | |
| 191 | listener_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 192 | this, &Worker::OnListenerThreadShutdown2, listener_event)); |
| 193 | } |
| 194 | |
| 195 | void OnListenerThreadShutdown2(WaitableEvent* listener_event) { |
| 196 | MessageLoop::current()->RunAllPending(); |
| 197 | listener_event->Signal(); |
| 198 | } |
| 199 | |
jam@chromium.org | 8a2c784 | 2010-12-24 15:19:28 +0900 | [diff] [blame] | 200 | bool OnMessageReceived(const Message& message) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 201 | IPC_BEGIN_MESSAGE_MAP(Worker, message) |
| 202 | IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay) |
| 203 | IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife, |
| 204 | OnAnswerDelay) |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 205 | IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelNestedTestMsg_String, |
| 206 | OnNestedTestMsg) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 207 | IPC_END_MESSAGE_MAP() |
jam@chromium.org | 8a2c784 | 2010-12-24 15:19:28 +0900 | [diff] [blame] | 208 | return true; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void StartThread(base::Thread* thread, MessageLoop::Type type) { |
| 212 | base::Thread::Options options; |
| 213 | options.message_loop_type = type; |
| 214 | thread->StartWithOptions(options); |
| 215 | } |
| 216 | |
| 217 | scoped_ptr<WaitableEvent> done_; |
| 218 | scoped_ptr<WaitableEvent> channel_created_; |
| 219 | std::string channel_name_; |
| 220 | Channel::Mode mode_; |
| 221 | scoped_ptr<SyncChannel> channel_; |
| 222 | base::Thread ipc_thread_; |
| 223 | base::Thread listener_thread_; |
| 224 | base::Thread* overrided_thread_; |
| 225 | |
| 226 | base::WaitableEvent shutdown_event_; |
| 227 | |
tfarina@chromium.org | b73eaee | 2010-06-07 11:10:18 +0900 | [diff] [blame] | 228 | DISALLOW_COPY_AND_ASSIGN(Worker); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | |
| 232 | // Starts the test with the given workers. This function deletes the workers |
| 233 | // when it's done. |
| 234 | void RunTest(std::vector<Worker*> workers) { |
| 235 | // First we create the workers that are channel servers, or else the other |
| 236 | // workers' channel initialization might fail because the pipe isn't created.. |
| 237 | for (size_t i = 0; i < workers.size(); ++i) { |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 238 | if (workers[i]->mode() & Channel::MODE_SERVER_FLAG) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 239 | workers[i]->Start(); |
| 240 | workers[i]->WaitForChannelCreation(); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // now create the clients |
| 245 | for (size_t i = 0; i < workers.size(); ++i) { |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 246 | if (workers[i]->mode() & Channel::MODE_CLIENT_FLAG) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 247 | workers[i]->Start(); |
| 248 | } |
| 249 | |
| 250 | // wait for all the workers to finish |
| 251 | for (size_t i = 0; i < workers.size(); ++i) |
| 252 | workers[i]->done_event()->Wait(); |
| 253 | |
| 254 | STLDeleteContainerPointers(workers.begin(), workers.end()); |
| 255 | } |
| 256 | |
| 257 | } // namespace |
| 258 | |
| 259 | class IPCSyncChannelTest : public testing::Test { |
| 260 | private: |
| 261 | MessageLoop message_loop_; |
| 262 | }; |
| 263 | |
| 264 | //----------------------------------------------------------------------------- |
| 265 | |
| 266 | namespace { |
| 267 | |
| 268 | class SimpleServer : public Worker { |
| 269 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 270 | explicit SimpleServer(bool pump_during_send) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 271 | : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 272 | pump_during_send_(pump_during_send) { } |
| 273 | void Run() { |
| 274 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 275 | Done(); |
| 276 | } |
| 277 | |
| 278 | bool pump_during_send_; |
| 279 | }; |
| 280 | |
| 281 | class SimpleClient : public Worker { |
| 282 | public: |
| 283 | SimpleClient() : Worker(Channel::MODE_CLIENT, "simple_client") { } |
| 284 | |
| 285 | void OnAnswer(int* answer) { |
| 286 | *answer = 42; |
| 287 | Done(); |
| 288 | } |
| 289 | }; |
| 290 | |
| 291 | void Simple(bool pump_during_send) { |
| 292 | std::vector<Worker*> workers; |
| 293 | workers.push_back(new SimpleServer(pump_during_send)); |
| 294 | workers.push_back(new SimpleClient()); |
| 295 | RunTest(workers); |
| 296 | } |
| 297 | |
| 298 | } // namespace |
| 299 | |
| 300 | // Tests basic synchronous call |
| 301 | TEST_F(IPCSyncChannelTest, Simple) { |
| 302 | Simple(false); |
| 303 | Simple(true); |
| 304 | } |
| 305 | |
| 306 | //----------------------------------------------------------------------------- |
| 307 | |
| 308 | namespace { |
| 309 | |
| 310 | class DelayClient : public Worker { |
| 311 | public: |
| 312 | DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { } |
| 313 | |
| 314 | void OnAnswerDelay(Message* reply_msg) { |
| 315 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 316 | Send(reply_msg); |
| 317 | Done(); |
| 318 | } |
| 319 | }; |
| 320 | |
| 321 | void DelayReply(bool pump_during_send) { |
| 322 | std::vector<Worker*> workers; |
| 323 | workers.push_back(new SimpleServer(pump_during_send)); |
| 324 | workers.push_back(new DelayClient()); |
| 325 | RunTest(workers); |
| 326 | } |
| 327 | |
| 328 | } // namespace |
| 329 | |
| 330 | // Tests that asynchronous replies work |
| 331 | TEST_F(IPCSyncChannelTest, DelayReply) { |
| 332 | DelayReply(false); |
| 333 | DelayReply(true); |
| 334 | } |
| 335 | |
| 336 | //----------------------------------------------------------------------------- |
| 337 | |
| 338 | namespace { |
| 339 | |
| 340 | class NoHangServer : public Worker { |
| 341 | public: |
| 342 | explicit NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send) |
| 343 | : Worker(Channel::MODE_SERVER, "no_hang_server"), |
| 344 | got_first_reply_(got_first_reply), |
| 345 | pump_during_send_(pump_during_send) { } |
| 346 | void Run() { |
| 347 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 348 | got_first_reply_->Signal(); |
| 349 | |
| 350 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, false); |
| 351 | Done(); |
| 352 | } |
| 353 | |
| 354 | WaitableEvent* got_first_reply_; |
| 355 | bool pump_during_send_; |
| 356 | }; |
| 357 | |
| 358 | class NoHangClient : public Worker { |
| 359 | public: |
| 360 | explicit NoHangClient(WaitableEvent* got_first_reply) |
| 361 | : Worker(Channel::MODE_CLIENT, "no_hang_client"), |
| 362 | got_first_reply_(got_first_reply) { } |
| 363 | |
| 364 | virtual void OnAnswerDelay(Message* reply_msg) { |
| 365 | // Use the DELAY_REPLY macro so that we can force the reply to be sent |
| 366 | // before this function returns (when the channel will be reset). |
| 367 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 368 | Send(reply_msg); |
| 369 | got_first_reply_->Wait(); |
| 370 | CloseChannel(); |
| 371 | Done(); |
| 372 | } |
| 373 | |
| 374 | WaitableEvent* got_first_reply_; |
| 375 | }; |
| 376 | |
| 377 | void NoHang(bool pump_during_send) { |
| 378 | WaitableEvent got_first_reply(false, false); |
| 379 | std::vector<Worker*> workers; |
| 380 | workers.push_back(new NoHangServer(&got_first_reply, pump_during_send)); |
| 381 | workers.push_back(new NoHangClient(&got_first_reply)); |
| 382 | RunTest(workers); |
| 383 | } |
| 384 | |
| 385 | } // namespace |
| 386 | |
| 387 | // Tests that caller doesn't hang if receiver dies |
| 388 | TEST_F(IPCSyncChannelTest, NoHang) { |
| 389 | NoHang(false); |
| 390 | NoHang(true); |
| 391 | } |
| 392 | |
| 393 | //----------------------------------------------------------------------------- |
| 394 | |
| 395 | namespace { |
| 396 | |
| 397 | class UnblockServer : public Worker { |
| 398 | public: |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 399 | UnblockServer(bool pump_during_send, bool delete_during_send) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 400 | : Worker(Channel::MODE_SERVER, "unblock_server"), |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 401 | pump_during_send_(pump_during_send), |
| 402 | delete_during_send_(delete_during_send) { } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 403 | void Run() { |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 404 | if (delete_during_send_) { |
| 405 | // Use custom code since race conditions mean the answer may or may not be |
| 406 | // available. |
| 407 | int answer = 0; |
| 408 | SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer); |
| 409 | if (pump_during_send_) |
| 410 | msg->EnableMessagePumping(); |
| 411 | Send(msg); |
| 412 | } else { |
| 413 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 414 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 415 | Done(); |
| 416 | } |
| 417 | |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 418 | void OnDoubleDelay(int in, Message* reply_msg) { |
| 419 | SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2); |
| 420 | Send(reply_msg); |
| 421 | if (delete_during_send_) |
| 422 | ResetChannel(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | bool pump_during_send_; |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 426 | bool delete_during_send_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | class UnblockClient : public Worker { |
| 430 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 431 | explicit UnblockClient(bool pump_during_send) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 432 | : Worker(Channel::MODE_CLIENT, "unblock_client"), |
| 433 | pump_during_send_(pump_during_send) { } |
| 434 | |
| 435 | void OnAnswer(int* answer) { |
| 436 | SendDouble(pump_during_send_, true); |
| 437 | *answer = 42; |
| 438 | Done(); |
| 439 | } |
| 440 | |
| 441 | bool pump_during_send_; |
| 442 | }; |
| 443 | |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 444 | void Unblock(bool server_pump, bool client_pump, bool delete_during_send) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 445 | std::vector<Worker*> workers; |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 446 | workers.push_back(new UnblockServer(server_pump, delete_during_send)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 447 | workers.push_back(new UnblockClient(client_pump)); |
| 448 | RunTest(workers); |
| 449 | } |
| 450 | |
| 451 | } // namespace |
| 452 | |
| 453 | // Tests that the caller unblocks to answer a sync message from the receiver. |
| 454 | TEST_F(IPCSyncChannelTest, Unblock) { |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 455 | Unblock(false, false, false); |
| 456 | Unblock(false, true, false); |
| 457 | Unblock(true, false, false); |
| 458 | Unblock(true, true, false); |
| 459 | } |
| 460 | |
| 461 | //----------------------------------------------------------------------------- |
| 462 | |
| 463 | // Tests that the the IPC::SyncChannel object can be deleted during a Send. |
| 464 | TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) { |
| 465 | Unblock(false, false, true); |
| 466 | Unblock(false, true, true); |
| 467 | Unblock(true, false, true); |
| 468 | Unblock(true, true, true); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | //----------------------------------------------------------------------------- |
| 472 | |
| 473 | namespace { |
| 474 | |
| 475 | class RecursiveServer : public Worker { |
| 476 | public: |
| 477 | explicit RecursiveServer( |
| 478 | bool expected_send_result, bool pump_first, bool pump_second) |
| 479 | : Worker(Channel::MODE_SERVER, "recursive_server"), |
| 480 | expected_send_result_(expected_send_result), |
| 481 | pump_first_(pump_first), pump_second_(pump_second) { } |
| 482 | void Run() { |
| 483 | SendDouble(pump_first_, expected_send_result_); |
| 484 | Done(); |
| 485 | } |
| 486 | |
| 487 | void OnDouble(int in, int* out) { |
| 488 | *out = in * 2; |
| 489 | SendAnswerToLife(pump_second_, base::kNoTimeout, expected_send_result_); |
| 490 | } |
| 491 | |
| 492 | bool expected_send_result_, pump_first_, pump_second_; |
| 493 | }; |
| 494 | |
| 495 | class RecursiveClient : public Worker { |
| 496 | public: |
| 497 | explicit RecursiveClient(bool pump_during_send, bool close_channel) |
| 498 | : Worker(Channel::MODE_CLIENT, "recursive_client"), |
| 499 | pump_during_send_(pump_during_send), close_channel_(close_channel) { } |
| 500 | |
| 501 | void OnDoubleDelay(int in, Message* reply_msg) { |
| 502 | SendDouble(pump_during_send_, !close_channel_); |
| 503 | if (close_channel_) { |
| 504 | delete reply_msg; |
| 505 | } else { |
| 506 | SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2); |
| 507 | Send(reply_msg); |
| 508 | } |
| 509 | Done(); |
| 510 | } |
| 511 | |
| 512 | void OnAnswerDelay(Message* reply_msg) { |
| 513 | if (close_channel_) { |
| 514 | delete reply_msg; |
| 515 | CloseChannel(); |
| 516 | } else { |
| 517 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 518 | Send(reply_msg); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | bool pump_during_send_, close_channel_; |
| 523 | }; |
| 524 | |
| 525 | void Recursive( |
| 526 | bool server_pump_first, bool server_pump_second, bool client_pump) { |
| 527 | std::vector<Worker*> workers; |
| 528 | workers.push_back( |
| 529 | new RecursiveServer(true, server_pump_first, server_pump_second)); |
| 530 | workers.push_back(new RecursiveClient(client_pump, false)); |
| 531 | RunTest(workers); |
| 532 | } |
| 533 | |
| 534 | } // namespace |
| 535 | |
| 536 | // Tests a server calling Send while another Send is pending. |
| 537 | TEST_F(IPCSyncChannelTest, Recursive) { |
| 538 | Recursive(false, false, false); |
| 539 | Recursive(false, false, true); |
| 540 | Recursive(false, true, false); |
| 541 | Recursive(false, true, true); |
| 542 | Recursive(true, false, false); |
| 543 | Recursive(true, false, true); |
| 544 | Recursive(true, true, false); |
| 545 | Recursive(true, true, true); |
| 546 | } |
| 547 | |
| 548 | //----------------------------------------------------------------------------- |
| 549 | |
| 550 | namespace { |
| 551 | |
| 552 | void RecursiveNoHang( |
| 553 | bool server_pump_first, bool server_pump_second, bool client_pump) { |
| 554 | std::vector<Worker*> workers; |
| 555 | workers.push_back( |
| 556 | new RecursiveServer(false, server_pump_first, server_pump_second)); |
| 557 | workers.push_back(new RecursiveClient(client_pump, true)); |
| 558 | RunTest(workers); |
| 559 | } |
| 560 | |
| 561 | } // namespace |
| 562 | |
| 563 | // Tests that if a caller makes a sync call during an existing sync call and |
| 564 | // the receiver dies, neither of the Send() calls hang. |
| 565 | TEST_F(IPCSyncChannelTest, RecursiveNoHang) { |
| 566 | RecursiveNoHang(false, false, false); |
| 567 | RecursiveNoHang(false, false, true); |
| 568 | RecursiveNoHang(false, true, false); |
| 569 | RecursiveNoHang(false, true, true); |
| 570 | RecursiveNoHang(true, false, false); |
| 571 | RecursiveNoHang(true, false, true); |
| 572 | RecursiveNoHang(true, true, false); |
| 573 | RecursiveNoHang(true, true, true); |
| 574 | } |
| 575 | |
| 576 | //----------------------------------------------------------------------------- |
| 577 | |
| 578 | namespace { |
| 579 | |
| 580 | class MultipleServer1 : public Worker { |
| 581 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 582 | explicit MultipleServer1(bool pump_during_send) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 583 | : Worker("test_channel1", Channel::MODE_SERVER), |
| 584 | pump_during_send_(pump_during_send) { } |
| 585 | |
| 586 | void Run() { |
| 587 | SendDouble(pump_during_send_, true); |
| 588 | Done(); |
| 589 | } |
| 590 | |
| 591 | bool pump_during_send_; |
| 592 | }; |
| 593 | |
| 594 | class MultipleClient1 : public Worker { |
| 595 | public: |
| 596 | MultipleClient1(WaitableEvent* client1_msg_received, |
| 597 | WaitableEvent* client1_can_reply) : |
| 598 | Worker("test_channel1", Channel::MODE_CLIENT), |
| 599 | client1_msg_received_(client1_msg_received), |
| 600 | client1_can_reply_(client1_can_reply) { } |
| 601 | |
| 602 | void OnDouble(int in, int* out) { |
| 603 | client1_msg_received_->Signal(); |
| 604 | *out = in * 2; |
| 605 | client1_can_reply_->Wait(); |
| 606 | Done(); |
| 607 | } |
| 608 | |
| 609 | private: |
| 610 | WaitableEvent *client1_msg_received_, *client1_can_reply_; |
| 611 | }; |
| 612 | |
| 613 | class MultipleServer2 : public Worker { |
| 614 | public: |
| 615 | MultipleServer2() : Worker("test_channel2", Channel::MODE_SERVER) { } |
| 616 | |
| 617 | void OnAnswer(int* result) { |
| 618 | *result = 42; |
| 619 | Done(); |
| 620 | } |
| 621 | }; |
| 622 | |
| 623 | class MultipleClient2 : public Worker { |
| 624 | public: |
| 625 | MultipleClient2( |
| 626 | WaitableEvent* client1_msg_received, WaitableEvent* client1_can_reply, |
| 627 | bool pump_during_send) |
| 628 | : Worker("test_channel2", Channel::MODE_CLIENT), |
| 629 | client1_msg_received_(client1_msg_received), |
| 630 | client1_can_reply_(client1_can_reply), |
| 631 | pump_during_send_(pump_during_send) { } |
| 632 | |
| 633 | void Run() { |
| 634 | client1_msg_received_->Wait(); |
| 635 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 636 | client1_can_reply_->Signal(); |
| 637 | Done(); |
| 638 | } |
| 639 | |
| 640 | private: |
| 641 | WaitableEvent *client1_msg_received_, *client1_can_reply_; |
| 642 | bool pump_during_send_; |
| 643 | }; |
| 644 | |
| 645 | void Multiple(bool server_pump, bool client_pump) { |
| 646 | std::vector<Worker*> workers; |
| 647 | |
| 648 | // A shared worker thread so that server1 and server2 run on one thread. |
| 649 | base::Thread worker_thread("Multiple"); |
| 650 | ASSERT_TRUE(worker_thread.Start()); |
| 651 | |
| 652 | // Server1 sends a sync msg to client1, which blocks the reply until |
| 653 | // server2 (which runs on the same worker thread as server1) responds |
| 654 | // to a sync msg from client2. |
| 655 | WaitableEvent client1_msg_received(false, false); |
| 656 | WaitableEvent client1_can_reply(false, false); |
| 657 | |
| 658 | Worker* worker; |
| 659 | |
| 660 | worker = new MultipleServer2(); |
| 661 | worker->OverrideThread(&worker_thread); |
| 662 | workers.push_back(worker); |
| 663 | |
| 664 | worker = new MultipleClient2( |
| 665 | &client1_msg_received, &client1_can_reply, client_pump); |
| 666 | workers.push_back(worker); |
| 667 | |
| 668 | worker = new MultipleServer1(server_pump); |
| 669 | worker->OverrideThread(&worker_thread); |
| 670 | workers.push_back(worker); |
| 671 | |
| 672 | worker = new MultipleClient1( |
| 673 | &client1_msg_received, &client1_can_reply); |
| 674 | workers.push_back(worker); |
| 675 | |
| 676 | RunTest(workers); |
| 677 | } |
| 678 | |
| 679 | } // namespace |
| 680 | |
| 681 | // Tests that multiple SyncObjects on the same listener thread can unblock each |
| 682 | // other. |
| 683 | TEST_F(IPCSyncChannelTest, Multiple) { |
| 684 | Multiple(false, false); |
| 685 | Multiple(false, true); |
| 686 | Multiple(true, false); |
| 687 | Multiple(true, true); |
| 688 | } |
| 689 | |
| 690 | //----------------------------------------------------------------------------- |
| 691 | |
| 692 | namespace { |
| 693 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 694 | // This class provides server side functionality to test the case where |
| 695 | // multiple sync channels are in use on the same thread on the client and |
| 696 | // nested calls are issued. |
| 697 | class QueuedReplyServer : public Worker { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 698 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 699 | QueuedReplyServer(base::Thread* listener_thread, |
| 700 | const std::string& channel_name, |
| 701 | const std::string& reply_text) |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 702 | : Worker(channel_name, Channel::MODE_SERVER), |
| 703 | reply_text_(reply_text) { |
| 704 | Worker::OverrideThread(listener_thread); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 705 | } |
| 706 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 707 | virtual void OnNestedTestMsg(Message* reply_msg) { |
pkasting@chromium.org | fcdd54b | 2010-10-20 08:50:00 +0900 | [diff] [blame] | 708 | VLOG(1) << __FUNCTION__ << " Sending reply: " << reply_text_; |
| 709 | SyncChannelNestedTestMsg_String::WriteReplyParams(reply_msg, reply_text_); |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 710 | Send(reply_msg); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 711 | Done(); |
| 712 | } |
| 713 | |
| 714 | private: |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 715 | std::string reply_text_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 716 | }; |
| 717 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 718 | // The QueuedReplyClient class provides functionality to test the case where |
| 719 | // multiple sync channels are in use on the same thread and they make nested |
| 720 | // sync calls, i.e. while the first channel waits for a response it makes a |
| 721 | // sync call on another channel. |
| 722 | // The callstack should unwind correctly, i.e. the outermost call should |
| 723 | // complete first, and so on. |
| 724 | class QueuedReplyClient : public Worker { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 725 | public: |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 726 | QueuedReplyClient(base::Thread* listener_thread, |
| 727 | const std::string& channel_name, |
| 728 | const std::string& expected_text, |
| 729 | bool pump_during_send) |
| 730 | : Worker(channel_name, Channel::MODE_CLIENT), |
thomasvl@google.com | 9a24207 | 2010-07-23 23:18:59 +0900 | [diff] [blame] | 731 | pump_during_send_(pump_during_send), |
| 732 | expected_text_(expected_text) { |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 733 | Worker::OverrideThread(listener_thread); |
| 734 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 735 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 736 | virtual void Run() { |
| 737 | std::string response; |
| 738 | SyncMessage* msg = new SyncChannelNestedTestMsg_String(&response); |
| 739 | if (pump_during_send_) |
| 740 | msg->EnableMessagePumping(); |
| 741 | bool result = Send(msg); |
| 742 | DCHECK(result); |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 743 | DCHECK_EQ(response, expected_text_); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 744 | |
pkasting@chromium.org | fcdd54b | 2010-10-20 08:50:00 +0900 | [diff] [blame] | 745 | VLOG(1) << __FUNCTION__ << " Received reply: " << response; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 746 | Done(); |
| 747 | } |
| 748 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 749 | private: |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 750 | bool pump_during_send_; |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 751 | std::string expected_text_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 752 | }; |
| 753 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 754 | void QueuedReply(bool client_pump) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 755 | std::vector<Worker*> workers; |
| 756 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 757 | // A shared worker thread for servers |
| 758 | base::Thread server_worker_thread("QueuedReply_ServerListener"); |
| 759 | ASSERT_TRUE(server_worker_thread.Start()); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 760 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 761 | base::Thread client_worker_thread("QueuedReply_ClientListener"); |
| 762 | ASSERT_TRUE(client_worker_thread.Start()); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 763 | |
| 764 | Worker* worker; |
| 765 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 766 | worker = new QueuedReplyServer(&server_worker_thread, |
| 767 | "QueuedReply_Server1", |
| 768 | "Got first message"); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 769 | workers.push_back(worker); |
| 770 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 771 | worker = new QueuedReplyServer(&server_worker_thread, |
| 772 | "QueuedReply_Server2", |
| 773 | "Got second message"); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 774 | workers.push_back(worker); |
| 775 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 776 | worker = new QueuedReplyClient(&client_worker_thread, |
| 777 | "QueuedReply_Server1", |
| 778 | "Got first message", |
| 779 | client_pump); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 780 | workers.push_back(worker); |
| 781 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 782 | worker = new QueuedReplyClient(&client_worker_thread, |
| 783 | "QueuedReply_Server2", |
| 784 | "Got second message", |
| 785 | client_pump); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 786 | workers.push_back(worker); |
| 787 | |
| 788 | RunTest(workers); |
| 789 | } |
| 790 | |
| 791 | } // namespace |
| 792 | |
| 793 | // While a blocking send is in progress, the listener thread might answer other |
| 794 | // synchronous messages. This tests that if during the response to another |
| 795 | // message the reply to the original messages comes, it is queued up correctly |
| 796 | // and the original Send is unblocked later. |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 797 | // We also test that the send call stacks unwind correctly when the channel |
| 798 | // pumps messages while waiting for a response. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 799 | TEST_F(IPCSyncChannelTest, QueuedReply) { |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 800 | QueuedReply(false); |
| 801 | QueuedReply(true); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | //----------------------------------------------------------------------------- |
| 805 | |
| 806 | namespace { |
| 807 | |
akalin@chromium.org | 434c5b6 | 2010-11-03 14:30:14 +0900 | [diff] [blame] | 808 | void DropAssert(const std::string&) {} |
| 809 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 810 | class BadServer : public Worker { |
| 811 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 812 | explicit BadServer(bool pump_during_send) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 813 | : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 814 | pump_during_send_(pump_during_send) { } |
| 815 | void Run() { |
| 816 | int answer = 0; |
| 817 | |
| 818 | SyncMessage* msg = new SyncMessage( |
| 819 | MSG_ROUTING_CONTROL, SyncChannelTestMsg_Double::ID, |
| 820 | Message::PRIORITY_NORMAL, NULL); |
| 821 | if (pump_during_send_) |
| 822 | msg->EnableMessagePumping(); |
| 823 | |
akalin@chromium.org | 434c5b6 | 2010-11-03 14:30:14 +0900 | [diff] [blame] | 824 | // Temporarily ignore asserts so that the assertion in |
| 825 | // ipc_message_utils doesn't cause termination. |
| 826 | logging::SetLogAssertHandler(&DropAssert); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 827 | bool result = Send(msg); |
akalin@chromium.org | 434c5b6 | 2010-11-03 14:30:14 +0900 | [diff] [blame] | 828 | logging::SetLogAssertHandler(NULL); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 829 | DCHECK(!result); |
| 830 | |
| 831 | // Need to send another message to get the client to call Done(). |
| 832 | result = Send(new SyncChannelTestMsg_AnswerToLife(&answer)); |
| 833 | DCHECK(result); |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 834 | DCHECK_EQ(answer, 42); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 835 | |
| 836 | Done(); |
| 837 | } |
| 838 | |
| 839 | bool pump_during_send_; |
| 840 | }; |
| 841 | |
| 842 | void BadMessage(bool pump_during_send) { |
| 843 | std::vector<Worker*> workers; |
| 844 | workers.push_back(new BadServer(pump_during_send)); |
| 845 | workers.push_back(new SimpleClient()); |
| 846 | RunTest(workers); |
| 847 | } |
| 848 | |
| 849 | } // namespace |
| 850 | |
| 851 | // Tests that if a message is not serialized correctly, the Send() will fail. |
| 852 | TEST_F(IPCSyncChannelTest, BadMessage) { |
| 853 | BadMessage(false); |
| 854 | BadMessage(true); |
| 855 | } |
| 856 | |
| 857 | //----------------------------------------------------------------------------- |
| 858 | |
| 859 | namespace { |
| 860 | |
| 861 | class ChattyClient : public Worker { |
| 862 | public: |
| 863 | ChattyClient() : |
| 864 | Worker(Channel::MODE_CLIENT, "chatty_client") { } |
| 865 | |
| 866 | void OnAnswer(int* answer) { |
| 867 | // The PostMessage limit is 10k. Send 20% more than that. |
| 868 | const int kMessageLimit = 10000; |
| 869 | const int kMessagesToSend = kMessageLimit * 120 / 100; |
| 870 | for (int i = 0; i < kMessagesToSend; ++i) { |
| 871 | if (!SendDouble(false, true)) |
| 872 | break; |
| 873 | } |
| 874 | *answer = 42; |
| 875 | Done(); |
| 876 | } |
| 877 | }; |
| 878 | |
| 879 | void ChattyServer(bool pump_during_send) { |
| 880 | std::vector<Worker*> workers; |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 881 | workers.push_back(new UnblockServer(pump_during_send, false)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 882 | workers.push_back(new ChattyClient()); |
| 883 | RunTest(workers); |
| 884 | } |
| 885 | |
| 886 | } // namespace |
| 887 | |
| 888 | // Tests http://b/1093251 - that sending lots of sync messages while |
| 889 | // the receiver is waiting for a sync reply does not overflow the PostMessage |
| 890 | // queue. |
| 891 | TEST_F(IPCSyncChannelTest, ChattyServer) { |
| 892 | ChattyServer(false); |
| 893 | ChattyServer(true); |
| 894 | } |
| 895 | |
| 896 | //------------------------------------------------------------------------------ |
| 897 | |
| 898 | namespace { |
| 899 | |
| 900 | class TimeoutServer : public Worker { |
| 901 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 902 | TimeoutServer(int timeout_ms, |
| 903 | std::vector<bool> timeout_seq, |
| 904 | bool pump_during_send) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 905 | : Worker(Channel::MODE_SERVER, "timeout_server"), |
| 906 | timeout_ms_(timeout_ms), |
| 907 | timeout_seq_(timeout_seq), |
| 908 | pump_during_send_(pump_during_send) { |
| 909 | } |
| 910 | |
| 911 | void Run() { |
| 912 | for (std::vector<bool>::const_iterator iter = timeout_seq_.begin(); |
| 913 | iter != timeout_seq_.end(); ++iter) { |
| 914 | SendAnswerToLife(pump_during_send_, timeout_ms_, !*iter); |
| 915 | } |
| 916 | Done(); |
| 917 | } |
| 918 | |
| 919 | private: |
| 920 | int timeout_ms_; |
| 921 | std::vector<bool> timeout_seq_; |
| 922 | bool pump_during_send_; |
| 923 | }; |
| 924 | |
| 925 | class UnresponsiveClient : public Worker { |
| 926 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 927 | explicit UnresponsiveClient(std::vector<bool> timeout_seq) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 928 | : Worker(Channel::MODE_CLIENT, "unresponsive_client"), |
| 929 | timeout_seq_(timeout_seq) { |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 930 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 931 | |
| 932 | void OnAnswerDelay(Message* reply_msg) { |
| 933 | DCHECK(!timeout_seq_.empty()); |
| 934 | if (!timeout_seq_[0]) { |
| 935 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 936 | Send(reply_msg); |
| 937 | } else { |
| 938 | // Don't reply. |
| 939 | delete reply_msg; |
| 940 | } |
| 941 | timeout_seq_.erase(timeout_seq_.begin()); |
| 942 | if (timeout_seq_.empty()) |
| 943 | Done(); |
| 944 | } |
| 945 | |
| 946 | private: |
| 947 | // Whether we should time-out or respond to the various messages we receive. |
| 948 | std::vector<bool> timeout_seq_; |
| 949 | }; |
| 950 | |
| 951 | void SendWithTimeoutOK(bool pump_during_send) { |
| 952 | std::vector<Worker*> workers; |
| 953 | std::vector<bool> timeout_seq; |
| 954 | timeout_seq.push_back(false); |
| 955 | timeout_seq.push_back(false); |
| 956 | timeout_seq.push_back(false); |
| 957 | workers.push_back(new TimeoutServer(5000, timeout_seq, pump_during_send)); |
| 958 | workers.push_back(new SimpleClient()); |
| 959 | RunTest(workers); |
| 960 | } |
| 961 | |
| 962 | void SendWithTimeoutTimeout(bool pump_during_send) { |
| 963 | std::vector<Worker*> workers; |
| 964 | std::vector<bool> timeout_seq; |
| 965 | timeout_seq.push_back(true); |
| 966 | timeout_seq.push_back(false); |
| 967 | timeout_seq.push_back(false); |
| 968 | workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send)); |
| 969 | workers.push_back(new UnresponsiveClient(timeout_seq)); |
| 970 | RunTest(workers); |
| 971 | } |
| 972 | |
| 973 | void SendWithTimeoutMixedOKAndTimeout(bool pump_during_send) { |
| 974 | std::vector<Worker*> workers; |
| 975 | std::vector<bool> timeout_seq; |
| 976 | timeout_seq.push_back(true); |
| 977 | timeout_seq.push_back(false); |
| 978 | timeout_seq.push_back(false); |
| 979 | timeout_seq.push_back(true); |
| 980 | timeout_seq.push_back(false); |
| 981 | workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send)); |
| 982 | workers.push_back(new UnresponsiveClient(timeout_seq)); |
| 983 | RunTest(workers); |
| 984 | } |
| 985 | |
| 986 | } // namespace |
| 987 | |
| 988 | // Tests that SendWithTimeout does not time-out if the response comes back fast |
| 989 | // enough. |
| 990 | TEST_F(IPCSyncChannelTest, SendWithTimeoutOK) { |
| 991 | SendWithTimeoutOK(false); |
| 992 | SendWithTimeoutOK(true); |
| 993 | } |
| 994 | |
| 995 | // Tests that SendWithTimeout does time-out. |
| 996 | TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) { |
| 997 | SendWithTimeoutTimeout(false); |
| 998 | SendWithTimeoutTimeout(true); |
| 999 | } |
| 1000 | |
| 1001 | // Sends some message that time-out and some that succeed. |
phajdan.jr@chromium.org | a8e3c09 | 2011-01-19 17:11:56 +0900 | [diff] [blame] | 1002 | // Crashes flakily, http://crbug.com/70075. |
| 1003 | TEST_F(IPCSyncChannelTest, DISABLED_SendWithTimeoutMixedOKAndTimeout) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1004 | SendWithTimeoutMixedOKAndTimeout(false); |
| 1005 | SendWithTimeoutMixedOKAndTimeout(true); |
| 1006 | } |
| 1007 | |
| 1008 | //------------------------------------------------------------------------------ |
| 1009 | |
| 1010 | namespace { |
| 1011 | |
| 1012 | class NestedTask : public Task { |
| 1013 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 1014 | explicit NestedTask(Worker* server) : server_(server) { } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1015 | void Run() { |
| 1016 | // Sleep a bit so that we wake up after the reply has been received. |
brettw@google.com | 7c5cc67 | 2011-01-01 11:17:08 +0900 | [diff] [blame] | 1017 | base::PlatformThread::Sleep(250); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1018 | server_->SendAnswerToLife(true, base::kNoTimeout, true); |
| 1019 | } |
| 1020 | |
| 1021 | Worker* server_; |
| 1022 | }; |
| 1023 | |
| 1024 | static bool timeout_occured = false; |
| 1025 | |
| 1026 | class TimeoutTask : public Task { |
| 1027 | public: |
| 1028 | void Run() { |
| 1029 | timeout_occured = true; |
| 1030 | } |
| 1031 | }; |
| 1032 | |
| 1033 | class DoneEventRaceServer : public Worker { |
| 1034 | public: |
| 1035 | DoneEventRaceServer() |
| 1036 | : Worker(Channel::MODE_SERVER, "done_event_race_server") { } |
| 1037 | |
| 1038 | void Run() { |
| 1039 | MessageLoop::current()->PostTask(FROM_HERE, new NestedTask(this)); |
| 1040 | MessageLoop::current()->PostDelayedTask(FROM_HERE, new TimeoutTask(), 9000); |
| 1041 | // Even though we have a timeout on the Send, it will succeed since for this |
| 1042 | // bug, the reply message comes back and is deserialized, however the done |
| 1043 | // event wasn't set. So we indirectly use the timeout task to notice if a |
| 1044 | // timeout occurred. |
| 1045 | SendAnswerToLife(true, 10000, true); |
| 1046 | DCHECK(!timeout_occured); |
| 1047 | Done(); |
| 1048 | } |
| 1049 | }; |
| 1050 | |
| 1051 | } // namespace |
| 1052 | |
| 1053 | // Tests http://b/1474092 - that if after the done_event is set but before |
| 1054 | // OnObjectSignaled is called another message is sent out, then after its |
| 1055 | // reply comes back OnObjectSignaled will be called for the first message. |
| 1056 | TEST_F(IPCSyncChannelTest, DoneEventRace) { |
| 1057 | std::vector<Worker*> workers; |
| 1058 | workers.push_back(new DoneEventRaceServer()); |
| 1059 | workers.push_back(new SimpleClient()); |
| 1060 | RunTest(workers); |
| 1061 | } |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 1062 | |
| 1063 | //----------------------------------------------------------------------------- |
| 1064 | |
| 1065 | namespace { |
| 1066 | |
| 1067 | class TestSyncMessageFilter : public IPC::SyncMessageFilter { |
| 1068 | public: |
| 1069 | TestSyncMessageFilter(base::WaitableEvent* shutdown_event, Worker* worker) |
| 1070 | : SyncMessageFilter(shutdown_event), |
| 1071 | worker_(worker), |
| 1072 | thread_("helper_thread") { |
| 1073 | base::Thread::Options options; |
| 1074 | options.message_loop_type = MessageLoop::TYPE_DEFAULT; |
| 1075 | thread_.StartWithOptions(options); |
| 1076 | } |
| 1077 | |
| 1078 | virtual void OnFilterAdded(Channel* channel) { |
| 1079 | SyncMessageFilter::OnFilterAdded(channel); |
| 1080 | thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 1081 | this, &TestSyncMessageFilter::SendMessageOnHelperThread)); |
| 1082 | } |
| 1083 | |
| 1084 | void SendMessageOnHelperThread() { |
| 1085 | int answer = 0; |
| 1086 | bool result = Send(new SyncChannelTestMsg_AnswerToLife(&answer)); |
| 1087 | DCHECK(result); |
| 1088 | DCHECK_EQ(answer, 42); |
| 1089 | |
| 1090 | worker_->Done(); |
| 1091 | } |
| 1092 | |
| 1093 | Worker* worker_; |
| 1094 | base::Thread thread_; |
| 1095 | }; |
| 1096 | |
| 1097 | class SyncMessageFilterServer : public Worker { |
| 1098 | public: |
| 1099 | SyncMessageFilterServer() |
| 1100 | : Worker(Channel::MODE_SERVER, "sync_message_filter_server") { |
| 1101 | filter_ = new TestSyncMessageFilter(shutdown_event(), this); |
| 1102 | } |
| 1103 | |
| 1104 | void Run() { |
| 1105 | channel()->AddFilter(filter_.get()); |
| 1106 | } |
| 1107 | |
| 1108 | scoped_refptr<TestSyncMessageFilter> filter_; |
| 1109 | }; |
| 1110 | |
ananta@chromium.org | 999f297 | 2010-09-03 06:45:50 +0900 | [diff] [blame] | 1111 | // This class provides functionality to test the case that a Send on the sync |
| 1112 | // channel does not crash after the channel has been closed. |
| 1113 | class ServerSendAfterClose : public Worker { |
| 1114 | public: |
| 1115 | ServerSendAfterClose() |
| 1116 | : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 1117 | send_result_(true) { |
| 1118 | } |
| 1119 | |
| 1120 | bool SendDummy() { |
| 1121 | ListenerThread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 1122 | this, &ServerSendAfterClose::Send, new SyncChannelTestMsg_NoArgs)); |
| 1123 | return true; |
| 1124 | } |
| 1125 | |
| 1126 | bool send_result() const { |
| 1127 | return send_result_; |
| 1128 | } |
| 1129 | |
| 1130 | private: |
| 1131 | virtual void Run() { |
| 1132 | CloseChannel(); |
| 1133 | Done(); |
| 1134 | } |
| 1135 | |
| 1136 | bool Send(Message* msg) { |
| 1137 | send_result_ = Worker::Send(msg); |
| 1138 | Done(); |
| 1139 | return send_result_; |
| 1140 | } |
| 1141 | |
| 1142 | bool send_result_; |
| 1143 | }; |
| 1144 | |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 1145 | } // namespace |
| 1146 | |
| 1147 | // Tests basic synchronous call |
| 1148 | TEST_F(IPCSyncChannelTest, SyncMessageFilter) { |
| 1149 | std::vector<Worker*> workers; |
| 1150 | workers.push_back(new SyncMessageFilterServer()); |
| 1151 | workers.push_back(new SimpleClient()); |
| 1152 | RunTest(workers); |
| 1153 | } |
ananta@chromium.org | 999f297 | 2010-09-03 06:45:50 +0900 | [diff] [blame] | 1154 | |
| 1155 | // Test the case when the channel is closed and a Send is attempted after that. |
| 1156 | TEST_F(IPCSyncChannelTest, SendAfterClose) { |
| 1157 | ServerSendAfterClose server; |
| 1158 | server.Start(); |
| 1159 | |
| 1160 | server.done_event()->Wait(); |
| 1161 | server.done_event()->Reset(); |
| 1162 | |
| 1163 | server.SendDummy(); |
| 1164 | server.done_event()->Wait(); |
| 1165 | |
| 1166 | EXPECT_FALSE(server.send_result()); |
| 1167 | } |
| 1168 | |
piman@google.com | 0cbefaa | 2011-04-08 12:38:21 +0900 | [diff] [blame^] | 1169 | //----------------------------------------------------------------------------- |
ananta@chromium.org | 999f297 | 2010-09-03 06:45:50 +0900 | [diff] [blame] | 1170 | |
piman@google.com | 0cbefaa | 2011-04-08 12:38:21 +0900 | [diff] [blame^] | 1171 | namespace { |
| 1172 | |
| 1173 | class RestrictedDispatchServer : public Worker { |
| 1174 | public: |
| 1175 | RestrictedDispatchServer(WaitableEvent* sent_ping_event) |
| 1176 | : Worker("restricted_channel", Channel::MODE_SERVER), |
| 1177 | sent_ping_event_(sent_ping_event) { } |
| 1178 | |
| 1179 | void OnDoPing(int ping) { |
| 1180 | // Send an asynchronous message that unblocks the caller. |
| 1181 | IPC::Message* msg = new SyncChannelTestMsg_Ping(ping); |
| 1182 | msg->set_unblock(true); |
| 1183 | Send(msg); |
| 1184 | // Signal the event after the message has been sent on the channel, on the |
| 1185 | // IPC thread. |
| 1186 | ipc_thread().message_loop()->PostTask(FROM_HERE, |
| 1187 | NewRunnableMethod(this, &RestrictedDispatchServer::OnPingSent)); |
| 1188 | } |
| 1189 | |
| 1190 | base::Thread* ListenerThread() { return Worker::ListenerThread(); } |
| 1191 | |
| 1192 | private: |
| 1193 | bool OnMessageReceived(const Message& message) { |
| 1194 | IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchServer, message) |
| 1195 | IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs) |
| 1196 | IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done) |
| 1197 | IPC_END_MESSAGE_MAP() |
| 1198 | return true; |
| 1199 | } |
| 1200 | |
| 1201 | void OnPingSent() { |
| 1202 | sent_ping_event_->Signal(); |
| 1203 | } |
| 1204 | |
| 1205 | void OnNoArgs() { } |
| 1206 | WaitableEvent* sent_ping_event_; |
| 1207 | }; |
| 1208 | |
| 1209 | class NonRestrictedDispatchServer : public Worker { |
| 1210 | public: |
| 1211 | NonRestrictedDispatchServer() |
| 1212 | : Worker("non_restricted_channel", Channel::MODE_SERVER) {} |
| 1213 | |
| 1214 | private: |
| 1215 | bool OnMessageReceived(const Message& message) { |
| 1216 | IPC_BEGIN_MESSAGE_MAP(NonRestrictedDispatchServer, message) |
| 1217 | IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs) |
| 1218 | IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done) |
| 1219 | IPC_END_MESSAGE_MAP() |
| 1220 | return true; |
| 1221 | } |
| 1222 | |
| 1223 | void OnNoArgs() { } |
| 1224 | }; |
| 1225 | |
| 1226 | class RestrictedDispatchClient : public Worker { |
| 1227 | public: |
| 1228 | RestrictedDispatchClient(WaitableEvent* sent_ping_event, |
| 1229 | RestrictedDispatchServer* server, |
| 1230 | int* success) |
| 1231 | : Worker("restricted_channel", Channel::MODE_CLIENT), |
| 1232 | ping_(0), |
| 1233 | server_(server), |
| 1234 | success_(success), |
| 1235 | sent_ping_event_(sent_ping_event) {} |
| 1236 | |
| 1237 | void Run() { |
| 1238 | // Incoming messages from our channel should only be dispatched when we |
| 1239 | // send a message on that same channel. |
| 1240 | channel()->SetRestrictDispatchToSameChannel(true); |
| 1241 | |
| 1242 | server_->ListenerThread()->message_loop()->PostTask(FROM_HERE, |
| 1243 | NewRunnableMethod(server_, &RestrictedDispatchServer::OnDoPing, 1)); |
| 1244 | sent_ping_event_->Wait(); |
| 1245 | Send(new SyncChannelTestMsg_NoArgs); |
| 1246 | if (ping_ == 1) |
| 1247 | ++*success_; |
| 1248 | else |
| 1249 | LOG(ERROR) << "Send failed to dispatch incoming message on same channel"; |
| 1250 | |
| 1251 | scoped_ptr<SyncChannel> non_restricted_channel(new SyncChannel( |
| 1252 | "non_restricted_channel", Channel::MODE_CLIENT, this, |
| 1253 | ipc_thread().message_loop(), true, shutdown_event())); |
| 1254 | |
| 1255 | server_->ListenerThread()->message_loop()->PostTask(FROM_HERE, |
| 1256 | NewRunnableMethod(server_, &RestrictedDispatchServer::OnDoPing, 2)); |
| 1257 | sent_ping_event_->Wait(); |
| 1258 | // Check that the incoming message is *not* dispatched when sending on the |
| 1259 | // non restricted channel. |
| 1260 | // TODO(piman): there is a possibility of a false positive race condition |
| 1261 | // here, if the message that was posted on the server-side end of the pipe |
| 1262 | // is not visible yet on the client side, but I don't know how to solve this |
| 1263 | // without hooking into the internals of SyncChannel. I haven't seen it in |
| 1264 | // practice (i.e. not setting SetRestrictDispatchToSameChannel does cause |
| 1265 | // the following to fail). |
| 1266 | non_restricted_channel->Send(new SyncChannelTestMsg_NoArgs); |
| 1267 | if (ping_ == 1) |
| 1268 | ++*success_; |
| 1269 | else |
| 1270 | LOG(ERROR) << "Send dispatched message from restricted channel"; |
| 1271 | |
| 1272 | Send(new SyncChannelTestMsg_NoArgs); |
| 1273 | if (ping_ == 2) |
| 1274 | ++*success_; |
| 1275 | else |
| 1276 | LOG(ERROR) << "Send failed to dispatch incoming message on same channel"; |
| 1277 | |
| 1278 | non_restricted_channel->Send(new SyncChannelTestMsg_Done); |
| 1279 | non_restricted_channel.reset(); |
| 1280 | Send(new SyncChannelTestMsg_Done); |
| 1281 | Done(); |
| 1282 | } |
| 1283 | |
| 1284 | private: |
| 1285 | bool OnMessageReceived(const Message& message) { |
| 1286 | IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchClient, message) |
| 1287 | IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Ping, OnPing) |
| 1288 | IPC_END_MESSAGE_MAP() |
| 1289 | return true; |
| 1290 | } |
| 1291 | |
| 1292 | void OnPing(int ping) { |
| 1293 | ping_ = ping; |
| 1294 | } |
| 1295 | |
| 1296 | int ping_; |
| 1297 | RestrictedDispatchServer* server_; |
| 1298 | int* success_; |
| 1299 | WaitableEvent* sent_ping_event_; |
| 1300 | }; |
| 1301 | |
| 1302 | } // namespace |
| 1303 | |
| 1304 | TEST_F(IPCSyncChannelTest, RestrictedDispatch) { |
| 1305 | WaitableEvent sent_ping_event(false, false); |
| 1306 | |
| 1307 | RestrictedDispatchServer* server = |
| 1308 | new RestrictedDispatchServer(&sent_ping_event); |
| 1309 | int success = 0; |
| 1310 | std::vector<Worker*> workers; |
| 1311 | workers.push_back(new NonRestrictedDispatchServer); |
| 1312 | workers.push_back(server); |
| 1313 | workers.push_back( |
| 1314 | new RestrictedDispatchClient(&sent_ping_event, server, &success)); |
| 1315 | RunTest(workers); |
| 1316 | EXPECT_EQ(3, success); |
| 1317 | } |