[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | } |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 123 | const std::string& channel_name() { return channel_name_; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | Channel::Mode mode() { return mode_; } |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 125 | WaitableEvent* done_event() { return done_.get(); } |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 126 | WaitableEvent* shutdown_event() { return &shutdown_event_; } |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 127 | void ResetChannel() { channel_.reset(); } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | // Derived classes need to call this when they've completed their part of |
| 129 | // the test. |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 130 | void Done() { done_->Signal(); } |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 131 | |
| 132 | protected: |
[email protected] | c9e0c733 | 2011-05-13 22:42:41 | [diff] [blame] | 133 | SyncChannel* channel() { return channel_.get(); } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | // Functions for dervied classes to implement if they wish. |
| 135 | virtual void Run() { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | virtual void OnAnswer(int* answer) { NOTREACHED(); } |
| 137 | virtual void OnAnswerDelay(Message* reply_msg) { |
| 138 | // The message handler map below can only take one entry for |
| 139 | // SyncChannelTestMsg_AnswerToLife, so since some classes want |
| 140 | // the normal version while other want the delayed reply, we |
| 141 | // call the normal version if the derived class didn't override |
| 142 | // this function. |
| 143 | int answer; |
| 144 | OnAnswer(&answer); |
| 145 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, answer); |
| 146 | Send(reply_msg); |
| 147 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 148 | virtual void OnDouble(int in, int* out) { NOTREACHED(); } |
| 149 | virtual void OnDoubleDelay(int in, Message* reply_msg) { |
| 150 | int result; |
| 151 | OnDouble(in, &result); |
| 152 | SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result); |
| 153 | Send(reply_msg); |
| 154 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 155 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 156 | virtual void OnNestedTestMsg(Message* reply_msg) { |
| 157 | NOTREACHED(); |
| 158 | } |
| 159 | |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 160 | virtual SyncChannel* CreateChannel() { |
| 161 | return new SyncChannel( |
| 162 | channel_name_, mode_, this, ipc_thread_.message_loop_proxy(), true, |
| 163 | &shutdown_event_); |
| 164 | } |
| 165 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 166 | base::Thread* ListenerThread() { |
| 167 | return overrided_thread_ ? overrided_thread_ : &listener_thread_; |
| 168 | } |
[email protected] | 87339f0 | 2010-09-02 21:45:50 | [diff] [blame] | 169 | |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 170 | const base::Thread& ipc_thread() const { return ipc_thread_; } |
| 171 | |
[email protected] | 87339f0 | 2010-09-02 21:45:50 | [diff] [blame] | 172 | private: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 173 | // Called on the listener thread to create the sync channel. |
| 174 | void OnStart() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 175 | // Link ipc_thread_, listener_thread_ and channel_ altogether. |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 176 | StartThread(&ipc_thread_, MessageLoop::TYPE_IO); |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 177 | channel_.reset(CreateChannel()); |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 178 | channel_created_->Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 179 | Run(); |
| 180 | } |
| 181 | |
[email protected] | 9d4ff5ed | 2009-03-03 00:21:56 | [diff] [blame] | 182 | void OnListenerThreadShutdown1(WaitableEvent* listener_event, |
| 183 | WaitableEvent* ipc_event) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 184 | // SyncChannel needs to be destructed on the thread that it was created on. |
| 185 | channel_.reset(); |
[email protected] | 9d4ff5ed | 2009-03-03 00:21:56 | [diff] [blame] | 186 | |
| 187 | MessageLoop::current()->RunAllPending(); |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 188 | |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 189 | ipc_thread_.message_loop()->PostTask( |
| 190 | FROM_HERE, base::Bind(&Worker::OnIPCThreadShutdown, this, |
| 191 | listener_event, ipc_event)); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | 9d4ff5ed | 2009-03-03 00:21:56 | [diff] [blame] | 194 | void OnIPCThreadShutdown(WaitableEvent* listener_event, |
| 195 | WaitableEvent* ipc_event) { |
| 196 | MessageLoop::current()->RunAllPending(); |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 197 | ipc_event->Signal(); |
[email protected] | 9d4ff5ed | 2009-03-03 00:21:56 | [diff] [blame] | 198 | |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 199 | listener_thread_.message_loop()->PostTask( |
| 200 | FROM_HERE, base::Bind(&Worker::OnListenerThreadShutdown2, this, |
| 201 | listener_event)); |
[email protected] | 9d4ff5ed | 2009-03-03 00:21:56 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void OnListenerThreadShutdown2(WaitableEvent* listener_event) { |
| 205 | MessageLoop::current()->RunAllPending(); |
| 206 | listener_event->Signal(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 207 | } |
| 208 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 209 | bool OnMessageReceived(const Message& message) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | IPC_BEGIN_MESSAGE_MAP(Worker, message) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 211 | IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 212 | IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife, |
| 213 | OnAnswerDelay) |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 214 | IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelNestedTestMsg_String, |
| 215 | OnNestedTestMsg) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | IPC_END_MESSAGE_MAP() |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 217 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 218 | } |
| 219 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 220 | void StartThread(base::Thread* thread, MessageLoop::Type type) { |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 221 | base::Thread::Options options; |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 222 | options.message_loop_type = type; |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 223 | thread->StartWithOptions(options); |
| 224 | } |
| 225 | |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 226 | scoped_ptr<WaitableEvent> done_; |
| 227 | scoped_ptr<WaitableEvent> channel_created_; |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 228 | std::string channel_name_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 229 | Channel::Mode mode_; |
| 230 | scoped_ptr<SyncChannel> channel_; |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 231 | base::Thread ipc_thread_; |
| 232 | base::Thread listener_thread_; |
| 233 | base::Thread* overrided_thread_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 234 | |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 235 | base::WaitableEvent shutdown_event_; |
| 236 | |
[email protected] | 73a797fb | 2010-06-07 02:10:18 | [diff] [blame] | 237 | DISALLOW_COPY_AND_ASSIGN(Worker); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | |
| 241 | // Starts the test with the given workers. This function deletes the workers |
| 242 | // when it's done. |
| 243 | void RunTest(std::vector<Worker*> workers) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 244 | // First we create the workers that are channel servers, or else the other |
| 245 | // workers' channel initialization might fail because the pipe isn't created.. |
| 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_SERVER_FLAG) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | workers[i]->Start(); |
| 249 | workers[i]->WaitForChannelCreation(); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // now create the clients |
| 254 | for (size_t i = 0; i < workers.size(); ++i) { |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 255 | if (workers[i]->mode() & Channel::MODE_CLIENT_FLAG) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 256 | workers[i]->Start(); |
| 257 | } |
| 258 | |
| 259 | // wait for all the workers to finish |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 260 | for (size_t i = 0; i < workers.size(); ++i) |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 261 | workers[i]->done_event()->Wait(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 262 | |
[email protected] | 82e5ee8 | 2009-04-03 02:29:45 | [diff] [blame] | 263 | STLDeleteContainerPointers(workers.begin(), workers.end()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 264 | } |
| 265 | |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 266 | } // namespace |
| 267 | |
[email protected] | 9629c0e | 2009-02-04 23:16:29 | [diff] [blame] | 268 | class IPCSyncChannelTest : public testing::Test { |
| 269 | private: |
| 270 | MessageLoop message_loop_; |
| 271 | }; |
| 272 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 273 | //----------------------------------------------------------------------------- |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 274 | |
| 275 | namespace { |
| 276 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 277 | class SimpleServer : public Worker { |
| 278 | public: |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 279 | explicit SimpleServer(bool pump_during_send) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 280 | : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 281 | pump_during_send_(pump_during_send) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 282 | void Run() { |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 283 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 284 | Done(); |
| 285 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 286 | |
| 287 | bool pump_during_send_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | class SimpleClient : public Worker { |
| 291 | public: |
| 292 | SimpleClient() : Worker(Channel::MODE_CLIENT, "simple_client") { } |
| 293 | |
| 294 | void OnAnswer(int* answer) { |
| 295 | *answer = 42; |
| 296 | Done(); |
| 297 | } |
| 298 | }; |
| 299 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 300 | void Simple(bool pump_during_send) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 301 | std::vector<Worker*> workers; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 302 | workers.push_back(new SimpleServer(pump_during_send)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 303 | workers.push_back(new SimpleClient()); |
| 304 | RunTest(workers); |
| 305 | } |
| 306 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 307 | } // namespace |
| 308 | |
| 309 | // Tests basic synchronous call |
| 310 | TEST_F(IPCSyncChannelTest, Simple) { |
| 311 | Simple(false); |
| 312 | Simple(true); |
| 313 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 314 | |
| 315 | //----------------------------------------------------------------------------- |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 316 | |
| 317 | namespace { |
| 318 | |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 319 | // Worker classes which override how the sync channel is created to use the |
| 320 | // two-step initialization (calling the lightweight constructor and then |
| 321 | // ChannelProxy::Init separately) process. |
| 322 | class TwoStepServer : public Worker { |
| 323 | public: |
| 324 | explicit TwoStepServer(bool create_pipe_now) |
| 325 | : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 326 | create_pipe_now_(create_pipe_now) { } |
| 327 | |
| 328 | void Run() { |
| 329 | SendAnswerToLife(false, base::kNoTimeout, true); |
| 330 | Done(); |
| 331 | } |
| 332 | |
| 333 | virtual SyncChannel* CreateChannel() { |
| 334 | SyncChannel* channel = new SyncChannel( |
| 335 | this, ipc_thread().message_loop_proxy(), shutdown_event()); |
| 336 | channel->Init(channel_name(), mode(), create_pipe_now_); |
| 337 | return channel; |
| 338 | } |
| 339 | |
| 340 | bool create_pipe_now_; |
| 341 | }; |
| 342 | |
| 343 | class TwoStepClient : public Worker { |
| 344 | public: |
| 345 | TwoStepClient(bool create_pipe_now) |
| 346 | : Worker(Channel::MODE_CLIENT, "simple_client"), |
| 347 | create_pipe_now_(create_pipe_now) { } |
| 348 | |
| 349 | void OnAnswer(int* answer) { |
| 350 | *answer = 42; |
| 351 | Done(); |
| 352 | } |
| 353 | |
| 354 | virtual SyncChannel* CreateChannel() { |
| 355 | SyncChannel* channel = new SyncChannel( |
| 356 | this, ipc_thread().message_loop_proxy(), shutdown_event()); |
| 357 | channel->Init(channel_name(), mode(), create_pipe_now_); |
| 358 | return channel; |
| 359 | } |
| 360 | |
| 361 | bool create_pipe_now_; |
| 362 | }; |
| 363 | |
| 364 | void TwoStep(bool create_server_pipe_now, bool create_client_pipe_now) { |
| 365 | std::vector<Worker*> workers; |
| 366 | workers.push_back(new TwoStepServer(create_server_pipe_now)); |
| 367 | workers.push_back(new TwoStepClient(create_client_pipe_now)); |
| 368 | RunTest(workers); |
| 369 | } |
| 370 | |
| 371 | } // namespace |
| 372 | |
| 373 | // Tests basic two-step initialization, where you call the lightweight |
| 374 | // constructor then Init. |
| 375 | TEST_F(IPCSyncChannelTest, TwoStepInitialization) { |
| 376 | TwoStep(false, false); |
| 377 | TwoStep(false, true); |
| 378 | TwoStep(true, false); |
| 379 | TwoStep(true, true); |
| 380 | } |
| 381 | |
| 382 | |
| 383 | //----------------------------------------------------------------------------- |
| 384 | |
| 385 | namespace { |
| 386 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 387 | class DelayClient : public Worker { |
| 388 | public: |
| 389 | DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { } |
| 390 | |
| 391 | void OnAnswerDelay(Message* reply_msg) { |
| 392 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 393 | Send(reply_msg); |
| 394 | Done(); |
| 395 | } |
| 396 | }; |
| 397 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 398 | void DelayReply(bool pump_during_send) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 399 | std::vector<Worker*> workers; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 400 | workers.push_back(new SimpleServer(pump_during_send)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 401 | workers.push_back(new DelayClient()); |
| 402 | RunTest(workers); |
| 403 | } |
| 404 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 405 | } // namespace |
| 406 | |
| 407 | // Tests that asynchronous replies work |
| 408 | TEST_F(IPCSyncChannelTest, DelayReply) { |
| 409 | DelayReply(false); |
| 410 | DelayReply(true); |
| 411 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 412 | |
| 413 | //----------------------------------------------------------------------------- |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 414 | |
| 415 | namespace { |
| 416 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 417 | class NoHangServer : public Worker { |
| 418 | public: |
[email protected] | e7e3803 | 2011-07-26 17:25:25 | [diff] [blame] | 419 | NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 420 | : Worker(Channel::MODE_SERVER, "no_hang_server"), |
| 421 | got_first_reply_(got_first_reply), |
| 422 | pump_during_send_(pump_during_send) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 423 | void Run() { |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 424 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 425 | got_first_reply_->Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 426 | |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 427 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 428 | Done(); |
| 429 | } |
| 430 | |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 431 | WaitableEvent* got_first_reply_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 432 | bool pump_during_send_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 433 | }; |
| 434 | |
| 435 | class NoHangClient : public Worker { |
| 436 | public: |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 437 | explicit NoHangClient(WaitableEvent* got_first_reply) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 438 | : Worker(Channel::MODE_CLIENT, "no_hang_client"), |
| 439 | got_first_reply_(got_first_reply) { } |
| 440 | |
| 441 | virtual void OnAnswerDelay(Message* reply_msg) { |
| 442 | // Use the DELAY_REPLY macro so that we can force the reply to be sent |
| 443 | // before this function returns (when the channel will be reset). |
| 444 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 445 | Send(reply_msg); |
| 446 | got_first_reply_->Wait(); |
| 447 | CloseChannel(); |
| 448 | Done(); |
| 449 | } |
| 450 | |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 451 | WaitableEvent* got_first_reply_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 452 | }; |
| 453 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 454 | void NoHang(bool pump_during_send) { |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 455 | WaitableEvent got_first_reply(false, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 456 | std::vector<Worker*> workers; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 457 | workers.push_back(new NoHangServer(&got_first_reply, pump_during_send)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 458 | workers.push_back(new NoHangClient(&got_first_reply)); |
| 459 | RunTest(workers); |
| 460 | } |
| 461 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 462 | } // namespace |
| 463 | |
| 464 | // Tests that caller doesn't hang if receiver dies |
| 465 | TEST_F(IPCSyncChannelTest, NoHang) { |
| 466 | NoHang(false); |
| 467 | NoHang(true); |
| 468 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 469 | |
| 470 | //----------------------------------------------------------------------------- |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 471 | |
| 472 | namespace { |
| 473 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 474 | class UnblockServer : public Worker { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 475 | public: |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 476 | UnblockServer(bool pump_during_send, bool delete_during_send) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 477 | : Worker(Channel::MODE_SERVER, "unblock_server"), |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 478 | pump_during_send_(pump_during_send), |
| 479 | delete_during_send_(delete_during_send) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 480 | void Run() { |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 481 | if (delete_during_send_) { |
| 482 | // Use custom code since race conditions mean the answer may or may not be |
| 483 | // available. |
| 484 | int answer = 0; |
| 485 | SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer); |
| 486 | if (pump_during_send_) |
| 487 | msg->EnableMessagePumping(); |
| 488 | Send(msg); |
| 489 | } else { |
| 490 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 491 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 492 | Done(); |
| 493 | } |
| 494 | |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 495 | void OnDoubleDelay(int in, Message* reply_msg) { |
| 496 | SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2); |
| 497 | Send(reply_msg); |
| 498 | if (delete_during_send_) |
| 499 | ResetChannel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 500 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 501 | |
| 502 | bool pump_during_send_; |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 503 | bool delete_during_send_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 504 | }; |
| 505 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 506 | class UnblockClient : public Worker { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 507 | public: |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 508 | explicit UnblockClient(bool pump_during_send) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 509 | : Worker(Channel::MODE_CLIENT, "unblock_client"), |
| 510 | pump_during_send_(pump_during_send) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 511 | |
| 512 | void OnAnswer(int* answer) { |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 513 | SendDouble(pump_during_send_, true); |
| 514 | *answer = 42; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 515 | Done(); |
| 516 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 517 | |
| 518 | bool pump_during_send_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 519 | }; |
| 520 | |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 521 | void Unblock(bool server_pump, bool client_pump, bool delete_during_send) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 522 | std::vector<Worker*> workers; |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 523 | workers.push_back(new UnblockServer(server_pump, delete_during_send)); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 524 | workers.push_back(new UnblockClient(client_pump)); |
| 525 | RunTest(workers); |
| 526 | } |
| 527 | |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 528 | } // namespace |
| 529 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 530 | // Tests that the caller unblocks to answer a sync message from the receiver. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 531 | TEST_F(IPCSyncChannelTest, Unblock) { |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 532 | Unblock(false, false, false); |
| 533 | Unblock(false, true, false); |
| 534 | Unblock(true, false, false); |
| 535 | Unblock(true, true, false); |
| 536 | } |
| 537 | |
| 538 | //----------------------------------------------------------------------------- |
| 539 | |
[email protected] | c9e0c733 | 2011-05-13 22:42:41 | [diff] [blame] | 540 | // Tests that the the SyncChannel object can be deleted during a Send. |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 541 | TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) { |
| 542 | Unblock(false, false, true); |
| 543 | Unblock(false, true, true); |
| 544 | Unblock(true, false, true); |
| 545 | Unblock(true, true, true); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | //----------------------------------------------------------------------------- |
| 549 | |
| 550 | namespace { |
| 551 | |
| 552 | class RecursiveServer : public Worker { |
| 553 | public: |
[email protected] | e7e3803 | 2011-07-26 17:25:25 | [diff] [blame] | 554 | RecursiveServer(bool expected_send_result, bool pump_first, bool pump_second) |
| 555 | : Worker(Channel::MODE_SERVER, "recursive_server"), |
| 556 | expected_send_result_(expected_send_result), |
| 557 | pump_first_(pump_first), pump_second_(pump_second) {} |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 558 | void Run() { |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 559 | SendDouble(pump_first_, expected_send_result_); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 560 | Done(); |
| 561 | } |
| 562 | |
| 563 | void OnDouble(int in, int* out) { |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 564 | *out = in * 2; |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 565 | SendAnswerToLife(pump_second_, base::kNoTimeout, expected_send_result_); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | bool expected_send_result_, pump_first_, pump_second_; |
| 569 | }; |
| 570 | |
| 571 | class RecursiveClient : public Worker { |
| 572 | public: |
[email protected] | e7e3803 | 2011-07-26 17:25:25 | [diff] [blame] | 573 | RecursiveClient(bool pump_during_send, bool close_channel) |
| 574 | : Worker(Channel::MODE_CLIENT, "recursive_client"), |
| 575 | pump_during_send_(pump_during_send), close_channel_(close_channel) {} |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 576 | |
| 577 | void OnDoubleDelay(int in, Message* reply_msg) { |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 578 | SendDouble(pump_during_send_, !close_channel_); |
[email protected] | c690a18 | 2008-10-24 23:10:32 | [diff] [blame] | 579 | if (close_channel_) { |
| 580 | delete reply_msg; |
| 581 | } else { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 582 | SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2); |
| 583 | Send(reply_msg); |
| 584 | } |
| 585 | Done(); |
| 586 | } |
| 587 | |
| 588 | void OnAnswerDelay(Message* reply_msg) { |
| 589 | if (close_channel_) { |
[email protected] | c690a18 | 2008-10-24 23:10:32 | [diff] [blame] | 590 | delete reply_msg; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 591 | CloseChannel(); |
| 592 | } else { |
| 593 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 594 | Send(reply_msg); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | bool pump_during_send_, close_channel_; |
| 599 | }; |
| 600 | |
| 601 | void Recursive( |
| 602 | bool server_pump_first, bool server_pump_second, bool client_pump) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 603 | std::vector<Worker*> workers; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 604 | workers.push_back( |
| 605 | new RecursiveServer(true, server_pump_first, server_pump_second)); |
| 606 | workers.push_back(new RecursiveClient(client_pump, false)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 607 | RunTest(workers); |
| 608 | } |
| 609 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 610 | } // namespace |
| 611 | |
| 612 | // Tests a server calling Send while another Send is pending. |
| 613 | TEST_F(IPCSyncChannelTest, Recursive) { |
| 614 | Recursive(false, false, false); |
| 615 | Recursive(false, false, true); |
| 616 | Recursive(false, true, false); |
| 617 | Recursive(false, true, true); |
| 618 | Recursive(true, false, false); |
| 619 | Recursive(true, false, true); |
| 620 | Recursive(true, true, false); |
| 621 | Recursive(true, true, true); |
| 622 | } |
| 623 | |
| 624 | //----------------------------------------------------------------------------- |
| 625 | |
| 626 | namespace { |
| 627 | |
| 628 | void RecursiveNoHang( |
| 629 | bool server_pump_first, bool server_pump_second, bool client_pump) { |
| 630 | std::vector<Worker*> workers; |
| 631 | workers.push_back( |
| 632 | new RecursiveServer(false, server_pump_first, server_pump_second)); |
| 633 | workers.push_back(new RecursiveClient(client_pump, true)); |
| 634 | RunTest(workers); |
| 635 | } |
| 636 | |
| 637 | } // namespace |
| 638 | |
| 639 | // Tests that if a caller makes a sync call during an existing sync call and |
| 640 | // the receiver dies, neither of the Send() calls hang. |
| 641 | TEST_F(IPCSyncChannelTest, RecursiveNoHang) { |
| 642 | RecursiveNoHang(false, false, false); |
| 643 | RecursiveNoHang(false, false, true); |
| 644 | RecursiveNoHang(false, true, false); |
| 645 | RecursiveNoHang(false, true, true); |
| 646 | RecursiveNoHang(true, false, false); |
| 647 | RecursiveNoHang(true, false, true); |
| 648 | RecursiveNoHang(true, true, false); |
| 649 | RecursiveNoHang(true, true, true); |
| 650 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 651 | |
| 652 | //----------------------------------------------------------------------------- |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 653 | |
| 654 | namespace { |
| 655 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 656 | class MultipleServer1 : public Worker { |
| 657 | public: |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 658 | explicit MultipleServer1(bool pump_during_send) |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 659 | : Worker("test_channel1", Channel::MODE_SERVER), |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 660 | pump_during_send_(pump_during_send) { } |
| 661 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 662 | void Run() { |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 663 | SendDouble(pump_during_send_, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 664 | Done(); |
| 665 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 666 | |
| 667 | bool pump_during_send_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 668 | }; |
| 669 | |
| 670 | class MultipleClient1 : public Worker { |
| 671 | public: |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 672 | MultipleClient1(WaitableEvent* client1_msg_received, |
| 673 | WaitableEvent* client1_can_reply) : |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 674 | Worker("test_channel1", Channel::MODE_CLIENT), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 675 | client1_msg_received_(client1_msg_received), |
| 676 | client1_can_reply_(client1_can_reply) { } |
| 677 | |
| 678 | void OnDouble(int in, int* out) { |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 679 | client1_msg_received_->Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 680 | *out = in * 2; |
| 681 | client1_can_reply_->Wait(); |
| 682 | Done(); |
| 683 | } |
| 684 | |
| 685 | private: |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 686 | WaitableEvent *client1_msg_received_, *client1_can_reply_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 687 | }; |
| 688 | |
| 689 | class MultipleServer2 : public Worker { |
| 690 | public: |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 691 | MultipleServer2() : Worker("test_channel2", Channel::MODE_SERVER) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 692 | |
| 693 | void OnAnswer(int* result) { |
| 694 | *result = 42; |
| 695 | Done(); |
| 696 | } |
| 697 | }; |
| 698 | |
| 699 | class MultipleClient2 : public Worker { |
| 700 | public: |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 701 | MultipleClient2( |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 702 | WaitableEvent* client1_msg_received, WaitableEvent* client1_can_reply, |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 703 | bool pump_during_send) |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 704 | : Worker("test_channel2", Channel::MODE_CLIENT), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 705 | client1_msg_received_(client1_msg_received), |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 706 | client1_can_reply_(client1_can_reply), |
| 707 | pump_during_send_(pump_during_send) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 708 | |
| 709 | void Run() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 710 | client1_msg_received_->Wait(); |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 711 | SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 712 | client1_can_reply_->Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 713 | Done(); |
| 714 | } |
| 715 | |
| 716 | private: |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 717 | WaitableEvent *client1_msg_received_, *client1_can_reply_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 718 | bool pump_during_send_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 719 | }; |
| 720 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 721 | void Multiple(bool server_pump, bool client_pump) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 722 | std::vector<Worker*> workers; |
| 723 | |
| 724 | // A shared worker thread so that server1 and server2 run on one thread. |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 725 | base::Thread worker_thread("Multiple"); |
[email protected] | 6314e6f6 | 2009-07-15 16:07:14 | [diff] [blame] | 726 | ASSERT_TRUE(worker_thread.Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 727 | |
| 728 | // Server1 sends a sync msg to client1, which blocks the reply until |
| 729 | // server2 (which runs on the same worker thread as server1) responds |
| 730 | // to a sync msg from client2. |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 731 | WaitableEvent client1_msg_received(false, false); |
| 732 | WaitableEvent client1_can_reply(false, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 733 | |
| 734 | Worker* worker; |
| 735 | |
| 736 | worker = new MultipleServer2(); |
| 737 | worker->OverrideThread(&worker_thread); |
| 738 | workers.push_back(worker); |
| 739 | |
| 740 | worker = new MultipleClient2( |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 741 | &client1_msg_received, &client1_can_reply, client_pump); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 742 | workers.push_back(worker); |
| 743 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 744 | worker = new MultipleServer1(server_pump); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 745 | worker->OverrideThread(&worker_thread); |
| 746 | workers.push_back(worker); |
| 747 | |
| 748 | worker = new MultipleClient1( |
| 749 | &client1_msg_received, &client1_can_reply); |
| 750 | workers.push_back(worker); |
| 751 | |
| 752 | RunTest(workers); |
| 753 | } |
| 754 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 755 | } // namespace |
| 756 | |
| 757 | // Tests that multiple SyncObjects on the same listener thread can unblock each |
| 758 | // other. |
| 759 | TEST_F(IPCSyncChannelTest, Multiple) { |
| 760 | Multiple(false, false); |
| 761 | Multiple(false, true); |
| 762 | Multiple(true, false); |
| 763 | Multiple(true, true); |
| 764 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 765 | |
| 766 | //----------------------------------------------------------------------------- |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 767 | |
| 768 | namespace { |
| 769 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 770 | // This class provides server side functionality to test the case where |
| 771 | // multiple sync channels are in use on the same thread on the client and |
| 772 | // nested calls are issued. |
| 773 | class QueuedReplyServer : public Worker { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 774 | public: |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 775 | QueuedReplyServer(base::Thread* listener_thread, |
| 776 | const std::string& channel_name, |
| 777 | const std::string& reply_text) |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 778 | : Worker(channel_name, Channel::MODE_SERVER), |
| 779 | reply_text_(reply_text) { |
| 780 | Worker::OverrideThread(listener_thread); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 781 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 782 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 783 | virtual void OnNestedTestMsg(Message* reply_msg) { |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 784 | VLOG(1) << __FUNCTION__ << " Sending reply: " << reply_text_; |
| 785 | SyncChannelNestedTestMsg_String::WriteReplyParams(reply_msg, reply_text_); |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 786 | Send(reply_msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 787 | Done(); |
| 788 | } |
| 789 | |
| 790 | private: |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 791 | std::string reply_text_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 792 | }; |
| 793 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 794 | // The QueuedReplyClient class provides functionality to test the case where |
| 795 | // multiple sync channels are in use on the same thread and they make nested |
| 796 | // sync calls, i.e. while the first channel waits for a response it makes a |
| 797 | // sync call on another channel. |
| 798 | // The callstack should unwind correctly, i.e. the outermost call should |
| 799 | // complete first, and so on. |
| 800 | class QueuedReplyClient : public Worker { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 801 | public: |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 802 | QueuedReplyClient(base::Thread* listener_thread, |
| 803 | const std::string& channel_name, |
| 804 | const std::string& expected_text, |
| 805 | bool pump_during_send) |
| 806 | : Worker(channel_name, Channel::MODE_CLIENT), |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 807 | pump_during_send_(pump_during_send), |
| 808 | expected_text_(expected_text) { |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 809 | Worker::OverrideThread(listener_thread); |
| 810 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 811 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 812 | virtual void Run() { |
| 813 | std::string response; |
| 814 | SyncMessage* msg = new SyncChannelNestedTestMsg_String(&response); |
| 815 | if (pump_during_send_) |
| 816 | msg->EnableMessagePumping(); |
| 817 | bool result = Send(msg); |
| 818 | DCHECK(result); |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 819 | DCHECK_EQ(response, expected_text_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 820 | |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 821 | VLOG(1) << __FUNCTION__ << " Received reply: " << response; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 822 | Done(); |
| 823 | } |
| 824 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 825 | private: |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 826 | bool pump_during_send_; |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 827 | std::string expected_text_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 828 | }; |
| 829 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 830 | void QueuedReply(bool client_pump) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 831 | std::vector<Worker*> workers; |
| 832 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 833 | // A shared worker thread for servers |
| 834 | base::Thread server_worker_thread("QueuedReply_ServerListener"); |
| 835 | ASSERT_TRUE(server_worker_thread.Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 836 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 837 | base::Thread client_worker_thread("QueuedReply_ClientListener"); |
| 838 | ASSERT_TRUE(client_worker_thread.Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 839 | |
| 840 | Worker* worker; |
| 841 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 842 | worker = new QueuedReplyServer(&server_worker_thread, |
| 843 | "QueuedReply_Server1", |
| 844 | "Got first message"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 845 | workers.push_back(worker); |
| 846 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 847 | worker = new QueuedReplyServer(&server_worker_thread, |
| 848 | "QueuedReply_Server2", |
| 849 | "Got second message"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 850 | workers.push_back(worker); |
| 851 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 852 | worker = new QueuedReplyClient(&client_worker_thread, |
| 853 | "QueuedReply_Server1", |
| 854 | "Got first message", |
| 855 | client_pump); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 856 | workers.push_back(worker); |
| 857 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 858 | worker = new QueuedReplyClient(&client_worker_thread, |
| 859 | "QueuedReply_Server2", |
| 860 | "Got second message", |
| 861 | client_pump); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 862 | workers.push_back(worker); |
| 863 | |
| 864 | RunTest(workers); |
| 865 | } |
| 866 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 867 | } // namespace |
| 868 | |
| 869 | // While a blocking send is in progress, the listener thread might answer other |
| 870 | // synchronous messages. This tests that if during the response to another |
| 871 | // message the reply to the original messages comes, it is queued up correctly |
| 872 | // and the original Send is unblocked later. |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 873 | // We also test that the send call stacks unwind correctly when the channel |
| 874 | // pumps messages while waiting for a response. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 875 | TEST_F(IPCSyncChannelTest, QueuedReply) { |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 876 | QueuedReply(false); |
| 877 | QueuedReply(true); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 878 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 879 | |
| 880 | //----------------------------------------------------------------------------- |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 881 | |
| 882 | namespace { |
| 883 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 884 | class ChattyClient : public Worker { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 885 | public: |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 886 | ChattyClient() : |
| 887 | Worker(Channel::MODE_CLIENT, "chatty_client") { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 888 | |
| 889 | void OnAnswer(int* answer) { |
| 890 | // The PostMessage limit is 10k. Send 20% more than that. |
| 891 | const int kMessageLimit = 10000; |
| 892 | const int kMessagesToSend = kMessageLimit * 120 / 100; |
| 893 | for (int i = 0; i < kMessagesToSend; ++i) { |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 894 | if (!SendDouble(false, true)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 895 | break; |
| 896 | } |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 897 | *answer = 42; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 898 | Done(); |
| 899 | } |
| 900 | }; |
| 901 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 902 | void ChattyServer(bool pump_during_send) { |
| 903 | std::vector<Worker*> workers; |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 904 | workers.push_back(new UnblockServer(pump_during_send, false)); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 905 | workers.push_back(new ChattyClient()); |
| 906 | RunTest(workers); |
| 907 | } |
| 908 | |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 909 | } // namespace |
| 910 | |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 911 | // Tests http://b/1093251 - that sending lots of sync messages while |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 912 | // the receiver is waiting for a sync reply does not overflow the PostMessage |
| 913 | // queue. |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 914 | TEST_F(IPCSyncChannelTest, ChattyServer) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 915 | ChattyServer(false); |
| 916 | ChattyServer(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 917 | } |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 918 | |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 919 | //------------------------------------------------------------------------------ |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 920 | |
| 921 | namespace { |
| 922 | |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 923 | class TimeoutServer : public Worker { |
| 924 | public: |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 925 | TimeoutServer(int timeout_ms, |
| 926 | std::vector<bool> timeout_seq, |
| 927 | bool pump_during_send) |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 928 | : Worker(Channel::MODE_SERVER, "timeout_server"), |
| 929 | timeout_ms_(timeout_ms), |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 930 | timeout_seq_(timeout_seq), |
| 931 | pump_during_send_(pump_during_send) { |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | void Run() { |
| 935 | for (std::vector<bool>::const_iterator iter = timeout_seq_.begin(); |
| 936 | iter != timeout_seq_.end(); ++iter) { |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 937 | SendAnswerToLife(pump_during_send_, timeout_ms_, !*iter); |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 938 | } |
| 939 | Done(); |
| 940 | } |
| 941 | |
| 942 | private: |
| 943 | int timeout_ms_; |
| 944 | std::vector<bool> timeout_seq_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 945 | bool pump_during_send_; |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 946 | }; |
| 947 | |
| 948 | class UnresponsiveClient : public Worker { |
| 949 | public: |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 950 | explicit UnresponsiveClient(std::vector<bool> timeout_seq) |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 951 | : Worker(Channel::MODE_CLIENT, "unresponsive_client"), |
| 952 | timeout_seq_(timeout_seq) { |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 953 | } |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 954 | |
| 955 | void OnAnswerDelay(Message* reply_msg) { |
| 956 | DCHECK(!timeout_seq_.empty()); |
| 957 | if (!timeout_seq_[0]) { |
| 958 | SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 959 | Send(reply_msg); |
| 960 | } else { |
| 961 | // Don't reply. |
[email protected] | 46366737 | 2008-08-20 20:20:52 | [diff] [blame] | 962 | delete reply_msg; |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 963 | } |
| 964 | timeout_seq_.erase(timeout_seq_.begin()); |
| 965 | if (timeout_seq_.empty()) |
| 966 | Done(); |
| 967 | } |
| 968 | |
| 969 | private: |
| 970 | // Whether we should time-out or respond to the various messages we receive. |
| 971 | std::vector<bool> timeout_seq_; |
| 972 | }; |
| 973 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 974 | void SendWithTimeoutOK(bool pump_during_send) { |
| 975 | std::vector<Worker*> workers; |
| 976 | std::vector<bool> timeout_seq; |
| 977 | timeout_seq.push_back(false); |
| 978 | timeout_seq.push_back(false); |
| 979 | timeout_seq.push_back(false); |
| 980 | workers.push_back(new TimeoutServer(5000, timeout_seq, pump_during_send)); |
| 981 | workers.push_back(new SimpleClient()); |
| 982 | RunTest(workers); |
| 983 | } |
| 984 | |
| 985 | void SendWithTimeoutTimeout(bool pump_during_send) { |
| 986 | std::vector<Worker*> workers; |
| 987 | std::vector<bool> timeout_seq; |
| 988 | timeout_seq.push_back(true); |
| 989 | timeout_seq.push_back(false); |
| 990 | timeout_seq.push_back(false); |
| 991 | workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send)); |
| 992 | workers.push_back(new UnresponsiveClient(timeout_seq)); |
| 993 | RunTest(workers); |
| 994 | } |
| 995 | |
| 996 | void SendWithTimeoutMixedOKAndTimeout(bool pump_during_send) { |
| 997 | std::vector<Worker*> workers; |
| 998 | std::vector<bool> timeout_seq; |
| 999 | timeout_seq.push_back(true); |
| 1000 | timeout_seq.push_back(false); |
| 1001 | timeout_seq.push_back(false); |
| 1002 | timeout_seq.push_back(true); |
| 1003 | timeout_seq.push_back(false); |
| 1004 | workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send)); |
| 1005 | workers.push_back(new UnresponsiveClient(timeout_seq)); |
| 1006 | RunTest(workers); |
| 1007 | } |
| 1008 | |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 1009 | } // namespace |
| 1010 | |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 1011 | // Tests that SendWithTimeout does not time-out if the response comes back fast |
| 1012 | // enough. |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 1013 | TEST_F(IPCSyncChannelTest, SendWithTimeoutOK) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 1014 | SendWithTimeoutOK(false); |
| 1015 | SendWithTimeoutOK(true); |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | // Tests that SendWithTimeout does time-out. |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 1019 | TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 1020 | SendWithTimeoutTimeout(false); |
| 1021 | SendWithTimeoutTimeout(true); |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | // Sends some message that time-out and some that succeed. |
[email protected] | 711032e | 2011-01-19 08:11:56 | [diff] [blame] | 1025 | // Crashes flakily, http://crbug.com/70075. |
| 1026 | TEST_F(IPCSyncChannelTest, DISABLED_SendWithTimeoutMixedOKAndTimeout) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 1027 | SendWithTimeoutMixedOKAndTimeout(false); |
| 1028 | SendWithTimeoutMixedOKAndTimeout(true); |
| 1029 | } |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 1030 | |
| 1031 | //------------------------------------------------------------------------------ |
| 1032 | |
| 1033 | namespace { |
| 1034 | |
[email protected] | 0633e315 | 2011-11-28 18:35:39 | [diff] [blame] | 1035 | void NestedCallback(Worker* server) { |
| 1036 | // Sleep a bit so that we wake up after the reply has been received. |
[email protected] | b539333 | 2012-01-13 00:11:01 | [diff] [blame^] | 1037 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(250)); |
[email protected] | 0633e315 | 2011-11-28 18:35:39 | [diff] [blame] | 1038 | server->SendAnswerToLife(true, base::kNoTimeout, true); |
| 1039 | } |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 1040 | |
[email protected] | 0633e315 | 2011-11-28 18:35:39 | [diff] [blame] | 1041 | bool timeout_occurred = false; |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 1042 | |
[email protected] | 0633e315 | 2011-11-28 18:35:39 | [diff] [blame] | 1043 | void TimeoutCallback() { |
| 1044 | timeout_occurred = true; |
| 1045 | } |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 1046 | |
| 1047 | class DoneEventRaceServer : public Worker { |
| 1048 | public: |
| 1049 | DoneEventRaceServer() |
| 1050 | : Worker(Channel::MODE_SERVER, "done_event_race_server") { } |
| 1051 | |
| 1052 | void Run() { |
[email protected] | 0633e315 | 2011-11-28 18:35:39 | [diff] [blame] | 1053 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1054 | base::Bind(&NestedCallback, this)); |
| 1055 | MessageLoop::current()->PostDelayedTask( |
| 1056 | FROM_HERE, base::Bind(&TimeoutCallback), 9000); |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 1057 | // Even though we have a timeout on the Send, it will succeed since for this |
| 1058 | // bug, the reply message comes back and is deserialized, however the done |
| 1059 | // event wasn't set. So we indirectly use the timeout task to notice if a |
| 1060 | // timeout occurred. |
| 1061 | SendAnswerToLife(true, 10000, true); |
[email protected] | 0633e315 | 2011-11-28 18:35:39 | [diff] [blame] | 1062 | DCHECK(!timeout_occurred); |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 1063 | Done(); |
| 1064 | } |
| 1065 | }; |
| 1066 | |
| 1067 | } // namespace |
| 1068 | |
| 1069 | // Tests http://b/1474092 - that if after the done_event is set but before |
| 1070 | // OnObjectSignaled is called another message is sent out, then after its |
| 1071 | // reply comes back OnObjectSignaled will be called for the first message. |
| 1072 | TEST_F(IPCSyncChannelTest, DoneEventRace) { |
| 1073 | std::vector<Worker*> workers; |
| 1074 | workers.push_back(new DoneEventRaceServer()); |
| 1075 | workers.push_back(new SimpleClient()); |
| 1076 | RunTest(workers); |
| 1077 | } |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 1078 | |
| 1079 | //----------------------------------------------------------------------------- |
| 1080 | |
| 1081 | namespace { |
| 1082 | |
[email protected] | c9e0c733 | 2011-05-13 22:42:41 | [diff] [blame] | 1083 | class TestSyncMessageFilter : public SyncMessageFilter { |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 1084 | public: |
| 1085 | TestSyncMessageFilter(base::WaitableEvent* shutdown_event, Worker* worker) |
| 1086 | : SyncMessageFilter(shutdown_event), |
| 1087 | worker_(worker), |
| 1088 | thread_("helper_thread") { |
| 1089 | base::Thread::Options options; |
| 1090 | options.message_loop_type = MessageLoop::TYPE_DEFAULT; |
| 1091 | thread_.StartWithOptions(options); |
| 1092 | } |
| 1093 | |
| 1094 | virtual void OnFilterAdded(Channel* channel) { |
| 1095 | SyncMessageFilter::OnFilterAdded(channel); |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 1096 | thread_.message_loop()->PostTask( |
| 1097 | FROM_HERE, |
| 1098 | base::Bind(&TestSyncMessageFilter::SendMessageOnHelperThread, this)); |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | void SendMessageOnHelperThread() { |
| 1102 | int answer = 0; |
| 1103 | bool result = Send(new SyncChannelTestMsg_AnswerToLife(&answer)); |
| 1104 | DCHECK(result); |
| 1105 | DCHECK_EQ(answer, 42); |
| 1106 | |
| 1107 | worker_->Done(); |
| 1108 | } |
| 1109 | |
| 1110 | Worker* worker_; |
| 1111 | base::Thread thread_; |
| 1112 | }; |
| 1113 | |
| 1114 | class SyncMessageFilterServer : public Worker { |
| 1115 | public: |
| 1116 | SyncMessageFilterServer() |
| 1117 | : Worker(Channel::MODE_SERVER, "sync_message_filter_server") { |
| 1118 | filter_ = new TestSyncMessageFilter(shutdown_event(), this); |
| 1119 | } |
| 1120 | |
| 1121 | void Run() { |
| 1122 | channel()->AddFilter(filter_.get()); |
| 1123 | } |
| 1124 | |
| 1125 | scoped_refptr<TestSyncMessageFilter> filter_; |
| 1126 | }; |
| 1127 | |
[email protected] | 87339f0 | 2010-09-02 21:45:50 | [diff] [blame] | 1128 | // This class provides functionality to test the case that a Send on the sync |
| 1129 | // channel does not crash after the channel has been closed. |
| 1130 | class ServerSendAfterClose : public Worker { |
| 1131 | public: |
| 1132 | ServerSendAfterClose() |
| 1133 | : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 1134 | send_result_(true) { |
| 1135 | } |
| 1136 | |
| 1137 | bool SendDummy() { |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 1138 | ListenerThread()->message_loop()->PostTask( |
[email protected] | dcde767 | 2012-01-06 02:37:17 | [diff] [blame] | 1139 | FROM_HERE, base::Bind(base::IgnoreResult(&ServerSendAfterClose::Send), |
| 1140 | this, new SyncChannelTestMsg_NoArgs)); |
[email protected] | 87339f0 | 2010-09-02 21:45:50 | [diff] [blame] | 1141 | return true; |
| 1142 | } |
| 1143 | |
| 1144 | bool send_result() const { |
| 1145 | return send_result_; |
| 1146 | } |
| 1147 | |
| 1148 | private: |
| 1149 | virtual void Run() { |
| 1150 | CloseChannel(); |
| 1151 | Done(); |
| 1152 | } |
| 1153 | |
| 1154 | bool Send(Message* msg) { |
| 1155 | send_result_ = Worker::Send(msg); |
| 1156 | Done(); |
| 1157 | return send_result_; |
| 1158 | } |
| 1159 | |
| 1160 | bool send_result_; |
| 1161 | }; |
| 1162 | |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 1163 | } // namespace |
| 1164 | |
| 1165 | // Tests basic synchronous call |
| 1166 | TEST_F(IPCSyncChannelTest, SyncMessageFilter) { |
| 1167 | std::vector<Worker*> workers; |
| 1168 | workers.push_back(new SyncMessageFilterServer()); |
| 1169 | workers.push_back(new SimpleClient()); |
| 1170 | RunTest(workers); |
| 1171 | } |
[email protected] | 87339f0 | 2010-09-02 21:45:50 | [diff] [blame] | 1172 | |
| 1173 | // Test the case when the channel is closed and a Send is attempted after that. |
| 1174 | TEST_F(IPCSyncChannelTest, SendAfterClose) { |
| 1175 | ServerSendAfterClose server; |
| 1176 | server.Start(); |
| 1177 | |
| 1178 | server.done_event()->Wait(); |
| 1179 | server.done_event()->Reset(); |
| 1180 | |
| 1181 | server.SendDummy(); |
| 1182 | server.done_event()->Wait(); |
| 1183 | |
| 1184 | EXPECT_FALSE(server.send_result()); |
| 1185 | } |
| 1186 | |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 1187 | //----------------------------------------------------------------------------- |
[email protected] | 87339f0 | 2010-09-02 21:45:50 | [diff] [blame] | 1188 | |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 1189 | namespace { |
| 1190 | |
| 1191 | class RestrictedDispatchServer : public Worker { |
| 1192 | public: |
| 1193 | RestrictedDispatchServer(WaitableEvent* sent_ping_event) |
| 1194 | : Worker("restricted_channel", Channel::MODE_SERVER), |
| 1195 | sent_ping_event_(sent_ping_event) { } |
| 1196 | |
| 1197 | void OnDoPing(int ping) { |
| 1198 | // Send an asynchronous message that unblocks the caller. |
[email protected] | c9e0c733 | 2011-05-13 22:42:41 | [diff] [blame] | 1199 | Message* msg = new SyncChannelTestMsg_Ping(ping); |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 1200 | msg->set_unblock(true); |
| 1201 | Send(msg); |
| 1202 | // Signal the event after the message has been sent on the channel, on the |
| 1203 | // IPC thread. |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 1204 | ipc_thread().message_loop()->PostTask( |
| 1205 | FROM_HERE, base::Bind(&RestrictedDispatchServer::OnPingSent, this)); |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | base::Thread* ListenerThread() { return Worker::ListenerThread(); } |
| 1209 | |
| 1210 | private: |
| 1211 | bool OnMessageReceived(const Message& message) { |
| 1212 | IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchServer, message) |
| 1213 | IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs) |
| 1214 | IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done) |
| 1215 | IPC_END_MESSAGE_MAP() |
| 1216 | return true; |
| 1217 | } |
| 1218 | |
| 1219 | void OnPingSent() { |
| 1220 | sent_ping_event_->Signal(); |
| 1221 | } |
| 1222 | |
| 1223 | void OnNoArgs() { } |
| 1224 | WaitableEvent* sent_ping_event_; |
| 1225 | }; |
| 1226 | |
| 1227 | class NonRestrictedDispatchServer : public Worker { |
| 1228 | public: |
| 1229 | NonRestrictedDispatchServer() |
| 1230 | : Worker("non_restricted_channel", Channel::MODE_SERVER) {} |
| 1231 | |
| 1232 | private: |
| 1233 | bool OnMessageReceived(const Message& message) { |
| 1234 | IPC_BEGIN_MESSAGE_MAP(NonRestrictedDispatchServer, message) |
| 1235 | IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs) |
| 1236 | IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done) |
| 1237 | IPC_END_MESSAGE_MAP() |
| 1238 | return true; |
| 1239 | } |
| 1240 | |
| 1241 | void OnNoArgs() { } |
| 1242 | }; |
| 1243 | |
| 1244 | class RestrictedDispatchClient : public Worker { |
| 1245 | public: |
| 1246 | RestrictedDispatchClient(WaitableEvent* sent_ping_event, |
| 1247 | RestrictedDispatchServer* server, |
| 1248 | int* success) |
| 1249 | : Worker("restricted_channel", Channel::MODE_CLIENT), |
| 1250 | ping_(0), |
| 1251 | server_(server), |
| 1252 | success_(success), |
| 1253 | sent_ping_event_(sent_ping_event) {} |
| 1254 | |
| 1255 | void Run() { |
| 1256 | // Incoming messages from our channel should only be dispatched when we |
| 1257 | // send a message on that same channel. |
| 1258 | channel()->SetRestrictDispatchToSameChannel(true); |
| 1259 | |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 1260 | server_->ListenerThread()->message_loop()->PostTask( |
| 1261 | FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 1)); |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 1262 | sent_ping_event_->Wait(); |
| 1263 | Send(new SyncChannelTestMsg_NoArgs); |
| 1264 | if (ping_ == 1) |
| 1265 | ++*success_; |
| 1266 | else |
| 1267 | LOG(ERROR) << "Send failed to dispatch incoming message on same channel"; |
| 1268 | |
| 1269 | scoped_ptr<SyncChannel> non_restricted_channel(new SyncChannel( |
| 1270 | "non_restricted_channel", Channel::MODE_CLIENT, this, |
[email protected] | 92bf906 | 2011-05-02 18:00:49 | [diff] [blame] | 1271 | ipc_thread().message_loop_proxy(), true, shutdown_event())); |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 1272 | |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 1273 | server_->ListenerThread()->message_loop()->PostTask( |
| 1274 | FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 2)); |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 1275 | sent_ping_event_->Wait(); |
| 1276 | // Check that the incoming message is *not* dispatched when sending on the |
| 1277 | // non restricted channel. |
| 1278 | // TODO(piman): there is a possibility of a false positive race condition |
| 1279 | // here, if the message that was posted on the server-side end of the pipe |
| 1280 | // is not visible yet on the client side, but I don't know how to solve this |
| 1281 | // without hooking into the internals of SyncChannel. I haven't seen it in |
| 1282 | // practice (i.e. not setting SetRestrictDispatchToSameChannel does cause |
| 1283 | // the following to fail). |
| 1284 | non_restricted_channel->Send(new SyncChannelTestMsg_NoArgs); |
| 1285 | if (ping_ == 1) |
| 1286 | ++*success_; |
| 1287 | else |
| 1288 | LOG(ERROR) << "Send dispatched message from restricted channel"; |
| 1289 | |
| 1290 | Send(new SyncChannelTestMsg_NoArgs); |
| 1291 | if (ping_ == 2) |
| 1292 | ++*success_; |
| 1293 | else |
| 1294 | LOG(ERROR) << "Send failed to dispatch incoming message on same channel"; |
| 1295 | |
| 1296 | non_restricted_channel->Send(new SyncChannelTestMsg_Done); |
| 1297 | non_restricted_channel.reset(); |
| 1298 | Send(new SyncChannelTestMsg_Done); |
| 1299 | Done(); |
| 1300 | } |
| 1301 | |
| 1302 | private: |
| 1303 | bool OnMessageReceived(const Message& message) { |
| 1304 | IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchClient, message) |
| 1305 | IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Ping, OnPing) |
| 1306 | IPC_END_MESSAGE_MAP() |
| 1307 | return true; |
| 1308 | } |
| 1309 | |
| 1310 | void OnPing(int ping) { |
| 1311 | ping_ = ping; |
| 1312 | } |
| 1313 | |
| 1314 | int ping_; |
| 1315 | RestrictedDispatchServer* server_; |
| 1316 | int* success_; |
| 1317 | WaitableEvent* sent_ping_event_; |
| 1318 | }; |
| 1319 | |
| 1320 | } // namespace |
| 1321 | |
| 1322 | TEST_F(IPCSyncChannelTest, RestrictedDispatch) { |
| 1323 | WaitableEvent sent_ping_event(false, false); |
| 1324 | |
| 1325 | RestrictedDispatchServer* server = |
| 1326 | new RestrictedDispatchServer(&sent_ping_event); |
| 1327 | int success = 0; |
| 1328 | std::vector<Worker*> workers; |
| 1329 | workers.push_back(new NonRestrictedDispatchServer); |
| 1330 | workers.push_back(server); |
| 1331 | workers.push_back( |
| 1332 | new RestrictedDispatchClient(&sent_ping_event, server, &success)); |
| 1333 | RunTest(workers); |
| 1334 | EXPECT_EQ(3, success); |
| 1335 | } |
[email protected] | c9e0c733 | 2011-05-13 22:42:41 | [diff] [blame] | 1336 | |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 1337 | //----------------------------------------------------------------------------- |
| 1338 | |
| 1339 | // This test case inspired by crbug.com/108491 |
| 1340 | // We create two servers that use the same ListenerThread but have |
| 1341 | // SetRestrictDispatchToSameChannel set to true. |
| 1342 | // We create clients, then use some specific WaitableEvent wait/signalling to |
| 1343 | // ensure that messages get dispatched in a way that causes a deadlock due to |
| 1344 | // a nested dispatch and an eligible message in a higher-level dispatch's |
| 1345 | // delayed_queue. Specifically, we start with client1 about so send an |
| 1346 | // unblocking message to server1, while the shared listener thread for the |
| 1347 | // servers server1 and server2 is about to send a non-unblocking message to |
| 1348 | // client1. At the same time, client2 will be about to send an unblocking |
| 1349 | // message to server2. Server1 will handle the client1->server1 message by |
| 1350 | // telling server2 to send a non-unblocking message to client2. |
| 1351 | // What should happen is that the send to server2 should find the pending, |
| 1352 | // same-context client2->server2 message to dispatch, causing client2 to |
| 1353 | // unblock then handle the server2->client2 message, so that the shared |
| 1354 | // servers' listener thread can then respond to the client1->server1 message. |
| 1355 | // Then client1 can handle the non-unblocking server1->client1 message. |
| 1356 | // The old code would end up in a state where the server2->client2 message is |
| 1357 | // sent, but the client2->server2 message (which is eligible for dispatch, and |
| 1358 | // which is what client2 is waiting for) is stashed in a local delayed_queue |
| 1359 | // that has server1's channel context, causing a deadlock. |
| 1360 | // WaitableEvents in the events array are used to: |
| 1361 | // event 0: indicate to client1 that server listener is in OnDoServerTask |
| 1362 | // event 1: indicate to client1 that client2 listener is in OnDoClient2Task |
| 1363 | // event 2: indicate to server1 that client2 listener is in OnDoClient2Task |
| 1364 | // event 3: indicate to client2 that server listener is in OnDoServerTask |
| 1365 | |
| 1366 | namespace { |
| 1367 | |
| 1368 | class RestrictedDispatchDeadlockServer : public Worker { |
| 1369 | public: |
| 1370 | RestrictedDispatchDeadlockServer(int server_num, |
| 1371 | WaitableEvent* server_ready_event, |
| 1372 | WaitableEvent** events, |
| 1373 | RestrictedDispatchDeadlockServer* peer) |
| 1374 | : Worker(server_num == 1 ? "channel1" : "channel2", Channel::MODE_SERVER), |
| 1375 | server_num_(server_num), |
| 1376 | server_ready_event_(server_ready_event), |
| 1377 | events_(events), |
| 1378 | peer_(peer), |
| 1379 | client_kicked_(false) { } |
| 1380 | |
| 1381 | void OnDoServerTask() { |
| 1382 | events_[3]->Signal(); |
| 1383 | events_[2]->Wait(); |
| 1384 | events_[0]->Signal(); |
| 1385 | SendMessageToClient(); |
| 1386 | } |
| 1387 | |
| 1388 | void Run() { |
| 1389 | channel()->SetRestrictDispatchToSameChannel(true); |
| 1390 | server_ready_event_->Signal(); |
| 1391 | } |
| 1392 | |
| 1393 | base::Thread* ListenerThread() { return Worker::ListenerThread(); } |
| 1394 | |
| 1395 | private: |
| 1396 | bool OnMessageReceived(const Message& message) { |
| 1397 | IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchDeadlockServer, message) |
| 1398 | IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs) |
| 1399 | IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done) |
| 1400 | IPC_END_MESSAGE_MAP() |
| 1401 | return true; |
| 1402 | } |
| 1403 | |
| 1404 | void OnNoArgs() { |
| 1405 | if (server_num_ == 1) { |
| 1406 | DCHECK(peer_ != NULL); |
| 1407 | peer_->SendMessageToClient(); |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | void SendMessageToClient() { |
| 1412 | Message* msg = new SyncChannelTestMsg_NoArgs; |
| 1413 | msg->set_unblock(false); |
| 1414 | DCHECK(!msg->should_unblock()); |
| 1415 | Send(msg); |
| 1416 | } |
| 1417 | |
| 1418 | int server_num_; |
| 1419 | WaitableEvent* server_ready_event_; |
| 1420 | WaitableEvent** events_; |
| 1421 | RestrictedDispatchDeadlockServer* peer_; |
| 1422 | bool client_kicked_; |
| 1423 | }; |
| 1424 | |
| 1425 | class RestrictedDispatchDeadlockClient2 : public Worker { |
| 1426 | public: |
| 1427 | RestrictedDispatchDeadlockClient2(RestrictedDispatchDeadlockServer* server, |
| 1428 | WaitableEvent* server_ready_event, |
| 1429 | WaitableEvent** events) |
| 1430 | : Worker("channel2", Channel::MODE_CLIENT), |
| 1431 | server_(server), |
| 1432 | server_ready_event_(server_ready_event), |
| 1433 | events_(events), |
| 1434 | received_msg_(false), |
| 1435 | received_noarg_reply_(false), |
| 1436 | done_issued_(false) {} |
| 1437 | |
| 1438 | void Run() { |
| 1439 | server_ready_event_->Wait(); |
| 1440 | } |
| 1441 | |
| 1442 | void OnDoClient2Task() { |
| 1443 | events_[3]->Wait(); |
| 1444 | events_[1]->Signal(); |
| 1445 | events_[2]->Signal(); |
| 1446 | DCHECK(received_msg_ == false); |
| 1447 | |
| 1448 | Message* message = new SyncChannelTestMsg_NoArgs; |
| 1449 | message->set_unblock(true); |
| 1450 | Send(message); |
| 1451 | received_noarg_reply_ = true; |
| 1452 | } |
| 1453 | |
| 1454 | base::Thread* ListenerThread() { return Worker::ListenerThread(); } |
| 1455 | private: |
| 1456 | bool OnMessageReceived(const Message& message) { |
| 1457 | IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchDeadlockClient2, message) |
| 1458 | IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs) |
| 1459 | IPC_END_MESSAGE_MAP() |
| 1460 | return true; |
| 1461 | } |
| 1462 | |
| 1463 | void OnNoArgs() { |
| 1464 | received_msg_ = true; |
| 1465 | PossiblyDone(); |
| 1466 | } |
| 1467 | |
| 1468 | void PossiblyDone() { |
| 1469 | if (received_noarg_reply_ && received_msg_) { |
| 1470 | DCHECK(done_issued_ == false); |
| 1471 | done_issued_ = true; |
| 1472 | Send(new SyncChannelTestMsg_Done); |
| 1473 | Done(); |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | RestrictedDispatchDeadlockServer* server_; |
| 1478 | WaitableEvent* server_ready_event_; |
| 1479 | WaitableEvent** events_; |
| 1480 | bool received_msg_; |
| 1481 | bool received_noarg_reply_; |
| 1482 | bool done_issued_; |
| 1483 | }; |
| 1484 | |
| 1485 | class RestrictedDispatchDeadlockClient1 : public Worker { |
| 1486 | public: |
| 1487 | RestrictedDispatchDeadlockClient1(RestrictedDispatchDeadlockServer* server, |
| 1488 | RestrictedDispatchDeadlockClient2* peer, |
| 1489 | WaitableEvent* server_ready_event, |
| 1490 | WaitableEvent** events) |
| 1491 | : Worker("channel1", Channel::MODE_CLIENT), |
| 1492 | server_(server), |
| 1493 | peer_(peer), |
| 1494 | server_ready_event_(server_ready_event), |
| 1495 | events_(events), |
| 1496 | received_msg_(false), |
| 1497 | received_noarg_reply_(false), |
| 1498 | done_issued_(false) {} |
| 1499 | |
| 1500 | void Run() { |
| 1501 | server_ready_event_->Wait(); |
| 1502 | server_->ListenerThread()->message_loop()->PostTask( |
| 1503 | FROM_HERE, |
| 1504 | base::Bind(&RestrictedDispatchDeadlockServer::OnDoServerTask, server_)); |
| 1505 | peer_->ListenerThread()->message_loop()->PostTask( |
| 1506 | FROM_HERE, |
| 1507 | base::Bind(&RestrictedDispatchDeadlockClient2::OnDoClient2Task, peer_)); |
| 1508 | events_[0]->Wait(); |
| 1509 | events_[1]->Wait(); |
| 1510 | DCHECK(received_msg_ == false); |
| 1511 | |
| 1512 | Message* message = new SyncChannelTestMsg_NoArgs; |
| 1513 | message->set_unblock(true); |
| 1514 | Send(message); |
| 1515 | received_noarg_reply_ = true; |
| 1516 | PossiblyDone(); |
| 1517 | } |
| 1518 | |
| 1519 | base::Thread* ListenerThread() { return Worker::ListenerThread(); } |
| 1520 | private: |
| 1521 | bool OnMessageReceived(const Message& message) { |
| 1522 | IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchDeadlockClient1, message) |
| 1523 | IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs) |
| 1524 | IPC_END_MESSAGE_MAP() |
| 1525 | return true; |
| 1526 | } |
| 1527 | |
| 1528 | void OnNoArgs() { |
| 1529 | received_msg_ = true; |
| 1530 | PossiblyDone(); |
| 1531 | } |
| 1532 | |
| 1533 | void PossiblyDone() { |
| 1534 | if (received_noarg_reply_ && received_msg_) { |
| 1535 | DCHECK(done_issued_ == false); |
| 1536 | done_issued_ = true; |
| 1537 | Send(new SyncChannelTestMsg_Done); |
| 1538 | Done(); |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | RestrictedDispatchDeadlockServer* server_; |
| 1543 | RestrictedDispatchDeadlockClient2* peer_; |
| 1544 | WaitableEvent* server_ready_event_; |
| 1545 | WaitableEvent** events_; |
| 1546 | bool received_msg_; |
| 1547 | bool received_noarg_reply_; |
| 1548 | bool done_issued_; |
| 1549 | }; |
| 1550 | |
| 1551 | } // namespace |
| 1552 | |
| 1553 | TEST_F(IPCSyncChannelTest, RestrictedDispatchDeadlock) { |
| 1554 | std::vector<Worker*> workers; |
| 1555 | |
| 1556 | // A shared worker thread so that server1 and server2 run on one thread. |
| 1557 | base::Thread worker_thread("RestrictedDispatchDeadlock"); |
| 1558 | ASSERT_TRUE(worker_thread.Start()); |
| 1559 | |
| 1560 | WaitableEvent server1_ready(false, false); |
| 1561 | WaitableEvent server2_ready(false, false); |
| 1562 | |
| 1563 | WaitableEvent event0(false, false); |
| 1564 | WaitableEvent event1(false, false); |
| 1565 | WaitableEvent event2(false, false); |
| 1566 | WaitableEvent event3(false, false); |
| 1567 | WaitableEvent* events[4] = {&event0, &event1, &event2, &event3}; |
| 1568 | |
| 1569 | RestrictedDispatchDeadlockServer* server1; |
| 1570 | RestrictedDispatchDeadlockServer* server2; |
| 1571 | RestrictedDispatchDeadlockClient1* client1; |
| 1572 | RestrictedDispatchDeadlockClient2* client2; |
| 1573 | |
| 1574 | server2 = new RestrictedDispatchDeadlockServer(2, &server2_ready, events, |
| 1575 | NULL); |
| 1576 | server2->OverrideThread(&worker_thread); |
| 1577 | workers.push_back(server2); |
| 1578 | |
| 1579 | client2 = new RestrictedDispatchDeadlockClient2(server2, &server2_ready, |
| 1580 | events); |
| 1581 | workers.push_back(client2); |
| 1582 | |
| 1583 | server1 = new RestrictedDispatchDeadlockServer(1, &server1_ready, events, |
| 1584 | server2); |
| 1585 | server1->OverrideThread(&worker_thread); |
| 1586 | workers.push_back(server1); |
| 1587 | |
| 1588 | client1 = new RestrictedDispatchDeadlockClient1(server1, client2, |
| 1589 | &server1_ready, events); |
| 1590 | workers.push_back(client1); |
| 1591 | |
| 1592 | RunTest(workers); |
| 1593 | } |
| 1594 | |
[email protected] | c9e0c733 | 2011-05-13 22:42:41 | [diff] [blame] | 1595 | } // namespace IPC |