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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
// Copyright 2013 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/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/sequence_checker.h"
#include "base/test/sequenced_worker_pool_owner.h"
#include "base/threading/thread.h"
#include "testing/gtest/include/gtest/gtest.h"
// Duplicated from base/sequence_checker.h so that we can be good citizens
// there and undef the macro.
#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
#define ENABLE_SEQUENCE_CHECKER 1
#else
#define ENABLE_SEQUENCE_CHECKER 0
#endif
namespace base {
namespace {
const size_t kNumWorkerThreads = 3;
// Simple class to exercise the basics of SequenceChecker.
// DoStuff should verify that it's called on a valid sequenced thread.
// SequenceCheckedObject can be destroyed on any thread (like WeakPtr).
class SequenceCheckedObject {
public:
SequenceCheckedObject() {}
~SequenceCheckedObject() {}
// Verifies that it was called on the same thread as the constructor.
void DoStuff() {
DCHECK(sequence_checker_.CalledOnValidSequencedThread());
}
void DetachFromSequence() {
sequence_checker_.DetachFromSequence();
}
private:
SequenceChecker sequence_checker_;
DISALLOW_COPY_AND_ASSIGN(SequenceCheckedObject);
};
class SequenceCheckerTest : public testing::Test {
public:
SequenceCheckerTest() : other_thread_("sequence_checker_test_other_thread") {}
virtual ~SequenceCheckerTest() {}
virtual void SetUp() OVERRIDE {
other_thread_.Start();
ResetPool();
}
virtual void TearDown() OVERRIDE {
other_thread_.Stop();
pool()->Shutdown();
}
protected:
base::Thread* other_thread() { return &other_thread_; }
const scoped_refptr<SequencedWorkerPool>& pool() {
return pool_owner_->pool();
}
void PostDoStuffToWorkerPool(SequenceCheckedObject* sequence_checked_object,
const std::string& token_name) {
pool()->PostNamedSequencedWorkerTask(
token_name,
FROM_HERE,
base::Bind(&SequenceCheckedObject::DoStuff,
base::Unretained(sequence_checked_object)));
}
void PostDoStuffToOtherThread(
SequenceCheckedObject* sequence_checked_object) {
other_thread()->message_loop()->PostTask(
FROM_HERE,
base::Bind(&SequenceCheckedObject::DoStuff,
base::Unretained(sequence_checked_object)));
}
void PostDeleteToOtherThread(
scoped_ptr<SequenceCheckedObject> sequence_checked_object) {
other_thread()->message_loop()->DeleteSoon(
FROM_HERE,
sequence_checked_object.release());
}
// Destroys the SequencedWorkerPool instance, blocking until it is fully shut
// down, and creates a new instance.
void ResetPool() {
pool_owner_.reset(new SequencedWorkerPoolOwner(kNumWorkerThreads, "test"));
}
void MethodOnDifferentThreadDeathTest();
void DetachThenCallFromDifferentThreadDeathTest();
void DifferentSequenceTokensDeathTest();
void WorkerPoolAndSimpleThreadDeathTest();
void TwoDifferentWorkerPoolsDeathTest();
private:
MessageLoop message_loop_; // Needed by SequencedWorkerPool to function.
base::Thread other_thread_;
scoped_ptr<SequencedWorkerPoolOwner> pool_owner_;
};
TEST_F(SequenceCheckerTest, CallsAllowedOnSameThread) {
scoped_ptr<SequenceCheckedObject> sequence_checked_object(
new SequenceCheckedObject);
// Verify that DoStuff doesn't assert.
sequence_checked_object->DoStuff();
// Verify that the destructor doesn't assert.
sequence_checked_object.reset();
}
TEST_F(SequenceCheckerTest, DestructorAllowedOnDifferentThread) {
scoped_ptr<SequenceCheckedObject> sequence_checked_object(
new SequenceCheckedObject);
// Verify the destructor doesn't assert when called on a different thread.
PostDeleteToOtherThread(sequence_checked_object.Pass());
other_thread()->Stop();
}
TEST_F(SequenceCheckerTest, DetachFromSequence) {
scoped_ptr<SequenceCheckedObject> sequence_checked_object(
new SequenceCheckedObject);
// Verify that DoStuff doesn't assert when called on a different thread after
// a call to DetachFromSequence.
sequence_checked_object->DetachFromSequence();
PostDoStuffToOtherThread(sequence_checked_object.get());
other_thread()->Stop();
}
TEST_F(SequenceCheckerTest, SameSequenceTokenValid) {
scoped_ptr<SequenceCheckedObject> sequence_checked_object(
new SequenceCheckedObject);
sequence_checked_object->DetachFromSequence();
PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
pool()->FlushForTesting();
PostDeleteToOtherThread(sequence_checked_object.Pass());
other_thread()->Stop();
}
TEST_F(SequenceCheckerTest, DetachSequenceTokenValid) {
scoped_ptr<SequenceCheckedObject> sequence_checked_object(
new SequenceCheckedObject);
sequence_checked_object->DetachFromSequence();
PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
pool()->FlushForTesting();
sequence_checked_object->DetachFromSequence();
PostDoStuffToWorkerPool(sequence_checked_object.get(), "B");
PostDoStuffToWorkerPool(sequence_checked_object.get(), "B");
pool()->FlushForTesting();
PostDeleteToOtherThread(sequence_checked_object.Pass());
other_thread()->Stop();
}
#if GTEST_HAS_DEATH_TEST || !ENABLE_SEQUENCE_CHECKER
void SequenceCheckerTest::MethodOnDifferentThreadDeathTest() {
scoped_ptr<SequenceCheckedObject> sequence_checked_object(
new SequenceCheckedObject);
// DoStuff should assert in debug builds only when called on a
// different thread.
PostDoStuffToOtherThread(sequence_checked_object.get());
other_thread()->Stop();
}
#if ENABLE_SEQUENCE_CHECKER
TEST_F(SequenceCheckerTest, MethodNotAllowedOnDifferentThreadDeathTestInDebug) {
// The default style "fast" does not support multi-threaded tests.
::testing::FLAGS_gtest_death_test_style = "threadsafe";
ASSERT_DEATH({
MethodOnDifferentThreadDeathTest();
}, "");
}
#else
TEST_F(SequenceCheckerTest, MethodAllowedOnDifferentThreadDeathTestInRelease) {
MethodOnDifferentThreadDeathTest();
}
#endif // ENABLE_SEQUENCE_CHECKER
void SequenceCheckerTest::DetachThenCallFromDifferentThreadDeathTest() {
scoped_ptr<SequenceCheckedObject> sequence_checked_object(
new SequenceCheckedObject);
// DoStuff doesn't assert when called on a different thread
// after a call to DetachFromSequence.
sequence_checked_object->DetachFromSequence();
PostDoStuffToOtherThread(sequence_checked_object.get());
other_thread()->Stop();
// DoStuff should assert in debug builds only after moving to
// another thread.
sequence_checked_object->DoStuff();
}
#if ENABLE_SEQUENCE_CHECKER
TEST_F(SequenceCheckerTest, DetachFromSequenceDeathTestInDebug) {
// The default style "fast" does not support multi-threaded tests.
::testing::FLAGS_gtest_death_test_style = "threadsafe";
ASSERT_DEATH({
DetachThenCallFromDifferentThreadDeathTest();
}, "");
}
#else
TEST_F(SequenceCheckerTest, DetachFromThreadDeathTestInRelease) {
DetachThenCallFromDifferentThreadDeathTest();
}
#endif // ENABLE_SEQUENCE_CHECKER
void SequenceCheckerTest::DifferentSequenceTokensDeathTest() {
scoped_ptr<SequenceCheckedObject> sequence_checked_object(
new SequenceCheckedObject);
sequence_checked_object->DetachFromSequence();
PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
PostDoStuffToWorkerPool(sequence_checked_object.get(), "B");
PostDoStuffToWorkerPool(sequence_checked_object.get(), "B");
pool()->FlushForTesting();
PostDeleteToOtherThread(sequence_checked_object.Pass());
other_thread()->Stop();
}
#if ENABLE_SEQUENCE_CHECKER
TEST_F(SequenceCheckerTest, DifferentSequenceTokensDeathTestInDebug) {
// The default style "fast" does not support multi-threaded tests.
::testing::FLAGS_gtest_death_test_style = "threadsafe";
ASSERT_DEATH({
DifferentSequenceTokensDeathTest();
}, "");
}
#else
TEST_F(SequenceCheckerTest, DifferentSequenceTokensDeathTestInRelease) {
DifferentSequenceTokensDeathTest();
}
#endif // ENABLE_SEQUENCE_CHECKER
void SequenceCheckerTest::WorkerPoolAndSimpleThreadDeathTest() {
scoped_ptr<SequenceCheckedObject> sequence_checked_object(
new SequenceCheckedObject);
sequence_checked_object->DetachFromSequence();
PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
pool()->FlushForTesting();
PostDoStuffToOtherThread(sequence_checked_object.get());
other_thread()->Stop();
}
#if ENABLE_SEQUENCE_CHECKER
TEST_F(SequenceCheckerTest, WorkerPoolAndSimpleThreadDeathTestInDebug) {
// The default style "fast" does not support multi-threaded tests.
::testing::FLAGS_gtest_death_test_style = "threadsafe";
ASSERT_DEATH({
WorkerPoolAndSimpleThreadDeathTest();
}, "");
}
#else
TEST_F(SequenceCheckerTest, WorkerPoolAndSimpleThreadDeathTestInRelease) {
WorkerPoolAndSimpleThreadDeathTest();
}
#endif // ENABLE_SEQUENCE_CHECKER
void SequenceCheckerTest::TwoDifferentWorkerPoolsDeathTest() {
scoped_ptr<SequenceCheckedObject> sequence_checked_object(
new SequenceCheckedObject);
sequence_checked_object->DetachFromSequence();
PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
pool()->FlushForTesting();
SequencedWorkerPoolOwner second_pool_owner(kNumWorkerThreads, "test2");
second_pool_owner.pool()->PostNamedSequencedWorkerTask(
"A",
FROM_HERE,
base::Bind(&SequenceCheckedObject::DoStuff,
base::Unretained(sequence_checked_object.get())));
second_pool_owner.pool()->FlushForTesting();
second_pool_owner.pool()->Shutdown();
}
#if ENABLE_SEQUENCE_CHECKER
TEST_F(SequenceCheckerTest, TwoDifferentWorkerPoolsDeathTestInDebug) {
// The default style "fast" does not support multi-threaded tests.
::testing::FLAGS_gtest_death_test_style = "threadsafe";
ASSERT_DEATH({
TwoDifferentWorkerPoolsDeathTest();
}, "");
}
#else
TEST_F(SequenceCheckerTest, TwoDifferentWorkerPoolsDeathTestInRelease) {
TwoDifferentWorkerPoolsDeathTest();
}
#endif // ENABLE_SEQUENCE_CHECKER
#endif // GTEST_HAS_DEATH_TEST || !ENABLE_SEQUENCE_CHECKER
} // namespace
} // namespace base
// Just in case we ever get lumped together with other compilation units.
#undef ENABLE_SEQUENCE_CHECKER
|