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
3 // found in the LICENSE file.
5 #include "base/observer_list.h"
6 #include "base/observer_list_threadsafe.h"
10 #include "base/compiler_specific.h"
11 #include "base/location.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/run_loop.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/threading/platform_thread.h"
16 #include "testing/gtest/include/gtest/gtest.h"
23 virtual void Observe(int x
) = 0;
27 class Adder
: public Foo
{
29 explicit Adder(int scaler
) : total(0), scaler_(scaler
) {}
30 void Observe(int x
) override
{ total
+= x
* scaler_
; }
38 class Disrupter
: public Foo
{
40 Disrupter(ObserverList
<Foo
>* list
, Foo
* doomed
)
44 ~Disrupter() override
{}
45 void Observe(int x
) override
{ list_
->RemoveObserver(doomed_
); }
48 ObserverList
<Foo
>* list_
;
52 class ThreadSafeDisrupter
: public Foo
{
54 ThreadSafeDisrupter(ObserverListThreadSafe
<Foo
>* list
, Foo
* doomed
)
58 ~ThreadSafeDisrupter() override
{}
59 void Observe(int x
) override
{ list_
->RemoveObserver(doomed_
); }
62 ObserverListThreadSafe
<Foo
>* list_
;
66 template <typename ObserverListType
>
67 class AddInObserve
: public Foo
{
69 explicit AddInObserve(ObserverListType
* observer_list
)
71 observer_list(observer_list
),
75 void Observe(int x
) override
{
78 observer_list
->AddObserver(&adder
);
83 ObserverListType
* observer_list
;
88 static const int kThreadRunTime
= 2000; // ms to run the multi-threaded test.
90 // A thread for use in the ThreadSafeObserver test
91 // which will add and remove itself from the notification
93 class AddRemoveThread
: public PlatformThread::Delegate
,
96 AddRemoveThread(ObserverListThreadSafe
<Foo
>* list
, bool notify
)
103 do_notifies_(notify
),
104 weak_factory_(this) {
107 ~AddRemoveThread() override
{}
109 void ThreadMain() override
{
110 loop_
= new MessageLoop(); // Fire up a message loop.
111 loop_
->task_runner()->PostTask(
113 base::Bind(&AddRemoveThread::AddTask
, weak_factory_
.GetWeakPtr()));
115 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " <<
116 // count_observes_ << ", " << count_addtask_;
118 loop_
= reinterpret_cast<MessageLoop
*>(0xdeadbeef);
122 // This task just keeps posting to itself in an attempt
123 // to race with the notifier.
127 if ((Time::Now() - start_
).InMilliseconds() > kThreadRunTime
) {
133 list_
->AddObserver(this);
138 list_
->Notify(FROM_HERE
, &Foo::Observe
, 10);
141 loop_
->task_runner()->PostTask(
143 base::Bind(&AddRemoveThread::AddTask
, weak_factory_
.GetWeakPtr()));
147 loop_
->task_runner()->PostTask(FROM_HERE
,
148 MessageLoop::QuitWhenIdleClosure());
151 void Observe(int x
) override
{
154 // If we're getting called after we removed ourselves from
155 // the list, that is very bad!
158 // This callback should fire on the appropriate thread
159 EXPECT_EQ(loop_
, MessageLoop::current());
161 list_
->RemoveObserver(this);
166 ObserverListThreadSafe
<Foo
>* list_
;
168 bool in_list_
; // Are we currently registered for notifications.
169 // in_list_ is only used on |this| thread.
170 Time start_
; // The time we started the test.
172 int count_observes_
; // Number of times we observed.
173 int count_addtask_
; // Number of times thread AddTask was called
174 bool do_notifies_
; // Whether these threads should do notifications.
176 base::WeakPtrFactory
<AddRemoveThread
> weak_factory_
;
179 TEST(ObserverListTest
, BasicTest
) {
180 ObserverList
<Foo
> observer_list
;
181 Adder
a(1), b(-1), c(1), d(-1), e(-1);
182 Disrupter
evil(&observer_list
, &c
);
184 observer_list
.AddObserver(&a
);
185 observer_list
.AddObserver(&b
);
187 EXPECT_TRUE(observer_list
.HasObserver(&a
));
188 EXPECT_FALSE(observer_list
.HasObserver(&c
));
190 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(10));
192 observer_list
.AddObserver(&evil
);
193 observer_list
.AddObserver(&c
);
194 observer_list
.AddObserver(&d
);
196 // Removing an observer not in the list should do nothing.
197 observer_list
.RemoveObserver(&e
);
199 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(10));
201 EXPECT_EQ(20, a
.total
);
202 EXPECT_EQ(-20, b
.total
);
203 EXPECT_EQ(0, c
.total
);
204 EXPECT_EQ(-10, d
.total
);
205 EXPECT_EQ(0, e
.total
);
208 TEST(ObserverListThreadSafeTest
, BasicTest
) {
211 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
212 new ObserverListThreadSafe
<Foo
>);
217 ThreadSafeDisrupter
evil(observer_list
.get(), &c
);
219 observer_list
->AddObserver(&a
);
220 observer_list
->AddObserver(&b
);
222 observer_list
->Notify(FROM_HERE
, &Foo::Observe
, 10);
223 RunLoop().RunUntilIdle();
225 observer_list
->AddObserver(&evil
);
226 observer_list
->AddObserver(&c
);
227 observer_list
->AddObserver(&d
);
229 observer_list
->Notify(FROM_HERE
, &Foo::Observe
, 10);
230 RunLoop().RunUntilIdle();
232 EXPECT_EQ(20, a
.total
);
233 EXPECT_EQ(-20, b
.total
);
234 EXPECT_EQ(0, c
.total
);
235 EXPECT_EQ(-10, d
.total
);
238 TEST(ObserverListThreadSafeTest
, RemoveObserver
) {
241 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
242 new ObserverListThreadSafe
<Foo
>);
245 // A workaround for the compiler bug. See http://crbug.com/121960.
248 // Should do nothing.
249 observer_list
->RemoveObserver(&a
);
250 observer_list
->RemoveObserver(&b
);
252 observer_list
->Notify(FROM_HERE
, &Foo::Observe
, 10);
253 RunLoop().RunUntilIdle();
255 EXPECT_EQ(0, a
.total
);
256 EXPECT_EQ(0, b
.total
);
258 observer_list
->AddObserver(&a
);
260 // Should also do nothing.
261 observer_list
->RemoveObserver(&b
);
263 observer_list
->Notify(FROM_HERE
, &Foo::Observe
, 10);
264 RunLoop().RunUntilIdle();
266 EXPECT_EQ(10, a
.total
);
267 EXPECT_EQ(0, b
.total
);
270 TEST(ObserverListThreadSafeTest
, WithoutMessageLoop
) {
271 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
272 new ObserverListThreadSafe
<Foo
>);
274 Adder
a(1), b(1), c(1);
276 // No MessageLoop, so these should not be added.
277 observer_list
->AddObserver(&a
);
278 observer_list
->AddObserver(&b
);
281 // Add c when there's a loop.
283 observer_list
->AddObserver(&c
);
285 observer_list
->Notify(FROM_HERE
, &Foo::Observe
, 10);
286 RunLoop().RunUntilIdle();
288 EXPECT_EQ(0, a
.total
);
289 EXPECT_EQ(0, b
.total
);
290 EXPECT_EQ(10, c
.total
);
292 // Now add a when there's a loop.
293 observer_list
->AddObserver(&a
);
295 // Remove c when there's a loop.
296 observer_list
->RemoveObserver(&c
);
299 observer_list
->Notify(FROM_HERE
, &Foo::Observe
, 20);
300 RunLoop().RunUntilIdle();
302 EXPECT_EQ(20, a
.total
);
303 EXPECT_EQ(0, b
.total
);
304 EXPECT_EQ(10, c
.total
);
307 // Removing should always succeed with or without a loop.
308 observer_list
->RemoveObserver(&a
);
310 // Notifying should not fail but should also be a no-op.
312 observer_list
->AddObserver(&b
);
313 observer_list
->Notify(FROM_HERE
, &Foo::Observe
, 30);
314 RunLoop().RunUntilIdle();
316 EXPECT_EQ(20, a
.total
);
317 EXPECT_EQ(30, b
.total
);
318 EXPECT_EQ(10, c
.total
);
321 class FooRemover
: public Foo
{
323 explicit FooRemover(ObserverListThreadSafe
<Foo
>* list
) : list_(list
) {}
324 ~FooRemover() override
{}
326 void AddFooToRemove(Foo
* foo
) {
327 foos_
.push_back(foo
);
330 void Observe(int x
) override
{
331 std::vector
<Foo
*> tmp
;
333 for (std::vector
<Foo
*>::iterator it
= tmp
.begin();
334 it
!= tmp
.end(); ++it
) {
335 list_
->RemoveObserver(*it
);
340 const scoped_refptr
<ObserverListThreadSafe
<Foo
> > list_
;
341 std::vector
<Foo
*> foos_
;
344 TEST(ObserverListThreadSafeTest
, RemoveMultipleObservers
) {
346 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
347 new ObserverListThreadSafe
<Foo
>);
349 FooRemover
a(observer_list
.get());
352 observer_list
->AddObserver(&a
);
353 observer_list
->AddObserver(&b
);
355 a
.AddFooToRemove(&a
);
356 a
.AddFooToRemove(&b
);
358 observer_list
->Notify(FROM_HERE
, &Foo::Observe
, 1);
359 RunLoop().RunUntilIdle();
362 // A test driver for a multi-threaded notification loop. Runs a number
363 // of observer threads, each of which constantly adds/removes itself
364 // from the observer list. Optionally, if cross_thread_notifies is set
365 // to true, the observer threads will also trigger notifications to
367 static void ThreadSafeObserverHarness(int num_threads
,
368 bool cross_thread_notifies
) {
371 const int kMaxThreads
= 15;
372 num_threads
= num_threads
> kMaxThreads
? kMaxThreads
: num_threads
;
374 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
375 new ObserverListThreadSafe
<Foo
>);
381 observer_list
->AddObserver(&a
);
382 observer_list
->AddObserver(&b
);
384 AddRemoveThread
* threaded_observer
[kMaxThreads
];
385 base::PlatformThreadHandle threads
[kMaxThreads
];
386 for (int index
= 0; index
< num_threads
; index
++) {
387 threaded_observer
[index
] = new AddRemoveThread(observer_list
.get(), false);
388 EXPECT_TRUE(PlatformThread::Create(0,
389 threaded_observer
[index
], &threads
[index
]));
392 Time start
= Time::Now();
394 if ((Time::Now() - start
).InMilliseconds() > kThreadRunTime
)
397 observer_list
->Notify(FROM_HERE
, &Foo::Observe
, 10);
399 RunLoop().RunUntilIdle();
402 for (int index
= 0; index
< num_threads
; index
++) {
403 threaded_observer
[index
]->Quit();
404 PlatformThread::Join(threads
[index
]);
408 TEST(ObserverListThreadSafeTest
, CrossThreadObserver
) {
409 // Use 7 observer threads. Notifications only come from
411 ThreadSafeObserverHarness(7, false);
414 TEST(ObserverListThreadSafeTest
, CrossThreadNotifications
) {
415 // Use 3 observer threads. Notifications will fire from
416 // the main thread and all 3 observer threads.
417 ThreadSafeObserverHarness(3, true);
420 TEST(ObserverListThreadSafeTest
, OutlivesMessageLoop
) {
421 MessageLoop
* loop
= new MessageLoop
;
422 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
423 new ObserverListThreadSafe
<Foo
>);
426 observer_list
->AddObserver(&a
);
428 // Test passes if we don't crash here.
429 observer_list
->Notify(FROM_HERE
, &Foo::Observe
, 1);
432 TEST(ObserverListTest
, Existing
) {
433 ObserverList
<Foo
> observer_list(ObserverList
<Foo
>::NOTIFY_EXISTING_ONLY
);
435 AddInObserve
<ObserverList
<Foo
> > b(&observer_list
);
437 observer_list
.AddObserver(&a
);
438 observer_list
.AddObserver(&b
);
440 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
442 EXPECT_TRUE(b
.added
);
443 // B's adder should not have been notified because it was added during
445 EXPECT_EQ(0, b
.adder
.total
);
447 // Notify again to make sure b's adder is notified.
448 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
449 EXPECT_EQ(1, b
.adder
.total
);
452 // Same as above, but for ObserverListThreadSafe
453 TEST(ObserverListThreadSafeTest
, Existing
) {
455 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
456 new ObserverListThreadSafe
<Foo
>(ObserverList
<Foo
>::NOTIFY_EXISTING_ONLY
));
458 AddInObserve
<ObserverListThreadSafe
<Foo
> > b(observer_list
.get());
460 observer_list
->AddObserver(&a
);
461 observer_list
->AddObserver(&b
);
463 observer_list
->Notify(FROM_HERE
, &Foo::Observe
, 1);
464 RunLoop().RunUntilIdle();
466 EXPECT_TRUE(b
.added
);
467 // B's adder should not have been notified because it was added during
469 EXPECT_EQ(0, b
.adder
.total
);
471 // Notify again to make sure b's adder is notified.
472 observer_list
->Notify(FROM_HERE
, &Foo::Observe
, 1);
473 RunLoop().RunUntilIdle();
474 EXPECT_EQ(1, b
.adder
.total
);
477 class AddInClearObserve
: public Foo
{
479 explicit AddInClearObserve(ObserverList
<Foo
>* list
)
480 : list_(list
), added_(false), adder_(1) {}
482 void Observe(int /* x */) override
{
484 list_
->AddObserver(&adder_
);
488 bool added() const { return added_
; }
489 const Adder
& adder() const { return adder_
; }
492 ObserverList
<Foo
>* const list_
;
498 TEST(ObserverListTest
, ClearNotifyAll
) {
499 ObserverList
<Foo
> observer_list
;
500 AddInClearObserve
a(&observer_list
);
502 observer_list
.AddObserver(&a
);
504 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
505 EXPECT_TRUE(a
.added());
506 EXPECT_EQ(1, a
.adder().total
)
507 << "Adder should observe once and have sum of 1.";
510 TEST(ObserverListTest
, ClearNotifyExistingOnly
) {
511 ObserverList
<Foo
> observer_list(ObserverList
<Foo
>::NOTIFY_EXISTING_ONLY
);
512 AddInClearObserve
a(&observer_list
);
514 observer_list
.AddObserver(&a
);
516 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
517 EXPECT_TRUE(a
.added());
518 EXPECT_EQ(0, a
.adder().total
)
519 << "Adder should not observe, so sum should still be 0.";
522 class ListDestructor
: public Foo
{
524 explicit ListDestructor(ObserverList
<Foo
>* list
) : list_(list
) {}
525 ~ListDestructor() override
{}
527 void Observe(int x
) override
{ delete list_
; }
530 ObserverList
<Foo
>* list_
;
534 TEST(ObserverListTest
, IteratorOutlivesList
) {
535 ObserverList
<Foo
>* observer_list
= new ObserverList
<Foo
>;
536 ListDestructor
a(observer_list
);
537 observer_list
->AddObserver(&a
);
539 FOR_EACH_OBSERVER(Foo
, *observer_list
, Observe(0));
540 // If this test fails, there'll be Valgrind errors when this function goes out