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.h"
13 #include "base/threading/platform_thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using base::PlatformThread
;
23 virtual void Observe(int x
) = 0;
27 class Adder
: public Foo
{
29 explicit Adder(int scaler
) : total(0), scaler_(scaler
) {}
30 virtual void Observe(int x
) OVERRIDE
{
40 class Disrupter
: public Foo
{
42 Disrupter(ObserverList
<Foo
>* list
, Foo
* doomed
)
46 virtual ~Disrupter() {}
47 virtual void Observe(int x
) OVERRIDE
{
48 list_
->RemoveObserver(doomed_
);
52 ObserverList
<Foo
>* list_
;
56 class ThreadSafeDisrupter
: public Foo
{
58 ThreadSafeDisrupter(ObserverListThreadSafe
<Foo
>* list
, Foo
* doomed
)
62 virtual ~ThreadSafeDisrupter() {}
63 virtual void Observe(int x
) OVERRIDE
{
64 list_
->RemoveObserver(doomed_
);
68 ObserverListThreadSafe
<Foo
>* list_
;
72 template <typename ObserverListType
>
73 class AddInObserve
: public Foo
{
75 explicit AddInObserve(ObserverListType
* observer_list
)
77 observer_list(observer_list
),
81 virtual void Observe(int x
) OVERRIDE
{
84 observer_list
->AddObserver(&adder
);
89 ObserverListType
* observer_list
;
94 static const int kThreadRunTime
= 2000; // ms to run the multi-threaded test.
96 // A thread for use in the ThreadSafeObserver test
97 // which will add and remove itself from the notification
99 class AddRemoveThread
: public PlatformThread::Delegate
,
102 AddRemoveThread(ObserverListThreadSafe
<Foo
>* list
, bool notify
)
109 do_notifies_(notify
),
110 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
113 virtual ~AddRemoveThread() {
116 virtual void ThreadMain() OVERRIDE
{
117 loop_
= new MessageLoop(); // Fire up a message loop.
120 base::Bind(&AddRemoveThread::AddTask
, weak_factory_
.GetWeakPtr()));
122 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " <<
123 // count_observes_ << ", " << count_addtask_;
125 loop_
= reinterpret_cast<MessageLoop
*>(0xdeadbeef);
129 // This task just keeps posting to itself in an attempt
130 // to race with the notifier.
134 if ((Time::Now() - start_
).InMilliseconds() > kThreadRunTime
) {
140 list_
->AddObserver(this);
145 list_
->Notify(&Foo::Observe
, 10);
150 base::Bind(&AddRemoveThread::AddTask
, weak_factory_
.GetWeakPtr()));
154 loop_
->PostTask(FROM_HERE
, MessageLoop::QuitClosure());
157 virtual void Observe(int x
) OVERRIDE
{
160 // If we're getting called after we removed ourselves from
161 // the list, that is very bad!
164 // This callback should fire on the appropriate thread
165 EXPECT_EQ(loop_
, MessageLoop::current());
167 list_
->RemoveObserver(this);
172 ObserverListThreadSafe
<Foo
>* list_
;
174 bool in_list_
; // Are we currently registered for notifications.
175 // in_list_ is only used on |this| thread.
176 Time start_
; // The time we started the test.
178 int count_observes_
; // Number of times we observed.
179 int count_addtask_
; // Number of times thread AddTask was called
180 bool do_notifies_
; // Whether these threads should do notifications.
182 base::WeakPtrFactory
<AddRemoveThread
> weak_factory_
;
185 TEST(ObserverListTest
, BasicTest
) {
186 ObserverList
<Foo
> observer_list
;
187 Adder
a(1), b(-1), c(1), d(-1), e(-1);
188 Disrupter
evil(&observer_list
, &c
);
190 observer_list
.AddObserver(&a
);
191 observer_list
.AddObserver(&b
);
193 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(10));
195 observer_list
.AddObserver(&evil
);
196 observer_list
.AddObserver(&c
);
197 observer_list
.AddObserver(&d
);
199 // Removing an observer not in the list should do nothing.
200 observer_list
.RemoveObserver(&e
);
202 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(10));
204 EXPECT_EQ(20, a
.total
);
205 EXPECT_EQ(-20, b
.total
);
206 EXPECT_EQ(0, c
.total
);
207 EXPECT_EQ(-10, d
.total
);
208 EXPECT_EQ(0, e
.total
);
211 TEST(ObserverListThreadSafeTest
, BasicTest
) {
214 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
215 new ObserverListThreadSafe
<Foo
>);
220 ThreadSafeDisrupter
evil(observer_list
.get(), &c
);
222 observer_list
->AddObserver(&a
);
223 observer_list
->AddObserver(&b
);
225 observer_list
->Notify(&Foo::Observe
, 10);
228 observer_list
->AddObserver(&evil
);
229 observer_list
->AddObserver(&c
);
230 observer_list
->AddObserver(&d
);
232 observer_list
->Notify(&Foo::Observe
, 10);
235 EXPECT_EQ(20, a
.total
);
236 EXPECT_EQ(-20, b
.total
);
237 EXPECT_EQ(0, c
.total
);
238 EXPECT_EQ(-10, d
.total
);
241 TEST(ObserverListThreadSafeTest
, RemoveObserver
) {
244 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
245 new ObserverListThreadSafe
<Foo
>);
248 // A workaround for the compiler bug. See http://crbug.com/121960.
251 // Should do nothing.
252 observer_list
->RemoveObserver(&a
);
253 observer_list
->RemoveObserver(&b
);
255 observer_list
->Notify(&Foo::Observe
, 10);
258 EXPECT_EQ(0, a
.total
);
259 EXPECT_EQ(0, b
.total
);
261 observer_list
->AddObserver(&a
);
263 // Should also do nothing.
264 observer_list
->RemoveObserver(&b
);
266 observer_list
->Notify(&Foo::Observe
, 10);
269 EXPECT_EQ(10, a
.total
);
270 EXPECT_EQ(0, b
.total
);
273 TEST(ObserverListThreadSafeTest
, WithoutMessageLoop
) {
274 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
275 new ObserverListThreadSafe
<Foo
>);
277 Adder
a(1), b(1), c(1);
279 // No MessageLoop, so these should not be added.
280 observer_list
->AddObserver(&a
);
281 observer_list
->AddObserver(&b
);
284 // Add c when there's a loop.
286 observer_list
->AddObserver(&c
);
288 observer_list
->Notify(&Foo::Observe
, 10);
291 EXPECT_EQ(0, a
.total
);
292 EXPECT_EQ(0, b
.total
);
293 EXPECT_EQ(10, c
.total
);
295 // Now add a when there's a loop.
296 observer_list
->AddObserver(&a
);
298 // Remove c when there's a loop.
299 observer_list
->RemoveObserver(&c
);
302 observer_list
->Notify(&Foo::Observe
, 20);
305 EXPECT_EQ(20, a
.total
);
306 EXPECT_EQ(0, b
.total
);
307 EXPECT_EQ(10, c
.total
);
310 // Removing should always succeed with or without a loop.
311 observer_list
->RemoveObserver(&a
);
313 // Notifying should not fail but should also be a no-op.
315 observer_list
->AddObserver(&b
);
316 observer_list
->Notify(&Foo::Observe
, 30);
319 EXPECT_EQ(20, a
.total
);
320 EXPECT_EQ(30, b
.total
);
321 EXPECT_EQ(10, c
.total
);
324 class FooRemover
: public Foo
{
326 explicit FooRemover(ObserverListThreadSafe
<Foo
>* list
) : list_(list
) {}
327 virtual ~FooRemover() {}
329 void AddFooToRemove(Foo
* foo
) {
330 foos_
.push_back(foo
);
333 virtual void Observe(int x
) OVERRIDE
{
334 std::vector
<Foo
*> tmp
;
336 for (std::vector
<Foo
*>::iterator it
= tmp
.begin();
337 it
!= tmp
.end(); ++it
) {
338 list_
->RemoveObserver(*it
);
343 const scoped_refptr
<ObserverListThreadSafe
<Foo
> > list_
;
344 std::vector
<Foo
*> foos_
;
347 TEST(ObserverListThreadSafeTest
, RemoveMultipleObservers
) {
349 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
350 new ObserverListThreadSafe
<Foo
>);
352 FooRemover
a(observer_list
);
355 observer_list
->AddObserver(&a
);
356 observer_list
->AddObserver(&b
);
358 a
.AddFooToRemove(&a
);
359 a
.AddFooToRemove(&b
);
361 observer_list
->Notify(&Foo::Observe
, 1);
365 // A test driver for a multi-threaded notification loop. Runs a number
366 // of observer threads, each of which constantly adds/removes itself
367 // from the observer list. Optionally, if cross_thread_notifies is set
368 // to true, the observer threads will also trigger notifications to
370 static void ThreadSafeObserverHarness(int num_threads
,
371 bool cross_thread_notifies
) {
374 const int kMaxThreads
= 15;
375 num_threads
= num_threads
> kMaxThreads
? kMaxThreads
: num_threads
;
377 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
378 new ObserverListThreadSafe
<Foo
>);
384 observer_list
->AddObserver(&a
);
385 observer_list
->AddObserver(&b
);
387 AddRemoveThread
* threaded_observer
[kMaxThreads
];
388 base::PlatformThreadHandle threads
[kMaxThreads
];
389 for (int index
= 0; index
< num_threads
; index
++) {
390 threaded_observer
[index
] = new AddRemoveThread(observer_list
.get(), false);
391 EXPECT_TRUE(PlatformThread::Create(0,
392 threaded_observer
[index
], &threads
[index
]));
395 Time start
= Time::Now();
397 if ((Time::Now() - start
).InMilliseconds() > kThreadRunTime
)
400 observer_list
->Notify(&Foo::Observe
, 10);
405 for (int index
= 0; index
< num_threads
; index
++) {
406 threaded_observer
[index
]->Quit();
407 PlatformThread::Join(threads
[index
]);
411 TEST(ObserverListThreadSafeTest
, CrossThreadObserver
) {
412 // Use 7 observer threads. Notifications only come from
414 ThreadSafeObserverHarness(7, false);
417 TEST(ObserverListThreadSafeTest
, CrossThreadNotifications
) {
418 // Use 3 observer threads. Notifications will fire from
419 // the main thread and all 3 observer threads.
420 ThreadSafeObserverHarness(3, true);
423 TEST(ObserverListThreadSafeTest
, OutlivesMessageLoop
) {
424 MessageLoop
* loop
= new MessageLoop
;
425 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
426 new ObserverListThreadSafe
<Foo
>);
429 observer_list
->AddObserver(&a
);
431 // Test passes if we don't crash here.
432 observer_list
->Notify(&Foo::Observe
, 1);
435 TEST(ObserverListTest
, Existing
) {
436 ObserverList
<Foo
> observer_list(ObserverList
<Foo
>::NOTIFY_EXISTING_ONLY
);
438 AddInObserve
<ObserverList
<Foo
> > b(&observer_list
);
440 observer_list
.AddObserver(&a
);
441 observer_list
.AddObserver(&b
);
443 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
445 EXPECT_TRUE(b
.added
);
446 // B's adder should not have been notified because it was added during
448 EXPECT_EQ(0, b
.adder
.total
);
450 // Notify again to make sure b's adder is notified.
451 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
452 EXPECT_EQ(1, b
.adder
.total
);
455 // Same as above, but for ObserverListThreadSafe
456 TEST(ObserverListThreadSafeTest
, Existing
) {
458 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
459 new ObserverListThreadSafe
<Foo
>(ObserverList
<Foo
>::NOTIFY_EXISTING_ONLY
));
461 AddInObserve
<ObserverListThreadSafe
<Foo
> > b(observer_list
.get());
463 observer_list
->AddObserver(&a
);
464 observer_list
->AddObserver(&b
);
466 observer_list
->Notify(&Foo::Observe
, 1);
469 EXPECT_TRUE(b
.added
);
470 // B's adder should not have been notified because it was added during
472 EXPECT_EQ(0, b
.adder
.total
);
474 // Notify again to make sure b's adder is notified.
475 observer_list
->Notify(&Foo::Observe
, 1);
477 EXPECT_EQ(1, b
.adder
.total
);
480 class AddInClearObserve
: public Foo
{
482 explicit AddInClearObserve(ObserverList
<Foo
>* list
)
483 : list_(list
), added_(false), adder_(1) {}
485 virtual void Observe(int /* x */) OVERRIDE
{
487 list_
->AddObserver(&adder_
);
491 bool added() const { return added_
; }
492 const Adder
& adder() const { return adder_
; }
495 ObserverList
<Foo
>* const list_
;
501 TEST(ObserverListTest
, ClearNotifyAll
) {
502 ObserverList
<Foo
> observer_list
;
503 AddInClearObserve
a(&observer_list
);
505 observer_list
.AddObserver(&a
);
507 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
508 EXPECT_TRUE(a
.added());
509 EXPECT_EQ(1, a
.adder().total
)
510 << "Adder should observe once and have sum of 1.";
513 TEST(ObserverListTest
, ClearNotifyExistingOnly
) {
514 ObserverList
<Foo
> observer_list(ObserverList
<Foo
>::NOTIFY_EXISTING_ONLY
);
515 AddInClearObserve
a(&observer_list
);
517 observer_list
.AddObserver(&a
);
519 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
520 EXPECT_TRUE(a
.added());
521 EXPECT_EQ(0, a
.adder().total
)
522 << "Adder should not observe, so sum should still be 0.";
525 class ListDestructor
: public Foo
{
527 explicit ListDestructor(ObserverList
<Foo
>* list
) : list_(list
) {}
528 virtual ~ListDestructor() {}
530 virtual void Observe(int x
) OVERRIDE
{
535 ObserverList
<Foo
>* list_
;
539 TEST(ObserverListTest
, IteratorOutlivesList
) {
540 ObserverList
<Foo
>* observer_list
= new ObserverList
<Foo
>;
541 ListDestructor
a(observer_list
);
542 observer_list
->AddObserver(&a
);
544 FOR_EACH_OBSERVER(Foo
, *observer_list
, Observe(0));
545 // If this test fails, there'll be Valgrind errors when this function goes out