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

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

Issue 20777008: Remove obsolete WeakPtr hack. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 4 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.h ('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) 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 <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/debug/leak_annotations.h" 10 #include "base/debug/leak_annotations.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 std::string member; 42 std::string member;
43 }; 43 };
44 struct Derived : public Base {}; 44 struct Derived : public Base {};
45 45
46 struct TargetBase {}; 46 struct TargetBase {};
47 struct Target : public TargetBase, public SupportsWeakPtr<Target> {}; 47 struct Target : public TargetBase, public SupportsWeakPtr<Target> {};
48 struct DerivedTarget : public Target {}; 48 struct DerivedTarget : public Target {};
49 struct Arrow { 49 struct Arrow {
50 WeakPtr<Target> target; 50 WeakPtr<Target> target;
51 }; 51 };
52 struct TargetWithFactory : public Target {
53 TargetWithFactory() : factory(this) {}
54 WeakPtrFactory<Target> factory;
55 };
52 56
53 // Helper class to create and destroy weak pointer copies 57 // Helper class to create and destroy weak pointer copies
54 // and delete objects on a background thread. 58 // and delete objects on a background thread.
55 class BackgroundThread : public Thread { 59 class BackgroundThread : public Thread {
56 public: 60 public:
57 BackgroundThread() : Thread("owner_thread") {} 61 BackgroundThread() : Thread("owner_thread") {}
58 62
59 virtual ~BackgroundThread() { 63 virtual ~BackgroundThread() {
60 Stop(); 64 Stop();
61 } 65 }
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 333
330 // The new WeakPtr is owned by background thread. 334 // The new WeakPtr is owned by background thread.
331 EXPECT_EQ(target, background.DeRef(&arrow)); 335 EXPECT_EQ(target, background.DeRef(&arrow));
332 } 336 }
333 337
334 // Target can only be deleted on background thread. 338 // Target can only be deleted on background thread.
335 background.DeleteTarget(target); 339 background.DeleteTarget(target);
336 background.DeleteArrow(arrow); 340 background.DeleteArrow(arrow);
337 } 341 }
338 342
339 TEST(WeakPtrTest, MoveOwnershipExplicitlyObjectNotReferenced) { 343 TEST(WeakPtrTest, MoveOwnershipOfUnreferencedObject) {
340 // Case 1: The target is not bound to any thread yet. So calling
341 // DetachFromThread() is a no-op.
342 Target target;
343 target.DetachFromThreadHack();
344
345 // Case 2: The target is bound to main thread but no WeakPtr is pointing to
346 // it. In this case, it will be re-bound to any thread trying to get a
347 // WeakPtr pointing to it. So detach function call is again no-op.
348 {
349 WeakPtr<Target> weak_ptr = target.AsWeakPtr();
350 }
351 target.DetachFromThreadHack();
352 }
353
354 TEST(WeakPtrTest, MoveOwnershipExplicitly) {
355 BackgroundThread background; 344 BackgroundThread background;
356 background.Start(); 345 background.Start();
357 346
358 Arrow* arrow; 347 Arrow* arrow;
359 { 348 {
360 Target target; 349 Target target;
361 // Background thread creates WeakPtr(and implicitly owns the object). 350 // Background thread creates WeakPtr.
362 background.CreateArrowFromTarget(&arrow, &target); 351 background.CreateArrowFromTarget(&arrow, &target);
352
353 // Bind to background thread.
363 EXPECT_EQ(&target, background.DeRef(arrow)); 354 EXPECT_EQ(&target, background.DeRef(arrow));
364 355
365 // Detach from background thread. 356 // Release the only WeakPtr.
366 target.DetachFromThreadHack(); 357 arrow->target.reset();
358
359 // Now we should be able to create a new reference from this thread.
360 arrow->target = target.AsWeakPtr();
367 361
368 // Re-bind to main thread. 362 // Re-bind to main thread.
369 EXPECT_EQ(&target, arrow->target.get()); 363 EXPECT_EQ(&target, arrow->target.get());
370 364
371 // Main thread can now delete the target. 365 // And the main thread can now delete the target.
372 } 366 }
373 367
374 // WeakPtr can be deleted on non-owner thread. 368 delete arrow;
375 background.DeleteArrow(arrow); 369 }
370
371 TEST(WeakPtrTest, MoveOwnershipAfterInvalidate) {
372 BackgroundThread background;
373 background.Start();
374
375 Arrow arrow;
376 scoped_ptr<TargetWithFactory> target(new TargetWithFactory);
377
378 // Bind to main thread.
379 arrow.target = target->factory.GetWeakPtr();
380 EXPECT_EQ(target.get(), arrow.target.get());
381
382 target->factory.InvalidateWeakPtrs();
383 EXPECT_EQ(NULL, arrow.target.get());
384
385 arrow.target = target->factory.GetWeakPtr();
386 // Re-bind to background thread.
387 EXPECT_EQ(target.get(), background.DeRef(&arrow));
388
389 // And the background thread can now delete the target.
390 background.DeleteTarget(target.release());
376 } 391 }
377 392
378 TEST(WeakPtrTest, MainThreadRefOutlivesBackgroundThreadRef) { 393 TEST(WeakPtrTest, MainThreadRefOutlivesBackgroundThreadRef) {
379 // Originating thread has a WeakPtr that outlives others. 394 // Originating thread has a WeakPtr that outlives others.
380 // - Main thread creates a WeakPtr 395 // - Main thread creates a WeakPtr
381 // - Background thread creates a WeakPtr copy from the one in main thread 396 // - Background thread creates a WeakPtr copy from the one in main thread
382 // - Destruct the WeakPtr on background thread 397 // - Destruct the WeakPtr on background thread
383 // - Destruct the WeakPtr on main thread 398 // - Destruct the WeakPtr on main thread
384 BackgroundThread background; 399 BackgroundThread background;
385 background.Start(); 400 background.Start();
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 background.Start(); 597 background.Start();
583 background.DeleteTarget(target.release()); 598 background.DeleteTarget(target.release());
584 599
585 // Main thread attempts to dereference the target, violating thread binding. 600 // Main thread attempts to dereference the target, violating thread binding.
586 ASSERT_DEATH(arrow.target.get(), ""); 601 ASSERT_DEATH(arrow.target.get(), "");
587 } 602 }
588 603
589 #endif 604 #endif
590 605
591 } // namespace base 606 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/weak_ptr.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698