groby@chromium.org | 042aa75 | 2013-08-25 00:00:33 +0900 | [diff] [blame] | 1 | // Copyright 2013 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. |
| 4 | |
| 5 | #include "base/barrier_closure.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | |
| 10 | namespace { |
| 11 | |
| 12 | void Increment(int* count) { (*count)++; } |
| 13 | |
| 14 | TEST(BarrierClosureTest, RunImmediatelyForZeroClosures) { |
| 15 | int count = 0; |
dzhioev | dc5c4c3 | 2015-05-07 10:05:35 +0900 | [diff] [blame] | 16 | base::Closure done_closure(base::Bind(&Increment, base::Unretained(&count))); |
groby@chromium.org | 042aa75 | 2013-08-25 00:00:33 +0900 | [diff] [blame] | 17 | |
dzhioev | dc5c4c3 | 2015-05-07 10:05:35 +0900 | [diff] [blame] | 18 | base::Closure barrier_closure = base::BarrierClosure(0, done_closure); |
groby@chromium.org | 042aa75 | 2013-08-25 00:00:33 +0900 | [diff] [blame] | 19 | EXPECT_EQ(1, count); |
| 20 | } |
| 21 | |
| 22 | TEST(BarrierClosureTest, RunAfterNumClosures) { |
| 23 | int count = 0; |
dzhioev | dc5c4c3 | 2015-05-07 10:05:35 +0900 | [diff] [blame] | 24 | base::Closure done_closure(base::Bind(&Increment, base::Unretained(&count))); |
groby@chromium.org | 042aa75 | 2013-08-25 00:00:33 +0900 | [diff] [blame] | 25 | |
dzhioev | dc5c4c3 | 2015-05-07 10:05:35 +0900 | [diff] [blame] | 26 | base::Closure barrier_closure = base::BarrierClosure(2, done_closure); |
groby@chromium.org | 042aa75 | 2013-08-25 00:00:33 +0900 | [diff] [blame] | 27 | EXPECT_EQ(0, count); |
| 28 | |
dzhioev | dc5c4c3 | 2015-05-07 10:05:35 +0900 | [diff] [blame] | 29 | barrier_closure.Run(); |
groby@chromium.org | 042aa75 | 2013-08-25 00:00:33 +0900 | [diff] [blame] | 30 | EXPECT_EQ(0, count); |
| 31 | |
dzhioev | dc5c4c3 | 2015-05-07 10:05:35 +0900 | [diff] [blame] | 32 | barrier_closure.Run(); |
groby@chromium.org | 042aa75 | 2013-08-25 00:00:33 +0900 | [diff] [blame] | 33 | EXPECT_EQ(1, count); |
| 34 | } |
| 35 | |
dzhioev | dc5c4c3 | 2015-05-07 10:05:35 +0900 | [diff] [blame] | 36 | class DestructionIndicator { |
| 37 | public: |
| 38 | // Sets |*destructed| to true in destructor. |
| 39 | DestructionIndicator(bool* destructed) : destructed_(destructed) { |
| 40 | *destructed_ = false; |
| 41 | } |
| 42 | |
| 43 | ~DestructionIndicator() { *destructed_ = true; } |
| 44 | |
| 45 | void DoNothing() {} |
| 46 | |
| 47 | private: |
| 48 | bool* destructed_; |
| 49 | }; |
| 50 | |
| 51 | TEST(BarrierClosureTest, ReleasesDoneClosureWhenDone) { |
| 52 | bool done_destructed = false; |
| 53 | base::Closure barrier_closure = base::BarrierClosure( |
tzik | 330d83f | 2017-06-26 15:13:17 +0900 | [diff] [blame] | 54 | 1, |
| 55 | base::BindOnce(&DestructionIndicator::DoNothing, |
| 56 | base::Owned(new DestructionIndicator(&done_destructed)))); |
dzhioev | dc5c4c3 | 2015-05-07 10:05:35 +0900 | [diff] [blame] | 57 | EXPECT_FALSE(done_destructed); |
| 58 | barrier_closure.Run(); |
| 59 | EXPECT_TRUE(done_destructed); |
| 60 | } |
| 61 | |
| 62 | void ResetBarrierClosure(base::Closure* closure) { |
| 63 | *closure = base::Closure(); |
| 64 | } |
| 65 | |
| 66 | // Tests a case when |done_closure| resets a |barrier_closure|. |
| 67 | // |barrier_closure| is a Closure holding the |done_closure|. |done_closure| |
| 68 | // holds a pointer back to the |barrier_closure|. When |barrier_closure| is |
| 69 | // Run() it calls ResetBarrierClosure() which erases the |barrier_closure| while |
| 70 | // still inside of its Run(). The Run() implementation (in base::BarrierClosure) |
| 71 | // must not try use itself after executing ResetBarrierClosure() or this test |
| 72 | // would crash inside Run(). |
| 73 | TEST(BarrierClosureTest, KeepingClosureAliveUntilDone) { |
| 74 | base::Closure barrier_closure; |
| 75 | base::Closure done_closure = |
| 76 | base::Bind(ResetBarrierClosure, &barrier_closure); |
| 77 | barrier_closure = base::BarrierClosure(1, done_closure); |
| 78 | barrier_closure.Run(); |
| 79 | } |
| 80 | |
groby@chromium.org | 042aa75 | 2013-08-25 00:00:33 +0900 | [diff] [blame] | 81 | } // namespace |