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