| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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" |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 int result; | 153 int result; |
| 154 OnDouble(in, &result); | 154 OnDouble(in, &result); |
| 155 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result); | 155 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result); |
| 156 Send(reply_msg); | 156 Send(reply_msg); |
| 157 } | 157 } |
| 158 | 158 |
| 159 virtual void OnNestedTestMsg(Message* reply_msg) { | 159 virtual void OnNestedTestMsg(Message* reply_msg) { |
| 160 NOTREACHED(); | 160 NOTREACHED(); |
| 161 } | 161 } |
| 162 | 162 |
| 163 private: | |
| 164 base::Thread* ListenerThread() { | 163 base::Thread* ListenerThread() { |
| 165 return overrided_thread_ ? overrided_thread_ : &listener_thread_; | 164 return overrided_thread_ ? overrided_thread_ : &listener_thread_; |
| 166 } | 165 } |
| 166 |
| 167 private: |
| 167 // Called on the listener thread to create the sync channel. | 168 // Called on the listener thread to create the sync channel. |
| 168 void OnStart() { | 169 void OnStart() { |
| 169 // Link ipc_thread_, listener_thread_ and channel_ altogether. | 170 // Link ipc_thread_, listener_thread_ and channel_ altogether. |
| 170 StartThread(&ipc_thread_, MessageLoop::TYPE_IO); | 171 StartThread(&ipc_thread_, MessageLoop::TYPE_IO); |
| 171 channel_.reset(new SyncChannel( | 172 channel_.reset(new SyncChannel( |
| 172 channel_name_, mode_, this, NULL, ipc_thread_.message_loop(), true, | 173 channel_name_, mode_, this, NULL, ipc_thread_.message_loop(), true, |
| 173 &shutdown_event_)); | 174 &shutdown_event_)); |
| 174 channel_created_->Signal(); | 175 channel_created_->Signal(); |
| 175 Run(); | 176 Run(); |
| 176 } | 177 } |
| (...skipping 927 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1104 filter_ = new TestSyncMessageFilter(shutdown_event(), this); | 1105 filter_ = new TestSyncMessageFilter(shutdown_event(), this); |
| 1105 } | 1106 } |
| 1106 | 1107 |
| 1107 void Run() { | 1108 void Run() { |
| 1108 channel()->AddFilter(filter_.get()); | 1109 channel()->AddFilter(filter_.get()); |
| 1109 } | 1110 } |
| 1110 | 1111 |
| 1111 scoped_refptr<TestSyncMessageFilter> filter_; | 1112 scoped_refptr<TestSyncMessageFilter> filter_; |
| 1112 }; | 1113 }; |
| 1113 | 1114 |
| 1115 // This class provides functionality to test the case that a Send on the sync |
| 1116 // channel does not crash after the channel has been closed. |
| 1117 class ServerSendAfterClose : public Worker { |
| 1118 public: |
| 1119 ServerSendAfterClose() |
| 1120 : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 1121 send_result_(true) { |
| 1122 } |
| 1123 |
| 1124 bool SendDummy() { |
| 1125 ListenerThread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 1126 this, &ServerSendAfterClose::Send, new SyncChannelTestMsg_NoArgs)); |
| 1127 return true; |
| 1128 } |
| 1129 |
| 1130 bool send_result() const { |
| 1131 return send_result_; |
| 1132 } |
| 1133 |
| 1134 private: |
| 1135 virtual void Run() { |
| 1136 CloseChannel(); |
| 1137 Done(); |
| 1138 } |
| 1139 |
| 1140 bool Send(Message* msg) { |
| 1141 send_result_ = Worker::Send(msg); |
| 1142 Done(); |
| 1143 return send_result_; |
| 1144 } |
| 1145 |
| 1146 bool send_result_; |
| 1147 }; |
| 1148 |
| 1114 } // namespace | 1149 } // namespace |
| 1115 | 1150 |
| 1116 // Tests basic synchronous call | 1151 // Tests basic synchronous call |
| 1117 TEST_F(IPCSyncChannelTest, SyncMessageFilter) { | 1152 TEST_F(IPCSyncChannelTest, SyncMessageFilter) { |
| 1118 std::vector<Worker*> workers; | 1153 std::vector<Worker*> workers; |
| 1119 workers.push_back(new SyncMessageFilterServer()); | 1154 workers.push_back(new SyncMessageFilterServer()); |
| 1120 workers.push_back(new SimpleClient()); | 1155 workers.push_back(new SimpleClient()); |
| 1121 RunTest(workers); | 1156 RunTest(workers); |
| 1122 } | 1157 } |
| 1158 |
| 1159 // Test the case when the channel is closed and a Send is attempted after that. |
| 1160 TEST_F(IPCSyncChannelTest, SendAfterClose) { |
| 1161 ServerSendAfterClose server; |
| 1162 server.Start(); |
| 1163 |
| 1164 server.done_event()->Wait(); |
| 1165 server.done_event()->Reset(); |
| 1166 |
| 1167 server.SendDummy(); |
| 1168 server.done_event()->Wait(); |
| 1169 |
| 1170 EXPECT_FALSE(server.send_result()); |
| 1171 } |
| 1172 |
| 1173 |
| OLD | NEW |