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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 bool SendDouble(bool pump, bool succeed) { | 112 bool SendDouble(bool pump, bool succeed) { |
113 int answer = 0; | 113 int answer = 0; |
114 SyncMessage* msg = new SyncChannelTestMsg_Double(5, &answer); | 114 SyncMessage* msg = new SyncChannelTestMsg_Double(5, &answer); |
115 if (pump) | 115 if (pump) |
116 msg->EnableMessagePumping(); | 116 msg->EnableMessagePumping(); |
117 bool result = Send(msg); | 117 bool result = Send(msg); |
118 DCHECK_EQ(result, succeed); | 118 DCHECK_EQ(result, succeed); |
119 DCHECK_EQ(answer, (succeed ? 10 : 0)); | 119 DCHECK_EQ(answer, (succeed ? 10 : 0)); |
120 return result; | 120 return result; |
121 } | 121 } |
122 const std::string& channel_name() { return channel_name_; } | |
122 Channel::Mode mode() { return mode_; } | 123 Channel::Mode mode() { return mode_; } |
123 WaitableEvent* done_event() { return done_.get(); } | 124 WaitableEvent* done_event() { return done_.get(); } |
124 WaitableEvent* shutdown_event() { return &shutdown_event_; } | 125 WaitableEvent* shutdown_event() { return &shutdown_event_; } |
125 void ResetChannel() { channel_.reset(); } | 126 void ResetChannel() { channel_.reset(); } |
126 // Derived classes need to call this when they've completed their part of | 127 // Derived classes need to call this when they've completed their part of |
127 // the test. | 128 // the test. |
128 void Done() { done_->Signal(); } | 129 void Done() { done_->Signal(); } |
129 | 130 |
130 protected: | 131 protected: |
131 SyncChannel* channel() { return channel_.get(); } | 132 SyncChannel* channel() { return channel_.get(); } |
(...skipping 16 matching lines...) Expand all Loading... | |
148 int result; | 149 int result; |
149 OnDouble(in, &result); | 150 OnDouble(in, &result); |
150 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result); | 151 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result); |
151 Send(reply_msg); | 152 Send(reply_msg); |
152 } | 153 } |
153 | 154 |
154 virtual void OnNestedTestMsg(Message* reply_msg) { | 155 virtual void OnNestedTestMsg(Message* reply_msg) { |
155 NOTREACHED(); | 156 NOTREACHED(); |
156 } | 157 } |
157 | 158 |
159 virtual SyncChannel* CreateChannel() { | |
160 return new SyncChannel( | |
161 channel_name_, mode_, this, ipc_thread_.message_loop_proxy(), true, | |
162 &shutdown_event_); | |
163 } | |
164 | |
158 base::Thread* ListenerThread() { | 165 base::Thread* ListenerThread() { |
159 return overrided_thread_ ? overrided_thread_ : &listener_thread_; | 166 return overrided_thread_ ? overrided_thread_ : &listener_thread_; |
160 } | 167 } |
161 | 168 |
162 const base::Thread& ipc_thread() const { return ipc_thread_; } | 169 const base::Thread& ipc_thread() const { return ipc_thread_; } |
163 | 170 |
164 private: | 171 private: |
165 // Called on the listener thread to create the sync channel. | 172 // Called on the listener thread to create the sync channel. |
166 void OnStart() { | 173 void OnStart() { |
167 // Link ipc_thread_, listener_thread_ and channel_ altogether. | 174 // Link ipc_thread_, listener_thread_ and channel_ altogether. |
168 StartThread(&ipc_thread_, MessageLoop::TYPE_IO); | 175 StartThread(&ipc_thread_, MessageLoop::TYPE_IO); |
169 channel_.reset(new SyncChannel( | 176 channel_.reset(CreateChannel()); |
170 channel_name_, mode_, this, ipc_thread_.message_loop_proxy(), true, | |
171 &shutdown_event_)); | |
172 channel_created_->Signal(); | 177 channel_created_->Signal(); |
173 Run(); | 178 Run(); |
174 } | 179 } |
175 | 180 |
176 void OnListenerThreadShutdown1(WaitableEvent* listener_event, | 181 void OnListenerThreadShutdown1(WaitableEvent* listener_event, |
177 WaitableEvent* ipc_event) { | 182 WaitableEvent* ipc_event) { |
178 // SyncChannel needs to be destructed on the thread that it was created on. | 183 // SyncChannel needs to be destructed on the thread that it was created on. |
179 channel_.reset(); | 184 channel_.reset(); |
180 | 185 |
181 MessageLoop::current()->RunAllPending(); | 186 MessageLoop::current()->RunAllPending(); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
300 | 305 |
301 // Tests basic synchronous call | 306 // Tests basic synchronous call |
302 TEST_F(IPCSyncChannelTest, Simple) { | 307 TEST_F(IPCSyncChannelTest, Simple) { |
303 Simple(false); | 308 Simple(false); |
304 Simple(true); | 309 Simple(true); |
305 } | 310 } |
306 | 311 |
307 //----------------------------------------------------------------------------- | 312 //----------------------------------------------------------------------------- |
308 | 313 |
309 namespace { | 314 namespace { |
310 | 315 |
dmac
2011/11/04 22:30:44
could you add a more detailed comment for this set
kkania
2011/11/07 20:25:16
Done.
| |
316 class TwoStepServer : public Worker { | |
317 public: | |
318 explicit TwoStepServer(bool create_pipe_now) | |
319 : Worker(Channel::MODE_SERVER, "simpler_server"), | |
320 create_pipe_now_(create_pipe_now) { } | |
321 | |
322 void Run() { | |
323 SendAnswerToLife(false, base::kNoTimeout, true); | |
324 Done(); | |
325 } | |
326 | |
327 virtual SyncChannel* CreateChannel() { | |
328 SyncChannel* channel = new SyncChannel( | |
329 this, ipc_thread().message_loop_proxy(), shutdown_event()); | |
330 channel->Init(channel_name(), mode(), create_pipe_now_); | |
331 return channel; | |
332 } | |
333 | |
334 bool create_pipe_now_; | |
335 }; | |
336 | |
337 class TwoStepClient : public Worker { | |
338 public: | |
339 TwoStepClient(bool create_pipe_now) | |
340 : Worker(Channel::MODE_CLIENT, "simple_client"), | |
341 create_pipe_now_(create_pipe_now) { } | |
342 | |
343 void OnAnswer(int* answer) { | |
344 *answer = 42; | |
345 Done(); | |
346 } | |
347 | |
348 virtual SyncChannel* CreateChannel() { | |
349 SyncChannel* channel = new SyncChannel( | |
350 this, ipc_thread().message_loop_proxy(), shutdown_event()); | |
351 channel->Init(channel_name(), mode(), create_pipe_now_); | |
352 return channel; | |
353 } | |
354 | |
355 bool create_pipe_now_; | |
356 }; | |
357 | |
358 void TwoStep(bool create_server_pipe_now, bool create_client_pipe_now) { | |
359 std::vector<Worker*> workers; | |
360 workers.push_back(new TwoStepServer(create_server_pipe_now)); | |
361 workers.push_back(new TwoStepClient(create_client_pipe_now)); | |
362 RunTest(workers); | |
363 } | |
364 | |
365 } // namespace | |
366 | |
367 // Tests basic two-step initialization, where you call constructor then Init. | |
368 TEST_F(IPCSyncChannelTest, TwoStepInitialization) { | |
369 TwoStep(false, false); | |
370 TwoStep(false, true); | |
371 TwoStep(true, false); | |
372 TwoStep(true, true); | |
373 } | |
374 | |
375 | |
376 //----------------------------------------------------------------------------- | |
377 | |
378 namespace { | |
379 | |
311 class DelayClient : public Worker { | 380 class DelayClient : public Worker { |
312 public: | 381 public: |
313 DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { } | 382 DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { } |
314 | 383 |
315 void OnAnswerDelay(Message* reply_msg) { | 384 void OnAnswerDelay(Message* reply_msg) { |
316 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); | 385 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
317 Send(reply_msg); | 386 Send(reply_msg); |
318 Done(); | 387 Done(); |
319 } | 388 } |
320 }; | 389 }; |
(...skipping 936 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1257 std::vector<Worker*> workers; | 1326 std::vector<Worker*> workers; |
1258 workers.push_back(new NonRestrictedDispatchServer); | 1327 workers.push_back(new NonRestrictedDispatchServer); |
1259 workers.push_back(server); | 1328 workers.push_back(server); |
1260 workers.push_back( | 1329 workers.push_back( |
1261 new RestrictedDispatchClient(&sent_ping_event, server, &success)); | 1330 new RestrictedDispatchClient(&sent_ping_event, server, &success)); |
1262 RunTest(workers); | 1331 RunTest(workers); |
1263 EXPECT_EQ(3, success); | 1332 EXPECT_EQ(3, success); |
1264 } | 1333 } |
1265 | 1334 |
1266 } // namespace IPC | 1335 } // namespace IPC |
OLD | NEW |