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