agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/basictypes.h" |
timurrrr@chromium.org | 03100a8 | 2009-10-27 20:28:58 +0900 | [diff] [blame^] | 11 | #include "base/dynamic_annotations.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 12 | #include "base/logging.h" |
| 13 | #include "base/message_loop.h" |
| 14 | #include "base/platform_thread.h" |
| 15 | #include "base/stl_util-inl.h" |
| 16 | #include "base/string_util.h" |
| 17 | #include "base/thread.h" |
| 18 | #include "base/waitable_event.h" |
| 19 | #include "ipc/ipc_message.h" |
| 20 | #include "ipc/ipc_sync_channel.h" |
| 21 | #include "testing/gtest/include/gtest/gtest.h" |
| 22 | |
| 23 | |
| 24 | #define MESSAGES_INTERNAL_FILE "ipc/ipc_sync_message_unittest.h" |
| 25 | #include "ipc/ipc_message_macros.h" |
| 26 | |
| 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); |
| 107 | DCHECK(result == succeed); |
| 108 | DCHECK(answer == (succeed ? 42 : 0)); |
| 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); |
| 117 | DCHECK(result == succeed); |
| 118 | DCHECK(answer == (succeed ? 10 : 0)); |
| 119 | return result; |
| 120 | } |
| 121 | Channel::Mode mode() { return mode_; } |
| 122 | WaitableEvent* done_event() { return done_.get(); } |
| 123 | |
| 124 | protected: |
| 125 | // Derived classes need to call this when they've completed their part of |
| 126 | // the test. |
| 127 | void Done() { done_->Signal(); } |
| 128 | // Functions for dervied classes to implement if they wish. |
| 129 | virtual void Run() { } |
| 130 | virtual void OnAnswer(int* answer) { NOTREACHED(); } |
| 131 | virtual void OnAnswerDelay(Message* reply_msg) { |
| 132 | // The message handler map below can only take one entry for |
| 133 | // SyncChannelTestMsg_AnswerToLife, so since some classes want |
| 134 | // the normal version while other want the delayed reply, we |
| 135 | // call the normal version if the derived class didn't override |
| 136 | // this function. |
| 137 | int answer; |
| 138 | OnAnswer(&answer); |
| 139 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, answer); |
| 140 | Send(reply_msg); |
| 141 | } |
| 142 | virtual void OnDouble(int in, int* out) { NOTREACHED(); } |
| 143 | virtual void OnDoubleDelay(int in, Message* reply_msg) { |
| 144 | int result; |
| 145 | OnDouble(in, &result); |
| 146 | SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result); |
| 147 | Send(reply_msg); |
| 148 | } |
| 149 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 150 | virtual void OnNestedTestMsg(Message* reply_msg) { |
| 151 | NOTREACHED(); |
| 152 | } |
| 153 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 154 | private: |
| 155 | base::Thread* ListenerThread() { |
| 156 | return overrided_thread_ ? overrided_thread_ : &listener_thread_; |
| 157 | } |
| 158 | // Called on the listener thread to create the sync channel. |
| 159 | void OnStart() { |
| 160 | // Link ipc_thread_, listener_thread_ and channel_ altogether. |
| 161 | StartThread(&ipc_thread_, MessageLoop::TYPE_IO); |
| 162 | channel_.reset(new SyncChannel( |
| 163 | channel_name_, mode_, this, NULL, ipc_thread_.message_loop(), true, |
| 164 | &shutdown_event_)); |
| 165 | channel_created_->Signal(); |
| 166 | Run(); |
| 167 | } |
| 168 | |
| 169 | void OnListenerThreadShutdown1(WaitableEvent* listener_event, |
| 170 | WaitableEvent* ipc_event) { |
| 171 | // SyncChannel needs to be destructed on the thread that it was created on. |
| 172 | channel_.reset(); |
| 173 | |
| 174 | MessageLoop::current()->RunAllPending(); |
| 175 | |
| 176 | ipc_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 177 | this, &Worker::OnIPCThreadShutdown, listener_event, ipc_event)); |
| 178 | } |
| 179 | |
| 180 | void OnIPCThreadShutdown(WaitableEvent* listener_event, |
| 181 | WaitableEvent* ipc_event) { |
| 182 | MessageLoop::current()->RunAllPending(); |
| 183 | ipc_event->Signal(); |
| 184 | |
| 185 | listener_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 186 | this, &Worker::OnListenerThreadShutdown2, listener_event)); |
| 187 | } |
| 188 | |
| 189 | void OnListenerThreadShutdown2(WaitableEvent* listener_event) { |
| 190 | MessageLoop::current()->RunAllPending(); |
| 191 | listener_event->Signal(); |
| 192 | } |
| 193 | |
| 194 | void OnMessageReceived(const Message& message) { |
| 195 | IPC_BEGIN_MESSAGE_MAP(Worker, message) |
| 196 | IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay) |
| 197 | IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife, |
| 198 | OnAnswerDelay) |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 199 | IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelNestedTestMsg_String, |
| 200 | OnNestedTestMsg) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 201 | IPC_END_MESSAGE_MAP() |
| 202 | } |
| 203 | |
| 204 | void StartThread(base::Thread* thread, MessageLoop::Type type) { |
| 205 | base::Thread::Options options; |
| 206 | options.message_loop_type = type; |
| 207 | thread->StartWithOptions(options); |
| 208 | } |
| 209 | |
| 210 | scoped_ptr<WaitableEvent> done_; |
| 211 | scoped_ptr<WaitableEvent> channel_created_; |
| 212 | std::string channel_name_; |
| 213 | Channel::Mode mode_; |
| 214 | scoped_ptr<SyncChannel> channel_; |
| 215 | base::Thread ipc_thread_; |
| 216 | base::Thread listener_thread_; |
| 217 | base::Thread* overrided_thread_; |
| 218 | |
| 219 | base::WaitableEvent shutdown_event_; |
| 220 | |
| 221 | DISALLOW_EVIL_CONSTRUCTORS(Worker); |
| 222 | }; |
| 223 | |
| 224 | |
| 225 | // Starts the test with the given workers. This function deletes the workers |
| 226 | // when it's done. |
| 227 | void RunTest(std::vector<Worker*> workers) { |
| 228 | // First we create the workers that are channel servers, or else the other |
| 229 | // workers' channel initialization might fail because the pipe isn't created.. |
| 230 | for (size_t i = 0; i < workers.size(); ++i) { |
| 231 | if (workers[i]->mode() == Channel::MODE_SERVER) { |
| 232 | workers[i]->Start(); |
| 233 | workers[i]->WaitForChannelCreation(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // now create the clients |
| 238 | for (size_t i = 0; i < workers.size(); ++i) { |
| 239 | if (workers[i]->mode() == Channel::MODE_CLIENT) |
| 240 | workers[i]->Start(); |
| 241 | } |
| 242 | |
| 243 | // wait for all the workers to finish |
| 244 | for (size_t i = 0; i < workers.size(); ++i) |
| 245 | workers[i]->done_event()->Wait(); |
| 246 | |
| 247 | STLDeleteContainerPointers(workers.begin(), workers.end()); |
| 248 | } |
| 249 | |
| 250 | } // namespace |
| 251 | |
| 252 | class IPCSyncChannelTest : public testing::Test { |
| 253 | private: |
| 254 | MessageLoop message_loop_; |
| 255 | }; |
| 256 | |
| 257 | //----------------------------------------------------------------------------- |
| 258 | |
| 259 | namespace { |
| 260 | |
| 261 | class SimpleServer : public Worker { |
| 262 | public: |
| 263 | SimpleServer(bool pump_during_send) |
| 264 | : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 265 | pump_during_send_(pump_during_send) { } |
| 266 | void Run() { |
| 267 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 268 | Done(); |
| 269 | } |
| 270 | |
| 271 | bool pump_during_send_; |
| 272 | }; |
| 273 | |
| 274 | class SimpleClient : public Worker { |
| 275 | public: |
| 276 | SimpleClient() : Worker(Channel::MODE_CLIENT, "simple_client") { } |
| 277 | |
| 278 | void OnAnswer(int* answer) { |
| 279 | *answer = 42; |
| 280 | Done(); |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | void Simple(bool pump_during_send) { |
| 285 | std::vector<Worker*> workers; |
| 286 | workers.push_back(new SimpleServer(pump_during_send)); |
| 287 | workers.push_back(new SimpleClient()); |
| 288 | RunTest(workers); |
| 289 | } |
| 290 | |
| 291 | } // namespace |
| 292 | |
| 293 | // Tests basic synchronous call |
| 294 | TEST_F(IPCSyncChannelTest, Simple) { |
| 295 | Simple(false); |
| 296 | Simple(true); |
| 297 | } |
| 298 | |
| 299 | //----------------------------------------------------------------------------- |
| 300 | |
| 301 | namespace { |
| 302 | |
| 303 | class DelayClient : public Worker { |
| 304 | public: |
| 305 | DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { } |
| 306 | |
| 307 | void OnAnswerDelay(Message* reply_msg) { |
| 308 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 309 | Send(reply_msg); |
| 310 | Done(); |
| 311 | } |
| 312 | }; |
| 313 | |
| 314 | void DelayReply(bool pump_during_send) { |
| 315 | std::vector<Worker*> workers; |
| 316 | workers.push_back(new SimpleServer(pump_during_send)); |
| 317 | workers.push_back(new DelayClient()); |
| 318 | RunTest(workers); |
| 319 | } |
| 320 | |
| 321 | } // namespace |
| 322 | |
| 323 | // Tests that asynchronous replies work |
| 324 | TEST_F(IPCSyncChannelTest, DelayReply) { |
| 325 | DelayReply(false); |
| 326 | DelayReply(true); |
| 327 | } |
| 328 | |
| 329 | //----------------------------------------------------------------------------- |
| 330 | |
| 331 | namespace { |
| 332 | |
| 333 | class NoHangServer : public Worker { |
| 334 | public: |
| 335 | explicit NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send) |
| 336 | : Worker(Channel::MODE_SERVER, "no_hang_server"), |
| 337 | got_first_reply_(got_first_reply), |
| 338 | pump_during_send_(pump_during_send) { } |
| 339 | void Run() { |
| 340 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 341 | got_first_reply_->Signal(); |
| 342 | |
| 343 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, false); |
| 344 | Done(); |
| 345 | } |
| 346 | |
| 347 | WaitableEvent* got_first_reply_; |
| 348 | bool pump_during_send_; |
| 349 | }; |
| 350 | |
| 351 | class NoHangClient : public Worker { |
| 352 | public: |
| 353 | explicit NoHangClient(WaitableEvent* got_first_reply) |
| 354 | : Worker(Channel::MODE_CLIENT, "no_hang_client"), |
| 355 | got_first_reply_(got_first_reply) { } |
| 356 | |
| 357 | virtual void OnAnswerDelay(Message* reply_msg) { |
| 358 | // Use the DELAY_REPLY macro so that we can force the reply to be sent |
| 359 | // before this function returns (when the channel will be reset). |
| 360 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 361 | Send(reply_msg); |
| 362 | got_first_reply_->Wait(); |
| 363 | CloseChannel(); |
| 364 | Done(); |
| 365 | } |
| 366 | |
| 367 | WaitableEvent* got_first_reply_; |
| 368 | }; |
| 369 | |
| 370 | void NoHang(bool pump_during_send) { |
| 371 | WaitableEvent got_first_reply(false, false); |
| 372 | std::vector<Worker*> workers; |
| 373 | workers.push_back(new NoHangServer(&got_first_reply, pump_during_send)); |
| 374 | workers.push_back(new NoHangClient(&got_first_reply)); |
| 375 | RunTest(workers); |
| 376 | } |
| 377 | |
| 378 | } // namespace |
| 379 | |
| 380 | // Tests that caller doesn't hang if receiver dies |
| 381 | TEST_F(IPCSyncChannelTest, NoHang) { |
| 382 | NoHang(false); |
| 383 | NoHang(true); |
| 384 | } |
| 385 | |
| 386 | //----------------------------------------------------------------------------- |
| 387 | |
| 388 | namespace { |
| 389 | |
| 390 | class UnblockServer : public Worker { |
| 391 | public: |
| 392 | UnblockServer(bool pump_during_send) |
| 393 | : Worker(Channel::MODE_SERVER, "unblock_server"), |
| 394 | pump_during_send_(pump_during_send) { } |
| 395 | void Run() { |
| 396 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 397 | Done(); |
| 398 | } |
| 399 | |
| 400 | void OnDouble(int in, int* out) { |
| 401 | *out = in * 2; |
| 402 | } |
| 403 | |
| 404 | bool pump_during_send_; |
| 405 | }; |
| 406 | |
| 407 | class UnblockClient : public Worker { |
| 408 | public: |
| 409 | UnblockClient(bool pump_during_send) |
| 410 | : Worker(Channel::MODE_CLIENT, "unblock_client"), |
| 411 | pump_during_send_(pump_during_send) { } |
| 412 | |
| 413 | void OnAnswer(int* answer) { |
| 414 | SendDouble(pump_during_send_, true); |
| 415 | *answer = 42; |
| 416 | Done(); |
| 417 | } |
| 418 | |
| 419 | bool pump_during_send_; |
| 420 | }; |
| 421 | |
| 422 | void Unblock(bool server_pump, bool client_pump) { |
| 423 | std::vector<Worker*> workers; |
| 424 | workers.push_back(new UnblockServer(server_pump)); |
| 425 | workers.push_back(new UnblockClient(client_pump)); |
| 426 | RunTest(workers); |
| 427 | } |
| 428 | |
| 429 | } // namespace |
| 430 | |
| 431 | // Tests that the caller unblocks to answer a sync message from the receiver. |
| 432 | TEST_F(IPCSyncChannelTest, Unblock) { |
| 433 | Unblock(false, false); |
| 434 | Unblock(false, true); |
| 435 | Unblock(true, false); |
| 436 | Unblock(true, true); |
| 437 | } |
| 438 | |
| 439 | //----------------------------------------------------------------------------- |
| 440 | |
| 441 | namespace { |
| 442 | |
| 443 | class RecursiveServer : public Worker { |
| 444 | public: |
| 445 | explicit RecursiveServer( |
| 446 | bool expected_send_result, bool pump_first, bool pump_second) |
| 447 | : Worker(Channel::MODE_SERVER, "recursive_server"), |
| 448 | expected_send_result_(expected_send_result), |
| 449 | pump_first_(pump_first), pump_second_(pump_second) { } |
| 450 | void Run() { |
| 451 | SendDouble(pump_first_, expected_send_result_); |
| 452 | Done(); |
| 453 | } |
| 454 | |
| 455 | void OnDouble(int in, int* out) { |
| 456 | *out = in * 2; |
| 457 | SendAnswerToLife(pump_second_, base::kNoTimeout, expected_send_result_); |
| 458 | } |
| 459 | |
| 460 | bool expected_send_result_, pump_first_, pump_second_; |
| 461 | }; |
| 462 | |
| 463 | class RecursiveClient : public Worker { |
| 464 | public: |
| 465 | explicit RecursiveClient(bool pump_during_send, bool close_channel) |
| 466 | : Worker(Channel::MODE_CLIENT, "recursive_client"), |
| 467 | pump_during_send_(pump_during_send), close_channel_(close_channel) { } |
| 468 | |
| 469 | void OnDoubleDelay(int in, Message* reply_msg) { |
| 470 | SendDouble(pump_during_send_, !close_channel_); |
| 471 | if (close_channel_) { |
| 472 | delete reply_msg; |
| 473 | } else { |
| 474 | SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2); |
| 475 | Send(reply_msg); |
| 476 | } |
| 477 | Done(); |
| 478 | } |
| 479 | |
| 480 | void OnAnswerDelay(Message* reply_msg) { |
| 481 | if (close_channel_) { |
| 482 | delete reply_msg; |
| 483 | CloseChannel(); |
| 484 | } else { |
| 485 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 486 | Send(reply_msg); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | bool pump_during_send_, close_channel_; |
| 491 | }; |
| 492 | |
| 493 | void Recursive( |
| 494 | bool server_pump_first, bool server_pump_second, bool client_pump) { |
| 495 | std::vector<Worker*> workers; |
| 496 | workers.push_back( |
| 497 | new RecursiveServer(true, server_pump_first, server_pump_second)); |
| 498 | workers.push_back(new RecursiveClient(client_pump, false)); |
| 499 | RunTest(workers); |
| 500 | } |
| 501 | |
| 502 | } // namespace |
| 503 | |
| 504 | // Tests a server calling Send while another Send is pending. |
| 505 | TEST_F(IPCSyncChannelTest, Recursive) { |
| 506 | Recursive(false, false, false); |
| 507 | Recursive(false, false, true); |
| 508 | Recursive(false, true, false); |
| 509 | Recursive(false, true, true); |
| 510 | Recursive(true, false, false); |
| 511 | Recursive(true, false, true); |
| 512 | Recursive(true, true, false); |
| 513 | Recursive(true, true, true); |
| 514 | } |
| 515 | |
| 516 | //----------------------------------------------------------------------------- |
| 517 | |
| 518 | namespace { |
| 519 | |
| 520 | void RecursiveNoHang( |
| 521 | bool server_pump_first, bool server_pump_second, bool client_pump) { |
| 522 | std::vector<Worker*> workers; |
| 523 | workers.push_back( |
| 524 | new RecursiveServer(false, server_pump_first, server_pump_second)); |
| 525 | workers.push_back(new RecursiveClient(client_pump, true)); |
| 526 | RunTest(workers); |
| 527 | } |
| 528 | |
| 529 | } // namespace |
| 530 | |
| 531 | // Tests that if a caller makes a sync call during an existing sync call and |
| 532 | // the receiver dies, neither of the Send() calls hang. |
| 533 | TEST_F(IPCSyncChannelTest, RecursiveNoHang) { |
| 534 | RecursiveNoHang(false, false, false); |
| 535 | RecursiveNoHang(false, false, true); |
| 536 | RecursiveNoHang(false, true, false); |
| 537 | RecursiveNoHang(false, true, true); |
| 538 | RecursiveNoHang(true, false, false); |
| 539 | RecursiveNoHang(true, false, true); |
| 540 | RecursiveNoHang(true, true, false); |
| 541 | RecursiveNoHang(true, true, true); |
| 542 | } |
| 543 | |
| 544 | //----------------------------------------------------------------------------- |
| 545 | |
| 546 | namespace { |
| 547 | |
| 548 | class MultipleServer1 : public Worker { |
| 549 | public: |
| 550 | MultipleServer1(bool pump_during_send) |
| 551 | : Worker("test_channel1", Channel::MODE_SERVER), |
| 552 | pump_during_send_(pump_during_send) { } |
| 553 | |
| 554 | void Run() { |
| 555 | SendDouble(pump_during_send_, true); |
| 556 | Done(); |
| 557 | } |
| 558 | |
| 559 | bool pump_during_send_; |
| 560 | }; |
| 561 | |
| 562 | class MultipleClient1 : public Worker { |
| 563 | public: |
| 564 | MultipleClient1(WaitableEvent* client1_msg_received, |
| 565 | WaitableEvent* client1_can_reply) : |
| 566 | Worker("test_channel1", Channel::MODE_CLIENT), |
| 567 | client1_msg_received_(client1_msg_received), |
| 568 | client1_can_reply_(client1_can_reply) { } |
| 569 | |
| 570 | void OnDouble(int in, int* out) { |
| 571 | client1_msg_received_->Signal(); |
| 572 | *out = in * 2; |
| 573 | client1_can_reply_->Wait(); |
| 574 | Done(); |
| 575 | } |
| 576 | |
| 577 | private: |
| 578 | WaitableEvent *client1_msg_received_, *client1_can_reply_; |
| 579 | }; |
| 580 | |
| 581 | class MultipleServer2 : public Worker { |
| 582 | public: |
| 583 | MultipleServer2() : Worker("test_channel2", Channel::MODE_SERVER) { } |
| 584 | |
| 585 | void OnAnswer(int* result) { |
| 586 | *result = 42; |
| 587 | Done(); |
| 588 | } |
| 589 | }; |
| 590 | |
| 591 | class MultipleClient2 : public Worker { |
| 592 | public: |
| 593 | MultipleClient2( |
| 594 | WaitableEvent* client1_msg_received, WaitableEvent* client1_can_reply, |
| 595 | bool pump_during_send) |
| 596 | : Worker("test_channel2", Channel::MODE_CLIENT), |
| 597 | client1_msg_received_(client1_msg_received), |
| 598 | client1_can_reply_(client1_can_reply), |
| 599 | pump_during_send_(pump_during_send) { } |
| 600 | |
| 601 | void Run() { |
| 602 | client1_msg_received_->Wait(); |
| 603 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 604 | client1_can_reply_->Signal(); |
| 605 | Done(); |
| 606 | } |
| 607 | |
| 608 | private: |
| 609 | WaitableEvent *client1_msg_received_, *client1_can_reply_; |
| 610 | bool pump_during_send_; |
| 611 | }; |
| 612 | |
| 613 | void Multiple(bool server_pump, bool client_pump) { |
| 614 | std::vector<Worker*> workers; |
| 615 | |
| 616 | // A shared worker thread so that server1 and server2 run on one thread. |
| 617 | base::Thread worker_thread("Multiple"); |
| 618 | ASSERT_TRUE(worker_thread.Start()); |
| 619 | |
| 620 | // Server1 sends a sync msg to client1, which blocks the reply until |
| 621 | // server2 (which runs on the same worker thread as server1) responds |
| 622 | // to a sync msg from client2. |
| 623 | WaitableEvent client1_msg_received(false, false); |
| 624 | WaitableEvent client1_can_reply(false, false); |
| 625 | |
| 626 | Worker* worker; |
| 627 | |
| 628 | worker = new MultipleServer2(); |
| 629 | worker->OverrideThread(&worker_thread); |
| 630 | workers.push_back(worker); |
| 631 | |
| 632 | worker = new MultipleClient2( |
| 633 | &client1_msg_received, &client1_can_reply, client_pump); |
| 634 | workers.push_back(worker); |
| 635 | |
| 636 | worker = new MultipleServer1(server_pump); |
| 637 | worker->OverrideThread(&worker_thread); |
| 638 | workers.push_back(worker); |
| 639 | |
| 640 | worker = new MultipleClient1( |
| 641 | &client1_msg_received, &client1_can_reply); |
| 642 | workers.push_back(worker); |
| 643 | |
| 644 | RunTest(workers); |
| 645 | } |
| 646 | |
| 647 | } // namespace |
| 648 | |
| 649 | // Tests that multiple SyncObjects on the same listener thread can unblock each |
| 650 | // other. |
| 651 | TEST_F(IPCSyncChannelTest, Multiple) { |
| 652 | Multiple(false, false); |
| 653 | Multiple(false, true); |
| 654 | Multiple(true, false); |
| 655 | Multiple(true, true); |
| 656 | } |
| 657 | |
| 658 | //----------------------------------------------------------------------------- |
| 659 | |
| 660 | namespace { |
| 661 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 662 | // This class provides server side functionality to test the case where |
| 663 | // multiple sync channels are in use on the same thread on the client and |
| 664 | // nested calls are issued. |
| 665 | class QueuedReplyServer : public Worker { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 666 | public: |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 667 | QueuedReplyServer(base::Thread* listener_thread, |
| 668 | const std::string& channel_name, |
| 669 | const std::string& reply_text) |
| 670 | : Worker(channel_name, Channel::MODE_SERVER), |
| 671 | reply_text_(reply_text) { |
| 672 | Worker::OverrideThread(listener_thread); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 673 | } |
| 674 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 675 | virtual void OnNestedTestMsg(Message* reply_msg) { |
| 676 | LOG(INFO) << __FUNCTION__ << " Sending reply: " |
| 677 | << reply_text_.c_str(); |
| 678 | SyncChannelNestedTestMsg_String::WriteReplyParams( |
| 679 | reply_msg, reply_text_); |
| 680 | Send(reply_msg); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 681 | Done(); |
| 682 | } |
| 683 | |
| 684 | private: |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 685 | std::string reply_text_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 686 | }; |
| 687 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 688 | // The QueuedReplyClient class provides functionality to test the case where |
| 689 | // multiple sync channels are in use on the same thread and they make nested |
| 690 | // sync calls, i.e. while the first channel waits for a response it makes a |
| 691 | // sync call on another channel. |
| 692 | // The callstack should unwind correctly, i.e. the outermost call should |
| 693 | // complete first, and so on. |
| 694 | class QueuedReplyClient : public Worker { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 695 | public: |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 696 | QueuedReplyClient(base::Thread* listener_thread, |
| 697 | const std::string& channel_name, |
| 698 | const std::string& expected_text, |
| 699 | bool pump_during_send) |
| 700 | : Worker(channel_name, Channel::MODE_CLIENT), |
| 701 | expected_text_(expected_text), |
| 702 | pump_during_send_(pump_during_send) { |
| 703 | Worker::OverrideThread(listener_thread); |
| 704 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 705 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 706 | virtual void Run() { |
| 707 | std::string response; |
| 708 | SyncMessage* msg = new SyncChannelNestedTestMsg_String(&response); |
| 709 | if (pump_during_send_) |
| 710 | msg->EnableMessagePumping(); |
| 711 | bool result = Send(msg); |
| 712 | DCHECK(result); |
| 713 | DCHECK(response == expected_text_); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 714 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 715 | LOG(INFO) << __FUNCTION__ << " Received reply: " |
| 716 | << response.c_str(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 717 | Done(); |
| 718 | } |
| 719 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 720 | private: |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 721 | bool pump_during_send_; |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 722 | std::string expected_text_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 723 | }; |
| 724 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 725 | void QueuedReply(bool client_pump) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 726 | std::vector<Worker*> workers; |
| 727 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 728 | // A shared worker thread for servers |
| 729 | base::Thread server_worker_thread("QueuedReply_ServerListener"); |
| 730 | ASSERT_TRUE(server_worker_thread.Start()); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 731 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 732 | base::Thread client_worker_thread("QueuedReply_ClientListener"); |
| 733 | ASSERT_TRUE(client_worker_thread.Start()); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 734 | |
| 735 | Worker* worker; |
| 736 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 737 | worker = new QueuedReplyServer(&server_worker_thread, |
| 738 | "QueuedReply_Server1", |
| 739 | "Got first message"); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 740 | workers.push_back(worker); |
| 741 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 742 | worker = new QueuedReplyServer(&server_worker_thread, |
| 743 | "QueuedReply_Server2", |
| 744 | "Got second message"); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 745 | workers.push_back(worker); |
| 746 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 747 | worker = new QueuedReplyClient(&client_worker_thread, |
| 748 | "QueuedReply_Server1", |
| 749 | "Got first message", |
| 750 | client_pump); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 751 | workers.push_back(worker); |
| 752 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 753 | worker = new QueuedReplyClient(&client_worker_thread, |
| 754 | "QueuedReply_Server2", |
| 755 | "Got second message", |
| 756 | client_pump); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 757 | workers.push_back(worker); |
| 758 | |
| 759 | RunTest(workers); |
| 760 | } |
| 761 | |
| 762 | } // namespace |
| 763 | |
| 764 | // While a blocking send is in progress, the listener thread might answer other |
| 765 | // synchronous messages. This tests that if during the response to another |
| 766 | // message the reply to the original messages comes, it is queued up correctly |
| 767 | // and the original Send is unblocked later. |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 768 | // We also test that the send call stacks unwind correctly when the channel |
| 769 | // pumps messages while waiting for a response. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 770 | TEST_F(IPCSyncChannelTest, QueuedReply) { |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 771 | QueuedReply(false); |
| 772 | QueuedReply(true); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | //----------------------------------------------------------------------------- |
| 776 | |
| 777 | namespace { |
| 778 | |
| 779 | class BadServer : public Worker { |
| 780 | public: |
| 781 | BadServer(bool pump_during_send) |
| 782 | : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 783 | pump_during_send_(pump_during_send) { } |
| 784 | void Run() { |
| 785 | int answer = 0; |
| 786 | |
| 787 | SyncMessage* msg = new SyncMessage( |
| 788 | MSG_ROUTING_CONTROL, SyncChannelTestMsg_Double::ID, |
| 789 | Message::PRIORITY_NORMAL, NULL); |
| 790 | if (pump_during_send_) |
| 791 | msg->EnableMessagePumping(); |
| 792 | |
| 793 | // Temporarily set the minimum logging very high so that the assertion |
| 794 | // in ipc_message_utils doesn't fire. |
| 795 | int log_level = logging::GetMinLogLevel(); |
| 796 | logging::SetMinLogLevel(kint32max); |
| 797 | bool result = Send(msg); |
| 798 | logging::SetMinLogLevel(log_level); |
| 799 | DCHECK(!result); |
| 800 | |
| 801 | // Need to send another message to get the client to call Done(). |
| 802 | result = Send(new SyncChannelTestMsg_AnswerToLife(&answer)); |
| 803 | DCHECK(result); |
| 804 | DCHECK(answer == 42); |
| 805 | |
| 806 | Done(); |
| 807 | } |
| 808 | |
| 809 | bool pump_during_send_; |
| 810 | }; |
| 811 | |
| 812 | void BadMessage(bool pump_during_send) { |
| 813 | std::vector<Worker*> workers; |
| 814 | workers.push_back(new BadServer(pump_during_send)); |
| 815 | workers.push_back(new SimpleClient()); |
| 816 | RunTest(workers); |
| 817 | } |
| 818 | |
| 819 | } // namespace |
| 820 | |
| 821 | // Tests that if a message is not serialized correctly, the Send() will fail. |
| 822 | TEST_F(IPCSyncChannelTest, BadMessage) { |
| 823 | BadMessage(false); |
| 824 | BadMessage(true); |
| 825 | } |
| 826 | |
| 827 | //----------------------------------------------------------------------------- |
| 828 | |
| 829 | namespace { |
| 830 | |
| 831 | class ChattyClient : public Worker { |
| 832 | public: |
| 833 | ChattyClient() : |
| 834 | Worker(Channel::MODE_CLIENT, "chatty_client") { } |
| 835 | |
| 836 | void OnAnswer(int* answer) { |
| 837 | // The PostMessage limit is 10k. Send 20% more than that. |
| 838 | const int kMessageLimit = 10000; |
| 839 | const int kMessagesToSend = kMessageLimit * 120 / 100; |
| 840 | for (int i = 0; i < kMessagesToSend; ++i) { |
| 841 | if (!SendDouble(false, true)) |
| 842 | break; |
| 843 | } |
| 844 | *answer = 42; |
| 845 | Done(); |
| 846 | } |
| 847 | }; |
| 848 | |
| 849 | void ChattyServer(bool pump_during_send) { |
| 850 | std::vector<Worker*> workers; |
| 851 | workers.push_back(new UnblockServer(pump_during_send)); |
| 852 | workers.push_back(new ChattyClient()); |
| 853 | RunTest(workers); |
| 854 | } |
| 855 | |
| 856 | } // namespace |
| 857 | |
| 858 | // Tests http://b/1093251 - that sending lots of sync messages while |
| 859 | // the receiver is waiting for a sync reply does not overflow the PostMessage |
| 860 | // queue. |
| 861 | TEST_F(IPCSyncChannelTest, ChattyServer) { |
| 862 | ChattyServer(false); |
| 863 | ChattyServer(true); |
| 864 | } |
| 865 | |
| 866 | //------------------------------------------------------------------------------ |
| 867 | |
| 868 | namespace { |
| 869 | |
| 870 | class TimeoutServer : public Worker { |
| 871 | public: |
| 872 | TimeoutServer(int timeout_ms, |
| 873 | std::vector<bool> timeout_seq, |
| 874 | bool pump_during_send) |
| 875 | : Worker(Channel::MODE_SERVER, "timeout_server"), |
| 876 | timeout_ms_(timeout_ms), |
| 877 | timeout_seq_(timeout_seq), |
| 878 | pump_during_send_(pump_during_send) { |
| 879 | } |
| 880 | |
| 881 | void Run() { |
| 882 | for (std::vector<bool>::const_iterator iter = timeout_seq_.begin(); |
| 883 | iter != timeout_seq_.end(); ++iter) { |
| 884 | SendAnswerToLife(pump_during_send_, timeout_ms_, !*iter); |
| 885 | } |
| 886 | Done(); |
| 887 | } |
| 888 | |
| 889 | private: |
| 890 | int timeout_ms_; |
| 891 | std::vector<bool> timeout_seq_; |
| 892 | bool pump_during_send_; |
| 893 | }; |
| 894 | |
| 895 | class UnresponsiveClient : public Worker { |
| 896 | public: |
| 897 | UnresponsiveClient(std::vector<bool> timeout_seq) |
| 898 | : Worker(Channel::MODE_CLIENT, "unresponsive_client"), |
| 899 | timeout_seq_(timeout_seq) { |
| 900 | } |
| 901 | |
| 902 | void OnAnswerDelay(Message* reply_msg) { |
| 903 | DCHECK(!timeout_seq_.empty()); |
| 904 | if (!timeout_seq_[0]) { |
| 905 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 906 | Send(reply_msg); |
| 907 | } else { |
| 908 | // Don't reply. |
| 909 | delete reply_msg; |
| 910 | } |
| 911 | timeout_seq_.erase(timeout_seq_.begin()); |
| 912 | if (timeout_seq_.empty()) |
| 913 | Done(); |
| 914 | } |
| 915 | |
| 916 | private: |
| 917 | // Whether we should time-out or respond to the various messages we receive. |
| 918 | std::vector<bool> timeout_seq_; |
| 919 | }; |
| 920 | |
| 921 | void SendWithTimeoutOK(bool pump_during_send) { |
| 922 | std::vector<Worker*> workers; |
| 923 | std::vector<bool> timeout_seq; |
| 924 | timeout_seq.push_back(false); |
| 925 | timeout_seq.push_back(false); |
| 926 | timeout_seq.push_back(false); |
| 927 | workers.push_back(new TimeoutServer(5000, timeout_seq, pump_during_send)); |
| 928 | workers.push_back(new SimpleClient()); |
| 929 | RunTest(workers); |
| 930 | } |
| 931 | |
| 932 | void SendWithTimeoutTimeout(bool pump_during_send) { |
| 933 | std::vector<Worker*> workers; |
| 934 | std::vector<bool> timeout_seq; |
| 935 | timeout_seq.push_back(true); |
| 936 | timeout_seq.push_back(false); |
| 937 | timeout_seq.push_back(false); |
| 938 | workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send)); |
| 939 | workers.push_back(new UnresponsiveClient(timeout_seq)); |
| 940 | RunTest(workers); |
| 941 | } |
| 942 | |
| 943 | void SendWithTimeoutMixedOKAndTimeout(bool pump_during_send) { |
| 944 | std::vector<Worker*> workers; |
| 945 | std::vector<bool> timeout_seq; |
| 946 | timeout_seq.push_back(true); |
| 947 | timeout_seq.push_back(false); |
| 948 | timeout_seq.push_back(false); |
| 949 | timeout_seq.push_back(true); |
| 950 | timeout_seq.push_back(false); |
| 951 | workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send)); |
| 952 | workers.push_back(new UnresponsiveClient(timeout_seq)); |
| 953 | RunTest(workers); |
| 954 | } |
| 955 | |
| 956 | } // namespace |
| 957 | |
| 958 | // Tests that SendWithTimeout does not time-out if the response comes back fast |
| 959 | // enough. |
| 960 | TEST_F(IPCSyncChannelTest, SendWithTimeoutOK) { |
| 961 | SendWithTimeoutOK(false); |
| 962 | SendWithTimeoutOK(true); |
| 963 | } |
| 964 | |
| 965 | // Tests that SendWithTimeout does time-out. |
| 966 | TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) { |
| 967 | SendWithTimeoutTimeout(false); |
| 968 | SendWithTimeoutTimeout(true); |
| 969 | } |
| 970 | |
| 971 | // Sends some message that time-out and some that succeed. |
| 972 | TEST_F(IPCSyncChannelTest, SendWithTimeoutMixedOKAndTimeout) { |
| 973 | SendWithTimeoutMixedOKAndTimeout(false); |
| 974 | SendWithTimeoutMixedOKAndTimeout(true); |
| 975 | } |
| 976 | |
| 977 | //------------------------------------------------------------------------------ |
| 978 | |
| 979 | namespace { |
| 980 | |
| 981 | class NestedTask : public Task { |
| 982 | public: |
| 983 | NestedTask(Worker* server) : server_(server) { } |
| 984 | void Run() { |
| 985 | // Sleep a bit so that we wake up after the reply has been received. |
| 986 | PlatformThread::Sleep(250); |
| 987 | server_->SendAnswerToLife(true, base::kNoTimeout, true); |
| 988 | } |
| 989 | |
| 990 | Worker* server_; |
| 991 | }; |
| 992 | |
| 993 | static bool timeout_occured = false; |
| 994 | |
| 995 | class TimeoutTask : public Task { |
| 996 | public: |
| 997 | void Run() { |
| 998 | timeout_occured = true; |
| 999 | } |
| 1000 | }; |
| 1001 | |
| 1002 | class DoneEventRaceServer : public Worker { |
| 1003 | public: |
| 1004 | DoneEventRaceServer() |
| 1005 | : Worker(Channel::MODE_SERVER, "done_event_race_server") { } |
| 1006 | |
| 1007 | void Run() { |
| 1008 | MessageLoop::current()->PostTask(FROM_HERE, new NestedTask(this)); |
| 1009 | MessageLoop::current()->PostDelayedTask(FROM_HERE, new TimeoutTask(), 9000); |
| 1010 | // Even though we have a timeout on the Send, it will succeed since for this |
| 1011 | // bug, the reply message comes back and is deserialized, however the done |
| 1012 | // event wasn't set. So we indirectly use the timeout task to notice if a |
| 1013 | // timeout occurred. |
| 1014 | SendAnswerToLife(true, 10000, true); |
| 1015 | DCHECK(!timeout_occured); |
| 1016 | Done(); |
| 1017 | } |
| 1018 | }; |
| 1019 | |
| 1020 | } // namespace |
| 1021 | |
| 1022 | // Tests http://b/1474092 - that if after the done_event is set but before |
| 1023 | // OnObjectSignaled is called another message is sent out, then after its |
| 1024 | // reply comes back OnObjectSignaled will be called for the first message. |
| 1025 | TEST_F(IPCSyncChannelTest, DoneEventRace) { |
| 1026 | std::vector<Worker*> workers; |
| 1027 | workers.push_back(new DoneEventRaceServer()); |
| 1028 | workers.push_back(new SimpleClient()); |
| 1029 | RunTest(workers); |
| 1030 | } |