blob: 0e24bac1afa9097fc0a85e7191083d7fa18aa0e4 [file] [log] [blame]
Gabriel Charette383448cd2020-03-02 19:55:411// 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
3// found in the LICENSE file.
4
5#include "base/task_runner_util.h"
6
7#include <utility>
8
9#include "base/bind.h"
10#include "base/location.h"
11#include "base/run_loop.h"
12#include "base/test/task_environment.h"
13#include "base/threading/thread_task_runner_handle.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16namespace base {
17
18namespace {
19
20int ReturnFourtyTwo() {
21 return 42;
22}
23
24void StoreValue(int* destination, int value) {
25 *destination = value;
26}
27
28void StoreDoubleValue(double* destination, double value) {
29 *destination = value;
30}
31
32int g_foo_destruct_count = 0;
33int g_foo_free_count = 0;
34
35struct Foo {
36 ~Foo() { ++g_foo_destruct_count; }
37};
38
39std::unique_ptr<Foo> CreateFoo() {
40 return std::unique_ptr<Foo>(new Foo);
41}
42
43void ExpectFoo(std::unique_ptr<Foo> foo) {
44 EXPECT_TRUE(foo.get());
45 std::unique_ptr<Foo> local_foo(std::move(foo));
46 EXPECT_TRUE(local_foo.get());
47 EXPECT_FALSE(foo.get());
48}
49
50struct FooDeleter {
51 void operator()(Foo* foo) const {
52 ++g_foo_free_count;
53 delete foo;
54 }
55};
56
57std::unique_ptr<Foo, FooDeleter> CreateScopedFoo() {
58 return std::unique_ptr<Foo, FooDeleter>(new Foo);
59}
60
61void ExpectScopedFoo(std::unique_ptr<Foo, FooDeleter> foo) {
62 EXPECT_TRUE(foo.get());
63 std::unique_ptr<Foo, FooDeleter> local_foo(std::move(foo));
64 EXPECT_TRUE(local_foo.get());
65 EXPECT_FALSE(foo.get());
66}
67
68struct FooWithoutDefaultConstructor {
69 explicit FooWithoutDefaultConstructor(int value) : value(value) {}
70 int value;
71};
72
73FooWithoutDefaultConstructor CreateFooWithoutDefaultConstructor(int value) {
74 return FooWithoutDefaultConstructor(value);
75}
76
77void SaveFooWithoutDefaultConstructor(int* output_value,
78 FooWithoutDefaultConstructor input) {
79 *output_value = input.value;
80}
81
82class TaskRunnerTest : public testing::Test {
83 public:
84 TaskRunnerTest() = default;
85
86 void SetUp() override {
87 g_foo_destruct_count = 0;
88 g_foo_free_count = 0;
89 }
90};
91
92} // namespace
93
94TEST_F(TaskRunnerTest, PostTaskAndReplyWithResult) {
95 int result = 0;
96
97 test::SingleThreadTaskEnvironment task_environment;
98 ThreadTaskRunnerHandle::Get()->PostTaskAndReplyWithResult(
99 FROM_HERE, BindOnce(&ReturnFourtyTwo), BindOnce(&StoreValue, &result));
100
101 RunLoop().RunUntilIdle();
102
103 EXPECT_EQ(42, result);
104}
105
106TEST_F(TaskRunnerTest, PostTaskAndReplyWithResultImplicitConvert) {
107 double result = 0;
108
109 test::SingleThreadTaskEnvironment task_environment;
110 ThreadTaskRunnerHandle::Get()->PostTaskAndReplyWithResult(
111 FROM_HERE, BindOnce(&ReturnFourtyTwo),
112 BindOnce(&StoreDoubleValue, &result));
113
114 RunLoop().RunUntilIdle();
115
116 EXPECT_DOUBLE_EQ(42.0, result);
117}
118
119TEST_F(TaskRunnerTest, PostTaskAndReplyWithResultPassed) {
120 test::SingleThreadTaskEnvironment task_environment;
121 ThreadTaskRunnerHandle::Get()->PostTaskAndReplyWithResult(
122 FROM_HERE, BindOnce(&CreateFoo), BindOnce(&ExpectFoo));
123
124 RunLoop().RunUntilIdle();
125
126 EXPECT_EQ(1, g_foo_destruct_count);
127 EXPECT_EQ(0, g_foo_free_count);
128}
129
130TEST_F(TaskRunnerTest, PostTaskAndReplyWithResultPassedFreeProc) {
131 test::SingleThreadTaskEnvironment task_environment;
132 ThreadTaskRunnerHandle::Get()->PostTaskAndReplyWithResult(
133 FROM_HERE, BindOnce(&CreateScopedFoo), BindOnce(&ExpectScopedFoo));
134
135 RunLoop().RunUntilIdle();
136
137 EXPECT_EQ(1, g_foo_destruct_count);
138 EXPECT_EQ(1, g_foo_free_count);
139}
140
141TEST_F(TaskRunnerTest, PostTaskAndReplyWithResultWithoutDefaultConstructor) {
142 const int kSomeVal = 17;
143
144 test::SingleThreadTaskEnvironment task_environment;
145 int actual = 0;
146
147 ThreadTaskRunnerHandle::Get()->PostTaskAndReplyWithResult(
148 FROM_HERE, BindOnce(&CreateFooWithoutDefaultConstructor, kSomeVal),
149 BindOnce(&SaveFooWithoutDefaultConstructor, &actual));
150
151 RunLoop().RunUntilIdle();
152
153 EXPECT_EQ(kSomeVal, actual);
154}
155
156} // namespace base