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