1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/message_loop/message_loop.h"
#include "base/threading/thread.h"
#include "jingle/glue/thread_wrapper.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::InvokeWithoutArgs;
using ::testing::Mock;
namespace jingle_glue {
static const uint32 kTestMessage1 = 1;
static const uint32 kTestMessage2 = 2;
static const int kTestDelayMs1 = 10;
static const int kTestDelayMs2 = 20;
static const int kTestDelayMs3 = 30;
static const int kTestDelayMs4 = 40;
static const int kMaxTestDelay = 40;
namespace {
class MockMessageHandler : public rtc::MessageHandler {
public:
MOCK_METHOD1(OnMessage, void(rtc::Message* msg));
};
MATCHER_P3(MatchMessage, handler, message_id, data, "") {
return arg->phandler == handler &&
arg->message_id == message_id &&
arg->pdata == data;
}
ACTION(DeleteMessageData) {
delete arg0->pdata;
}
// Helper class used in the Dispose test.
class DeletableObject {
public:
DeletableObject(bool* deleted)
: deleted_(deleted) {
*deleted = false;
}
~DeletableObject() {
*deleted_ = true;
}
private:
bool* deleted_;
};
} // namespace
class ThreadWrapperTest : public testing::Test {
public:
// This method is used by the SendDuringSend test. It sends message to the
// main thread synchronously using Send().
void PingMainThread() {
rtc::MessageData* data = new rtc::MessageData();
MockMessageHandler handler;
EXPECT_CALL(handler, OnMessage(
MatchMessage(&handler, kTestMessage2, data)))
.WillOnce(DeleteMessageData());
thread_->Send(&handler, kTestMessage2, data);
}
protected:
ThreadWrapperTest()
: thread_(NULL) {
}
virtual void SetUp() OVERRIDE {
JingleThreadWrapper::EnsureForCurrentMessageLoop();
thread_ = rtc::Thread::Current();
}
// ThreadWrapper destroyes itself when |message_loop_| is destroyed.
base::MessageLoop message_loop_;
rtc::Thread* thread_;
MockMessageHandler handler1_;
MockMessageHandler handler2_;
};
TEST_F(ThreadWrapperTest, Post) {
rtc::MessageData* data1 = new rtc::MessageData();
rtc::MessageData* data2 = new rtc::MessageData();
rtc::MessageData* data3 = new rtc::MessageData();
rtc::MessageData* data4 = new rtc::MessageData();
thread_->Post(&handler1_, kTestMessage1, data1);
thread_->Post(&handler1_, kTestMessage2, data2);
thread_->Post(&handler2_, kTestMessage1, data3);
thread_->Post(&handler2_, kTestMessage1, data4);
InSequence in_seq;
EXPECT_CALL(handler1_, OnMessage(
MatchMessage(&handler1_, kTestMessage1, data1)))
.WillOnce(DeleteMessageData());
EXPECT_CALL(handler1_, OnMessage(
MatchMessage(&handler1_, kTestMessage2, data2)))
.WillOnce(DeleteMessageData());
EXPECT_CALL(handler2_, OnMessage(
MatchMessage(&handler2_, kTestMessage1, data3)))
.WillOnce(DeleteMessageData());
EXPECT_CALL(handler2_, OnMessage(
MatchMessage(&handler2_, kTestMessage1, data4)))
.WillOnce(DeleteMessageData());
message_loop_.RunUntilIdle();
}
TEST_F(ThreadWrapperTest, PostDelayed) {
rtc::MessageData* data1 = new rtc::MessageData();
rtc::MessageData* data2 = new rtc::MessageData();
rtc::MessageData* data3 = new rtc::MessageData();
rtc::MessageData* data4 = new rtc::MessageData();
thread_->PostDelayed(kTestDelayMs1, &handler1_, kTestMessage1, data1);
thread_->PostDelayed(kTestDelayMs2, &handler1_, kTestMessage2, data2);
thread_->PostDelayed(kTestDelayMs3, &handler2_, kTestMessage1, data3);
thread_->PostDelayed(kTestDelayMs4, &handler2_, kTestMessage1, data4);
InSequence in_seq;
EXPECT_CALL(handler1_, OnMessage(
MatchMessage(&handler1_, kTestMessage1, data1)))
.WillOnce(DeleteMessageData());
EXPECT_CALL(handler1_, OnMessage(
MatchMessage(&handler1_, kTestMessage2, data2)))
.WillOnce(DeleteMessageData());
EXPECT_CALL(handler2_, OnMessage(
MatchMessage(&handler2_, kTestMessage1, data3)))
.WillOnce(DeleteMessageData());
EXPECT_CALL(handler2_, OnMessage(
MatchMessage(&handler2_, kTestMessage1, data4)))
.WillOnce(DeleteMessageData());
message_loop_.PostDelayedTask(
FROM_HERE,
base::MessageLoop::QuitClosure(),
base::TimeDelta::FromMilliseconds(kMaxTestDelay));
message_loop_.Run();
}
TEST_F(ThreadWrapperTest, Clear) {
thread_->Post(&handler1_, kTestMessage1, NULL);
thread_->Post(&handler1_, kTestMessage2, NULL);
thread_->Post(&handler2_, kTestMessage1, NULL);
thread_->Post(&handler2_, kTestMessage2, NULL);
thread_->Clear(&handler1_, kTestMessage2);
InSequence in_seq;
rtc::MessageData* null_data = NULL;
EXPECT_CALL(handler1_, OnMessage(
MatchMessage(&handler1_, kTestMessage1, null_data)))
.WillOnce(DeleteMessageData());
EXPECT_CALL(handler2_, OnMessage(
MatchMessage(&handler2_, kTestMessage1, null_data)))
.WillOnce(DeleteMessageData());
EXPECT_CALL(handler2_, OnMessage(
MatchMessage(&handler2_, kTestMessage2, null_data)))
.WillOnce(DeleteMessageData());
message_loop_.RunUntilIdle();
}
TEST_F(ThreadWrapperTest, ClearDelayed) {
thread_->PostDelayed(kTestDelayMs1, &handler1_, kTestMessage1, NULL);
thread_->PostDelayed(kTestDelayMs2, &handler1_, kTestMessage2, NULL);
thread_->PostDelayed(kTestDelayMs3, &handler2_, kTestMessage1, NULL);
thread_->PostDelayed(kTestDelayMs4, &handler2_, kTestMessage1, NULL);
thread_->Clear(&handler1_, kTestMessage2);
InSequence in_seq;
rtc::MessageData* null_data = NULL;
EXPECT_CALL(handler1_, OnMessage(
MatchMessage(&handler1_, kTestMessage1, null_data)))
.WillOnce(DeleteMessageData());
EXPECT_CALL(handler2_, OnMessage(
MatchMessage(&handler2_, kTestMessage1, null_data)))
.WillOnce(DeleteMessageData());
EXPECT_CALL(handler2_, OnMessage(
MatchMessage(&handler2_, kTestMessage1, null_data)))
.WillOnce(DeleteMessageData());
message_loop_.PostDelayedTask(
FROM_HERE,
base::MessageLoop::QuitClosure(),
base::TimeDelta::FromMilliseconds(kMaxTestDelay));
message_loop_.Run();
}
// Verify that the queue is cleared when a handler is destroyed.
TEST_F(ThreadWrapperTest, ClearDestoroyed) {
MockMessageHandler* handler_ptr;
{
MockMessageHandler handler;
handler_ptr = &handler;
thread_->Post(&handler, kTestMessage1, NULL);
}
rtc::MessageList removed;
thread_->Clear(handler_ptr, rtc::MQID_ANY, &removed);
DCHECK_EQ(0U, removed.size());
}
// Verify that Send() calls handler synchronously when called on the
// same thread.
TEST_F(ThreadWrapperTest, SendSameThread) {
rtc::MessageData* data = new rtc::MessageData();
EXPECT_CALL(handler1_, OnMessage(
MatchMessage(&handler1_, kTestMessage1, data)))
.WillOnce(DeleteMessageData());
thread_->Send(&handler1_, kTestMessage1, data);
}
void InitializeWrapperForNewThread(rtc::Thread** thread,
base::WaitableEvent* done_event) {
JingleThreadWrapper::EnsureForCurrentMessageLoop();
JingleThreadWrapper::current()->set_send_allowed(true);
*thread = JingleThreadWrapper::current();
done_event->Signal();
}
// Verify that Send() calls handler synchronously when called for a
// different thread.
TEST_F(ThreadWrapperTest, SendToOtherThread) {
JingleThreadWrapper::current()->set_send_allowed(true);
base::Thread second_thread("JingleThreadWrapperTest");
second_thread.Start();
base::WaitableEvent initialized_event(true, false);
rtc::Thread* target;
second_thread.message_loop()->PostTask(
FROM_HERE, base::Bind(&InitializeWrapperForNewThread,
&target, &initialized_event));
initialized_event.Wait();
ASSERT_TRUE(target != NULL);
rtc::MessageData* data = new rtc::MessageData();
EXPECT_CALL(handler1_, OnMessage(
MatchMessage(&handler1_, kTestMessage1, data)))
.WillOnce(DeleteMessageData());
target->Send(&handler1_, kTestMessage1, data);
Mock::VerifyAndClearExpectations(&handler1_);
}
// Verify that thread handles Send() while another Send() is
// pending. The test creates second thread and Send()s kTestMessage1
// to that thread. kTestMessage1 handler calls PingMainThread() which
// tries to Send() kTestMessage2 to the main thread.
TEST_F(ThreadWrapperTest, SendDuringSend) {
JingleThreadWrapper::current()->set_send_allowed(true);
base::Thread second_thread("JingleThreadWrapperTest");
second_thread.Start();
base::WaitableEvent initialized_event(true, false);
rtc::Thread* target;
second_thread.message_loop()->PostTask(
FROM_HERE, base::Bind(&InitializeWrapperForNewThread,
&target, &initialized_event));
initialized_event.Wait();
ASSERT_TRUE(target != NULL);
rtc::MessageData* data = new rtc::MessageData();
EXPECT_CALL(handler1_, OnMessage(
MatchMessage(&handler1_, kTestMessage1, data)))
.WillOnce(DoAll(
InvokeWithoutArgs(
this, &ThreadWrapperTest::PingMainThread),
DeleteMessageData()));
target->Send(&handler1_, kTestMessage1, data);
Mock::VerifyAndClearExpectations(&handler1_);
}
TEST_F(ThreadWrapperTest, Dispose) {
bool deleted_ = false;
thread_->Dispose(new DeletableObject(&deleted_));
EXPECT_FALSE(deleted_);
message_loop_.RunUntilIdle();
EXPECT_TRUE(deleted_);
}
} // namespace jingle_glue
|