cancelable_callback_unittest.cc 4.36 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "butil/cancelable_callback.h"
gejun's avatar
gejun committed
6

7 8 9
#include "butil/bind.h"
#include "butil/bind_helpers.h"
#include "butil/memory/ref_counted.h"
gejun's avatar
gejun committed
10 11
#include <gtest/gtest.h>

12
namespace butil {
gejun's avatar
gejun committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
namespace {

class TestRefCounted : public RefCountedThreadSafe<TestRefCounted> {
 private:
  friend class RefCountedThreadSafe<TestRefCounted>;
  ~TestRefCounted() {};
};

void Increment(int* count) { (*count)++; }
void IncrementBy(int* count, int n) { (*count) += n; }
void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) {}

// Cancel().
//  - Callback can be run multiple times.
//  - After Cancel(), Run() completes but has no effect.
TEST(CancelableCallbackTest, Cancel) {
  int count = 0;
  CancelableClosure cancelable(
31
      butil::Bind(&Increment, butil::Unretained(&count)));
gejun's avatar
gejun committed
32

33
  butil::Closure callback = cancelable.callback();
gejun's avatar
gejun committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  callback.Run();
  EXPECT_EQ(1, count);

  callback.Run();
  EXPECT_EQ(2, count);

  cancelable.Cancel();
  callback.Run();
  EXPECT_EQ(2, count);
}

// Cancel() called multiple times.
//  - Cancel() cancels all copies of the wrapped callback.
//  - Calling Cancel() more than once has no effect.
//  - After Cancel(), callback() returns a null callback.
TEST(CancelableCallbackTest, MultipleCancel) {
  int count = 0;
  CancelableClosure cancelable(
52
      butil::Bind(&Increment, butil::Unretained(&count)));
gejun's avatar
gejun committed
53

54 55
  butil::Closure callback1 = cancelable.callback();
  butil::Closure callback2 = cancelable.callback();
gejun's avatar
gejun committed
56 57 58 59 60 61 62 63 64 65 66 67
  cancelable.Cancel();

  callback1.Run();
  EXPECT_EQ(0, count);

  callback2.Run();
  EXPECT_EQ(0, count);

  // Calling Cancel() again has no effect.
  cancelable.Cancel();

  // callback() of a cancelled callback is null.
68
  butil::Closure callback3 = cancelable.callback();
gejun's avatar
gejun committed
69 70 71 72 73 74 75
  EXPECT_TRUE(callback3.is_null());
}

// CancelableCallback destroyed before callback is run.
//  - Destruction of CancelableCallback cancels outstanding callbacks.
TEST(CancelableCallbackTest, CallbackCanceledOnDestruction) {
  int count = 0;
76
  butil::Closure callback;
gejun's avatar
gejun committed
77 78 79

  {
    CancelableClosure cancelable(
80
        butil::Bind(&Increment, butil::Unretained(&count)));
gejun's avatar
gejun committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

    callback = cancelable.callback();
    callback.Run();
    EXPECT_EQ(1, count);
  }

  callback.Run();
  EXPECT_EQ(1, count);
}

// Cancel() called on bound closure with a RefCounted parameter.
//  - Cancel drops wrapped callback (and, implicitly, its bound arguments).
TEST(CancelableCallbackTest, CancelDropsCallback) {
  scoped_refptr<TestRefCounted> ref_counted = new TestRefCounted;
  EXPECT_TRUE(ref_counted->HasOneRef());

97
  CancelableClosure cancelable(butil::Bind(RefCountedParam, ref_counted));
gejun's avatar
gejun committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  EXPECT_FALSE(cancelable.IsCancelled());
  EXPECT_TRUE(ref_counted.get());
  EXPECT_FALSE(ref_counted->HasOneRef());

  // There is only one reference to |ref_counted| after the Cancel().
  cancelable.Cancel();
  EXPECT_TRUE(cancelable.IsCancelled());
  EXPECT_TRUE(ref_counted.get());
  EXPECT_TRUE(ref_counted->HasOneRef());
}

// Reset().
//  - Reset() replaces the existing wrapped callback with a new callback.
//  - Reset() deactivates outstanding callbacks.
TEST(CancelableCallbackTest, Reset) {
  int count = 0;
  CancelableClosure cancelable(
115
      butil::Bind(&Increment, butil::Unretained(&count)));
gejun's avatar
gejun committed
116

117
  butil::Closure callback = cancelable.callback();
gejun's avatar
gejun committed
118 119 120 121 122 123 124
  callback.Run();
  EXPECT_EQ(1, count);

  callback.Run();
  EXPECT_EQ(2, count);

  cancelable.Reset(
125
      butil::Bind(&IncrementBy, butil::Unretained(&count), 3));
gejun's avatar
gejun committed
126 127 128 129 130 131 132 133 134
  EXPECT_FALSE(cancelable.IsCancelled());

  // The stale copy of the cancelable callback is non-null.
  ASSERT_FALSE(callback.is_null());

  // The stale copy of the cancelable callback is no longer active.
  callback.Run();
  EXPECT_EQ(2, count);

135
  butil::Closure callback2 = cancelable.callback();
gejun's avatar
gejun committed
136 137 138 139 140 141 142 143 144 145 146 147 148
  ASSERT_FALSE(callback2.is_null());

  callback2.Run();
  EXPECT_EQ(5, count);
}

// IsCanceled().
//  - Cancel() transforms the CancelableCallback into a cancelled state.
TEST(CancelableCallbackTest, IsNull) {
  CancelableClosure cancelable;
  EXPECT_TRUE(cancelable.IsCancelled());

  int count = 0;
149 150
  cancelable.Reset(butil::Bind(&Increment,
                              butil::Unretained(&count)));
gejun's avatar
gejun committed
151 152 153 154 155 156 157
  EXPECT_FALSE(cancelable.IsCancelled());

  cancelable.Cancel();
  EXPECT_TRUE(cancelable.IsCancelled());
}

}  // namespace
158
}  // namespace butil