Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: jingle/glue/thread_wrapper_unittest.cc

Issue 2037873003: Roll WebRTC 13098:13104 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Roll WebRTC Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « jingle/glue/thread_wrapper.cc ('k') | third_party/libjingle/libjingle_nacl.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/threading/thread.h" 10 #include "base/threading/thread.h"
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 public: 67 public:
68 // This method is used by the SendDuringSend test. It sends message to the 68 // This method is used by the SendDuringSend test. It sends message to the
69 // main thread synchronously using Send(). 69 // main thread synchronously using Send().
70 void PingMainThread() { 70 void PingMainThread() {
71 rtc::MessageData* data = new rtc::MessageData(); 71 rtc::MessageData* data = new rtc::MessageData();
72 MockMessageHandler handler; 72 MockMessageHandler handler;
73 73
74 EXPECT_CALL(handler, OnMessage( 74 EXPECT_CALL(handler, OnMessage(
75 MatchMessage(&handler, kTestMessage2, data))) 75 MatchMessage(&handler, kTestMessage2, data)))
76 .WillOnce(DeleteMessageData()); 76 .WillOnce(DeleteMessageData());
77 thread_->Send(&handler, kTestMessage2, data); 77 thread_->Send(RTC_FROM_HERE, &handler, kTestMessage2, data);
78 } 78 }
79 79
80 protected: 80 protected:
81 ThreadWrapperTest() 81 ThreadWrapperTest()
82 : thread_(NULL) { 82 : thread_(NULL) {
83 } 83 }
84 84
85 void SetUp() override { 85 void SetUp() override {
86 JingleThreadWrapper::EnsureForCurrentMessageLoop(); 86 JingleThreadWrapper::EnsureForCurrentMessageLoop();
87 thread_ = rtc::Thread::Current(); 87 thread_ = rtc::Thread::Current();
88 } 88 }
89 89
90 // ThreadWrapper destroyes itself when |message_loop_| is destroyed. 90 // ThreadWrapper destroyes itself when |message_loop_| is destroyed.
91 base::MessageLoop message_loop_; 91 base::MessageLoop message_loop_;
92 rtc::Thread* thread_; 92 rtc::Thread* thread_;
93 MockMessageHandler handler1_; 93 MockMessageHandler handler1_;
94 MockMessageHandler handler2_; 94 MockMessageHandler handler2_;
95 }; 95 };
96 96
97 TEST_F(ThreadWrapperTest, Post) { 97 TEST_F(ThreadWrapperTest, Post) {
98 rtc::MessageData* data1 = new rtc::MessageData(); 98 rtc::MessageData* data1 = new rtc::MessageData();
99 rtc::MessageData* data2 = new rtc::MessageData(); 99 rtc::MessageData* data2 = new rtc::MessageData();
100 rtc::MessageData* data3 = new rtc::MessageData(); 100 rtc::MessageData* data3 = new rtc::MessageData();
101 rtc::MessageData* data4 = new rtc::MessageData(); 101 rtc::MessageData* data4 = new rtc::MessageData();
102 102
103 thread_->Post(&handler1_, kTestMessage1, data1); 103 thread_->Post(RTC_FROM_HERE, &handler1_, kTestMessage1, data1);
104 thread_->Post(&handler1_, kTestMessage2, data2); 104 thread_->Post(RTC_FROM_HERE, &handler1_, kTestMessage2, data2);
105 thread_->Post(&handler2_, kTestMessage1, data3); 105 thread_->Post(RTC_FROM_HERE, &handler2_, kTestMessage1, data3);
106 thread_->Post(&handler2_, kTestMessage1, data4); 106 thread_->Post(RTC_FROM_HERE, &handler2_, kTestMessage1, data4);
107 107
108 InSequence in_seq; 108 InSequence in_seq;
109 109
110 EXPECT_CALL(handler1_, OnMessage( 110 EXPECT_CALL(handler1_, OnMessage(
111 MatchMessage(&handler1_, kTestMessage1, data1))) 111 MatchMessage(&handler1_, kTestMessage1, data1)))
112 .WillOnce(DeleteMessageData()); 112 .WillOnce(DeleteMessageData());
113 EXPECT_CALL(handler1_, OnMessage( 113 EXPECT_CALL(handler1_, OnMessage(
114 MatchMessage(&handler1_, kTestMessage2, data2))) 114 MatchMessage(&handler1_, kTestMessage2, data2)))
115 .WillOnce(DeleteMessageData()); 115 .WillOnce(DeleteMessageData());
116 EXPECT_CALL(handler2_, OnMessage( 116 EXPECT_CALL(handler2_, OnMessage(
117 MatchMessage(&handler2_, kTestMessage1, data3))) 117 MatchMessage(&handler2_, kTestMessage1, data3)))
118 .WillOnce(DeleteMessageData()); 118 .WillOnce(DeleteMessageData());
119 EXPECT_CALL(handler2_, OnMessage( 119 EXPECT_CALL(handler2_, OnMessage(
120 MatchMessage(&handler2_, kTestMessage1, data4))) 120 MatchMessage(&handler2_, kTestMessage1, data4)))
121 .WillOnce(DeleteMessageData()); 121 .WillOnce(DeleteMessageData());
122 122
123 message_loop_.RunUntilIdle(); 123 message_loop_.RunUntilIdle();
124 } 124 }
125 125
126 TEST_F(ThreadWrapperTest, PostDelayed) { 126 TEST_F(ThreadWrapperTest, PostDelayed) {
127 rtc::MessageData* data1 = new rtc::MessageData(); 127 rtc::MessageData* data1 = new rtc::MessageData();
128 rtc::MessageData* data2 = new rtc::MessageData(); 128 rtc::MessageData* data2 = new rtc::MessageData();
129 rtc::MessageData* data3 = new rtc::MessageData(); 129 rtc::MessageData* data3 = new rtc::MessageData();
130 rtc::MessageData* data4 = new rtc::MessageData(); 130 rtc::MessageData* data4 = new rtc::MessageData();
131 131
132 thread_->PostDelayed(kTestDelayMs1, &handler1_, kTestMessage1, data1); 132 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs1, &handler1_, kTestMessage1,
133 thread_->PostDelayed(kTestDelayMs2, &handler1_, kTestMessage2, data2); 133 data1);
134 thread_->PostDelayed(kTestDelayMs3, &handler2_, kTestMessage1, data3); 134 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs2, &handler1_, kTestMessage2,
135 thread_->PostDelayed(kTestDelayMs4, &handler2_, kTestMessage1, data4); 135 data2);
136 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs3, &handler2_, kTestMessage1,
137 data3);
138 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs4, &handler2_, kTestMessage1,
139 data4);
136 140
137 InSequence in_seq; 141 InSequence in_seq;
138 142
139 EXPECT_CALL(handler1_, OnMessage( 143 EXPECT_CALL(handler1_, OnMessage(
140 MatchMessage(&handler1_, kTestMessage1, data1))) 144 MatchMessage(&handler1_, kTestMessage1, data1)))
141 .WillOnce(DeleteMessageData()); 145 .WillOnce(DeleteMessageData());
142 EXPECT_CALL(handler1_, OnMessage( 146 EXPECT_CALL(handler1_, OnMessage(
143 MatchMessage(&handler1_, kTestMessage2, data2))) 147 MatchMessage(&handler1_, kTestMessage2, data2)))
144 .WillOnce(DeleteMessageData()); 148 .WillOnce(DeleteMessageData());
145 EXPECT_CALL(handler2_, OnMessage( 149 EXPECT_CALL(handler2_, OnMessage(
146 MatchMessage(&handler2_, kTestMessage1, data3))) 150 MatchMessage(&handler2_, kTestMessage1, data3)))
147 .WillOnce(DeleteMessageData()); 151 .WillOnce(DeleteMessageData());
148 EXPECT_CALL(handler2_, OnMessage( 152 EXPECT_CALL(handler2_, OnMessage(
149 MatchMessage(&handler2_, kTestMessage1, data4))) 153 MatchMessage(&handler2_, kTestMessage1, data4)))
150 .WillOnce(DeleteMessageData()); 154 .WillOnce(DeleteMessageData());
151 155
152 message_loop_.PostDelayedTask( 156 message_loop_.PostDelayedTask(
153 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), 157 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
154 base::TimeDelta::FromMilliseconds(kMaxTestDelay)); 158 base::TimeDelta::FromMilliseconds(kMaxTestDelay));
155 message_loop_.Run(); 159 message_loop_.Run();
156 } 160 }
157 161
158 TEST_F(ThreadWrapperTest, Clear) { 162 TEST_F(ThreadWrapperTest, Clear) {
159 thread_->Post(&handler1_, kTestMessage1, NULL); 163 thread_->Post(RTC_FROM_HERE, &handler1_, kTestMessage1, NULL);
160 thread_->Post(&handler1_, kTestMessage2, NULL); 164 thread_->Post(RTC_FROM_HERE, &handler1_, kTestMessage2, NULL);
161 thread_->Post(&handler2_, kTestMessage1, NULL); 165 thread_->Post(RTC_FROM_HERE, &handler2_, kTestMessage1, NULL);
162 thread_->Post(&handler2_, kTestMessage2, NULL); 166 thread_->Post(RTC_FROM_HERE, &handler2_, kTestMessage2, NULL);
163 167
164 thread_->Clear(&handler1_, kTestMessage2); 168 thread_->Clear(&handler1_, kTestMessage2);
165 169
166 InSequence in_seq; 170 InSequence in_seq;
167 171
168 rtc::MessageData* null_data = NULL; 172 rtc::MessageData* null_data = NULL;
169 EXPECT_CALL(handler1_, OnMessage( 173 EXPECT_CALL(handler1_, OnMessage(
170 MatchMessage(&handler1_, kTestMessage1, null_data))) 174 MatchMessage(&handler1_, kTestMessage1, null_data)))
171 .WillOnce(DeleteMessageData()); 175 .WillOnce(DeleteMessageData());
172 EXPECT_CALL(handler2_, OnMessage( 176 EXPECT_CALL(handler2_, OnMessage(
173 MatchMessage(&handler2_, kTestMessage1, null_data))) 177 MatchMessage(&handler2_, kTestMessage1, null_data)))
174 .WillOnce(DeleteMessageData()); 178 .WillOnce(DeleteMessageData());
175 EXPECT_CALL(handler2_, OnMessage( 179 EXPECT_CALL(handler2_, OnMessage(
176 MatchMessage(&handler2_, kTestMessage2, null_data))) 180 MatchMessage(&handler2_, kTestMessage2, null_data)))
177 .WillOnce(DeleteMessageData()); 181 .WillOnce(DeleteMessageData());
178 182
179 message_loop_.RunUntilIdle(); 183 message_loop_.RunUntilIdle();
180 } 184 }
181 185
182 TEST_F(ThreadWrapperTest, ClearDelayed) { 186 TEST_F(ThreadWrapperTest, ClearDelayed) {
183 thread_->PostDelayed(kTestDelayMs1, &handler1_, kTestMessage1, NULL); 187 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs1, &handler1_, kTestMessage1,
184 thread_->PostDelayed(kTestDelayMs2, &handler1_, kTestMessage2, NULL); 188 NULL);
185 thread_->PostDelayed(kTestDelayMs3, &handler2_, kTestMessage1, NULL); 189 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs2, &handler1_, kTestMessage2,
186 thread_->PostDelayed(kTestDelayMs4, &handler2_, kTestMessage1, NULL); 190 NULL);
191 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs3, &handler2_, kTestMessage1,
192 NULL);
193 thread_->PostDelayed(RTC_FROM_HERE, kTestDelayMs4, &handler2_, kTestMessage1,
194 NULL);
187 195
188 thread_->Clear(&handler1_, kTestMessage2); 196 thread_->Clear(&handler1_, kTestMessage2);
189 197
190 InSequence in_seq; 198 InSequence in_seq;
191 199
192 rtc::MessageData* null_data = NULL; 200 rtc::MessageData* null_data = NULL;
193 EXPECT_CALL(handler1_, OnMessage( 201 EXPECT_CALL(handler1_, OnMessage(
194 MatchMessage(&handler1_, kTestMessage1, null_data))) 202 MatchMessage(&handler1_, kTestMessage1, null_data)))
195 .WillOnce(DeleteMessageData()); 203 .WillOnce(DeleteMessageData());
196 EXPECT_CALL(handler2_, OnMessage( 204 EXPECT_CALL(handler2_, OnMessage(
197 MatchMessage(&handler2_, kTestMessage1, null_data))) 205 MatchMessage(&handler2_, kTestMessage1, null_data)))
198 .WillOnce(DeleteMessageData()); 206 .WillOnce(DeleteMessageData());
199 EXPECT_CALL(handler2_, OnMessage( 207 EXPECT_CALL(handler2_, OnMessage(
200 MatchMessage(&handler2_, kTestMessage1, null_data))) 208 MatchMessage(&handler2_, kTestMessage1, null_data)))
201 .WillOnce(DeleteMessageData()); 209 .WillOnce(DeleteMessageData());
202 210
203 message_loop_.PostDelayedTask( 211 message_loop_.PostDelayedTask(
204 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), 212 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
205 base::TimeDelta::FromMilliseconds(kMaxTestDelay)); 213 base::TimeDelta::FromMilliseconds(kMaxTestDelay));
206 message_loop_.Run(); 214 message_loop_.Run();
207 } 215 }
208 216
209 // Verify that the queue is cleared when a handler is destroyed. 217 // Verify that the queue is cleared when a handler is destroyed.
210 TEST_F(ThreadWrapperTest, ClearDestoroyed) { 218 TEST_F(ThreadWrapperTest, ClearDestroyed) {
211 MockMessageHandler* handler_ptr; 219 MockMessageHandler* handler_ptr;
212 { 220 {
213 MockMessageHandler handler; 221 MockMessageHandler handler;
214 handler_ptr = &handler; 222 handler_ptr = &handler;
215 thread_->Post(&handler, kTestMessage1, NULL); 223 thread_->Post(RTC_FROM_HERE, &handler, kTestMessage1, NULL);
216 } 224 }
217 rtc::MessageList removed; 225 rtc::MessageList removed;
218 thread_->Clear(handler_ptr, rtc::MQID_ANY, &removed); 226 thread_->Clear(handler_ptr, rtc::MQID_ANY, &removed);
219 DCHECK_EQ(0U, removed.size()); 227 DCHECK_EQ(0U, removed.size());
220 } 228 }
221 229
222 // Verify that Send() calls handler synchronously when called on the 230 // Verify that Send() calls handler synchronously when called on the
223 // same thread. 231 // same thread.
224 TEST_F(ThreadWrapperTest, SendSameThread) { 232 TEST_F(ThreadWrapperTest, SendSameThread) {
225 rtc::MessageData* data = new rtc::MessageData(); 233 rtc::MessageData* data = new rtc::MessageData();
226 234
227 EXPECT_CALL(handler1_, OnMessage( 235 EXPECT_CALL(handler1_, OnMessage(
228 MatchMessage(&handler1_, kTestMessage1, data))) 236 MatchMessage(&handler1_, kTestMessage1, data)))
229 .WillOnce(DeleteMessageData()); 237 .WillOnce(DeleteMessageData());
230 thread_->Send(&handler1_, kTestMessage1, data); 238 thread_->Send(RTC_FROM_HERE, &handler1_, kTestMessage1, data);
231 } 239 }
232 240
233 void InitializeWrapperForNewThread(rtc::Thread** thread, 241 void InitializeWrapperForNewThread(rtc::Thread** thread,
234 base::WaitableEvent* done_event) { 242 base::WaitableEvent* done_event) {
235 JingleThreadWrapper::EnsureForCurrentMessageLoop(); 243 JingleThreadWrapper::EnsureForCurrentMessageLoop();
236 JingleThreadWrapper::current()->set_send_allowed(true); 244 JingleThreadWrapper::current()->set_send_allowed(true);
237 *thread = JingleThreadWrapper::current(); 245 *thread = JingleThreadWrapper::current();
238 done_event->Signal(); 246 done_event->Signal();
239 } 247 }
240 248
(...skipping 14 matching lines...) Expand all
255 &target, &initialized_event)); 263 &target, &initialized_event));
256 initialized_event.Wait(); 264 initialized_event.Wait();
257 265
258 ASSERT_TRUE(target != NULL); 266 ASSERT_TRUE(target != NULL);
259 267
260 rtc::MessageData* data = new rtc::MessageData(); 268 rtc::MessageData* data = new rtc::MessageData();
261 269
262 EXPECT_CALL(handler1_, OnMessage( 270 EXPECT_CALL(handler1_, OnMessage(
263 MatchMessage(&handler1_, kTestMessage1, data))) 271 MatchMessage(&handler1_, kTestMessage1, data)))
264 .WillOnce(DeleteMessageData()); 272 .WillOnce(DeleteMessageData());
265 target->Send(&handler1_, kTestMessage1, data); 273 target->Send(RTC_FROM_HERE, &handler1_, kTestMessage1, data);
266 274
267 Mock::VerifyAndClearExpectations(&handler1_); 275 Mock::VerifyAndClearExpectations(&handler1_);
268 } 276 }
269 277
270 // Verify that thread handles Send() while another Send() is 278 // Verify that thread handles Send() while another Send() is
271 // pending. The test creates second thread and Send()s kTestMessage1 279 // pending. The test creates second thread and Send()s kTestMessage1
272 // to that thread. kTestMessage1 handler calls PingMainThread() which 280 // to that thread. kTestMessage1 handler calls PingMainThread() which
273 // tries to Send() kTestMessage2 to the main thread. 281 // tries to Send() kTestMessage2 to the main thread.
274 TEST_F(ThreadWrapperTest, SendDuringSend) { 282 TEST_F(ThreadWrapperTest, SendDuringSend) {
275 JingleThreadWrapper::current()->set_send_allowed(true); 283 JingleThreadWrapper::current()->set_send_allowed(true);
(...skipping 13 matching lines...) Expand all
289 ASSERT_TRUE(target != NULL); 297 ASSERT_TRUE(target != NULL);
290 298
291 rtc::MessageData* data = new rtc::MessageData(); 299 rtc::MessageData* data = new rtc::MessageData();
292 300
293 EXPECT_CALL(handler1_, OnMessage( 301 EXPECT_CALL(handler1_, OnMessage(
294 MatchMessage(&handler1_, kTestMessage1, data))) 302 MatchMessage(&handler1_, kTestMessage1, data)))
295 .WillOnce(DoAll( 303 .WillOnce(DoAll(
296 InvokeWithoutArgs( 304 InvokeWithoutArgs(
297 this, &ThreadWrapperTest::PingMainThread), 305 this, &ThreadWrapperTest::PingMainThread),
298 DeleteMessageData())); 306 DeleteMessageData()));
299 target->Send(&handler1_, kTestMessage1, data); 307 target->Send(RTC_FROM_HERE, &handler1_, kTestMessage1, data);
300 308
301 Mock::VerifyAndClearExpectations(&handler1_); 309 Mock::VerifyAndClearExpectations(&handler1_);
302 } 310 }
303 311
304 TEST_F(ThreadWrapperTest, Dispose) { 312 TEST_F(ThreadWrapperTest, Dispose) {
305 bool deleted_ = false; 313 bool deleted_ = false;
306 thread_->Dispose(new DeletableObject(&deleted_)); 314 thread_->Dispose(new DeletableObject(&deleted_));
307 EXPECT_FALSE(deleted_); 315 EXPECT_FALSE(deleted_);
308 message_loop_.RunUntilIdle(); 316 message_loop_.RunUntilIdle();
309 EXPECT_TRUE(deleted_); 317 EXPECT_TRUE(deleted_);
310 } 318 }
311 319
312 } // namespace jingle_glue 320 } // namespace jingle_glue
OLDNEW
« no previous file with comments | « jingle/glue/thread_wrapper.cc ('k') | third_party/libjingle/libjingle_nacl.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698