tfarina@chromium.org | b73eaee | 2010-06-07 11:10:18 +0900 | [diff] [blame] | 1 | // Copyright (c) 2010 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" |
| 14 | #include "base/message_loop.h" |
| 15 | #include "base/platform_thread.h" |
erg@chromium.org | a752852 | 2010-07-16 02:23:23 +0900 | [diff] [blame] | 16 | #include "base/scoped_ptr.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 17 | #include "base/stl_util-inl.h" |
| 18 | #include "base/string_util.h" |
timurrrr@chromium.org | f39c3ff | 2010-05-14 17:24:42 +0900 | [diff] [blame] | 19 | #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 20 | #include "base/thread.h" |
| 21 | #include "base/waitable_event.h" |
| 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 | |
| 161 | private: |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 162 | // Called on the listener thread to create the sync channel. |
| 163 | void OnStart() { |
| 164 | // Link ipc_thread_, listener_thread_ and channel_ altogether. |
| 165 | StartThread(&ipc_thread_, MessageLoop::TYPE_IO); |
| 166 | channel_.reset(new SyncChannel( |
jam@chromium.org | e57135c | 2010-12-03 04:16:07 +0900 | [diff] [blame] | 167 | channel_name_, mode_, this, ipc_thread_.message_loop(), true, |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 168 | &shutdown_event_)); |
| 169 | channel_created_->Signal(); |
| 170 | Run(); |
| 171 | } |
| 172 | |
| 173 | void OnListenerThreadShutdown1(WaitableEvent* listener_event, |
| 174 | WaitableEvent* ipc_event) { |
| 175 | // SyncChannel needs to be destructed on the thread that it was created on. |
| 176 | channel_.reset(); |
| 177 | |
| 178 | MessageLoop::current()->RunAllPending(); |
| 179 | |
| 180 | ipc_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 181 | this, &Worker::OnIPCThreadShutdown, listener_event, ipc_event)); |
| 182 | } |
| 183 | |
| 184 | void OnIPCThreadShutdown(WaitableEvent* listener_event, |
| 185 | WaitableEvent* ipc_event) { |
| 186 | MessageLoop::current()->RunAllPending(); |
| 187 | ipc_event->Signal(); |
| 188 | |
| 189 | listener_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 190 | this, &Worker::OnListenerThreadShutdown2, listener_event)); |
| 191 | } |
| 192 | |
| 193 | void OnListenerThreadShutdown2(WaitableEvent* listener_event) { |
| 194 | MessageLoop::current()->RunAllPending(); |
| 195 | listener_event->Signal(); |
| 196 | } |
| 197 | |
| 198 | void OnMessageReceived(const Message& message) { |
| 199 | IPC_BEGIN_MESSAGE_MAP(Worker, message) |
| 200 | IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay) |
| 201 | IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife, |
| 202 | OnAnswerDelay) |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 203 | IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelNestedTestMsg_String, |
| 204 | OnNestedTestMsg) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 205 | IPC_END_MESSAGE_MAP() |
| 206 | } |
| 207 | |
| 208 | void StartThread(base::Thread* thread, MessageLoop::Type type) { |
| 209 | base::Thread::Options options; |
| 210 | options.message_loop_type = type; |
| 211 | thread->StartWithOptions(options); |
| 212 | } |
| 213 | |
| 214 | scoped_ptr<WaitableEvent> done_; |
| 215 | scoped_ptr<WaitableEvent> channel_created_; |
| 216 | std::string channel_name_; |
| 217 | Channel::Mode mode_; |
| 218 | scoped_ptr<SyncChannel> channel_; |
| 219 | base::Thread ipc_thread_; |
| 220 | base::Thread listener_thread_; |
| 221 | base::Thread* overrided_thread_; |
| 222 | |
| 223 | base::WaitableEvent shutdown_event_; |
| 224 | |
tfarina@chromium.org | b73eaee | 2010-06-07 11:10:18 +0900 | [diff] [blame] | 225 | DISALLOW_COPY_AND_ASSIGN(Worker); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | |
| 229 | // Starts the test with the given workers. This function deletes the workers |
| 230 | // when it's done. |
| 231 | void RunTest(std::vector<Worker*> workers) { |
| 232 | // First we create the workers that are channel servers, or else the other |
| 233 | // workers' channel initialization might fail because the pipe isn't created.. |
| 234 | for (size_t i = 0; i < workers.size(); ++i) { |
| 235 | if (workers[i]->mode() == Channel::MODE_SERVER) { |
| 236 | workers[i]->Start(); |
| 237 | workers[i]->WaitForChannelCreation(); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // now create the clients |
| 242 | for (size_t i = 0; i < workers.size(); ++i) { |
| 243 | if (workers[i]->mode() == Channel::MODE_CLIENT) |
| 244 | workers[i]->Start(); |
| 245 | } |
| 246 | |
| 247 | // wait for all the workers to finish |
| 248 | for (size_t i = 0; i < workers.size(); ++i) |
| 249 | workers[i]->done_event()->Wait(); |
| 250 | |
| 251 | STLDeleteContainerPointers(workers.begin(), workers.end()); |
| 252 | } |
| 253 | |
| 254 | } // namespace |
| 255 | |
| 256 | class IPCSyncChannelTest : public testing::Test { |
| 257 | private: |
| 258 | MessageLoop message_loop_; |
| 259 | }; |
| 260 | |
| 261 | //----------------------------------------------------------------------------- |
| 262 | |
| 263 | namespace { |
| 264 | |
| 265 | class SimpleServer : public Worker { |
| 266 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 267 | explicit SimpleServer(bool pump_during_send) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 268 | : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 269 | pump_during_send_(pump_during_send) { } |
| 270 | void Run() { |
| 271 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 272 | Done(); |
| 273 | } |
| 274 | |
| 275 | bool pump_during_send_; |
| 276 | }; |
| 277 | |
| 278 | class SimpleClient : public Worker { |
| 279 | public: |
| 280 | SimpleClient() : Worker(Channel::MODE_CLIENT, "simple_client") { } |
| 281 | |
| 282 | void OnAnswer(int* answer) { |
| 283 | *answer = 42; |
| 284 | Done(); |
| 285 | } |
| 286 | }; |
| 287 | |
| 288 | void Simple(bool pump_during_send) { |
| 289 | std::vector<Worker*> workers; |
| 290 | workers.push_back(new SimpleServer(pump_during_send)); |
| 291 | workers.push_back(new SimpleClient()); |
| 292 | RunTest(workers); |
| 293 | } |
| 294 | |
| 295 | } // namespace |
| 296 | |
| 297 | // Tests basic synchronous call |
| 298 | TEST_F(IPCSyncChannelTest, Simple) { |
| 299 | Simple(false); |
| 300 | Simple(true); |
| 301 | } |
| 302 | |
| 303 | //----------------------------------------------------------------------------- |
| 304 | |
| 305 | namespace { |
| 306 | |
| 307 | class DelayClient : public Worker { |
| 308 | public: |
| 309 | DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { } |
| 310 | |
| 311 | void OnAnswerDelay(Message* reply_msg) { |
| 312 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 313 | Send(reply_msg); |
| 314 | Done(); |
| 315 | } |
| 316 | }; |
| 317 | |
| 318 | void DelayReply(bool pump_during_send) { |
| 319 | std::vector<Worker*> workers; |
| 320 | workers.push_back(new SimpleServer(pump_during_send)); |
| 321 | workers.push_back(new DelayClient()); |
| 322 | RunTest(workers); |
| 323 | } |
| 324 | |
| 325 | } // namespace |
| 326 | |
| 327 | // Tests that asynchronous replies work |
| 328 | TEST_F(IPCSyncChannelTest, DelayReply) { |
| 329 | DelayReply(false); |
| 330 | DelayReply(true); |
| 331 | } |
| 332 | |
| 333 | //----------------------------------------------------------------------------- |
| 334 | |
| 335 | namespace { |
| 336 | |
| 337 | class NoHangServer : public Worker { |
| 338 | public: |
| 339 | explicit NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send) |
| 340 | : Worker(Channel::MODE_SERVER, "no_hang_server"), |
| 341 | got_first_reply_(got_first_reply), |
| 342 | pump_during_send_(pump_during_send) { } |
| 343 | void Run() { |
| 344 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 345 | got_first_reply_->Signal(); |
| 346 | |
| 347 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, false); |
| 348 | Done(); |
| 349 | } |
| 350 | |
| 351 | WaitableEvent* got_first_reply_; |
| 352 | bool pump_during_send_; |
| 353 | }; |
| 354 | |
| 355 | class NoHangClient : public Worker { |
| 356 | public: |
| 357 | explicit NoHangClient(WaitableEvent* got_first_reply) |
| 358 | : Worker(Channel::MODE_CLIENT, "no_hang_client"), |
| 359 | got_first_reply_(got_first_reply) { } |
| 360 | |
| 361 | virtual void OnAnswerDelay(Message* reply_msg) { |
| 362 | // Use the DELAY_REPLY macro so that we can force the reply to be sent |
| 363 | // before this function returns (when the channel will be reset). |
| 364 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 365 | Send(reply_msg); |
| 366 | got_first_reply_->Wait(); |
| 367 | CloseChannel(); |
| 368 | Done(); |
| 369 | } |
| 370 | |
| 371 | WaitableEvent* got_first_reply_; |
| 372 | }; |
| 373 | |
| 374 | void NoHang(bool pump_during_send) { |
| 375 | WaitableEvent got_first_reply(false, false); |
| 376 | std::vector<Worker*> workers; |
| 377 | workers.push_back(new NoHangServer(&got_first_reply, pump_during_send)); |
| 378 | workers.push_back(new NoHangClient(&got_first_reply)); |
| 379 | RunTest(workers); |
| 380 | } |
| 381 | |
| 382 | } // namespace |
| 383 | |
| 384 | // Tests that caller doesn't hang if receiver dies |
| 385 | TEST_F(IPCSyncChannelTest, NoHang) { |
| 386 | NoHang(false); |
| 387 | NoHang(true); |
| 388 | } |
| 389 | |
| 390 | //----------------------------------------------------------------------------- |
| 391 | |
| 392 | namespace { |
| 393 | |
| 394 | class UnblockServer : public Worker { |
| 395 | public: |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 396 | UnblockServer(bool pump_during_send, bool delete_during_send) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 397 | : Worker(Channel::MODE_SERVER, "unblock_server"), |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 398 | pump_during_send_(pump_during_send), |
| 399 | delete_during_send_(delete_during_send) { } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 400 | void Run() { |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 401 | if (delete_during_send_) { |
| 402 | // Use custom code since race conditions mean the answer may or may not be |
| 403 | // available. |
| 404 | int answer = 0; |
| 405 | SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer); |
| 406 | if (pump_during_send_) |
| 407 | msg->EnableMessagePumping(); |
| 408 | Send(msg); |
| 409 | } else { |
| 410 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 411 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 412 | Done(); |
| 413 | } |
| 414 | |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 415 | void OnDoubleDelay(int in, Message* reply_msg) { |
| 416 | SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2); |
| 417 | Send(reply_msg); |
| 418 | if (delete_during_send_) |
| 419 | ResetChannel(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | bool pump_during_send_; |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 423 | bool delete_during_send_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 424 | }; |
| 425 | |
| 426 | class UnblockClient : public Worker { |
| 427 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 428 | explicit UnblockClient(bool pump_during_send) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 429 | : Worker(Channel::MODE_CLIENT, "unblock_client"), |
| 430 | pump_during_send_(pump_during_send) { } |
| 431 | |
| 432 | void OnAnswer(int* answer) { |
| 433 | SendDouble(pump_during_send_, true); |
| 434 | *answer = 42; |
| 435 | Done(); |
| 436 | } |
| 437 | |
| 438 | bool pump_during_send_; |
| 439 | }; |
| 440 | |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 441 | 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] | 442 | std::vector<Worker*> workers; |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 443 | workers.push_back(new UnblockServer(server_pump, delete_during_send)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 444 | workers.push_back(new UnblockClient(client_pump)); |
| 445 | RunTest(workers); |
| 446 | } |
| 447 | |
| 448 | } // namespace |
| 449 | |
| 450 | // Tests that the caller unblocks to answer a sync message from the receiver. |
| 451 | TEST_F(IPCSyncChannelTest, Unblock) { |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 452 | Unblock(false, false, false); |
| 453 | Unblock(false, true, false); |
| 454 | Unblock(true, false, false); |
| 455 | Unblock(true, true, false); |
| 456 | } |
| 457 | |
| 458 | //----------------------------------------------------------------------------- |
| 459 | |
| 460 | // Tests that the the IPC::SyncChannel object can be deleted during a Send. |
| 461 | TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) { |
| 462 | Unblock(false, false, true); |
| 463 | Unblock(false, true, true); |
| 464 | Unblock(true, false, true); |
| 465 | Unblock(true, true, true); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | //----------------------------------------------------------------------------- |
| 469 | |
| 470 | namespace { |
| 471 | |
| 472 | class RecursiveServer : public Worker { |
| 473 | public: |
| 474 | explicit RecursiveServer( |
| 475 | bool expected_send_result, bool pump_first, bool pump_second) |
| 476 | : Worker(Channel::MODE_SERVER, "recursive_server"), |
| 477 | expected_send_result_(expected_send_result), |
| 478 | pump_first_(pump_first), pump_second_(pump_second) { } |
| 479 | void Run() { |
| 480 | SendDouble(pump_first_, expected_send_result_); |
| 481 | Done(); |
| 482 | } |
| 483 | |
| 484 | void OnDouble(int in, int* out) { |
| 485 | *out = in * 2; |
| 486 | SendAnswerToLife(pump_second_, base::kNoTimeout, expected_send_result_); |
| 487 | } |
| 488 | |
| 489 | bool expected_send_result_, pump_first_, pump_second_; |
| 490 | }; |
| 491 | |
| 492 | class RecursiveClient : public Worker { |
| 493 | public: |
| 494 | explicit RecursiveClient(bool pump_during_send, bool close_channel) |
| 495 | : Worker(Channel::MODE_CLIENT, "recursive_client"), |
| 496 | pump_during_send_(pump_during_send), close_channel_(close_channel) { } |
| 497 | |
| 498 | void OnDoubleDelay(int in, Message* reply_msg) { |
| 499 | SendDouble(pump_during_send_, !close_channel_); |
| 500 | if (close_channel_) { |
| 501 | delete reply_msg; |
| 502 | } else { |
| 503 | SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2); |
| 504 | Send(reply_msg); |
| 505 | } |
| 506 | Done(); |
| 507 | } |
| 508 | |
| 509 | void OnAnswerDelay(Message* reply_msg) { |
| 510 | if (close_channel_) { |
| 511 | delete reply_msg; |
| 512 | CloseChannel(); |
| 513 | } else { |
| 514 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 515 | Send(reply_msg); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | bool pump_during_send_, close_channel_; |
| 520 | }; |
| 521 | |
| 522 | void Recursive( |
| 523 | bool server_pump_first, bool server_pump_second, bool client_pump) { |
| 524 | std::vector<Worker*> workers; |
| 525 | workers.push_back( |
| 526 | new RecursiveServer(true, server_pump_first, server_pump_second)); |
| 527 | workers.push_back(new RecursiveClient(client_pump, false)); |
| 528 | RunTest(workers); |
| 529 | } |
| 530 | |
| 531 | } // namespace |
| 532 | |
| 533 | // Tests a server calling Send while another Send is pending. |
| 534 | TEST_F(IPCSyncChannelTest, Recursive) { |
| 535 | Recursive(false, false, false); |
| 536 | Recursive(false, false, true); |
| 537 | Recursive(false, true, false); |
| 538 | Recursive(false, true, true); |
| 539 | Recursive(true, false, false); |
| 540 | Recursive(true, false, true); |
| 541 | Recursive(true, true, false); |
| 542 | Recursive(true, true, true); |
| 543 | } |
| 544 | |
| 545 | //----------------------------------------------------------------------------- |
| 546 | |
| 547 | namespace { |
| 548 | |
| 549 | void RecursiveNoHang( |
| 550 | bool server_pump_first, bool server_pump_second, bool client_pump) { |
| 551 | std::vector<Worker*> workers; |
| 552 | workers.push_back( |
| 553 | new RecursiveServer(false, server_pump_first, server_pump_second)); |
| 554 | workers.push_back(new RecursiveClient(client_pump, true)); |
| 555 | RunTest(workers); |
| 556 | } |
| 557 | |
| 558 | } // namespace |
| 559 | |
| 560 | // Tests that if a caller makes a sync call during an existing sync call and |
| 561 | // the receiver dies, neither of the Send() calls hang. |
| 562 | TEST_F(IPCSyncChannelTest, RecursiveNoHang) { |
| 563 | RecursiveNoHang(false, false, false); |
| 564 | RecursiveNoHang(false, false, true); |
| 565 | RecursiveNoHang(false, true, false); |
| 566 | RecursiveNoHang(false, true, true); |
| 567 | RecursiveNoHang(true, false, false); |
| 568 | RecursiveNoHang(true, false, true); |
| 569 | RecursiveNoHang(true, true, false); |
| 570 | RecursiveNoHang(true, true, true); |
| 571 | } |
| 572 | |
| 573 | //----------------------------------------------------------------------------- |
| 574 | |
| 575 | namespace { |
| 576 | |
| 577 | class MultipleServer1 : public Worker { |
| 578 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 579 | explicit MultipleServer1(bool pump_during_send) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 580 | : Worker("test_channel1", Channel::MODE_SERVER), |
| 581 | pump_during_send_(pump_during_send) { } |
| 582 | |
| 583 | void Run() { |
| 584 | SendDouble(pump_during_send_, true); |
| 585 | Done(); |
| 586 | } |
| 587 | |
| 588 | bool pump_during_send_; |
| 589 | }; |
| 590 | |
| 591 | class MultipleClient1 : public Worker { |
| 592 | public: |
| 593 | MultipleClient1(WaitableEvent* client1_msg_received, |
| 594 | WaitableEvent* client1_can_reply) : |
| 595 | Worker("test_channel1", Channel::MODE_CLIENT), |
| 596 | client1_msg_received_(client1_msg_received), |
| 597 | client1_can_reply_(client1_can_reply) { } |
| 598 | |
| 599 | void OnDouble(int in, int* out) { |
| 600 | client1_msg_received_->Signal(); |
| 601 | *out = in * 2; |
| 602 | client1_can_reply_->Wait(); |
| 603 | Done(); |
| 604 | } |
| 605 | |
| 606 | private: |
| 607 | WaitableEvent *client1_msg_received_, *client1_can_reply_; |
| 608 | }; |
| 609 | |
| 610 | class MultipleServer2 : public Worker { |
| 611 | public: |
| 612 | MultipleServer2() : Worker("test_channel2", Channel::MODE_SERVER) { } |
| 613 | |
| 614 | void OnAnswer(int* result) { |
| 615 | *result = 42; |
| 616 | Done(); |
| 617 | } |
| 618 | }; |
| 619 | |
| 620 | class MultipleClient2 : public Worker { |
| 621 | public: |
| 622 | MultipleClient2( |
| 623 | WaitableEvent* client1_msg_received, WaitableEvent* client1_can_reply, |
| 624 | bool pump_during_send) |
| 625 | : Worker("test_channel2", Channel::MODE_CLIENT), |
| 626 | client1_msg_received_(client1_msg_received), |
| 627 | client1_can_reply_(client1_can_reply), |
| 628 | pump_during_send_(pump_during_send) { } |
| 629 | |
| 630 | void Run() { |
| 631 | client1_msg_received_->Wait(); |
| 632 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 633 | client1_can_reply_->Signal(); |
| 634 | Done(); |
| 635 | } |
| 636 | |
| 637 | private: |
| 638 | WaitableEvent *client1_msg_received_, *client1_can_reply_; |
| 639 | bool pump_during_send_; |
| 640 | }; |
| 641 | |
| 642 | void Multiple(bool server_pump, bool client_pump) { |
| 643 | std::vector<Worker*> workers; |
| 644 | |
| 645 | // A shared worker thread so that server1 and server2 run on one thread. |
| 646 | base::Thread worker_thread("Multiple"); |
| 647 | ASSERT_TRUE(worker_thread.Start()); |
| 648 | |
| 649 | // Server1 sends a sync msg to client1, which blocks the reply until |
| 650 | // server2 (which runs on the same worker thread as server1) responds |
| 651 | // to a sync msg from client2. |
| 652 | WaitableEvent client1_msg_received(false, false); |
| 653 | WaitableEvent client1_can_reply(false, false); |
| 654 | |
| 655 | Worker* worker; |
| 656 | |
| 657 | worker = new MultipleServer2(); |
| 658 | worker->OverrideThread(&worker_thread); |
| 659 | workers.push_back(worker); |
| 660 | |
| 661 | worker = new MultipleClient2( |
| 662 | &client1_msg_received, &client1_can_reply, client_pump); |
| 663 | workers.push_back(worker); |
| 664 | |
| 665 | worker = new MultipleServer1(server_pump); |
| 666 | worker->OverrideThread(&worker_thread); |
| 667 | workers.push_back(worker); |
| 668 | |
| 669 | worker = new MultipleClient1( |
| 670 | &client1_msg_received, &client1_can_reply); |
| 671 | workers.push_back(worker); |
| 672 | |
| 673 | RunTest(workers); |
| 674 | } |
| 675 | |
| 676 | } // namespace |
| 677 | |
| 678 | // Tests that multiple SyncObjects on the same listener thread can unblock each |
| 679 | // other. |
| 680 | TEST_F(IPCSyncChannelTest, Multiple) { |
| 681 | Multiple(false, false); |
| 682 | Multiple(false, true); |
| 683 | Multiple(true, false); |
| 684 | Multiple(true, true); |
| 685 | } |
| 686 | |
| 687 | //----------------------------------------------------------------------------- |
| 688 | |
| 689 | namespace { |
| 690 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 691 | // This class provides server side functionality to test the case where |
| 692 | // multiple sync channels are in use on the same thread on the client and |
| 693 | // nested calls are issued. |
| 694 | class QueuedReplyServer : public Worker { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 695 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 696 | QueuedReplyServer(base::Thread* listener_thread, |
| 697 | const std::string& channel_name, |
| 698 | const std::string& reply_text) |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 699 | : Worker(channel_name, Channel::MODE_SERVER), |
| 700 | reply_text_(reply_text) { |
| 701 | Worker::OverrideThread(listener_thread); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 702 | } |
| 703 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 704 | virtual void OnNestedTestMsg(Message* reply_msg) { |
pkasting@chromium.org | fcdd54b | 2010-10-20 08:50:00 +0900 | [diff] [blame] | 705 | VLOG(1) << __FUNCTION__ << " Sending reply: " << reply_text_; |
| 706 | SyncChannelNestedTestMsg_String::WriteReplyParams(reply_msg, reply_text_); |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 707 | Send(reply_msg); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 708 | Done(); |
| 709 | } |
| 710 | |
| 711 | private: |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 712 | std::string reply_text_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 713 | }; |
| 714 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 715 | // The QueuedReplyClient class provides functionality to test the case where |
| 716 | // multiple sync channels are in use on the same thread and they make nested |
| 717 | // sync calls, i.e. while the first channel waits for a response it makes a |
| 718 | // sync call on another channel. |
| 719 | // The callstack should unwind correctly, i.e. the outermost call should |
| 720 | // complete first, and so on. |
| 721 | class QueuedReplyClient : public Worker { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 722 | public: |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 723 | QueuedReplyClient(base::Thread* listener_thread, |
| 724 | const std::string& channel_name, |
| 725 | const std::string& expected_text, |
| 726 | bool pump_during_send) |
| 727 | : Worker(channel_name, Channel::MODE_CLIENT), |
thomasvl@google.com | 9a24207 | 2010-07-23 23:18:59 +0900 | [diff] [blame] | 728 | pump_during_send_(pump_during_send), |
| 729 | expected_text_(expected_text) { |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 730 | Worker::OverrideThread(listener_thread); |
| 731 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 732 | |
ananta@chromium.org | 31b338f | 2009-10-15 01:22:02 +0900 | [diff] [blame] | 733 | virtual void Run() { |
| 734 | std::string response; |
| 735 | SyncMessage* msg = new SyncChannelNestedTestMsg_String(&response); |
| 736 | if (pump_during_send_) |
| 737 | msg->EnableMessagePumping(); |
| 738 | bool result = Send(msg); |
| 739 | DCHECK(result); |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 740 | DCHECK_EQ(response, expected_text_); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 741 | |
pkasting@chromium.org | fcdd54b | 2010-10-20 08:50:00 +0900 | [diff] [blame] | 742 | VLOG(1) << __FUNCTION__ << " Received reply: " << response; |
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 | |
akalin@chromium.org | 434c5b6 | 2010-11-03 14:30:14 +0900 | [diff] [blame] | 805 | void DropAssert(const std::string&) {} |
| 806 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 807 | class BadServer : public Worker { |
| 808 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 809 | explicit BadServer(bool pump_during_send) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 810 | : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 811 | pump_during_send_(pump_during_send) { } |
| 812 | void Run() { |
| 813 | int answer = 0; |
| 814 | |
| 815 | SyncMessage* msg = new SyncMessage( |
| 816 | MSG_ROUTING_CONTROL, SyncChannelTestMsg_Double::ID, |
| 817 | Message::PRIORITY_NORMAL, NULL); |
| 818 | if (pump_during_send_) |
| 819 | msg->EnableMessagePumping(); |
| 820 | |
akalin@chromium.org | 434c5b6 | 2010-11-03 14:30:14 +0900 | [diff] [blame] | 821 | // Temporarily ignore asserts so that the assertion in |
| 822 | // ipc_message_utils doesn't cause termination. |
| 823 | logging::SetLogAssertHandler(&DropAssert); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 824 | bool result = Send(msg); |
akalin@chromium.org | 434c5b6 | 2010-11-03 14:30:14 +0900 | [diff] [blame] | 825 | logging::SetLogAssertHandler(NULL); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 826 | DCHECK(!result); |
| 827 | |
| 828 | // Need to send another message to get the client to call Done(). |
| 829 | result = Send(new SyncChannelTestMsg_AnswerToLife(&answer)); |
| 830 | DCHECK(result); |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 831 | DCHECK_EQ(answer, 42); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 832 | |
| 833 | Done(); |
| 834 | } |
| 835 | |
| 836 | bool pump_during_send_; |
| 837 | }; |
| 838 | |
| 839 | void BadMessage(bool pump_during_send) { |
| 840 | std::vector<Worker*> workers; |
| 841 | workers.push_back(new BadServer(pump_during_send)); |
| 842 | workers.push_back(new SimpleClient()); |
| 843 | RunTest(workers); |
| 844 | } |
| 845 | |
| 846 | } // namespace |
| 847 | |
| 848 | // Tests that if a message is not serialized correctly, the Send() will fail. |
| 849 | TEST_F(IPCSyncChannelTest, BadMessage) { |
| 850 | BadMessage(false); |
| 851 | BadMessage(true); |
| 852 | } |
| 853 | |
| 854 | //----------------------------------------------------------------------------- |
| 855 | |
| 856 | namespace { |
| 857 | |
| 858 | class ChattyClient : public Worker { |
| 859 | public: |
| 860 | ChattyClient() : |
| 861 | Worker(Channel::MODE_CLIENT, "chatty_client") { } |
| 862 | |
| 863 | void OnAnswer(int* answer) { |
| 864 | // The PostMessage limit is 10k. Send 20% more than that. |
| 865 | const int kMessageLimit = 10000; |
| 866 | const int kMessagesToSend = kMessageLimit * 120 / 100; |
| 867 | for (int i = 0; i < kMessagesToSend; ++i) { |
| 868 | if (!SendDouble(false, true)) |
| 869 | break; |
| 870 | } |
| 871 | *answer = 42; |
| 872 | Done(); |
| 873 | } |
| 874 | }; |
| 875 | |
| 876 | void ChattyServer(bool pump_during_send) { |
| 877 | std::vector<Worker*> workers; |
jam@chromium.org | ebd0718 | 2009-12-01 11:34:18 +0900 | [diff] [blame] | 878 | workers.push_back(new UnblockServer(pump_during_send, false)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 879 | workers.push_back(new ChattyClient()); |
| 880 | RunTest(workers); |
| 881 | } |
| 882 | |
| 883 | } // namespace |
| 884 | |
| 885 | // Tests http://b/1093251 - that sending lots of sync messages while |
| 886 | // the receiver is waiting for a sync reply does not overflow the PostMessage |
| 887 | // queue. |
| 888 | TEST_F(IPCSyncChannelTest, ChattyServer) { |
| 889 | ChattyServer(false); |
| 890 | ChattyServer(true); |
| 891 | } |
| 892 | |
| 893 | //------------------------------------------------------------------------------ |
| 894 | |
| 895 | namespace { |
| 896 | |
| 897 | class TimeoutServer : public Worker { |
| 898 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 899 | TimeoutServer(int timeout_ms, |
| 900 | std::vector<bool> timeout_seq, |
| 901 | bool pump_during_send) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 902 | : Worker(Channel::MODE_SERVER, "timeout_server"), |
| 903 | timeout_ms_(timeout_ms), |
| 904 | timeout_seq_(timeout_seq), |
| 905 | pump_during_send_(pump_during_send) { |
| 906 | } |
| 907 | |
| 908 | void Run() { |
| 909 | for (std::vector<bool>::const_iterator iter = timeout_seq_.begin(); |
| 910 | iter != timeout_seq_.end(); ++iter) { |
| 911 | SendAnswerToLife(pump_during_send_, timeout_ms_, !*iter); |
| 912 | } |
| 913 | Done(); |
| 914 | } |
| 915 | |
| 916 | private: |
| 917 | int timeout_ms_; |
| 918 | std::vector<bool> timeout_seq_; |
| 919 | bool pump_during_send_; |
| 920 | }; |
| 921 | |
| 922 | class UnresponsiveClient : public Worker { |
| 923 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 924 | explicit UnresponsiveClient(std::vector<bool> timeout_seq) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 925 | : Worker(Channel::MODE_CLIENT, "unresponsive_client"), |
| 926 | timeout_seq_(timeout_seq) { |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 927 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 928 | |
| 929 | void OnAnswerDelay(Message* reply_msg) { |
| 930 | DCHECK(!timeout_seq_.empty()); |
| 931 | if (!timeout_seq_[0]) { |
| 932 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 933 | Send(reply_msg); |
| 934 | } else { |
| 935 | // Don't reply. |
| 936 | delete reply_msg; |
| 937 | } |
| 938 | timeout_seq_.erase(timeout_seq_.begin()); |
| 939 | if (timeout_seq_.empty()) |
| 940 | Done(); |
| 941 | } |
| 942 | |
| 943 | private: |
| 944 | // Whether we should time-out or respond to the various messages we receive. |
| 945 | std::vector<bool> timeout_seq_; |
| 946 | }; |
| 947 | |
| 948 | void SendWithTimeoutOK(bool pump_during_send) { |
| 949 | std::vector<Worker*> workers; |
| 950 | std::vector<bool> timeout_seq; |
| 951 | timeout_seq.push_back(false); |
| 952 | timeout_seq.push_back(false); |
| 953 | timeout_seq.push_back(false); |
| 954 | workers.push_back(new TimeoutServer(5000, timeout_seq, pump_during_send)); |
| 955 | workers.push_back(new SimpleClient()); |
| 956 | RunTest(workers); |
| 957 | } |
| 958 | |
| 959 | void SendWithTimeoutTimeout(bool pump_during_send) { |
| 960 | std::vector<Worker*> workers; |
| 961 | std::vector<bool> timeout_seq; |
| 962 | timeout_seq.push_back(true); |
| 963 | timeout_seq.push_back(false); |
| 964 | timeout_seq.push_back(false); |
| 965 | workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send)); |
| 966 | workers.push_back(new UnresponsiveClient(timeout_seq)); |
| 967 | RunTest(workers); |
| 968 | } |
| 969 | |
| 970 | void SendWithTimeoutMixedOKAndTimeout(bool pump_during_send) { |
| 971 | std::vector<Worker*> workers; |
| 972 | std::vector<bool> timeout_seq; |
| 973 | timeout_seq.push_back(true); |
| 974 | timeout_seq.push_back(false); |
| 975 | timeout_seq.push_back(false); |
| 976 | timeout_seq.push_back(true); |
| 977 | timeout_seq.push_back(false); |
| 978 | workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send)); |
| 979 | workers.push_back(new UnresponsiveClient(timeout_seq)); |
| 980 | RunTest(workers); |
| 981 | } |
| 982 | |
| 983 | } // namespace |
| 984 | |
| 985 | // Tests that SendWithTimeout does not time-out if the response comes back fast |
| 986 | // enough. |
| 987 | TEST_F(IPCSyncChannelTest, SendWithTimeoutOK) { |
| 988 | SendWithTimeoutOK(false); |
| 989 | SendWithTimeoutOK(true); |
| 990 | } |
| 991 | |
| 992 | // Tests that SendWithTimeout does time-out. |
| 993 | TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) { |
| 994 | SendWithTimeoutTimeout(false); |
| 995 | SendWithTimeoutTimeout(true); |
| 996 | } |
| 997 | |
| 998 | // Sends some message that time-out and some that succeed. |
| 999 | TEST_F(IPCSyncChannelTest, SendWithTimeoutMixedOKAndTimeout) { |
| 1000 | SendWithTimeoutMixedOKAndTimeout(false); |
| 1001 | SendWithTimeoutMixedOKAndTimeout(true); |
| 1002 | } |
| 1003 | |
| 1004 | //------------------------------------------------------------------------------ |
| 1005 | |
| 1006 | namespace { |
| 1007 | |
| 1008 | class NestedTask : public Task { |
| 1009 | public: |
thestig@chromium.org | 60bd7c0 | 2010-07-23 14:23:13 +0900 | [diff] [blame] | 1010 | explicit NestedTask(Worker* server) : server_(server) { } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1011 | void Run() { |
| 1012 | // Sleep a bit so that we wake up after the reply has been received. |
| 1013 | PlatformThread::Sleep(250); |
| 1014 | server_->SendAnswerToLife(true, base::kNoTimeout, true); |
| 1015 | } |
| 1016 | |
| 1017 | Worker* server_; |
| 1018 | }; |
| 1019 | |
| 1020 | static bool timeout_occured = false; |
| 1021 | |
| 1022 | class TimeoutTask : public Task { |
| 1023 | public: |
| 1024 | void Run() { |
| 1025 | timeout_occured = true; |
| 1026 | } |
| 1027 | }; |
| 1028 | |
| 1029 | class DoneEventRaceServer : public Worker { |
| 1030 | public: |
| 1031 | DoneEventRaceServer() |
| 1032 | : Worker(Channel::MODE_SERVER, "done_event_race_server") { } |
| 1033 | |
| 1034 | void Run() { |
| 1035 | MessageLoop::current()->PostTask(FROM_HERE, new NestedTask(this)); |
| 1036 | MessageLoop::current()->PostDelayedTask(FROM_HERE, new TimeoutTask(), 9000); |
| 1037 | // Even though we have a timeout on the Send, it will succeed since for this |
| 1038 | // bug, the reply message comes back and is deserialized, however the done |
| 1039 | // event wasn't set. So we indirectly use the timeout task to notice if a |
| 1040 | // timeout occurred. |
| 1041 | SendAnswerToLife(true, 10000, true); |
| 1042 | DCHECK(!timeout_occured); |
| 1043 | Done(); |
| 1044 | } |
| 1045 | }; |
| 1046 | |
| 1047 | } // namespace |
| 1048 | |
| 1049 | // Tests http://b/1474092 - that if after the done_event is set but before |
| 1050 | // OnObjectSignaled is called another message is sent out, then after its |
| 1051 | // reply comes back OnObjectSignaled will be called for the first message. |
| 1052 | TEST_F(IPCSyncChannelTest, DoneEventRace) { |
| 1053 | std::vector<Worker*> workers; |
| 1054 | workers.push_back(new DoneEventRaceServer()); |
| 1055 | workers.push_back(new SimpleClient()); |
| 1056 | RunTest(workers); |
| 1057 | } |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 1058 | |
| 1059 | //----------------------------------------------------------------------------- |
| 1060 | |
| 1061 | namespace { |
| 1062 | |
| 1063 | class TestSyncMessageFilter : public IPC::SyncMessageFilter { |
| 1064 | public: |
| 1065 | TestSyncMessageFilter(base::WaitableEvent* shutdown_event, Worker* worker) |
| 1066 | : SyncMessageFilter(shutdown_event), |
| 1067 | worker_(worker), |
| 1068 | thread_("helper_thread") { |
| 1069 | base::Thread::Options options; |
| 1070 | options.message_loop_type = MessageLoop::TYPE_DEFAULT; |
| 1071 | thread_.StartWithOptions(options); |
| 1072 | } |
| 1073 | |
| 1074 | virtual void OnFilterAdded(Channel* channel) { |
| 1075 | SyncMessageFilter::OnFilterAdded(channel); |
| 1076 | thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 1077 | this, &TestSyncMessageFilter::SendMessageOnHelperThread)); |
| 1078 | } |
| 1079 | |
| 1080 | void SendMessageOnHelperThread() { |
| 1081 | int answer = 0; |
| 1082 | bool result = Send(new SyncChannelTestMsg_AnswerToLife(&answer)); |
| 1083 | DCHECK(result); |
| 1084 | DCHECK_EQ(answer, 42); |
| 1085 | |
| 1086 | worker_->Done(); |
| 1087 | } |
| 1088 | |
| 1089 | Worker* worker_; |
| 1090 | base::Thread thread_; |
| 1091 | }; |
| 1092 | |
| 1093 | class SyncMessageFilterServer : public Worker { |
| 1094 | public: |
| 1095 | SyncMessageFilterServer() |
| 1096 | : Worker(Channel::MODE_SERVER, "sync_message_filter_server") { |
| 1097 | filter_ = new TestSyncMessageFilter(shutdown_event(), this); |
| 1098 | } |
| 1099 | |
| 1100 | void Run() { |
| 1101 | channel()->AddFilter(filter_.get()); |
| 1102 | } |
| 1103 | |
| 1104 | scoped_refptr<TestSyncMessageFilter> filter_; |
| 1105 | }; |
| 1106 | |
ananta@chromium.org | 999f297 | 2010-09-03 06:45:50 +0900 | [diff] [blame] | 1107 | // This class provides functionality to test the case that a Send on the sync |
| 1108 | // channel does not crash after the channel has been closed. |
| 1109 | class ServerSendAfterClose : public Worker { |
| 1110 | public: |
| 1111 | ServerSendAfterClose() |
| 1112 | : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 1113 | send_result_(true) { |
| 1114 | } |
| 1115 | |
| 1116 | bool SendDummy() { |
| 1117 | ListenerThread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 1118 | this, &ServerSendAfterClose::Send, new SyncChannelTestMsg_NoArgs)); |
| 1119 | return true; |
| 1120 | } |
| 1121 | |
| 1122 | bool send_result() const { |
| 1123 | return send_result_; |
| 1124 | } |
| 1125 | |
| 1126 | private: |
| 1127 | virtual void Run() { |
| 1128 | CloseChannel(); |
| 1129 | Done(); |
| 1130 | } |
| 1131 | |
| 1132 | bool Send(Message* msg) { |
| 1133 | send_result_ = Worker::Send(msg); |
| 1134 | Done(); |
| 1135 | return send_result_; |
| 1136 | } |
| 1137 | |
| 1138 | bool send_result_; |
| 1139 | }; |
| 1140 | |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 1141 | } // namespace |
| 1142 | |
| 1143 | // Tests basic synchronous call |
| 1144 | TEST_F(IPCSyncChannelTest, SyncMessageFilter) { |
| 1145 | std::vector<Worker*> workers; |
| 1146 | workers.push_back(new SyncMessageFilterServer()); |
| 1147 | workers.push_back(new SimpleClient()); |
| 1148 | RunTest(workers); |
| 1149 | } |
ananta@chromium.org | 999f297 | 2010-09-03 06:45:50 +0900 | [diff] [blame] | 1150 | |
| 1151 | // Test the case when the channel is closed and a Send is attempted after that. |
| 1152 | TEST_F(IPCSyncChannelTest, SendAfterClose) { |
| 1153 | ServerSendAfterClose server; |
| 1154 | server.Start(); |
| 1155 | |
| 1156 | server.done_event()->Wait(); |
| 1157 | server.done_event()->Reset(); |
| 1158 | |
| 1159 | server.SendDummy(); |
| 1160 | server.done_event()->Wait(); |
| 1161 | |
| 1162 | EXPECT_FALSE(server.send_result()); |
| 1163 | } |
| 1164 | |
| 1165 | |