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