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/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 virtual void Observe(int x
) OVERRIDE
{
39 class Disrupter
: public Foo
{
41 Disrupter(ObserverList
<Foo
>* list
, Foo
* doomed
)
45 virtual ~Disrupter() {}
46 virtual void Observe(int x
) OVERRIDE
{
47 list_
->RemoveObserver(doomed_
);
51 ObserverList
<Foo
>* list_
;
55 class ThreadSafeDisrupter
: public Foo
{
57 ThreadSafeDisrupter(ObserverListThreadSafe
<Foo
>* list
, Foo
* doomed
)
61 virtual ~ThreadSafeDisrupter() {}
62 virtual void Observe(int x
) OVERRIDE
{
63 list_
->RemoveObserver(doomed_
);
67 ObserverListThreadSafe
<Foo
>* list_
;
71 template <typename ObserverListType
>
72 class AddInObserve
: public Foo
{
74 explicit AddInObserve(ObserverListType
* observer_list
)
76 observer_list(observer_list
),
80 virtual void Observe(int x
) OVERRIDE
{
83 observer_list
->AddObserver(&adder
);
88 ObserverListType
* observer_list
;
93 static const int kThreadRunTime
= 2000; // ms to run the multi-threaded test.
95 // A thread for use in the ThreadSafeObserver test
96 // which will add and remove itself from the notification
98 class AddRemoveThread
: public PlatformThread::Delegate
,
101 AddRemoveThread(ObserverListThreadSafe
<Foo
>* list
, bool notify
)
108 do_notifies_(notify
),
109 weak_factory_(this) {
112 virtual ~AddRemoveThread() {
115 virtual void ThreadMain() OVERRIDE
{
116 loop_
= new MessageLoop(); // Fire up a message loop.
119 base::Bind(&AddRemoveThread::AddTask
, weak_factory_
.GetWeakPtr()));
121 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " <<
122 // count_observes_ << ", " << count_addtask_;
124 loop_
= reinterpret_cast<MessageLoop
*>(0xdeadbeef);
128 // This task just keeps posting to itself in an attempt
129 // to race with the notifier.
133 if ((Time::Now() - start_
).InMilliseconds() > kThreadRunTime
) {
139 list_
->AddObserver(this);
144 list_
->Notify(&Foo::Observe
, 10);
149 base::Bind(&AddRemoveThread::AddTask
, weak_factory_
.GetWeakPtr()));
153 loop_
->PostTask(FROM_HERE
, MessageLoop::QuitWhenIdleClosure());
156 virtual void Observe(int x
) OVERRIDE
{
159 // If we're getting called after we removed ourselves from
160 // the list, that is very bad!
163 // This callback should fire on the appropriate thread
164 EXPECT_EQ(loop_
, MessageLoop::current());
166 list_
->RemoveObserver(this);
171 ObserverListThreadSafe
<Foo
>* list_
;
173 bool in_list_
; // Are we currently registered for notifications.
174 // in_list_ is only used on |this| thread.
175 Time start_
; // The time we started the test.
177 int count_observes_
; // Number of times we observed.
178 int count_addtask_
; // Number of times thread AddTask was called
179 bool do_notifies_
; // Whether these threads should do notifications.
181 base::WeakPtrFactory
<AddRemoveThread
> weak_factory_
;
184 TEST(ObserverListTest
, BasicTest
) {
185 ObserverList
<Foo
> observer_list
;
186 Adder
a(1), b(-1), c(1), d(-1), e(-1);
187 Disrupter
evil(&observer_list
, &c
);
189 observer_list
.AddObserver(&a
);
190 observer_list
.AddObserver(&b
);
192 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(10));
194 observer_list
.AddObserver(&evil
);
195 observer_list
.AddObserver(&c
);
196 observer_list
.AddObserver(&d
);
198 // Removing an observer not in the list should do nothing.
199 observer_list
.RemoveObserver(&e
);
201 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(10));
203 EXPECT_EQ(20, a
.total
);
204 EXPECT_EQ(-20, b
.total
);
205 EXPECT_EQ(0, c
.total
);
206 EXPECT_EQ(-10, d
.total
);
207 EXPECT_EQ(0, e
.total
);
210 TEST(ObserverListThreadSafeTest
, BasicTest
) {
213 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
214 new ObserverListThreadSafe
<Foo
>);
219 ThreadSafeDisrupter
evil(observer_list
.get(), &c
);
221 observer_list
->AddObserver(&a
);
222 observer_list
->AddObserver(&b
);
224 observer_list
->Notify(&Foo::Observe
, 10);
225 RunLoop().RunUntilIdle();
227 observer_list
->AddObserver(&evil
);
228 observer_list
->AddObserver(&c
);
229 observer_list
->AddObserver(&d
);
231 observer_list
->Notify(&Foo::Observe
, 10);
232 RunLoop().RunUntilIdle();
234 EXPECT_EQ(20, a
.total
);
235 EXPECT_EQ(-20, b
.total
);
236 EXPECT_EQ(0, c
.total
);
237 EXPECT_EQ(-10, d
.total
);
240 TEST(ObserverListThreadSafeTest
, RemoveObserver
) {
243 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
244 new ObserverListThreadSafe
<Foo
>);
247 // A workaround for the compiler bug. See http://crbug.com/121960.
250 // Should do nothing.
251 observer_list
->RemoveObserver(&a
);
252 observer_list
->RemoveObserver(&b
);
254 observer_list
->Notify(&Foo::Observe
, 10);
255 RunLoop().RunUntilIdle();
257 EXPECT_EQ(0, a
.total
);
258 EXPECT_EQ(0, b
.total
);
260 observer_list
->AddObserver(&a
);
262 // Should also do nothing.
263 observer_list
->RemoveObserver(&b
);
265 observer_list
->Notify(&Foo::Observe
, 10);
266 RunLoop().RunUntilIdle();
268 EXPECT_EQ(10, a
.total
);
269 EXPECT_EQ(0, b
.total
);
272 TEST(ObserverListThreadSafeTest
, WithoutMessageLoop
) {
273 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
274 new ObserverListThreadSafe
<Foo
>);
276 Adder
a(1), b(1), c(1);
278 // No MessageLoop, so these should not be added.
279 observer_list
->AddObserver(&a
);
280 observer_list
->AddObserver(&b
);
283 // Add c when there's a loop.
285 observer_list
->AddObserver(&c
);
287 observer_list
->Notify(&Foo::Observe
, 10);
288 RunLoop().RunUntilIdle();
290 EXPECT_EQ(0, a
.total
);
291 EXPECT_EQ(0, b
.total
);
292 EXPECT_EQ(10, c
.total
);
294 // Now add a when there's a loop.
295 observer_list
->AddObserver(&a
);
297 // Remove c when there's a loop.
298 observer_list
->RemoveObserver(&c
);
301 observer_list
->Notify(&Foo::Observe
, 20);
302 RunLoop().RunUntilIdle();
304 EXPECT_EQ(20, a
.total
);
305 EXPECT_EQ(0, b
.total
);
306 EXPECT_EQ(10, c
.total
);
309 // Removing should always succeed with or without a loop.
310 observer_list
->RemoveObserver(&a
);
312 // Notifying should not fail but should also be a no-op.
314 observer_list
->AddObserver(&b
);
315 observer_list
->Notify(&Foo::Observe
, 30);
316 RunLoop().RunUntilIdle();
318 EXPECT_EQ(20, a
.total
);
319 EXPECT_EQ(30, b
.total
);
320 EXPECT_EQ(10, c
.total
);
323 class FooRemover
: public Foo
{
325 explicit FooRemover(ObserverListThreadSafe
<Foo
>* list
) : list_(list
) {}
326 virtual ~FooRemover() {}
328 void AddFooToRemove(Foo
* foo
) {
329 foos_
.push_back(foo
);
332 virtual void Observe(int x
) OVERRIDE
{
333 std::vector
<Foo
*> tmp
;
335 for (std::vector
<Foo
*>::iterator it
= tmp
.begin();
336 it
!= tmp
.end(); ++it
) {
337 list_
->RemoveObserver(*it
);
342 const scoped_refptr
<ObserverListThreadSafe
<Foo
> > list_
;
343 std::vector
<Foo
*> foos_
;
346 TEST(ObserverListThreadSafeTest
, RemoveMultipleObservers
) {
348 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
349 new ObserverListThreadSafe
<Foo
>);
351 FooRemover
a(observer_list
.get());
354 observer_list
->AddObserver(&a
);
355 observer_list
->AddObserver(&b
);
357 a
.AddFooToRemove(&a
);
358 a
.AddFooToRemove(&b
);
360 observer_list
->Notify(&Foo::Observe
, 1);
361 RunLoop().RunUntilIdle();
364 // A test driver for a multi-threaded notification loop. Runs a number
365 // of observer threads, each of which constantly adds/removes itself
366 // from the observer list. Optionally, if cross_thread_notifies is set
367 // to true, the observer threads will also trigger notifications to
369 static void ThreadSafeObserverHarness(int num_threads
,
370 bool cross_thread_notifies
) {
373 const int kMaxThreads
= 15;
374 num_threads
= num_threads
> kMaxThreads
? kMaxThreads
: num_threads
;
376 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
377 new ObserverListThreadSafe
<Foo
>);
383 observer_list
->AddObserver(&a
);
384 observer_list
->AddObserver(&b
);
386 AddRemoveThread
* threaded_observer
[kMaxThreads
];
387 base::PlatformThreadHandle threads
[kMaxThreads
];
388 for (int index
= 0; index
< num_threads
; index
++) {
389 threaded_observer
[index
] = new AddRemoveThread(observer_list
.get(), false);
390 EXPECT_TRUE(PlatformThread::Create(0,
391 threaded_observer
[index
], &threads
[index
]));
394 Time start
= Time::Now();
396 if ((Time::Now() - start
).InMilliseconds() > kThreadRunTime
)
399 observer_list
->Notify(&Foo::Observe
, 10);
401 RunLoop().RunUntilIdle();
404 for (int index
= 0; index
< num_threads
; index
++) {
405 threaded_observer
[index
]->Quit();
406 PlatformThread::Join(threads
[index
]);
410 TEST(ObserverListThreadSafeTest
, CrossThreadObserver
) {
411 // Use 7 observer threads. Notifications only come from
413 ThreadSafeObserverHarness(7, false);
416 TEST(ObserverListThreadSafeTest
, CrossThreadNotifications
) {
417 // Use 3 observer threads. Notifications will fire from
418 // the main thread and all 3 observer threads.
419 ThreadSafeObserverHarness(3, true);
422 TEST(ObserverListThreadSafeTest
, OutlivesMessageLoop
) {
423 MessageLoop
* loop
= new MessageLoop
;
424 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
425 new ObserverListThreadSafe
<Foo
>);
428 observer_list
->AddObserver(&a
);
430 // Test passes if we don't crash here.
431 observer_list
->Notify(&Foo::Observe
, 1);
434 TEST(ObserverListTest
, Existing
) {
435 ObserverList
<Foo
> observer_list(ObserverList
<Foo
>::NOTIFY_EXISTING_ONLY
);
437 AddInObserve
<ObserverList
<Foo
> > b(&observer_list
);
439 observer_list
.AddObserver(&a
);
440 observer_list
.AddObserver(&b
);
442 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
444 EXPECT_TRUE(b
.added
);
445 // B's adder should not have been notified because it was added during
447 EXPECT_EQ(0, b
.adder
.total
);
449 // Notify again to make sure b's adder is notified.
450 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
451 EXPECT_EQ(1, b
.adder
.total
);
454 // Same as above, but for ObserverListThreadSafe
455 TEST(ObserverListThreadSafeTest
, Existing
) {
457 scoped_refptr
<ObserverListThreadSafe
<Foo
> > observer_list(
458 new ObserverListThreadSafe
<Foo
>(ObserverList
<Foo
>::NOTIFY_EXISTING_ONLY
));
460 AddInObserve
<ObserverListThreadSafe
<Foo
> > b(observer_list
.get());
462 observer_list
->AddObserver(&a
);
463 observer_list
->AddObserver(&b
);
465 observer_list
->Notify(&Foo::Observe
, 1);
466 RunLoop().RunUntilIdle();
468 EXPECT_TRUE(b
.added
);
469 // B's adder should not have been notified because it was added during
471 EXPECT_EQ(0, b
.adder
.total
);
473 // Notify again to make sure b's adder is notified.
474 observer_list
->Notify(&Foo::Observe
, 1);
475 RunLoop().RunUntilIdle();
476 EXPECT_EQ(1, b
.adder
.total
);
479 class AddInClearObserve
: public Foo
{
481 explicit AddInClearObserve(ObserverList
<Foo
>* list
)
482 : list_(list
), added_(false), adder_(1) {}
484 virtual void Observe(int /* x */) OVERRIDE
{
486 list_
->AddObserver(&adder_
);
490 bool added() const { return added_
; }
491 const Adder
& adder() const { return adder_
; }
494 ObserverList
<Foo
>* const list_
;
500 TEST(ObserverListTest
, ClearNotifyAll
) {
501 ObserverList
<Foo
> observer_list
;
502 AddInClearObserve
a(&observer_list
);
504 observer_list
.AddObserver(&a
);
506 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
507 EXPECT_TRUE(a
.added());
508 EXPECT_EQ(1, a
.adder().total
)
509 << "Adder should observe once and have sum of 1.";
512 TEST(ObserverListTest
, ClearNotifyExistingOnly
) {
513 ObserverList
<Foo
> observer_list(ObserverList
<Foo
>::NOTIFY_EXISTING_ONLY
);
514 AddInClearObserve
a(&observer_list
);
516 observer_list
.AddObserver(&a
);
518 FOR_EACH_OBSERVER(Foo
, observer_list
, Observe(1));
519 EXPECT_TRUE(a
.added());
520 EXPECT_EQ(0, a
.adder().total
)
521 << "Adder should not observe, so sum should still be 0.";
524 class ListDestructor
: public Foo
{
526 explicit ListDestructor(ObserverList
<Foo
>* list
) : list_(list
) {}
527 virtual ~ListDestructor() {}
529 virtual void Observe(int x
) OVERRIDE
{
534 ObserverList
<Foo
>* list_
;
538 TEST(ObserverListTest
, IteratorOutlivesList
) {
539 ObserverList
<Foo
>* observer_list
= new ObserverList
<Foo
>;
540 ListDestructor
a(observer_list
);
541 observer_list
->AddObserver(&a
);
543 FOR_EACH_OBSERVER(Foo
, *observer_list
, Observe(0));
544 // If this test fails, there'll be Valgrind errors when this function goes out