[email protected] | 73a797fb | 2010-06-07 02:10:18 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | // |
| 5 | // Unit test for SyncChannel. |
| 6 | |
[email protected] | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame^] | 7 | #include "ipc/ipc_sync_channel.h" |
| 8 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [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" |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 15 | #include "base/platform_thread.h" |
[email protected] | 5097dc8 | 2010-07-15 17:23:23 | [diff] [blame] | 16 | #include "base/scoped_ptr.h" |
[email protected] | 80720414 | 2009-05-05 03:31:44 | [diff] [blame] | 17 | #include "base/stl_util-inl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | #include "base/string_util.h" |
[email protected] | ee85751 | 2010-05-14 08:24:42 | [diff] [blame] | 19 | #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | #include "base/thread.h" |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 21 | #include "base/waitable_event.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 22 | #include "ipc/ipc_message.h" |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 23 | #include "ipc/ipc_sync_message_filter.h" |
[email protected] | 21fa3a1 | 2010-12-08 23:34:16 | [diff] [blame] | 24 | #include "ipc/ipc_sync_message_unittest.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | #include "testing/gtest/include/gtest/gtest.h" |
| 26 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | using namespace IPC; |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 28 | using base::WaitableEvent; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 30 | namespace { |
| 31 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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) |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 37 | : done_(new WaitableEvent(false, false)), |
| 38 | channel_created_(new WaitableEvent(false, false)), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | mode_(mode), |
| 40 | ipc_thread_((thread_name + "_ipc").c_str()), |
| 41 | listener_thread_((thread_name + "_listener").c_str()), |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 42 | overrided_thread_(NULL), |
[email protected] | 8a73442 | 2009-10-27 11:28:58 | [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 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | |
| 51 | // Will create a named channel and use this name for the threads' name. |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 52 | Worker(const std::string& channel_name, Channel::Mode mode) |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 53 | : done_(new WaitableEvent(false, false)), |
| 54 | channel_created_(new WaitableEvent(false, false)), |
| 55 | channel_name_(channel_name), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | mode_(mode), |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 57 | ipc_thread_((channel_name + "_ipc").c_str()), |
| 58 | listener_thread_((channel_name + "_listener").c_str()), |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 59 | overrided_thread_(NULL), |
[email protected] | 8a73442 | 2009-10-27 11:28:58 | [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 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | |
| 68 | // The IPC thread needs to outlive SyncChannel, so force the correct order of |
| 69 | // destruction. |
| 70 | virtual ~Worker() { |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 71 | WaitableEvent listener_done(false, false), ipc_done(false, false); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 72 | ListenerThread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
[email protected] | 9d4ff5ed | 2009-03-03 00:21:56 | [diff] [blame] | 73 | this, &Worker::OnListenerThreadShutdown1, &listener_done, |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 74 | &ipc_done)); |
| 75 | listener_done.Wait(); |
| 76 | ipc_done.Wait(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | ipc_thread_.Stop(); |
| 78 | listener_thread_.Stop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | } |
| 80 | void AddRef() { } |
| 81 | void Release() { } |
[email protected] | 39fe32a | 2009-09-30 04:29:20 | [diff] [blame] | 82 | static bool ImplementsThreadSafeReferenceCounting() { return true; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | bool Send(Message* msg) { return channel_->Send(msg); } |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 84 | bool SendWithTimeout(Message* msg, int timeout_ms) { |
| 85 | return channel_->SendWithTimeout(msg, timeout_ms); |
| 86 | } |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 87 | void WaitForChannelCreation() { channel_created_->Wait(); } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 88 | void CloseChannel() { |
| 89 | DCHECK(MessageLoop::current() == ListenerThread()->message_loop()); |
| 90 | channel_->Close(); |
| 91 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | void Start() { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 93 | StartThread(&listener_thread_, MessageLoop::TYPE_DEFAULT); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 94 | ListenerThread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | this, &Worker::OnStart)); |
| 96 | } |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 97 | void OverrideThread(base::Thread* overrided_thread) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | DCHECK(overrided_thread_ == NULL); |
| 99 | overrided_thread_ = overrided_thread; |
| 100 | } |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 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); |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 107 | DCHECK_EQ(result, succeed); |
| 108 | DCHECK_EQ(answer, (succeed ? 42 : 0)); |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [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); |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 117 | DCHECK_EQ(result, succeed); |
| 118 | DCHECK_EQ(answer, (succeed ? 10 : 0)); |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 119 | return result; |
| 120 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | Channel::Mode mode() { return mode_; } |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 122 | WaitableEvent* done_event() { return done_.get(); } |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 123 | WaitableEvent* shutdown_event() { return &shutdown_event_; } |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 124 | void ResetChannel() { channel_.reset(); } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | // Derived classes need to call this when they've completed their part of |
| 126 | // the test. |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 127 | void Done() { done_->Signal(); } |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 128 | |
| 129 | protected: |
| 130 | IPC::SyncChannel* channel() { return channel_.get(); } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | // Functions for dervied classes to implement if they wish. |
| 132 | virtual void Run() { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 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 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 153 | virtual void OnNestedTestMsg(Message* reply_msg) { |
| 154 | NOTREACHED(); |
| 155 | } |
| 156 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 157 | base::Thread* ListenerThread() { |
| 158 | return overrided_thread_ ? overrided_thread_ : &listener_thread_; |
| 159 | } |
[email protected] | 87339f0 | 2010-09-02 21:45:50 | [diff] [blame] | 160 | |
| 161 | private: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | // Called on the listener thread to create the sync channel. |
| 163 | void OnStart() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | // Link ipc_thread_, listener_thread_ and channel_ altogether. |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 165 | StartThread(&ipc_thread_, MessageLoop::TYPE_IO); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 166 | channel_.reset(new SyncChannel( |
[email protected] | 4b580bf | 2010-12-02 19:16:07 | [diff] [blame] | 167 | channel_name_, mode_, this, ipc_thread_.message_loop(), true, |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 168 | &shutdown_event_)); |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 169 | channel_created_->Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 170 | Run(); |
| 171 | } |
| 172 | |
[email protected] | 9d4ff5ed | 2009-03-03 00:21:56 | [diff] [blame] | 173 | void OnListenerThreadShutdown1(WaitableEvent* listener_event, |
| 174 | WaitableEvent* ipc_event) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 175 | // SyncChannel needs to be destructed on the thread that it was created on. |
| 176 | channel_.reset(); |
[email protected] | 9d4ff5ed | 2009-03-03 00:21:56 | [diff] [blame] | 177 | |
| 178 | MessageLoop::current()->RunAllPending(); |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 179 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 180 | ipc_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
[email protected] | 9d4ff5ed | 2009-03-03 00:21:56 | [diff] [blame] | 181 | this, &Worker::OnIPCThreadShutdown, listener_event, ipc_event)); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 182 | } |
| 183 | |
[email protected] | 9d4ff5ed | 2009-03-03 00:21:56 | [diff] [blame] | 184 | void OnIPCThreadShutdown(WaitableEvent* listener_event, |
| 185 | WaitableEvent* ipc_event) { |
| 186 | MessageLoop::current()->RunAllPending(); |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 187 | ipc_event->Signal(); |
[email protected] | 9d4ff5ed | 2009-03-03 00:21:56 | [diff] [blame] | 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(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 196 | } |
| 197 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 198 | void OnMessageReceived(const Message& message) { |
| 199 | IPC_BEGIN_MESSAGE_MAP(Worker, message) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 200 | IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 201 | IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife, |
| 202 | OnAnswerDelay) |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 203 | IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelNestedTestMsg_String, |
| 204 | OnNestedTestMsg) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | IPC_END_MESSAGE_MAP() |
| 206 | } |
| 207 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 208 | void StartThread(base::Thread* thread, MessageLoop::Type type) { |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 209 | base::Thread::Options options; |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 210 | options.message_loop_type = type; |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 211 | thread->StartWithOptions(options); |
| 212 | } |
| 213 | |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 214 | scoped_ptr<WaitableEvent> done_; |
| 215 | scoped_ptr<WaitableEvent> channel_created_; |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 216 | std::string channel_name_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 217 | Channel::Mode mode_; |
| 218 | scoped_ptr<SyncChannel> channel_; |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 219 | base::Thread ipc_thread_; |
| 220 | base::Thread listener_thread_; |
| 221 | base::Thread* overrided_thread_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 223 | base::WaitableEvent shutdown_event_; |
| 224 | |
[email protected] | 73a797fb | 2010-06-07 02:10:18 | [diff] [blame] | 225 | DISALLOW_COPY_AND_ASSIGN(Worker); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [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) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | for (size_t i = 0; i < workers.size(); ++i) |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 249 | workers[i]->done_event()->Wait(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | |
[email protected] | 82e5ee8 | 2009-04-03 02:29:45 | [diff] [blame] | 251 | STLDeleteContainerPointers(workers.begin(), workers.end()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 252 | } |
| 253 | |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 254 | } // namespace |
| 255 | |
[email protected] | 9629c0e | 2009-02-04 23:16:29 | [diff] [blame] | 256 | class IPCSyncChannelTest : public testing::Test { |
| 257 | private: |
| 258 | MessageLoop message_loop_; |
| 259 | }; |
| 260 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 261 | //----------------------------------------------------------------------------- |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 262 | |
| 263 | namespace { |
| 264 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 265 | class SimpleServer : public Worker { |
| 266 | public: |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 267 | explicit SimpleServer(bool pump_during_send) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 268 | : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 269 | pump_during_send_(pump_during_send) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 270 | void Run() { |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 271 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 272 | Done(); |
| 273 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 274 | |
| 275 | bool pump_during_send_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 288 | void Simple(bool pump_during_send) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 289 | std::vector<Worker*> workers; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 290 | workers.push_back(new SimpleServer(pump_during_send)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 291 | workers.push_back(new SimpleClient()); |
| 292 | RunTest(workers); |
| 293 | } |
| 294 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 295 | } // namespace |
| 296 | |
| 297 | // Tests basic synchronous call |
| 298 | TEST_F(IPCSyncChannelTest, Simple) { |
| 299 | Simple(false); |
| 300 | Simple(true); |
| 301 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 302 | |
| 303 | //----------------------------------------------------------------------------- |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 304 | |
| 305 | namespace { |
| 306 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 318 | void DelayReply(bool pump_during_send) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 319 | std::vector<Worker*> workers; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 320 | workers.push_back(new SimpleServer(pump_during_send)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 321 | workers.push_back(new DelayClient()); |
| 322 | RunTest(workers); |
| 323 | } |
| 324 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 325 | } // namespace |
| 326 | |
| 327 | // Tests that asynchronous replies work |
| 328 | TEST_F(IPCSyncChannelTest, DelayReply) { |
| 329 | DelayReply(false); |
| 330 | DelayReply(true); |
| 331 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 332 | |
| 333 | //----------------------------------------------------------------------------- |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 334 | |
| 335 | namespace { |
| 336 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 337 | class NoHangServer : public Worker { |
| 338 | public: |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 339 | explicit NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 340 | : Worker(Channel::MODE_SERVER, "no_hang_server"), |
| 341 | got_first_reply_(got_first_reply), |
| 342 | pump_during_send_(pump_during_send) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 343 | void Run() { |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 344 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 345 | got_first_reply_->Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 346 | |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 347 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 348 | Done(); |
| 349 | } |
| 350 | |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 351 | WaitableEvent* got_first_reply_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 352 | bool pump_during_send_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | class NoHangClient : public Worker { |
| 356 | public: |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 357 | explicit NoHangClient(WaitableEvent* got_first_reply) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 371 | WaitableEvent* got_first_reply_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 372 | }; |
| 373 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 374 | void NoHang(bool pump_during_send) { |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 375 | WaitableEvent got_first_reply(false, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 376 | std::vector<Worker*> workers; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 377 | workers.push_back(new NoHangServer(&got_first_reply, pump_during_send)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 378 | workers.push_back(new NoHangClient(&got_first_reply)); |
| 379 | RunTest(workers); |
| 380 | } |
| 381 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 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 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 389 | |
| 390 | //----------------------------------------------------------------------------- |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 391 | |
| 392 | namespace { |
| 393 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 394 | class UnblockServer : public Worker { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 395 | public: |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 396 | UnblockServer(bool pump_during_send, bool delete_during_send) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 397 | : Worker(Channel::MODE_SERVER, "unblock_server"), |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 398 | pump_during_send_(pump_during_send), |
| 399 | delete_during_send_(delete_during_send) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 400 | void Run() { |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [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 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 412 | Done(); |
| 413 | } |
| 414 | |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [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(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 420 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 421 | |
| 422 | bool pump_during_send_; |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 423 | bool delete_during_send_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 424 | }; |
| 425 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 426 | class UnblockClient : public Worker { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 427 | public: |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 428 | explicit UnblockClient(bool pump_during_send) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 429 | : Worker(Channel::MODE_CLIENT, "unblock_client"), |
| 430 | pump_during_send_(pump_during_send) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 431 | |
| 432 | void OnAnswer(int* answer) { |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 433 | SendDouble(pump_during_send_, true); |
| 434 | *answer = 42; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 435 | Done(); |
| 436 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 437 | |
| 438 | bool pump_during_send_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 439 | }; |
| 440 | |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 441 | void Unblock(bool server_pump, bool client_pump, bool delete_during_send) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 442 | std::vector<Worker*> workers; |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 443 | workers.push_back(new UnblockServer(server_pump, delete_during_send)); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 444 | workers.push_back(new UnblockClient(client_pump)); |
| 445 | RunTest(workers); |
| 446 | } |
| 447 | |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 448 | } // namespace |
| 449 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 450 | // Tests that the caller unblocks to answer a sync message from the receiver. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 451 | TEST_F(IPCSyncChannelTest, Unblock) { |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [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); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [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() { |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 480 | SendDouble(pump_first_, expected_send_result_); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 481 | Done(); |
| 482 | } |
| 483 | |
| 484 | void OnDouble(int in, int* out) { |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 485 | *out = in * 2; |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 486 | SendAnswerToLife(pump_second_, base::kNoTimeout, expected_send_result_); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 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) { |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 499 | SendDouble(pump_during_send_, !close_channel_); |
[email protected] | c690a18 | 2008-10-24 23:10:32 | [diff] [blame] | 500 | if (close_channel_) { |
| 501 | delete reply_msg; |
| 502 | } else { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 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_) { |
[email protected] | c690a18 | 2008-10-24 23:10:32 | [diff] [blame] | 511 | delete reply_msg; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 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) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 524 | std::vector<Worker*> workers; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 525 | workers.push_back( |
| 526 | new RecursiveServer(true, server_pump_first, server_pump_second)); |
| 527 | workers.push_back(new RecursiveClient(client_pump, false)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 528 | RunTest(workers); |
| 529 | } |
| 530 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 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 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 572 | |
| 573 | //----------------------------------------------------------------------------- |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 574 | |
| 575 | namespace { |
| 576 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 577 | class MultipleServer1 : public Worker { |
| 578 | public: |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 579 | explicit MultipleServer1(bool pump_during_send) |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 580 | : Worker("test_channel1", Channel::MODE_SERVER), |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 581 | pump_during_send_(pump_during_send) { } |
| 582 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 583 | void Run() { |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 584 | SendDouble(pump_during_send_, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 585 | Done(); |
| 586 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 587 | |
| 588 | bool pump_during_send_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 589 | }; |
| 590 | |
| 591 | class MultipleClient1 : public Worker { |
| 592 | public: |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 593 | MultipleClient1(WaitableEvent* client1_msg_received, |
| 594 | WaitableEvent* client1_can_reply) : |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 595 | Worker("test_channel1", Channel::MODE_CLIENT), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 596 | client1_msg_received_(client1_msg_received), |
| 597 | client1_can_reply_(client1_can_reply) { } |
| 598 | |
| 599 | void OnDouble(int in, int* out) { |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 600 | client1_msg_received_->Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 601 | *out = in * 2; |
| 602 | client1_can_reply_->Wait(); |
| 603 | Done(); |
| 604 | } |
| 605 | |
| 606 | private: |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 607 | WaitableEvent *client1_msg_received_, *client1_can_reply_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 608 | }; |
| 609 | |
| 610 | class MultipleServer2 : public Worker { |
| 611 | public: |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 612 | MultipleServer2() : Worker("test_channel2", Channel::MODE_SERVER) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 613 | |
| 614 | void OnAnswer(int* result) { |
| 615 | *result = 42; |
| 616 | Done(); |
| 617 | } |
| 618 | }; |
| 619 | |
| 620 | class MultipleClient2 : public Worker { |
| 621 | public: |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 622 | MultipleClient2( |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 623 | WaitableEvent* client1_msg_received, WaitableEvent* client1_can_reply, |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 624 | bool pump_during_send) |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 625 | : Worker("test_channel2", Channel::MODE_CLIENT), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 626 | client1_msg_received_(client1_msg_received), |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 627 | client1_can_reply_(client1_can_reply), |
| 628 | pump_during_send_(pump_during_send) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 629 | |
| 630 | void Run() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 631 | client1_msg_received_->Wait(); |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 632 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 633 | client1_can_reply_->Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 634 | Done(); |
| 635 | } |
| 636 | |
| 637 | private: |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 638 | WaitableEvent *client1_msg_received_, *client1_can_reply_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 639 | bool pump_during_send_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 640 | }; |
| 641 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 642 | void Multiple(bool server_pump, bool client_pump) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 643 | std::vector<Worker*> workers; |
| 644 | |
| 645 | // A shared worker thread so that server1 and server2 run on one thread. |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 646 | base::Thread worker_thread("Multiple"); |
[email protected] | 6314e6f6 | 2009-07-15 16:07:14 | [diff] [blame] | 647 | ASSERT_TRUE(worker_thread.Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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. |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 652 | WaitableEvent client1_msg_received(false, false); |
| 653 | WaitableEvent client1_can_reply(false, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 654 | |
| 655 | Worker* worker; |
| 656 | |
| 657 | worker = new MultipleServer2(); |
| 658 | worker->OverrideThread(&worker_thread); |
| 659 | workers.push_back(worker); |
| 660 | |
| 661 | worker = new MultipleClient2( |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 662 | &client1_msg_received, &client1_can_reply, client_pump); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 663 | workers.push_back(worker); |
| 664 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 665 | worker = new MultipleServer1(server_pump); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 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 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 686 | |
| 687 | //----------------------------------------------------------------------------- |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 688 | |
| 689 | namespace { |
| 690 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [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 { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 695 | public: |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 696 | QueuedReplyServer(base::Thread* listener_thread, |
| 697 | const std::string& channel_name, |
| 698 | const std::string& reply_text) |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 699 | : Worker(channel_name, Channel::MODE_SERVER), |
| 700 | reply_text_(reply_text) { |
| 701 | Worker::OverrideThread(listener_thread); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 702 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 703 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 704 | virtual void OnNestedTestMsg(Message* reply_msg) { |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 705 | VLOG(1) << __FUNCTION__ << " Sending reply: " << reply_text_; |
| 706 | SyncChannelNestedTestMsg_String::WriteReplyParams(reply_msg, reply_text_); |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 707 | Send(reply_msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 708 | Done(); |
| 709 | } |
| 710 | |
| 711 | private: |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 712 | std::string reply_text_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 713 | }; |
| 714 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [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 { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 722 | public: |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [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), |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 728 | pump_during_send_(pump_during_send), |
| 729 | expected_text_(expected_text) { |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 730 | Worker::OverrideThread(listener_thread); |
| 731 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 732 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [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); |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 740 | DCHECK_EQ(response, expected_text_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 741 | |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 742 | VLOG(1) << __FUNCTION__ << " Received reply: " << response; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 743 | Done(); |
| 744 | } |
| 745 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 746 | private: |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 747 | bool pump_during_send_; |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 748 | std::string expected_text_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 749 | }; |
| 750 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 751 | void QueuedReply(bool client_pump) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 752 | std::vector<Worker*> workers; |
| 753 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 754 | // A shared worker thread for servers |
| 755 | base::Thread server_worker_thread("QueuedReply_ServerListener"); |
| 756 | ASSERT_TRUE(server_worker_thread.Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 757 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 758 | base::Thread client_worker_thread("QueuedReply_ClientListener"); |
| 759 | ASSERT_TRUE(client_worker_thread.Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 760 | |
| 761 | Worker* worker; |
| 762 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 763 | worker = new QueuedReplyServer(&server_worker_thread, |
| 764 | "QueuedReply_Server1", |
| 765 | "Got first message"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 766 | workers.push_back(worker); |
| 767 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 768 | worker = new QueuedReplyServer(&server_worker_thread, |
| 769 | "QueuedReply_Server2", |
| 770 | "Got second message"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 771 | workers.push_back(worker); |
| 772 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 773 | worker = new QueuedReplyClient(&client_worker_thread, |
| 774 | "QueuedReply_Server1", |
| 775 | "Got first message", |
| 776 | client_pump); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 777 | workers.push_back(worker); |
| 778 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 779 | worker = new QueuedReplyClient(&client_worker_thread, |
| 780 | "QueuedReply_Server2", |
| 781 | "Got second message", |
| 782 | client_pump); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 783 | workers.push_back(worker); |
| 784 | |
| 785 | RunTest(workers); |
| 786 | } |
| 787 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 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. |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 794 | // We also test that the send call stacks unwind correctly when the channel |
| 795 | // pumps messages while waiting for a response. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 796 | TEST_F(IPCSyncChannelTest, QueuedReply) { |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 797 | QueuedReply(false); |
| 798 | QueuedReply(true); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 799 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 800 | |
| 801 | //----------------------------------------------------------------------------- |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 802 | |
| 803 | namespace { |
| 804 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 805 | void DropAssert(const std::string&) {} |
| 806 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 807 | class BadServer : public Worker { |
| 808 | public: |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 809 | explicit BadServer(bool pump_during_send) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 810 | : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 811 | pump_during_send_(pump_during_send) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 812 | void Run() { |
| 813 | int answer = 0; |
| 814 | |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 815 | SyncMessage* msg = new SyncMessage( |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 816 | MSG_ROUTING_CONTROL, SyncChannelTestMsg_Double::ID, |
| 817 | Message::PRIORITY_NORMAL, NULL); |
| 818 | if (pump_during_send_) |
| 819 | msg->EnableMessagePumping(); |
| 820 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 821 | // Temporarily ignore asserts so that the assertion in |
| 822 | // ipc_message_utils doesn't cause termination. |
| 823 | logging::SetLogAssertHandler(&DropAssert); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 824 | bool result = Send(msg); |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 825 | logging::SetLogAssertHandler(NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [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); |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 831 | DCHECK_EQ(answer, 42); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 832 | |
| 833 | Done(); |
| 834 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 835 | |
| 836 | bool pump_during_send_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 837 | }; |
| 838 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 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 | |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 846 | } // namespace |
| 847 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 848 | // Tests that if a message is not serialized correctly, the Send() will fail. |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 849 | TEST_F(IPCSyncChannelTest, BadMessage) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 850 | BadMessage(false); |
| 851 | BadMessage(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 852 | } |
| 853 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 854 | //----------------------------------------------------------------------------- |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 855 | |
| 856 | namespace { |
| 857 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 858 | class ChattyClient : public Worker { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 859 | public: |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 860 | ChattyClient() : |
| 861 | Worker(Channel::MODE_CLIENT, "chatty_client") { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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) { |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 868 | if (!SendDouble(false, true)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 869 | break; |
| 870 | } |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 871 | *answer = 42; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 872 | Done(); |
| 873 | } |
| 874 | }; |
| 875 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 876 | void ChattyServer(bool pump_during_send) { |
| 877 | std::vector<Worker*> workers; |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 878 | workers.push_back(new UnblockServer(pump_during_send, false)); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 879 | workers.push_back(new ChattyClient()); |
| 880 | RunTest(workers); |
| 881 | } |
| 882 | |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 883 | } // namespace |
| 884 | |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 885 | // Tests http://b/1093251 - that sending lots of sync messages while |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 886 | // the receiver is waiting for a sync reply does not overflow the PostMessage |
| 887 | // queue. |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 888 | TEST_F(IPCSyncChannelTest, ChattyServer) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 889 | ChattyServer(false); |
| 890 | ChattyServer(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 891 | } |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 892 | |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 893 | //------------------------------------------------------------------------------ |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 894 | |
| 895 | namespace { |
| 896 | |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 897 | class TimeoutServer : public Worker { |
| 898 | public: |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 899 | TimeoutServer(int timeout_ms, |
| 900 | std::vector<bool> timeout_seq, |
| 901 | bool pump_during_send) |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 902 | : Worker(Channel::MODE_SERVER, "timeout_server"), |
| 903 | timeout_ms_(timeout_ms), |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 904 | timeout_seq_(timeout_seq), |
| 905 | pump_during_send_(pump_during_send) { |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | void Run() { |
| 909 | for (std::vector<bool>::const_iterator iter = timeout_seq_.begin(); |
| 910 | iter != timeout_seq_.end(); ++iter) { |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 911 | SendAnswerToLife(pump_during_send_, timeout_ms_, !*iter); |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 912 | } |
| 913 | Done(); |
| 914 | } |
| 915 | |
| 916 | private: |
| 917 | int timeout_ms_; |
| 918 | std::vector<bool> timeout_seq_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 919 | bool pump_during_send_; |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 920 | }; |
| 921 | |
| 922 | class UnresponsiveClient : public Worker { |
| 923 | public: |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 924 | explicit UnresponsiveClient(std::vector<bool> timeout_seq) |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 925 | : Worker(Channel::MODE_CLIENT, "unresponsive_client"), |
| 926 | timeout_seq_(timeout_seq) { |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 927 | } |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [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. |
[email protected] | 46366737 | 2008-08-20 20:20:52 | [diff] [blame] | 936 | delete reply_msg; |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 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 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 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 | |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 983 | } // namespace |
| 984 | |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 985 | // Tests that SendWithTimeout does not time-out if the response comes back fast |
| 986 | // enough. |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 987 | TEST_F(IPCSyncChannelTest, SendWithTimeoutOK) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 988 | SendWithTimeoutOK(false); |
| 989 | SendWithTimeoutOK(true); |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | // Tests that SendWithTimeout does time-out. |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 993 | TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 994 | SendWithTimeoutTimeout(false); |
| 995 | SendWithTimeoutTimeout(true); |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | // Sends some message that time-out and some that succeed. |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 999 | TEST_F(IPCSyncChannelTest, SendWithTimeoutMixedOKAndTimeout) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 1000 | SendWithTimeoutMixedOKAndTimeout(false); |
| 1001 | SendWithTimeoutMixedOKAndTimeout(true); |
| 1002 | } |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 1003 | |
| 1004 | //------------------------------------------------------------------------------ |
| 1005 | |
| 1006 | namespace { |
| 1007 | |
| 1008 | class NestedTask : public Task { |
| 1009 | public: |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 1010 | explicit NestedTask(Worker* server) : server_(server) { } |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 1011 | void Run() { |
| 1012 | // Sleep a bit so that we wake up after the reply has been received. |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 1013 | PlatformThread::Sleep(250); |
| 1014 | server_->SendAnswerToLife(true, base::kNoTimeout, true); |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 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 | } |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [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 | |
[email protected] | 87339f0 | 2010-09-02 21:45:50 | [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 | |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [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 | } |
[email protected] | 87339f0 | 2010-09-02 21:45:50 | [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 | |