| Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
| Gabriel Charette | 383448cd | 2020-03-02 19:55:41 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| Sean Maher | acb01c4 | 2022-12-01 18:22:23 | [diff] [blame] | 5 | #include "base/task/task_runner.h" |
| Gabriel Charette | 383448cd | 2020-03-02 19:55:41 | [diff] [blame] | 6 | |
| Peter Boström | fd85123 | 2021-03-31 17:05:33 | [diff] [blame] | 7 | #include <memory> |
| Gabriel Charette | 383448cd | 2020-03-02 19:55:41 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
| Avi Drissman | 63e1f99 | 2023-01-13 18:54:43 | [diff] [blame] | 10 | #include "base/functional/bind.h" |
| Gabriel Charette | 383448cd | 2020-03-02 19:55:41 | [diff] [blame] | 11 | #include "base/location.h" |
| 12 | #include "base/run_loop.h" |
| Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 13 | #include "base/task/single_thread_task_runner.h" |
| Gabriel Charette | 383448cd | 2020-03-02 19:55:41 | [diff] [blame] | 14 | #include "base/test/task_environment.h" |
| Gabriel Charette | 383448cd | 2020-03-02 19:55:41 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
| 17 | namespace base { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | int ReturnFourtyTwo() { |
| 22 | return 42; |
| 23 | } |
| 24 | |
| 25 | void StoreValue(int* destination, int value) { |
| 26 | *destination = value; |
| 27 | } |
| 28 | |
| 29 | void StoreDoubleValue(double* destination, double value) { |
| 30 | *destination = value; |
| 31 | } |
| 32 | |
| 33 | int g_foo_destruct_count = 0; |
| 34 | int g_foo_free_count = 0; |
| 35 | |
| 36 | struct Foo { |
| 37 | ~Foo() { ++g_foo_destruct_count; } |
| 38 | }; |
| 39 | |
| 40 | std::unique_ptr<Foo> CreateFoo() { |
| Peter Boström | fd85123 | 2021-03-31 17:05:33 | [diff] [blame] | 41 | return std::make_unique<Foo>(); |
| Gabriel Charette | 383448cd | 2020-03-02 19:55:41 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void ExpectFoo(std::unique_ptr<Foo> foo) { |
| 45 | EXPECT_TRUE(foo.get()); |
| 46 | std::unique_ptr<Foo> local_foo(std::move(foo)); |
| 47 | EXPECT_TRUE(local_foo.get()); |
| 48 | EXPECT_FALSE(foo.get()); |
| 49 | } |
| 50 | |
| 51 | struct FooDeleter { |
| 52 | void operator()(Foo* foo) const { |
| 53 | ++g_foo_free_count; |
| 54 | delete foo; |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | std::unique_ptr<Foo, FooDeleter> CreateScopedFoo() { |
| 59 | return std::unique_ptr<Foo, FooDeleter>(new Foo); |
| 60 | } |
| 61 | |
| 62 | void ExpectScopedFoo(std::unique_ptr<Foo, FooDeleter> foo) { |
| 63 | EXPECT_TRUE(foo.get()); |
| 64 | std::unique_ptr<Foo, FooDeleter> local_foo(std::move(foo)); |
| 65 | EXPECT_TRUE(local_foo.get()); |
| 66 | EXPECT_FALSE(foo.get()); |
| 67 | } |
| 68 | |
| 69 | struct FooWithoutDefaultConstructor { |
| 70 | explicit FooWithoutDefaultConstructor(int value) : value(value) {} |
| 71 | int value; |
| 72 | }; |
| 73 | |
| 74 | FooWithoutDefaultConstructor CreateFooWithoutDefaultConstructor(int value) { |
| 75 | return FooWithoutDefaultConstructor(value); |
| 76 | } |
| 77 | |
| 78 | void SaveFooWithoutDefaultConstructor(int* output_value, |
| 79 | FooWithoutDefaultConstructor input) { |
| 80 | *output_value = input.value; |
| 81 | } |
| 82 | |
| 83 | class TaskRunnerTest : public testing::Test { |
| 84 | public: |
| 85 | TaskRunnerTest() = default; |
| 86 | |
| 87 | void SetUp() override { |
| 88 | g_foo_destruct_count = 0; |
| 89 | g_foo_free_count = 0; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | } // namespace |
| 94 | |
| 95 | TEST_F(TaskRunnerTest, PostTaskAndReplyWithResult) { |
| 96 | int result = 0; |
| 97 | |
| 98 | test::SingleThreadTaskEnvironment task_environment; |
| Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 99 | SingleThreadTaskRunner::GetCurrentDefault()->PostTaskAndReplyWithResult( |
| Gabriel Charette | 383448cd | 2020-03-02 19:55:41 | [diff] [blame] | 100 | FROM_HERE, BindOnce(&ReturnFourtyTwo), BindOnce(&StoreValue, &result)); |
| 101 | |
| 102 | RunLoop().RunUntilIdle(); |
| 103 | |
| 104 | EXPECT_EQ(42, result); |
| 105 | } |
| 106 | |
| cfredric | acb00a2 | 2021-08-12 18:00:37 | [diff] [blame] | 107 | TEST_F(TaskRunnerTest, PostTaskAndReplyWithResultRepeatingCallbacks) { |
| 108 | int result = 0; |
| 109 | |
| 110 | test::SingleThreadTaskEnvironment task_environment; |
| Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 111 | SingleThreadTaskRunner::GetCurrentDefault()->PostTaskAndReplyWithResult( |
| cfredric | acb00a2 | 2021-08-12 18:00:37 | [diff] [blame] | 112 | FROM_HERE, BindRepeating(&ReturnFourtyTwo), |
| 113 | BindRepeating(&StoreValue, &result)); |
| 114 | |
| 115 | RunLoop().RunUntilIdle(); |
| 116 | |
| 117 | EXPECT_EQ(42, result); |
| 118 | } |
| 119 | |
| Gabriel Charette | 383448cd | 2020-03-02 19:55:41 | [diff] [blame] | 120 | TEST_F(TaskRunnerTest, PostTaskAndReplyWithResultImplicitConvert) { |
| 121 | double result = 0; |
| 122 | |
| 123 | test::SingleThreadTaskEnvironment task_environment; |
| Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 124 | SingleThreadTaskRunner::GetCurrentDefault()->PostTaskAndReplyWithResult( |
| Gabriel Charette | 383448cd | 2020-03-02 19:55:41 | [diff] [blame] | 125 | FROM_HERE, BindOnce(&ReturnFourtyTwo), |
| 126 | BindOnce(&StoreDoubleValue, &result)); |
| 127 | |
| 128 | RunLoop().RunUntilIdle(); |
| 129 | |
| 130 | EXPECT_DOUBLE_EQ(42.0, result); |
| 131 | } |
| 132 | |
| 133 | TEST_F(TaskRunnerTest, PostTaskAndReplyWithResultPassed) { |
| 134 | test::SingleThreadTaskEnvironment task_environment; |
| Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 135 | SingleThreadTaskRunner::GetCurrentDefault()->PostTaskAndReplyWithResult( |
| Gabriel Charette | 383448cd | 2020-03-02 19:55:41 | [diff] [blame] | 136 | FROM_HERE, BindOnce(&CreateFoo), BindOnce(&ExpectFoo)); |
| 137 | |
| 138 | RunLoop().RunUntilIdle(); |
| 139 | |
| 140 | EXPECT_EQ(1, g_foo_destruct_count); |
| 141 | EXPECT_EQ(0, g_foo_free_count); |
| 142 | } |
| 143 | |
| 144 | TEST_F(TaskRunnerTest, PostTaskAndReplyWithResultPassedFreeProc) { |
| 145 | test::SingleThreadTaskEnvironment task_environment; |
| Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 146 | SingleThreadTaskRunner::GetCurrentDefault()->PostTaskAndReplyWithResult( |
| Gabriel Charette | 383448cd | 2020-03-02 19:55:41 | [diff] [blame] | 147 | FROM_HERE, BindOnce(&CreateScopedFoo), BindOnce(&ExpectScopedFoo)); |
| 148 | |
| 149 | RunLoop().RunUntilIdle(); |
| 150 | |
| 151 | EXPECT_EQ(1, g_foo_destruct_count); |
| 152 | EXPECT_EQ(1, g_foo_free_count); |
| 153 | } |
| 154 | |
| 155 | TEST_F(TaskRunnerTest, PostTaskAndReplyWithResultWithoutDefaultConstructor) { |
| 156 | const int kSomeVal = 17; |
| 157 | |
| 158 | test::SingleThreadTaskEnvironment task_environment; |
| 159 | int actual = 0; |
| 160 | |
| Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 161 | SingleThreadTaskRunner::GetCurrentDefault()->PostTaskAndReplyWithResult( |
| Gabriel Charette | 383448cd | 2020-03-02 19:55:41 | [diff] [blame] | 162 | FROM_HERE, BindOnce(&CreateFooWithoutDefaultConstructor, kSomeVal), |
| 163 | BindOnce(&SaveFooWithoutDefaultConstructor, &actual)); |
| 164 | |
| 165 | RunLoop().RunUntilIdle(); |
| 166 | |
| 167 | EXPECT_EQ(kSomeVal, actual); |
| 168 | } |
| 169 | |
| 170 | } // namespace base |