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