| 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/memory/weak_ptr.h" | 5 #include "base/memory/weak_ptr.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 | 580 |
| 581 // Background copies the WeakPtr. | 581 // Background copies the WeakPtr. |
| 582 Arrow* arrow_copy; | 582 Arrow* arrow_copy; |
| 583 background.CreateArrowFromArrow(&arrow_copy, &arrow); | 583 background.CreateArrowFromArrow(&arrow_copy, &arrow); |
| 584 | 584 |
| 585 // The copy is still bound to main thread so I can deref. | 585 // The copy is still bound to main thread so I can deref. |
| 586 EXPECT_EQ(arrow.target.get(), arrow_copy->target.get()); | 586 EXPECT_EQ(arrow.target.get(), arrow_copy->target.get()); |
| 587 | 587 |
| 588 // Although background thread created the copy, it can not deref the copied | 588 // Although background thread created the copy, it can not deref the copied |
| 589 // WeakPtr. | 589 // WeakPtr. |
| 590 ASSERT_DCHECK_DEATH(background.DeRef(arrow_copy), ""); | 590 ASSERT_DCHECK_DEATH(background.DeRef(arrow_copy)); |
| 591 | 591 |
| 592 background.DeleteArrow(arrow_copy); | 592 background.DeleteArrow(arrow_copy); |
| 593 } | 593 } |
| 594 | 594 |
| 595 TEST(WeakPtrDeathTest, NonOwnerThreadDereferencesWeakPtrAfterReference) { | 595 TEST(WeakPtrDeathTest, NonOwnerThreadDereferencesWeakPtrAfterReference) { |
| 596 // The default style "fast" does not support multi-threaded tests | 596 // The default style "fast" does not support multi-threaded tests |
| 597 // (introduces deadlock on Linux). | 597 // (introduces deadlock on Linux). |
| 598 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; | 598 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 599 | 599 |
| 600 // Main thread creates a Target object. | 600 // Main thread creates a Target object. |
| 601 Target target; | 601 Target target; |
| 602 | 602 |
| 603 // Main thread creates an arrow referencing the Target (so target's | 603 // Main thread creates an arrow referencing the Target (so target's |
| 604 // thread ownership can not be implicitly moved). | 604 // thread ownership can not be implicitly moved). |
| 605 Arrow arrow; | 605 Arrow arrow; |
| 606 arrow.target = target.AsWeakPtr(); | 606 arrow.target = target.AsWeakPtr(); |
| 607 arrow.target.get(); | 607 arrow.target.get(); |
| 608 | 608 |
| 609 // Background thread tries to deref target, which violates thread ownership. | 609 // Background thread tries to deref target, which violates thread ownership. |
| 610 BackgroundThread background; | 610 BackgroundThread background; |
| 611 background.Start(); | 611 background.Start(); |
| 612 ASSERT_DCHECK_DEATH(background.DeRef(&arrow), ""); | 612 ASSERT_DCHECK_DEATH(background.DeRef(&arrow)); |
| 613 } | 613 } |
| 614 | 614 |
| 615 TEST(WeakPtrDeathTest, NonOwnerThreadDeletesWeakPtrAfterReference) { | 615 TEST(WeakPtrDeathTest, NonOwnerThreadDeletesWeakPtrAfterReference) { |
| 616 // The default style "fast" does not support multi-threaded tests | 616 // The default style "fast" does not support multi-threaded tests |
| 617 // (introduces deadlock on Linux). | 617 // (introduces deadlock on Linux). |
| 618 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; | 618 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 619 | 619 |
| 620 std::unique_ptr<Target> target(new Target()); | 620 std::unique_ptr<Target> target(new Target()); |
| 621 | 621 |
| 622 // Main thread creates an arrow referencing the Target. | 622 // Main thread creates an arrow referencing the Target. |
| 623 Arrow arrow; | 623 Arrow arrow; |
| 624 arrow.target = target->AsWeakPtr(); | 624 arrow.target = target->AsWeakPtr(); |
| 625 | 625 |
| 626 // Background thread tries to deref target, binding it to the thread. | 626 // Background thread tries to deref target, binding it to the thread. |
| 627 BackgroundThread background; | 627 BackgroundThread background; |
| 628 background.Start(); | 628 background.Start(); |
| 629 background.DeRef(&arrow); | 629 background.DeRef(&arrow); |
| 630 | 630 |
| 631 // Main thread deletes Target, violating thread binding. | 631 // Main thread deletes Target, violating thread binding. |
| 632 ASSERT_DCHECK_DEATH(target.reset(), ""); | 632 ASSERT_DCHECK_DEATH(target.reset()); |
| 633 | 633 |
| 634 // |target.reset()| died so |target| still holds the object, so we | 634 // |target.reset()| died so |target| still holds the object, so we |
| 635 // must pass it to the background thread to teardown. | 635 // must pass it to the background thread to teardown. |
| 636 background.DeleteTarget(target.release()); | 636 background.DeleteTarget(target.release()); |
| 637 } | 637 } |
| 638 | 638 |
| 639 TEST(WeakPtrDeathTest, NonOwnerThreadDeletesObjectAfterReference) { | 639 TEST(WeakPtrDeathTest, NonOwnerThreadDeletesObjectAfterReference) { |
| 640 // The default style "fast" does not support multi-threaded tests | 640 // The default style "fast" does not support multi-threaded tests |
| 641 // (introduces deadlock on Linux). | 641 // (introduces deadlock on Linux). |
| 642 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; | 642 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 643 | 643 |
| 644 std::unique_ptr<Target> target(new Target()); | 644 std::unique_ptr<Target> target(new Target()); |
| 645 | 645 |
| 646 // Main thread creates an arrow referencing the Target, and references it, so | 646 // Main thread creates an arrow referencing the Target, and references it, so |
| 647 // that it becomes bound to the thread. | 647 // that it becomes bound to the thread. |
| 648 Arrow arrow; | 648 Arrow arrow; |
| 649 arrow.target = target->AsWeakPtr(); | 649 arrow.target = target->AsWeakPtr(); |
| 650 arrow.target.get(); | 650 arrow.target.get(); |
| 651 | 651 |
| 652 // Background thread tries to delete target, volating thread binding. | 652 // Background thread tries to delete target, volating thread binding. |
| 653 BackgroundThread background; | 653 BackgroundThread background; |
| 654 background.Start(); | 654 background.Start(); |
| 655 ASSERT_DCHECK_DEATH(background.DeleteTarget(target.release()), ""); | 655 ASSERT_DCHECK_DEATH(background.DeleteTarget(target.release())); |
| 656 } | 656 } |
| 657 | 657 |
| 658 TEST(WeakPtrDeathTest, NonOwnerThreadReferencesObjectAfterDeletion) { | 658 TEST(WeakPtrDeathTest, NonOwnerThreadReferencesObjectAfterDeletion) { |
| 659 // The default style "fast" does not support multi-threaded tests | 659 // The default style "fast" does not support multi-threaded tests |
| 660 // (introduces deadlock on Linux). | 660 // (introduces deadlock on Linux). |
| 661 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; | 661 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 662 | 662 |
| 663 std::unique_ptr<Target> target(new Target()); | 663 std::unique_ptr<Target> target(new Target()); |
| 664 | 664 |
| 665 // Main thread creates an arrow referencing the Target. | 665 // Main thread creates an arrow referencing the Target. |
| 666 Arrow arrow; | 666 Arrow arrow; |
| 667 arrow.target = target->AsWeakPtr(); | 667 arrow.target = target->AsWeakPtr(); |
| 668 | 668 |
| 669 // Background thread tries to delete target, binding the object to the thread. | 669 // Background thread tries to delete target, binding the object to the thread. |
| 670 BackgroundThread background; | 670 BackgroundThread background; |
| 671 background.Start(); | 671 background.Start(); |
| 672 background.DeleteTarget(target.release()); | 672 background.DeleteTarget(target.release()); |
| 673 | 673 |
| 674 // Main thread attempts to dereference the target, violating thread binding. | 674 // Main thread attempts to dereference the target, violating thread binding. |
| 675 ASSERT_DCHECK_DEATH(arrow.target.get(), ""); | 675 ASSERT_DCHECK_DEATH(arrow.target.get()); |
| 676 } | 676 } |
| 677 | 677 |
| 678 } // namespace base | 678 } // namespace base |
| OLD | NEW |