summaryrefslogtreecommitdiffstats
path: root/base/task_unittest.cc
blob: 25c201d267b801c0008c2a3074b4fd84755fc253 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// 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.

#include "base/memory/ref_counted.h"
#include "base/task.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

class CancelInDestructor : public base::RefCounted<CancelInDestructor> {
 public:
  CancelInDestructor() : cancelable_task_(NULL) {}

  void Start() {
    if (cancelable_task_) {
      ADD_FAILURE();
      return;
    }
    AddRef();
    cancelable_task_ = NewRunnableMethod(
        this, &CancelInDestructor::NeverIssuedCallback);
    Release();
  }

  CancelableTask* cancelable_task() {
    return cancelable_task_;
  }

 private:
  friend class base::RefCounted<CancelInDestructor>;

  ~CancelInDestructor() {
    if (cancelable_task_)
      cancelable_task_->Cancel();
  }

  void NeverIssuedCallback() { NOTREACHED(); }

  CancelableTask* cancelable_task_;
};

TEST(TaskTest, TestCancelInDestructor) {
  // Intentionally not using a scoped_refptr for cancel_in_destructor.
  CancelInDestructor* cancel_in_destructor = new CancelInDestructor();
  cancel_in_destructor->Start();
  CancelableTask* cancelable_task = cancel_in_destructor->cancelable_task();
  ASSERT_TRUE(cancelable_task);
  delete cancelable_task;
}

}  // namespace