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 <windows.h> | 7 #include <windows.h> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 bool Send(Message* msg) { return channel_->Send(msg); } | 97 bool Send(Message* msg) { return channel_->Send(msg); } |
98 bool SendWithTimeout(Message* msg, int timeout_ms) { | 98 bool SendWithTimeout(Message* msg, int timeout_ms) { |
99 return channel_->SendWithTimeout(msg, timeout_ms); | 99 return channel_->SendWithTimeout(msg, timeout_ms); |
100 } | 100 } |
101 void WaitForChannelCreation() { channel_created_.Wait(); } | 101 void WaitForChannelCreation() { channel_created_.Wait(); } |
102 void CloseChannel() { | 102 void CloseChannel() { |
103 DCHECK(MessageLoop::current() == ListenerThread()->message_loop()); | 103 DCHECK(MessageLoop::current() == ListenerThread()->message_loop()); |
104 channel_->Close(); | 104 channel_->Close(); |
105 } | 105 } |
106 void Start() { | 106 void Start() { |
107 StartThread(&listener_thread_); | 107 StartThread(&listener_thread_, MessageLoop::TYPE_DEFAULT); |
108 ListenerThread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 108 ListenerThread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
109 this, &Worker::OnStart)); | 109 this, &Worker::OnStart)); |
110 } | 110 } |
111 void OverrideThread(base::Thread* overrided_thread) { | 111 void OverrideThread(base::Thread* overrided_thread) { |
112 DCHECK(overrided_thread_ == NULL); | 112 DCHECK(overrided_thread_ == NULL); |
113 overrided_thread_ = overrided_thread; | 113 overrided_thread_ = overrided_thread; |
114 } | 114 } |
115 Channel::Mode mode() { return mode_; } | 115 Channel::Mode mode() { return mode_; } |
116 HANDLE done_event() { return done_.handle(); } | 116 HANDLE done_event() { return done_.handle(); } |
117 | 117 |
(...skipping 24 matching lines...) Expand all Loading... |
142 Send(reply_msg); | 142 Send(reply_msg); |
143 } | 143 } |
144 | 144 |
145 private: | 145 private: |
146 base::Thread* ListenerThread() { | 146 base::Thread* ListenerThread() { |
147 return overrided_thread_ ? overrided_thread_ : &listener_thread_; | 147 return overrided_thread_ ? overrided_thread_ : &listener_thread_; |
148 } | 148 } |
149 // Called on the listener thread to create the sync channel. | 149 // Called on the listener thread to create the sync channel. |
150 void OnStart() { | 150 void OnStart() { |
151 // Link ipc_thread_, listener_thread_ and channel_ altogether. | 151 // Link ipc_thread_, listener_thread_ and channel_ altogether. |
152 StartThread(&ipc_thread_); | 152 StartThread(&ipc_thread_, MessageLoop::TYPE_IO); |
153 channel_.reset(new SyncChannel( | 153 channel_.reset(new SyncChannel( |
154 channel_name_, mode_, this, NULL, ipc_thread_.message_loop(), true, | 154 channel_name_, mode_, this, NULL, ipc_thread_.message_loop(), true, |
155 TestProcess::GetShutDownEvent())); | 155 TestProcess::GetShutDownEvent())); |
156 channel_created_.Set(); | 156 channel_created_.Set(); |
157 Run(); | 157 Run(); |
158 } | 158 } |
159 | 159 |
160 void OnListenerThreadShutdown(HANDLE listener_event, HANDLE ipc_event) { | 160 void OnListenerThreadShutdown(HANDLE listener_event, HANDLE ipc_event) { |
161 // SyncChannel needs to be destructed on the thread that it was created on. | 161 // SyncChannel needs to be destructed on the thread that it was created on. |
162 channel_.reset(); | 162 channel_.reset(); |
163 SetEvent(listener_event); | 163 SetEvent(listener_event); |
164 | 164 |
165 ipc_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 165 ipc_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
166 this, &Worker::OnIPCThreadShutdown, ipc_event)); | 166 this, &Worker::OnIPCThreadShutdown, ipc_event)); |
167 } | 167 } |
168 | 168 |
169 void OnIPCThreadShutdown(HANDLE ipc_event) { | 169 void OnIPCThreadShutdown(HANDLE ipc_event) { |
170 SetEvent(ipc_event); | 170 SetEvent(ipc_event); |
171 } | 171 } |
172 | 172 |
173 void OnMessageReceived(const Message& message) { | 173 void OnMessageReceived(const Message& message) { |
174 IPC_BEGIN_MESSAGE_MAP(Worker, message) | 174 IPC_BEGIN_MESSAGE_MAP(Worker, message) |
175 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay) | 175 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay) |
176 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife, | 176 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife, |
177 OnAnswerDelay) | 177 OnAnswerDelay) |
178 IPC_END_MESSAGE_MAP() | 178 IPC_END_MESSAGE_MAP() |
179 } | 179 } |
180 | 180 |
181 void StartThread(base::Thread* thread) { | 181 void StartThread(base::Thread* thread, MessageLoop::Type type) { |
182 base::Thread::Options options; | 182 base::Thread::Options options; |
183 options.message_loop_type = MessageLoop::TYPE_IO; | 183 options.message_loop_type = type; |
184 thread->StartWithOptions(options); | 184 thread->StartWithOptions(options); |
185 } | 185 } |
186 | 186 |
187 Event done_; | 187 Event done_; |
188 Event channel_created_; | 188 Event channel_created_; |
189 std::wstring channel_name_; | 189 std::wstring channel_name_; |
190 Channel::Mode mode_; | 190 Channel::Mode mode_; |
191 scoped_ptr<SyncChannel> channel_; | 191 scoped_ptr<SyncChannel> channel_; |
192 base::Thread ipc_thread_; | 192 base::Thread ipc_thread_; |
193 base::Thread listener_thread_; | 193 base::Thread listener_thread_; |
(...skipping 820 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1014 TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) { | 1014 TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) { |
1015 SendWithTimeoutTimeout(false); | 1015 SendWithTimeoutTimeout(false); |
1016 SendWithTimeoutTimeout(true); | 1016 SendWithTimeoutTimeout(true); |
1017 } | 1017 } |
1018 | 1018 |
1019 // Sends some message that time-out and some that succeed. | 1019 // Sends some message that time-out and some that succeed. |
1020 TEST_F(IPCSyncChannelTest, SendWithTimeoutMixedOKAndTimeout) { | 1020 TEST_F(IPCSyncChannelTest, SendWithTimeoutMixedOKAndTimeout) { |
1021 SendWithTimeoutMixedOKAndTimeout(false); | 1021 SendWithTimeoutMixedOKAndTimeout(false); |
1022 SendWithTimeoutMixedOKAndTimeout(true); | 1022 SendWithTimeoutMixedOKAndTimeout(true); |
1023 } | 1023 } |
OLD | NEW |