OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/dynamic_annotations.h" | 11 #include "base/dynamic_annotations.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/platform_thread.h" | 14 #include "base/platform_thread.h" |
15 #include "base/stl_util-inl.h" | 15 #include "base/stl_util-inl.h" |
16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
17 #include "base/thread.h" | 17 #include "base/thread.h" |
18 #include "base/waitable_event.h" | 18 #include "base/waitable_event.h" |
19 #include "ipc/ipc_message.h" | 19 #include "ipc/ipc_message.h" |
20 #include "ipc/ipc_sync_channel.h" | 20 #include "ipc/ipc_sync_channel.h" |
| 21 #include "ipc/ipc_sync_message_filter.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 | 23 |
23 | 24 |
24 #define MESSAGES_INTERNAL_FILE "ipc/ipc_sync_message_unittest.h" | 25 #define MESSAGES_INTERNAL_FILE "ipc/ipc_sync_message_unittest.h" |
25 #include "ipc/ipc_message_macros.h" | 26 #include "ipc/ipc_message_macros.h" |
26 | 27 |
27 using namespace IPC; | 28 using namespace IPC; |
28 using base::WaitableEvent; | 29 using base::WaitableEvent; |
29 | 30 |
30 namespace { | 31 namespace { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 SyncMessage* msg = new SyncChannelTestMsg_Double(5, &answer); | 114 SyncMessage* msg = new SyncChannelTestMsg_Double(5, &answer); |
114 if (pump) | 115 if (pump) |
115 msg->EnableMessagePumping(); | 116 msg->EnableMessagePumping(); |
116 bool result = Send(msg); | 117 bool result = Send(msg); |
117 DCHECK_EQ(result, succeed); | 118 DCHECK_EQ(result, succeed); |
118 DCHECK_EQ(answer, (succeed ? 10 : 0)); | 119 DCHECK_EQ(answer, (succeed ? 10 : 0)); |
119 return result; | 120 return result; |
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(); } |
| 124 WaitableEvent* shutdown_event() { return &shutdown_event_; } |
123 void ResetChannel() { channel_.reset(); } | 125 void ResetChannel() { channel_.reset(); } |
124 | |
125 protected: | |
126 // 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 |
127 // the test. | 127 // the test. |
128 void Done() { done_->Signal(); } | 128 void Done() { done_->Signal(); } |
| 129 |
| 130 protected: |
| 131 IPC::SyncChannel* channel() { return channel_.get(); } |
129 // Functions for dervied classes to implement if they wish. | 132 // Functions for dervied classes to implement if they wish. |
130 virtual void Run() { } | 133 virtual void Run() { } |
131 virtual void OnAnswer(int* answer) { NOTREACHED(); } | 134 virtual void OnAnswer(int* answer) { NOTREACHED(); } |
132 virtual void OnAnswerDelay(Message* reply_msg) { | 135 virtual void OnAnswerDelay(Message* reply_msg) { |
133 // The message handler map below can only take one entry for | 136 // The message handler map below can only take one entry for |
134 // SyncChannelTestMsg_AnswerToLife, so since some classes want | 137 // SyncChannelTestMsg_AnswerToLife, so since some classes want |
135 // the normal version while other want the delayed reply, we | 138 // the normal version while other want the delayed reply, we |
136 // call the normal version if the derived class didn't override | 139 // call the normal version if the derived class didn't override |
137 // this function. | 140 // this function. |
138 int answer; | 141 int answer; |
(...skipping 908 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1047 | 1050 |
1048 // Tests http://b/1474092 - that if after the done_event is set but before | 1051 // Tests http://b/1474092 - that if after the done_event is set but before |
1049 // OnObjectSignaled is called another message is sent out, then after its | 1052 // OnObjectSignaled is called another message is sent out, then after its |
1050 // reply comes back OnObjectSignaled will be called for the first message. | 1053 // reply comes back OnObjectSignaled will be called for the first message. |
1051 TEST_F(IPCSyncChannelTest, DoneEventRace) { | 1054 TEST_F(IPCSyncChannelTest, DoneEventRace) { |
1052 std::vector<Worker*> workers; | 1055 std::vector<Worker*> workers; |
1053 workers.push_back(new DoneEventRaceServer()); | 1056 workers.push_back(new DoneEventRaceServer()); |
1054 workers.push_back(new SimpleClient()); | 1057 workers.push_back(new SimpleClient()); |
1055 RunTest(workers); | 1058 RunTest(workers); |
1056 } | 1059 } |
| 1060 |
| 1061 //----------------------------------------------------------------------------- |
| 1062 |
| 1063 namespace { |
| 1064 |
| 1065 class TestSyncMessageFilter : public IPC::SyncMessageFilter { |
| 1066 public: |
| 1067 TestSyncMessageFilter(base::WaitableEvent* shutdown_event, Worker* worker) |
| 1068 : SyncMessageFilter(shutdown_event), |
| 1069 worker_(worker), |
| 1070 thread_("helper_thread") { |
| 1071 base::Thread::Options options; |
| 1072 options.message_loop_type = MessageLoop::TYPE_DEFAULT; |
| 1073 thread_.StartWithOptions(options); |
| 1074 } |
| 1075 |
| 1076 virtual void OnFilterAdded(Channel* channel) { |
| 1077 SyncMessageFilter::OnFilterAdded(channel); |
| 1078 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 1079 this, &TestSyncMessageFilter::SendMessageOnHelperThread)); |
| 1080 } |
| 1081 |
| 1082 void SendMessageOnHelperThread() { |
| 1083 int answer = 0; |
| 1084 bool result = Send(new SyncChannelTestMsg_AnswerToLife(&answer)); |
| 1085 DCHECK(result); |
| 1086 DCHECK_EQ(answer, 42); |
| 1087 |
| 1088 worker_->Done(); |
| 1089 } |
| 1090 |
| 1091 Worker* worker_; |
| 1092 base::Thread thread_; |
| 1093 }; |
| 1094 |
| 1095 class SyncMessageFilterServer : public Worker { |
| 1096 public: |
| 1097 SyncMessageFilterServer() |
| 1098 : Worker(Channel::MODE_SERVER, "sync_message_filter_server") { |
| 1099 filter_ = new TestSyncMessageFilter(shutdown_event(), this); |
| 1100 } |
| 1101 |
| 1102 void Run() { |
| 1103 channel()->AddFilter(filter_.get()); |
| 1104 } |
| 1105 |
| 1106 scoped_refptr<TestSyncMessageFilter> filter_; |
| 1107 }; |
| 1108 |
| 1109 } // namespace |
| 1110 |
| 1111 // Tests basic synchronous call |
| 1112 TEST_F(IPCSyncChannelTest, SyncMessageFilter) { |
| 1113 std::vector<Worker*> workers; |
| 1114 workers.push_back(new SyncMessageFilterServer()); |
| 1115 workers.push_back(new SimpleClient()); |
| 1116 RunTest(workers); |
| 1117 } |
OLD | NEW |