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/memory/weak_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "base/threading/platform_thread.h"
15 #include "testing/gtest/include/gtest/gtest.h"
22 virtual void Observe(int x
) = 0;
26 class Adder
: public Foo
{
28 explicit Adder(int scaler
) : total(0), scaler_(scaler
) {}
29 void Observe(int x
) override
{ total
+= x
* scaler_
; }
37 class Disrupter
: public Foo
{
39 Disrupter(ObserverList
<Foo
>* list
, Foo
* doomed
)
43 ~Disrupter() override
{}
44 void Observe(int x
) override
{ list_
->RemoveObserver(doomed_
); }
47 ObserverList
<Foo
>* list_
;
51 class ThreadSafeDisrupter
: public Foo
{
53 ThreadSafeDisrupter(ObserverListThreadSafe
<Foo
>* list
, Foo
* doomed
)
57 ~ThreadSafeDisrupter() override
{}
58 void Observe(int x
) override
{ list_
->RemoveObserver(doomed_
); }
61 ObserverListThreadSafe
<Foo
>* list_
;
65 template <typename ObserverListType
>
66 class AddInObserve
: public Foo
{
68 explicit AddInObserve(ObserverListType
* observer_list
)
70 observer_list(observer_list
),
74 virtual void Observe(int x
) override
{
77 observer_list
->AddObserver(&adder
);
82 ObserverListType
* observer_list
;
87 static const int kThreadRunTime
= 2000; // ms to run the multi-threaded test.
89 // A thread for use in the ThreadSafeObserver test
90 // which will add and remove itself from the notification
92 class AddRemoveThread
: public PlatformThread::Delegate
,
95 AddRemoveThread(ObserverListThreadSafe
<Foo
>* list
, bool notify
)
102 do_notifies_(notify
),
103 weak_factory_(this) {
106 ~AddRemoveThread() override
{}
108 void ThreadMain() override
{
109 loop_
= new MessageLoop(); // Fire up a message loop.
112 base::Bind(&AddRemoveThread::AddTask
, weak_factory_
.GetWeakPtr()));
114 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " <<
115 // count_observes_ << ", " << count_addtask_;
117 loop_
= reinterpret_cast<MessageLoop
*>(0xdeadbeef);
121 // This task just keeps posting to itself in an attempt
122 // to race with the notifier.
126 if ((Time::Now() - start_
).InMilliseconds() > kThreadRunTime
) {
132 list_
->AddObserver(this);
137 list_
->Notify(&Foo::Observe
, 10);
142 base::Bind(&AddRemoveThread::AddTask
, weak_factory_
.GetWeakPtr()));
146 loop_
->PostTask(FROM_HERE
, MessageLoop::QuitWhenIdleClosure());
149 void Observe(int x
) override
{
152 // If we're getting called after we removed ourselves from
153 // the list, that is very bad!
156 // This callback should fire on the appropriate thread
157 EXPECT_EQ(loop_
, MessageLoop::current());
159 list_
->RemoveObserver(this);
164 ObserverListThreadSafe
<Foo
>* list_
;
166 bool in_list_
; // Are we currently registered for notifications.
167 // in_list_ is only used on |this| thread.
168 Time start_
; // The time we started the test.
170 int count_observes_
; // Number of times we observed.
171 int count_addtask_
; // Number of times thread AddTask was called
172 bool do_notifies_
; // Whether these threads should do notifications.
174 base::WeakPtrFactory
<AddRemoveThread
> weak_factory_
;
177 TEST(ObserverListTest
, BasicTest
) {
178 ObserverList
<Foo
> observer_list
;
179 Adder
a(1), b(-1), c(1), d(-1), e(-1);
180 Disrupter
evil(&observer_list
, &c
);
182 observer_list
.AddObserver(&a
);
183 observer_list
.AddObserver(&b
);
185 EXPECT_TRUE(observer_list
.HasObserver(&a
));
186 EXPECT_FALSE(observer_list
.HasObserver(&c
));
188 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(10));
190 observer_list
.AddObserver(&evil
);
191 observer_list
.AddObserver(&c
);
192 observer_list
.AddObserver(&d
);
194 // Removing an observer not in the list should do nothing.
195 observer_list
.RemoveObserver(&e
);
197 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(10));
199 EXPECT_EQ(20, a
.total
);
200 EXPECT_EQ(-20, b
.total
);
201 EXPECT_EQ(0, c
.total
);
202 EXPECT_EQ(-10, d
.total
);
203 EXPECT_EQ(0, e
.total
);
206 TEST(ObserverListThreadSafeTest
, BasicTest
) {
209 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
210 new ObserverListThreadSafe
<Foo
>);
215 ThreadSafeDisrupter
evil(observer_list
.get(), &c
);
217 observer_list
->AddObserver(&a
);
218 observer_list
->AddObserver(&b
);
220 observer_list
->Notify(&Foo::Observe
, 10);
221 RunLoop().RunUntilIdle();
223 observer_list
->AddObserver(&evil
);
224 observer_list
->AddObserver(&c
);
225 observer_list
->AddObserver(&d
);
227 observer_list
->Notify(&Foo::Observe
, 10);
228 RunLoop().RunUntilIdle();
230 EXPECT_EQ(20, a
.total
);
231 EXPECT_EQ(-20, b
.total
);
232 EXPECT_EQ(0, c
.total
);
233 EXPECT_EQ(-10, d
.total
);
236 TEST(ObserverListThreadSafeTest
, RemoveObserver
) {
239 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
240 new ObserverListThreadSafe
<Foo
>);
243 // A workaround for the compiler bug. See http://crbug.com/121960.
246 // Should do nothing.
247 observer_list
->RemoveObserver(&a
);
248 observer_list
->RemoveObserver(&b
);
250 observer_list
->Notify(&Foo::Observe
, 10);
251 RunLoop().RunUntilIdle();
253 EXPECT_EQ(0, a
.total
);
254 EXPECT_EQ(0, b
.total
);
256 observer_list
->AddObserver(&a
);
258 // Should also do nothing.
259 observer_list
->RemoveObserver(&b
);
261 observer_list
->Notify(&Foo::Observe
, 10);
262 RunLoop().RunUntilIdle();
264 EXPECT_EQ(10, a
.total
);
265 EXPECT_EQ(0, b
.total
);
268 TEST(ObserverListThreadSafeTest
, WithoutMessageLoop
) {
269 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
270 new ObserverListThreadSafe
<Foo
>);
272 Adder
a(1), b(1), c(1);
274 // No MessageLoop, so these should not be added.
275 observer_list
->AddObserver(&a
);
276 observer_list
->AddObserver(&b
);
279 // Add c when there's a loop.
281 observer_list
->AddObserver(&c
);
283 observer_list
->Notify(&Foo::Observe
, 10);
284 RunLoop().RunUntilIdle();
286 EXPECT_EQ(0, a
.total
);
287 EXPECT_EQ(0, b
.total
);
288 EXPECT_EQ(10, c
.total
);
290 // Now add a when there's a loop.
291 observer_list
->AddObserver(&a
);
293 // Remove c when there's a loop.
294 observer_list
->RemoveObserver(&c
);
297 observer_list
->Notify(&Foo::Observe
, 20);
298 RunLoop().RunUntilIdle();
300 EXPECT_EQ(20, a
.total
);
301 EXPECT_EQ(0, b
.total
);
302 EXPECT_EQ(10, c
.total
);
305 // Removing should always succeed with or without a loop.
306 observer_list
->RemoveObserver(&a
);
308 // Notifying should not fail but should also be a no-op.
310 observer_list
->AddObserver(&b
);
311 observer_list
->Notify(&Foo::Observe
, 30);
312 RunLoop().RunUntilIdle();
314 EXPECT_EQ(20, a
.total
);
315 EXPECT_EQ(30, b
.total
);
316 EXPECT_EQ(10, c
.total
);
319 class FooRemover
: public Foo
{
321 explicit FooRemover(ObserverListThreadSafe
<Foo
>* list
) : list_(list
) {}
322 ~FooRemover() override
{}
324 void AddFooToRemove(Foo
* foo
) {
325 foos_
.push_back(foo
);
328 void Observe(int x
) override
{
329 std::vector
<Foo
*> tmp
;
331 for (std::vector
<Foo
*>::iterator it
= tmp
.begin();
332 it
!= tmp
.end(); ++it
) {
333 list_
->RemoveObserver(*it
);
338 const scoped_refptr
<ObserverListThreadSafe
<Foo
> > list_
;
339 std::vector
<Foo
*> foos_
;
342 TEST(ObserverListThreadSafeTest
, RemoveMultipleObservers
) {
344 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
345 new ObserverListThreadSafe
<Foo
>);
347 FooRemover
a(observer_list
.get());
350 observer_list
->AddObserver(&a
);
351 observer_list
->AddObserver(&b
);
353 a
.AddFooToRemove(&a
);
354 a
.AddFooToRemove(&b
);
356 observer_list
->Notify(&Foo::Observe
, 1);
357 RunLoop().RunUntilIdle();
360 // A test driver for a multi-threaded notification loop. Runs a number
361 // of observer threads, each of which constantly adds/removes itself
362 // from the observer list. Optionally, if cross_thread_notifies is set
363 // to true, the observer threads will also trigger notifications to
365 static void ThreadSafeObserverHarness(int num_threads
,
366 bool cross_thread_notifies
) {
369 const int kMaxThreads
= 15;
370 num_threads
= num_threads
> kMaxThreads
? kMaxThreads
: num_threads
;
372 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
373 new ObserverListThreadSafe
<Foo
>);
379 observer_list
->AddObserver(&a
);
380 observer_list
->AddObserver(&b
);
382 AddRemoveThread
* threaded_observer
[kMaxThreads
];
383 base::PlatformThreadHandle threads
[kMaxThreads
];
384 for (int index
= 0; index
< num_threads
; index
++) {
385 threaded_observer
[index
] = new AddRemoveThread(observer_list
.get(), false);
386 EXPECT_TRUE(PlatformThread::Create(0,
387 threaded_observer
[index
], &threads
[index
]));
390 Time start
= Time::Now();
392 if ((Time::Now() - start
).InMilliseconds() > kThreadRunTime
)
395 observer_list
->Notify(&Foo::Observe
, 10);
397 RunLoop().RunUntilIdle();
400 for (int index
= 0; index
< num_threads
; index
++) {
401 threaded_observer
[index
]->Quit();
402 PlatformThread::Join(threads
[index
]);
406 TEST(ObserverListThreadSafeTest
, CrossThreadObserver
) {
407 // Use 7 observer threads. Notifications only come from
409 ThreadSafeObserverHarness(7, false);
412 TEST(ObserverListThreadSafeTest
, CrossThreadNotifications
) {
413 // Use 3 observer threads. Notifications will fire from
414 // the main thread and all 3 observer threads.
415 ThreadSafeObserverHarness(3, true);
418 TEST(ObserverListThreadSafeTest
, OutlivesMessageLoop
) {
419 MessageLoop
* loop
= new MessageLoop
;
420 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
421 new ObserverListThreadSafe
<Foo
>);
424 observer_list
->AddObserver(&a
);
426 // Test passes if we don't crash here.
427 observer_list
->Notify(&Foo::Observe
, 1);
430 TEST(ObserverListTest
, Existing
) {
431 ObserverList
<Foo
> observer_list(ObserverList
<Foo
>::NOTIFY_EXISTING_ONLY
);
433 AddInObserve
<ObserverList
<Foo
> > b(&observer_list
);
435 observer_list
.AddObserver(&a
);
436 observer_list
.AddObserver(&b
);
438 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
440 EXPECT_TRUE(b
.added
);
441 // B's adder should not have been notified because it was added during
443 EXPECT_EQ(0, b
.adder
.total
);
445 // Notify again to make sure b's adder is notified.
446 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
447 EXPECT_EQ(1, b
.adder
.total
);
450 // Same as above, but for ObserverListThreadSafe
451 TEST(ObserverListThreadSafeTest
, Existing
) {
453 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
454 new ObserverListThreadSafe
<Foo
>(ObserverList
<Foo
>::NOTIFY_EXISTING_ONLY
));
456 AddInObserve
<ObserverListThreadSafe
<Foo
> > b(observer_list
.get());
458 observer_list
->AddObserver(&a
);
459 observer_list
->AddObserver(&b
);
461 observer_list
->Notify(&Foo::Observe
, 1);
462 RunLoop().RunUntilIdle();
464 EXPECT_TRUE(b
.added
);
465 // B's adder should not have been notified because it was added during
467 EXPECT_EQ(0, b
.adder
.total
);
469 // Notify again to make sure b's adder is notified.
470 observer_list
->Notify(&Foo::Observe
, 1);
471 RunLoop().RunUntilIdle();
472 EXPECT_EQ(1, b
.adder
.total
);
475 class AddInClearObserve
: public Foo
{
477 explicit AddInClearObserve(ObserverList
<Foo
>* list
)
478 : list_(list
), added_(false), adder_(1) {}
480 void Observe(int /* x */) override
{
482 list_
->AddObserver(&adder_
);
486 bool added() const { return added_
; }
487 const Adder
& adder() const { return adder_
; }
490 ObserverList
<Foo
>* const list_
;
496 TEST(ObserverListTest
, ClearNotifyAll
) {
497 ObserverList
<Foo
> observer_list
;
498 AddInClearObserve
a(&observer_list
);
500 observer_list
.AddObserver(&a
);
502 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
503 EXPECT_TRUE(a
.added());
504 EXPECT_EQ(1, a
.adder().total
)
505 << "Adder should observe once and have sum of 1.";
508 TEST(ObserverListTest
, ClearNotifyExistingOnly
) {
509 ObserverList
<Foo
> observer_list(ObserverList
<Foo
>::NOTIFY_EXISTING_ONLY
);
510 AddInClearObserve
a(&observer_list
);
512 observer_list
.AddObserver(&a
);
514 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
515 EXPECT_TRUE(a
.added());
516 EXPECT_EQ(0, a
.adder().total
)
517 << "Adder should not observe, so sum should still be 0.";
520 class ListDestructor
: public Foo
{
522 explicit ListDestructor(ObserverList
<Foo
>* list
) : list_(list
) {}
523 ~ListDestructor() override
{}
525 void Observe(int x
) override
{ delete list_
; }
528 ObserverList
<Foo
>* list_
;
532 TEST(ObserverListTest
, IteratorOutlivesList
) {
533 ObserverList
<Foo
>* observer_list
= new ObserverList
<Foo
>;
534 ListDestructor
a(observer_list
);
535 observer_list
->AddObserver(&a
);
537 FOR_EACH_OBSERVER(Foo
, *observer_list
, Observe(0));
538 // If this test fails, there'll be Valgrind errors when this function goes out