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