OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/observer_list.h" | 5 #include "base/observer_list.h" |
6 #include "base/observer_list_threadsafe.h" | 6 #include "base/observer_list_threadsafe.h" |
7 | 7 |
| 8 #include <utility> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
| 11 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
11 #include "base/location.h" | 13 #include "base/location.h" |
12 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
13 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/sequenced_task_runner.h" |
14 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/task_scheduler/post_task.h" |
| 19 #include "base/test/scoped_task_scheduler.h" |
15 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
17 | 22 |
18 namespace base { | 23 namespace base { |
19 namespace { | 24 namespace { |
20 | 25 |
21 class Foo { | 26 class Foo { |
22 public: | 27 public: |
23 virtual void Observe(int x) = 0; | 28 virtual void Observe(int x) = 0; |
24 virtual ~Foo() {} | 29 virtual ~Foo() {} |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 } | 63 } |
59 | 64 |
60 void SetDoomed(Foo* doomed) { doomed_ = doomed; } | 65 void SetDoomed(Foo* doomed) { doomed_ = doomed; } |
61 | 66 |
62 private: | 67 private: |
63 ObserverList<Foo>* list_; | 68 ObserverList<Foo>* list_; |
64 Foo* doomed_; | 69 Foo* doomed_; |
65 bool remove_self_; | 70 bool remove_self_; |
66 }; | 71 }; |
67 | 72 |
68 class ThreadSafeDisrupter : public Foo { | |
69 public: | |
70 ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed) | |
71 : list_(list), | |
72 doomed_(doomed) { | |
73 } | |
74 ~ThreadSafeDisrupter() override {} | |
75 void Observe(int x) override { list_->RemoveObserver(doomed_); } | |
76 | |
77 private: | |
78 ObserverListThreadSafe<Foo>* list_; | |
79 Foo* doomed_; | |
80 }; | |
81 | |
82 template <typename ObserverListType> | 73 template <typename ObserverListType> |
83 class AddInObserve : public Foo { | 74 class AddInObserve : public Foo { |
84 public: | 75 public: |
85 explicit AddInObserve(ObserverListType* observer_list) | 76 explicit AddInObserve(ObserverListType* observer_list) |
86 : observer_list(observer_list), to_add_() {} | 77 : observer_list(observer_list), to_add_() {} |
87 | 78 |
88 void SetToAdd(Foo* to_add) { to_add_ = to_add; } | 79 void SetToAdd(Foo* to_add) { to_add_ = to_add; } |
89 | 80 |
90 void Observe(int x) override { | 81 void Observe(int x) override { |
91 if (to_add_) { | 82 if (to_add_) { |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 | 260 |
270 TEST(ObserverListThreadSafeTest, BasicTest) { | 261 TEST(ObserverListThreadSafeTest, BasicTest) { |
271 MessageLoop loop; | 262 MessageLoop loop; |
272 | 263 |
273 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( | 264 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
274 new ObserverListThreadSafe<Foo>); | 265 new ObserverListThreadSafe<Foo>); |
275 Adder a(1); | 266 Adder a(1); |
276 Adder b(-1); | 267 Adder b(-1); |
277 Adder c(1); | 268 Adder c(1); |
278 Adder d(-1); | 269 Adder d(-1); |
279 ThreadSafeDisrupter evil(observer_list.get(), &c); | |
280 | 270 |
281 observer_list->AddObserver(&a); | 271 observer_list->AddObserver(&a); |
282 observer_list->AddObserver(&b); | 272 observer_list->AddObserver(&b); |
283 | 273 |
284 observer_list->Notify(FROM_HERE, &Foo::Observe, 10); | 274 observer_list->Notify(FROM_HERE, &Foo::Observe, 10); |
285 RunLoop().RunUntilIdle(); | 275 RunLoop().RunUntilIdle(); |
286 | 276 |
287 observer_list->AddObserver(&evil); | |
288 observer_list->AddObserver(&c); | 277 observer_list->AddObserver(&c); |
289 observer_list->AddObserver(&d); | 278 observer_list->AddObserver(&d); |
290 | 279 |
291 observer_list->Notify(FROM_HERE, &Foo::Observe, 10); | 280 observer_list->Notify(FROM_HERE, &Foo::Observe, 10); |
| 281 observer_list->RemoveObserver(&c); |
292 RunLoop().RunUntilIdle(); | 282 RunLoop().RunUntilIdle(); |
293 | 283 |
294 EXPECT_EQ(20, a.total); | 284 EXPECT_EQ(20, a.total); |
295 EXPECT_EQ(-20, b.total); | 285 EXPECT_EQ(-20, b.total); |
296 EXPECT_EQ(0, c.total); | 286 EXPECT_EQ(0, c.total); |
297 EXPECT_EQ(-10, d.total); | 287 EXPECT_EQ(-10, d.total); |
298 } | 288 } |
299 | 289 |
300 TEST(ObserverListThreadSafeTest, RemoveObserver) { | 290 TEST(ObserverListThreadSafeTest, RemoveObserver) { |
301 MessageLoop loop; | 291 MessageLoop loop; |
(...skipping 20 matching lines...) Expand all Loading... |
322 // Should also do nothing. | 312 // Should also do nothing. |
323 observer_list->RemoveObserver(&b); | 313 observer_list->RemoveObserver(&b); |
324 | 314 |
325 observer_list->Notify(FROM_HERE, &Foo::Observe, 10); | 315 observer_list->Notify(FROM_HERE, &Foo::Observe, 10); |
326 RunLoop().RunUntilIdle(); | 316 RunLoop().RunUntilIdle(); |
327 | 317 |
328 EXPECT_EQ(10, a.total); | 318 EXPECT_EQ(10, a.total); |
329 EXPECT_EQ(0, b.total); | 319 EXPECT_EQ(0, b.total); |
330 } | 320 } |
331 | 321 |
332 TEST(ObserverListThreadSafeTest, WithoutMessageLoop) { | 322 TEST(ObserverListThreadSafeTest, WithoutSequence) { |
333 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( | 323 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
334 new ObserverListThreadSafe<Foo>); | 324 new ObserverListThreadSafe<Foo>); |
335 | 325 |
336 Adder a(1), b(1), c(1); | 326 Adder a(1), b(1), c(1); |
337 | 327 |
338 // No MessageLoop, so these should not be added. | 328 // No sequence, so these should not be added. |
339 observer_list->AddObserver(&a); | 329 observer_list->AddObserver(&a); |
340 observer_list->AddObserver(&b); | 330 observer_list->AddObserver(&b); |
341 | 331 |
342 { | 332 { |
343 // Add c when there's a loop. | 333 // Add c when there's a sequence. |
344 MessageLoop loop; | 334 MessageLoop loop; |
345 observer_list->AddObserver(&c); | 335 observer_list->AddObserver(&c); |
346 | 336 |
347 observer_list->Notify(FROM_HERE, &Foo::Observe, 10); | 337 observer_list->Notify(FROM_HERE, &Foo::Observe, 10); |
348 RunLoop().RunUntilIdle(); | 338 RunLoop().RunUntilIdle(); |
349 | 339 |
350 EXPECT_EQ(0, a.total); | 340 EXPECT_EQ(0, a.total); |
351 EXPECT_EQ(0, b.total); | 341 EXPECT_EQ(0, b.total); |
352 EXPECT_EQ(10, c.total); | 342 EXPECT_EQ(10, c.total); |
353 | 343 |
354 // Now add a when there's a loop. | 344 // Now add a when there's a sequence. |
355 observer_list->AddObserver(&a); | 345 observer_list->AddObserver(&a); |
356 | 346 |
357 // Remove c when there's a loop. | 347 // Remove c when there's a sequence. |
358 observer_list->RemoveObserver(&c); | 348 observer_list->RemoveObserver(&c); |
359 | 349 |
360 // Notify again. | 350 // Notify again. |
361 observer_list->Notify(FROM_HERE, &Foo::Observe, 20); | 351 observer_list->Notify(FROM_HERE, &Foo::Observe, 20); |
362 RunLoop().RunUntilIdle(); | 352 RunLoop().RunUntilIdle(); |
363 | 353 |
364 EXPECT_EQ(20, a.total); | 354 EXPECT_EQ(20, a.total); |
365 EXPECT_EQ(0, b.total); | 355 EXPECT_EQ(0, b.total); |
366 EXPECT_EQ(10, c.total); | 356 EXPECT_EQ(10, c.total); |
367 } | 357 } |
368 | 358 |
369 // Removing should always succeed with or without a loop. | 359 // Removing should always succeed with or without a sequence. |
370 observer_list->RemoveObserver(&a); | 360 observer_list->RemoveObserver(&a); |
371 | 361 |
372 // Notifying should not fail but should also be a no-op. | 362 // Notifying should not fail but should also be a no-op. |
373 MessageLoop loop; | 363 MessageLoop loop; |
374 observer_list->AddObserver(&b); | 364 observer_list->AddObserver(&b); |
375 observer_list->Notify(FROM_HERE, &Foo::Observe, 30); | 365 observer_list->Notify(FROM_HERE, &Foo::Observe, 30); |
376 RunLoop().RunUntilIdle(); | 366 RunLoop().RunUntilIdle(); |
377 | 367 |
378 EXPECT_EQ(20, a.total); | 368 EXPECT_EQ(20, a.total); |
379 EXPECT_EQ(30, b.total); | 369 EXPECT_EQ(30, b.total); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( | 474 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
485 new ObserverListThreadSafe<Foo>); | 475 new ObserverListThreadSafe<Foo>); |
486 | 476 |
487 Adder a(1); | 477 Adder a(1); |
488 observer_list->AddObserver(&a); | 478 observer_list->AddObserver(&a); |
489 delete loop; | 479 delete loop; |
490 // Test passes if we don't crash here. | 480 // Test passes if we don't crash here. |
491 observer_list->Notify(FROM_HERE, &Foo::Observe, 1); | 481 observer_list->Notify(FROM_HERE, &Foo::Observe, 1); |
492 } | 482 } |
493 | 483 |
| 484 namespace { |
| 485 |
| 486 class SequenceVerificationObserver : public Foo { |
| 487 public: |
| 488 explicit SequenceVerificationObserver( |
| 489 scoped_refptr<SequencedTaskRunner> task_runner) |
| 490 : task_runner_(std::move(task_runner)) {} |
| 491 ~SequenceVerificationObserver() override = default; |
| 492 |
| 493 void Observe(int x) override { |
| 494 called_on_valid_sequence_ = task_runner_->RunsTasksOnCurrentThread(); |
| 495 } |
| 496 |
| 497 bool called_on_valid_sequence() const { return called_on_valid_sequence_; } |
| 498 |
| 499 private: |
| 500 const scoped_refptr<SequencedTaskRunner> task_runner_; |
| 501 bool called_on_valid_sequence_ = false; |
| 502 |
| 503 DISALLOW_COPY_AND_ASSIGN(SequenceVerificationObserver); |
| 504 }; |
| 505 |
| 506 } // namespace |
| 507 |
| 508 // Verify that observers are notified on the correct sequence. |
| 509 TEST(ObserverListThreadSafeTest, NotificationOnValidSequence) { |
| 510 test::ScopedTaskScheduler scoped_task_scheduler; |
| 511 |
| 512 auto task_runner_1 = CreateSequencedTaskRunnerWithTraits(TaskTraits()); |
| 513 auto task_runner_2 = CreateSequencedTaskRunnerWithTraits(TaskTraits()); |
| 514 |
| 515 auto observer_list = make_scoped_refptr(new ObserverListThreadSafe<Foo>()); |
| 516 |
| 517 SequenceVerificationObserver observer_1(task_runner_1); |
| 518 SequenceVerificationObserver observer_2(task_runner_2); |
| 519 |
| 520 task_runner_1->PostTask( |
| 521 FROM_HERE, Bind(&ObserverListThreadSafe<Foo>::AddObserver, observer_list, |
| 522 Unretained(&observer_1))); |
| 523 task_runner_2->PostTask( |
| 524 FROM_HERE, Bind(&ObserverListThreadSafe<Foo>::AddObserver, observer_list, |
| 525 Unretained(&observer_2))); |
| 526 |
| 527 RunLoop().RunUntilIdle(); |
| 528 |
| 529 observer_list->Notify(FROM_HERE, &Foo::Observe, 1); |
| 530 |
| 531 RunLoop().RunUntilIdle(); |
| 532 |
| 533 EXPECT_TRUE(observer_1.called_on_valid_sequence()); |
| 534 EXPECT_TRUE(observer_2.called_on_valid_sequence()); |
| 535 } |
| 536 |
| 537 // Verify that when an observer is added to a NOTIFY_ALL ObserverListThreadSafe |
| 538 // from a notification, it is itself notified. |
| 539 TEST(ObserverListThreadSafeTest, AddObserverFromNotificationNotifyAll) { |
| 540 MessageLoop message_loop; |
| 541 auto observer_list = make_scoped_refptr(new ObserverListThreadSafe<Foo>()); |
| 542 |
| 543 Adder observer_added_from_notification(1); |
| 544 |
| 545 AddInObserve<ObserverListThreadSafe<Foo>> initial_observer( |
| 546 observer_list.get()); |
| 547 initial_observer.SetToAdd(&observer_added_from_notification); |
| 548 observer_list->AddObserver(&initial_observer); |
| 549 |
| 550 observer_list->Notify(FROM_HERE, &Foo::Observe, 1); |
| 551 |
| 552 RunLoop().RunUntilIdle(); |
| 553 |
| 554 EXPECT_EQ(1, observer_added_from_notification.GetValue()); |
| 555 } |
| 556 |
494 TEST(ObserverListTest, Existing) { | 557 TEST(ObserverListTest, Existing) { |
495 ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY); | 558 ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY); |
496 Adder a(1); | 559 Adder a(1); |
497 AddInObserve<ObserverList<Foo> > b(&observer_list); | 560 AddInObserve<ObserverList<Foo> > b(&observer_list); |
498 Adder c(1); | 561 Adder c(1); |
499 b.SetToAdd(&c); | 562 b.SetToAdd(&c); |
500 | 563 |
501 observer_list.AddObserver(&a); | 564 observer_list.AddObserver(&a); |
502 observer_list.AddObserver(&b); | 565 observer_list.AddObserver(&b); |
503 | 566 |
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
926 // However, the first Observe() call will add a second observer: at this | 989 // However, the first Observe() call will add a second observer: at this |
927 // point, it != observer_list.end() should be true, and Observe() should be | 990 // point, it != observer_list.end() should be true, and Observe() should be |
928 // called on the newly added observer on the next iteration of the loop. | 991 // called on the newly added observer on the next iteration of the loop. |
929 observer.Observe(10); | 992 observer.Observe(10); |
930 } | 993 } |
931 | 994 |
932 EXPECT_EQ(-10, b.total); | 995 EXPECT_EQ(-10, b.total); |
933 } | 996 } |
934 | 997 |
935 } // namespace base | 998 } // namespace base |
OLD | NEW |