OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Unit test for SyncChannel. | 5 // Unit test for SyncChannel. |
6 | 6 |
7 #include "ipc/ipc_sync_channel.h" | 7 #include "ipc/ipc_sync_channel.h" |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 bool SendDouble(bool pump, bool succeed) { | 113 bool SendDouble(bool pump, bool succeed) { |
114 int answer = 0; | 114 int answer = 0; |
115 SyncMessage* msg = new SyncChannelTestMsg_Double(5, &answer); | 115 SyncMessage* msg = new SyncChannelTestMsg_Double(5, &answer); |
116 if (pump) | 116 if (pump) |
117 msg->EnableMessagePumping(); | 117 msg->EnableMessagePumping(); |
118 bool result = Send(msg); | 118 bool result = Send(msg); |
119 DCHECK_EQ(result, succeed); | 119 DCHECK_EQ(result, succeed); |
120 DCHECK_EQ(answer, (succeed ? 10 : 0)); | 120 DCHECK_EQ(answer, (succeed ? 10 : 0)); |
121 return result; | 121 return result; |
122 } | 122 } |
| 123 const std::string& channel_name() { return channel_name_; } |
123 Channel::Mode mode() { return mode_; } | 124 Channel::Mode mode() { return mode_; } |
124 WaitableEvent* done_event() { return done_.get(); } | 125 WaitableEvent* done_event() { return done_.get(); } |
125 WaitableEvent* shutdown_event() { return &shutdown_event_; } | 126 WaitableEvent* shutdown_event() { return &shutdown_event_; } |
126 void ResetChannel() { channel_.reset(); } | 127 void ResetChannel() { channel_.reset(); } |
127 // Derived classes need to call this when they've completed their part of | 128 // Derived classes need to call this when they've completed their part of |
128 // the test. | 129 // the test. |
129 void Done() { done_->Signal(); } | 130 void Done() { done_->Signal(); } |
130 | 131 |
131 protected: | 132 protected: |
132 SyncChannel* channel() { return channel_.get(); } | 133 SyncChannel* channel() { return channel_.get(); } |
(...skipping 16 matching lines...) Expand all Loading... |
149 int result; | 150 int result; |
150 OnDouble(in, &result); | 151 OnDouble(in, &result); |
151 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result); | 152 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result); |
152 Send(reply_msg); | 153 Send(reply_msg); |
153 } | 154 } |
154 | 155 |
155 virtual void OnNestedTestMsg(Message* reply_msg) { | 156 virtual void OnNestedTestMsg(Message* reply_msg) { |
156 NOTREACHED(); | 157 NOTREACHED(); |
157 } | 158 } |
158 | 159 |
| 160 virtual SyncChannel* CreateChannel() { |
| 161 return new SyncChannel( |
| 162 channel_name_, mode_, this, ipc_thread_.message_loop_proxy(), true, |
| 163 &shutdown_event_); |
| 164 } |
| 165 |
159 base::Thread* ListenerThread() { | 166 base::Thread* ListenerThread() { |
160 return overrided_thread_ ? overrided_thread_ : &listener_thread_; | 167 return overrided_thread_ ? overrided_thread_ : &listener_thread_; |
161 } | 168 } |
162 | 169 |
163 const base::Thread& ipc_thread() const { return ipc_thread_; } | 170 const base::Thread& ipc_thread() const { return ipc_thread_; } |
164 | 171 |
165 private: | 172 private: |
166 // Called on the listener thread to create the sync channel. | 173 // Called on the listener thread to create the sync channel. |
167 void OnStart() { | 174 void OnStart() { |
168 // Link ipc_thread_, listener_thread_ and channel_ altogether. | 175 // Link ipc_thread_, listener_thread_ and channel_ altogether. |
169 StartThread(&ipc_thread_, MessageLoop::TYPE_IO); | 176 StartThread(&ipc_thread_, MessageLoop::TYPE_IO); |
170 channel_.reset(new SyncChannel( | 177 channel_.reset(CreateChannel()); |
171 channel_name_, mode_, this, ipc_thread_.message_loop_proxy(), true, | |
172 &shutdown_event_)); | |
173 channel_created_->Signal(); | 178 channel_created_->Signal(); |
174 Run(); | 179 Run(); |
175 } | 180 } |
176 | 181 |
177 void OnListenerThreadShutdown1(WaitableEvent* listener_event, | 182 void OnListenerThreadShutdown1(WaitableEvent* listener_event, |
178 WaitableEvent* ipc_event) { | 183 WaitableEvent* ipc_event) { |
179 // SyncChannel needs to be destructed on the thread that it was created on. | 184 // SyncChannel needs to be destructed on the thread that it was created on. |
180 channel_.reset(); | 185 channel_.reset(); |
181 | 186 |
182 MessageLoop::current()->RunAllPending(); | 187 MessageLoop::current()->RunAllPending(); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 // Tests basic synchronous call | 309 // Tests basic synchronous call |
305 TEST_F(IPCSyncChannelTest, Simple) { | 310 TEST_F(IPCSyncChannelTest, Simple) { |
306 Simple(false); | 311 Simple(false); |
307 Simple(true); | 312 Simple(true); |
308 } | 313 } |
309 | 314 |
310 //----------------------------------------------------------------------------- | 315 //----------------------------------------------------------------------------- |
311 | 316 |
312 namespace { | 317 namespace { |
313 | 318 |
| 319 // Worker classes which override how the sync channel is created to use the |
| 320 // two-step initialization (calling the lightweight constructor and then |
| 321 // ChannelProxy::Init separately) process. |
| 322 class TwoStepServer : public Worker { |
| 323 public: |
| 324 explicit TwoStepServer(bool create_pipe_now) |
| 325 : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 326 create_pipe_now_(create_pipe_now) { } |
| 327 |
| 328 void Run() { |
| 329 SendAnswerToLife(false, base::kNoTimeout, true); |
| 330 Done(); |
| 331 } |
| 332 |
| 333 virtual SyncChannel* CreateChannel() { |
| 334 SyncChannel* channel = new SyncChannel( |
| 335 this, ipc_thread().message_loop_proxy(), shutdown_event()); |
| 336 channel->Init(channel_name(), mode(), create_pipe_now_); |
| 337 return channel; |
| 338 } |
| 339 |
| 340 bool create_pipe_now_; |
| 341 }; |
| 342 |
| 343 class TwoStepClient : public Worker { |
| 344 public: |
| 345 TwoStepClient(bool create_pipe_now) |
| 346 : Worker(Channel::MODE_CLIENT, "simple_client"), |
| 347 create_pipe_now_(create_pipe_now) { } |
| 348 |
| 349 void OnAnswer(int* answer) { |
| 350 *answer = 42; |
| 351 Done(); |
| 352 } |
| 353 |
| 354 virtual SyncChannel* CreateChannel() { |
| 355 SyncChannel* channel = new SyncChannel( |
| 356 this, ipc_thread().message_loop_proxy(), shutdown_event()); |
| 357 channel->Init(channel_name(), mode(), create_pipe_now_); |
| 358 return channel; |
| 359 } |
| 360 |
| 361 bool create_pipe_now_; |
| 362 }; |
| 363 |
| 364 void TwoStep(bool create_server_pipe_now, bool create_client_pipe_now) { |
| 365 std::vector<Worker*> workers; |
| 366 workers.push_back(new TwoStepServer(create_server_pipe_now)); |
| 367 workers.push_back(new TwoStepClient(create_client_pipe_now)); |
| 368 RunTest(workers); |
| 369 } |
| 370 |
| 371 } // namespace |
| 372 |
| 373 // Tests basic two-step initialization, where you call the lightweight |
| 374 // constructor then Init. |
| 375 TEST_F(IPCSyncChannelTest, TwoStepInitialization) { |
| 376 TwoStep(false, false); |
| 377 TwoStep(false, true); |
| 378 TwoStep(true, false); |
| 379 TwoStep(true, true); |
| 380 } |
| 381 |
| 382 |
| 383 //----------------------------------------------------------------------------- |
| 384 |
| 385 namespace { |
| 386 |
314 class DelayClient : public Worker { | 387 class DelayClient : public Worker { |
315 public: | 388 public: |
316 DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { } | 389 DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { } |
317 | 390 |
318 void OnAnswerDelay(Message* reply_msg) { | 391 void OnAnswerDelay(Message* reply_msg) { |
319 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); | 392 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
320 Send(reply_msg); | 393 Send(reply_msg); |
321 Done(); | 394 Done(); |
322 } | 395 } |
323 }; | 396 }; |
(...skipping 939 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1263 std::vector<Worker*> workers; | 1336 std::vector<Worker*> workers; |
1264 workers.push_back(new NonRestrictedDispatchServer); | 1337 workers.push_back(new NonRestrictedDispatchServer); |
1265 workers.push_back(server); | 1338 workers.push_back(server); |
1266 workers.push_back( | 1339 workers.push_back( |
1267 new RestrictedDispatchClient(&sent_ping_event, server, &success)); | 1340 new RestrictedDispatchClient(&sent_ping_event, server, &success)); |
1268 RunTest(workers); | 1341 RunTest(workers); |
1269 EXPECT_EQ(3, success); | 1342 EXPECT_EQ(3, success); |
1270 } | 1343 } |
1271 | 1344 |
1272 } // namespace IPC | 1345 } // namespace IPC |
OLD | NEW |