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