| 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> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 16 #include "base/stl_util-inl.h" | 16 #include "base/stl_util-inl.h" |
| 17 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 18 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 18 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 19 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
| 20 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
| 21 #include "base/synchronization/waitable_event.h" | 21 #include "base/synchronization/waitable_event.h" |
| 22 #include "ipc/ipc_message.h" | 22 #include "ipc/ipc_message.h" |
| 23 #include "ipc/ipc_sync_message_filter.h" | 23 #include "ipc/ipc_sync_message_filter.h" |
| 24 #include "ipc/ipc_sync_message_unittest.h" | 24 #include "ipc/ipc_sync_message_unittest.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 26 |
| 27 using namespace IPC; | |
| 28 using base::WaitableEvent; | 27 using base::WaitableEvent; |
| 29 | 28 |
| 29 namespace IPC { |
| 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 // Base class for a "process" with listener and IPC threads. | 33 // Base class for a "process" with listener and IPC threads. |
| 33 class Worker : public Channel::Listener, public Message::Sender { | 34 class Worker : public Channel::Listener, public Message::Sender { |
| 34 public: | 35 public: |
| 35 // Will create a channel without a name. | 36 // Will create a channel without a name. |
| 36 Worker(Channel::Mode mode, const std::string& thread_name) | 37 Worker(Channel::Mode mode, const std::string& thread_name) |
| 37 : done_(new WaitableEvent(false, false)), | 38 : done_(new WaitableEvent(false, false)), |
| 38 channel_created_(new WaitableEvent(false, false)), | 39 channel_created_(new WaitableEvent(false, false)), |
| 39 mode_(mode), | 40 mode_(mode), |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 } | 121 } |
| 121 Channel::Mode mode() { return mode_; } | 122 Channel::Mode mode() { return mode_; } |
| 122 WaitableEvent* done_event() { return done_.get(); } | 123 WaitableEvent* done_event() { return done_.get(); } |
| 123 WaitableEvent* shutdown_event() { return &shutdown_event_; } | 124 WaitableEvent* shutdown_event() { return &shutdown_event_; } |
| 124 void ResetChannel() { channel_.reset(); } | 125 void ResetChannel() { channel_.reset(); } |
| 125 // Derived classes need to call this when they've completed their part of | 126 // Derived classes need to call this when they've completed their part of |
| 126 // the test. | 127 // the test. |
| 127 void Done() { done_->Signal(); } | 128 void Done() { done_->Signal(); } |
| 128 | 129 |
| 129 protected: | 130 protected: |
| 130 IPC::SyncChannel* channel() { return channel_.get(); } | 131 SyncChannel* channel() { return channel_.get(); } |
| 131 // Functions for dervied classes to implement if they wish. | 132 // Functions for dervied classes to implement if they wish. |
| 132 virtual void Run() { } | 133 virtual void Run() { } |
| 133 virtual void OnAnswer(int* answer) { NOTREACHED(); } | 134 virtual void OnAnswer(int* answer) { NOTREACHED(); } |
| 134 virtual void OnAnswerDelay(Message* reply_msg) { | 135 virtual void OnAnswerDelay(Message* reply_msg) { |
| 135 // The message handler map below can only take one entry for | 136 // The message handler map below can only take one entry for |
| 136 // SyncChannelTestMsg_AnswerToLife, so since some classes want | 137 // SyncChannelTestMsg_AnswerToLife, so since some classes want |
| 137 // the normal version while other want the delayed reply, we | 138 // the normal version while other want the delayed reply, we |
| 138 // call the normal version if the derived class didn't override | 139 // call the normal version if the derived class didn't override |
| 139 // this function. | 140 // this function. |
| 140 int answer; | 141 int answer; |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 // Tests that the caller unblocks to answer a sync message from the receiver. | 454 // Tests that the caller unblocks to answer a sync message from the receiver. |
| 454 TEST_F(IPCSyncChannelTest, Unblock) { | 455 TEST_F(IPCSyncChannelTest, Unblock) { |
| 455 Unblock(false, false, false); | 456 Unblock(false, false, false); |
| 456 Unblock(false, true, false); | 457 Unblock(false, true, false); |
| 457 Unblock(true, false, false); | 458 Unblock(true, false, false); |
| 458 Unblock(true, true, false); | 459 Unblock(true, true, false); |
| 459 } | 460 } |
| 460 | 461 |
| 461 //----------------------------------------------------------------------------- | 462 //----------------------------------------------------------------------------- |
| 462 | 463 |
| 463 // Tests that the the IPC::SyncChannel object can be deleted during a Send. | 464 // Tests that the the SyncChannel object can be deleted during a Send. |
| 464 TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) { | 465 TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) { |
| 465 Unblock(false, false, true); | 466 Unblock(false, false, true); |
| 466 Unblock(false, true, true); | 467 Unblock(false, true, true); |
| 467 Unblock(true, false, true); | 468 Unblock(true, false, true); |
| 468 Unblock(true, true, true); | 469 Unblock(true, true, true); |
| 469 } | 470 } |
| 470 | 471 |
| 471 //----------------------------------------------------------------------------- | 472 //----------------------------------------------------------------------------- |
| 472 | 473 |
| 473 namespace { | 474 namespace { |
| (...skipping 583 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1057 std::vector<Worker*> workers; | 1058 std::vector<Worker*> workers; |
| 1058 workers.push_back(new DoneEventRaceServer()); | 1059 workers.push_back(new DoneEventRaceServer()); |
| 1059 workers.push_back(new SimpleClient()); | 1060 workers.push_back(new SimpleClient()); |
| 1060 RunTest(workers); | 1061 RunTest(workers); |
| 1061 } | 1062 } |
| 1062 | 1063 |
| 1063 //----------------------------------------------------------------------------- | 1064 //----------------------------------------------------------------------------- |
| 1064 | 1065 |
| 1065 namespace { | 1066 namespace { |
| 1066 | 1067 |
| 1067 class TestSyncMessageFilter : public IPC::SyncMessageFilter { | 1068 class TestSyncMessageFilter : public SyncMessageFilter { |
| 1068 public: | 1069 public: |
| 1069 TestSyncMessageFilter(base::WaitableEvent* shutdown_event, Worker* worker) | 1070 TestSyncMessageFilter(base::WaitableEvent* shutdown_event, Worker* worker) |
| 1070 : SyncMessageFilter(shutdown_event), | 1071 : SyncMessageFilter(shutdown_event), |
| 1071 worker_(worker), | 1072 worker_(worker), |
| 1072 thread_("helper_thread") { | 1073 thread_("helper_thread") { |
| 1073 base::Thread::Options options; | 1074 base::Thread::Options options; |
| 1074 options.message_loop_type = MessageLoop::TYPE_DEFAULT; | 1075 options.message_loop_type = MessageLoop::TYPE_DEFAULT; |
| 1075 thread_.StartWithOptions(options); | 1076 thread_.StartWithOptions(options); |
| 1076 } | 1077 } |
| 1077 | 1078 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1171 namespace { | 1172 namespace { |
| 1172 | 1173 |
| 1173 class RestrictedDispatchServer : public Worker { | 1174 class RestrictedDispatchServer : public Worker { |
| 1174 public: | 1175 public: |
| 1175 RestrictedDispatchServer(WaitableEvent* sent_ping_event) | 1176 RestrictedDispatchServer(WaitableEvent* sent_ping_event) |
| 1176 : Worker("restricted_channel", Channel::MODE_SERVER), | 1177 : Worker("restricted_channel", Channel::MODE_SERVER), |
| 1177 sent_ping_event_(sent_ping_event) { } | 1178 sent_ping_event_(sent_ping_event) { } |
| 1178 | 1179 |
| 1179 void OnDoPing(int ping) { | 1180 void OnDoPing(int ping) { |
| 1180 // Send an asynchronous message that unblocks the caller. | 1181 // Send an asynchronous message that unblocks the caller. |
| 1181 IPC::Message* msg = new SyncChannelTestMsg_Ping(ping); | 1182 Message* msg = new SyncChannelTestMsg_Ping(ping); |
| 1182 msg->set_unblock(true); | 1183 msg->set_unblock(true); |
| 1183 Send(msg); | 1184 Send(msg); |
| 1184 // Signal the event after the message has been sent on the channel, on the | 1185 // Signal the event after the message has been sent on the channel, on the |
| 1185 // IPC thread. | 1186 // IPC thread. |
| 1186 ipc_thread().message_loop()->PostTask(FROM_HERE, | 1187 ipc_thread().message_loop()->PostTask(FROM_HERE, |
| 1187 NewRunnableMethod(this, &RestrictedDispatchServer::OnPingSent)); | 1188 NewRunnableMethod(this, &RestrictedDispatchServer::OnPingSent)); |
| 1188 } | 1189 } |
| 1189 | 1190 |
| 1190 base::Thread* ListenerThread() { return Worker::ListenerThread(); } | 1191 base::Thread* ListenerThread() { return Worker::ListenerThread(); } |
| 1191 | 1192 |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1308 new RestrictedDispatchServer(&sent_ping_event); | 1309 new RestrictedDispatchServer(&sent_ping_event); |
| 1309 int success = 0; | 1310 int success = 0; |
| 1310 std::vector<Worker*> workers; | 1311 std::vector<Worker*> workers; |
| 1311 workers.push_back(new NonRestrictedDispatchServer); | 1312 workers.push_back(new NonRestrictedDispatchServer); |
| 1312 workers.push_back(server); | 1313 workers.push_back(server); |
| 1313 workers.push_back( | 1314 workers.push_back( |
| 1314 new RestrictedDispatchClient(&sent_ping_event, server, &success)); | 1315 new RestrictedDispatchClient(&sent_ping_event, server, &success)); |
| 1315 RunTest(workers); | 1316 RunTest(workers); |
| 1316 EXPECT_EQ(3, success); | 1317 EXPECT_EQ(3, success); |
| 1317 } | 1318 } |
| 1319 |
| 1320 } // namespace IPC |
| OLD | NEW |