Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: base/memory/weak_ptr_unittest.cc

Issue 7013059: Fix base::internal::WeakReferenceOwner races. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/memory/weak_ptr.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "base/memory/weak_ptr.h" 6 #include "base/memory/weak_ptr.h"
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/task.h"
9 #include "base/threading/thread.h" 10 #include "base/threading/thread.h"
10 11
11 namespace base { 12 namespace base {
12 namespace { 13 namespace {
13 14
14 template <class T> 15 template <class T>
15 class OffThreadObjectCreator { 16 class OffThreadObjectCreator {
16 public: 17 public:
17 static T* NewObject() { 18 static T* NewObject() {
18 T* result; 19 T* result;
(...skipping 12 matching lines...) Expand all
31 *result = new T; 32 *result = new T;
32 } 33 }
33 }; 34 };
34 35
35 struct Base {}; 36 struct Base {};
36 struct Derived : Base {}; 37 struct Derived : Base {};
37 38
38 struct Producer : SupportsWeakPtr<Producer> {}; 39 struct Producer : SupportsWeakPtr<Producer> {};
39 struct Consumer { WeakPtr<Producer> producer; }; 40 struct Consumer { WeakPtr<Producer> producer; };
40 41
41 } // namespace
42
43 TEST(WeakPtrTest, Basic) { 42 TEST(WeakPtrTest, Basic) {
44 int data; 43 int data;
45 WeakPtrFactory<int> factory(&data); 44 WeakPtrFactory<int> factory(&data);
46 WeakPtr<int> ptr = factory.GetWeakPtr(); 45 WeakPtr<int> ptr = factory.GetWeakPtr();
47 EXPECT_EQ(&data, ptr.get()); 46 EXPECT_EQ(&data, ptr.get());
48 } 47 }
49 48
50 TEST(WeakPtrTest, Comparison) { 49 TEST(WeakPtrTest, Comparison) {
51 int data; 50 int data;
52 WeakPtrFactory<int> factory(&data); 51 WeakPtrFactory<int> factory(&data);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 TEST(WeakPtrTest, SingleThreaded2) { 126 TEST(WeakPtrTest, SingleThreaded2) {
128 // Test that it is OK to create a class that has a WeakPtr member on one 127 // Test that it is OK to create a class that has a WeakPtr member on one
129 // thread, but use it on another. This tests that we do not trip runtime 128 // thread, but use it on another. This tests that we do not trip runtime
130 // checks that ensure that a weak reference is not used by multiple threads. 129 // checks that ensure that a weak reference is not used by multiple threads.
131 scoped_ptr<Consumer> consumer(OffThreadObjectCreator<Consumer>::NewObject()); 130 scoped_ptr<Consumer> consumer(OffThreadObjectCreator<Consumer>::NewObject());
132 Producer producer; 131 Producer producer;
133 consumer->producer = producer.AsWeakPtr(); 132 consumer->producer = producer.AsWeakPtr();
134 EXPECT_EQ(&producer, consumer->producer.get()); 133 EXPECT_EQ(&producer, consumer->producer.get());
135 } 134 }
136 135
136 TEST(WeakPtrTest, DeleteWeakPtrOnDifferentThread) {
137 Producer producer;
138 {
139 WeakPtr<Producer>* ptr = new WeakPtr<Producer>(producer.AsWeakPtr());
140 Thread thread("I delete WeakPtrs, w00t!");
141 thread.Start();
142 thread.message_loop()->PostTask(FROM_HERE,
143 new DeleteTask<WeakPtr<Producer> >(ptr));
144 // |thread| will be joined on destruction.
145 }
146 WeakPtr<Producer> weak_ptr = producer.AsWeakPtr();
147 EXPECT_TRUE(weak_ptr.get() != NULL);
148 }
149
150 } // namespace
151
137 } // namespace base 152 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/weak_ptr.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698