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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/test/repeating_test_future.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "base/test/task_environment.h"
#include "testing/gtest/include/gtest/gtest-spi.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base::test {
namespace {
struct MoveOnlyValue {
public:
MoveOnlyValue() = default;
explicit MoveOnlyValue(std::string data) : data(std::move(data)) {}
MoveOnlyValue(const MoveOnlyValue&) = delete;
auto& operator=(const MoveOnlyValue&) = delete;
MoveOnlyValue(MoveOnlyValue&&) = default;
MoveOnlyValue& operator=(MoveOnlyValue&&) = default;
~MoveOnlyValue() = default;
std::string data;
};
} // namespace
class RepeatingTestFutureTest : public ::testing::Test {
public:
RepeatingTestFutureTest() = default;
RepeatingTestFutureTest(const RepeatingTestFutureTest&) = delete;
RepeatingTestFutureTest& operator=(const RepeatingTestFutureTest&) = delete;
~RepeatingTestFutureTest() override = default;
void RunLater(OnceClosure callable) {
SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE,
std::move(callable));
}
private:
test::SingleThreadTaskEnvironment environment_;
};
TEST_F(RepeatingTestFutureTest, ShouldBeEmptyInitially) {
RepeatingTestFuture<std::string> future;
EXPECT_TRUE(future.IsEmpty());
}
TEST_F(RepeatingTestFutureTest, ShouldNotBeEmptyAfterAddingAValue) {
RepeatingTestFuture<std::string> future;
future.AddValue("a value");
EXPECT_FALSE(future.IsEmpty());
}
TEST_F(RepeatingTestFutureTest, ShouldBeEmptyAfterTakingTheOnlyElement) {
RepeatingTestFuture<std::string> future;
future.AddValue("a value");
future.Take();
EXPECT_TRUE(future.IsEmpty());
}
TEST_F(RepeatingTestFutureTest,
ShouldNotBeEmptyIfTakingOneElementFromAfutureWith2Elements) {
RepeatingTestFuture<std::string> future;
future.AddValue("first value");
future.AddValue("second value");
future.Take();
EXPECT_FALSE(future.IsEmpty());
}
TEST_F(RepeatingTestFutureTest, ShouldTakeElementsFiFo) {
RepeatingTestFuture<std::string> future;
future.AddValue("first value");
future.AddValue("second value");
EXPECT_EQ(future.Take(), "first value");
EXPECT_EQ(future.Take(), "second value");
}
TEST_F(RepeatingTestFutureTest, WaitShouldBlockUntilElementArrives) {
RepeatingTestFuture<std::string> future;
RunLater(BindLambdaForTesting([&future] { future.AddValue("a value"); }));
EXPECT_TRUE(future.IsEmpty());
EXPECT_TRUE(future.Wait());
EXPECT_FALSE(future.IsEmpty());
}
TEST_F(RepeatingTestFutureTest, WaitShouldReturnTrueWhenValueArrives) {
RepeatingTestFuture<std::string> future;
RunLater(BindLambdaForTesting([&future] { future.AddValue("a value"); }));
EXPECT_TRUE(future.Wait());
EXPECT_FALSE(future.IsEmpty());
}
TEST_F(RepeatingTestFutureTest,
WaitShouldReturnTrueImmediatelyWhenValueIsAlreadyPresent) {
RepeatingTestFuture<std::string> future;
future.AddValue("value already present");
EXPECT_TRUE(future.Wait());
}
TEST_F(RepeatingTestFutureTest, WaitShouldReturnFalseIfTimeoutHappens) {
test::ScopedRunLoopTimeout timeout(FROM_HERE, Milliseconds(1));
// `ScopedRunLoopTimeout` will automatically fail the test when a timeout
// happens, so we use EXPECT_NONFATAL_FAILURE to handle this failure.
// EXPECT_NONFATAL_FAILURE only works on static objects.
static bool success;
static RepeatingTestFuture<std::string> future;
EXPECT_NONFATAL_FAILURE({ success = future.Wait(); }, "timed out");
EXPECT_FALSE(success);
}
TEST_F(RepeatingTestFutureTest, TakeShouldBlockUntilAnElementArrives) {
RepeatingTestFuture<std::string> future;
RunLater(BindLambdaForTesting(
[&future] { future.AddValue("value pushed delayed"); }));
EXPECT_EQ(future.Take(), "value pushed delayed");
}
TEST_F(RepeatingTestFutureTest, TakeShouldDcheckIfTimeoutHappens) {
test::ScopedRunLoopTimeout timeout(FROM_HERE, Milliseconds(1));
RepeatingTestFuture<std::string> future;
EXPECT_DCHECK_DEATH_WITH(future.Take(), "timed out");
}
TEST_F(RepeatingTestFutureTest, TakeShouldWorkWithMoveOnlyValue) {
RepeatingTestFuture<MoveOnlyValue> future;
RunLater(BindLambdaForTesting(
[&future] { future.AddValue(MoveOnlyValue("move only value")); }));
MoveOnlyValue result = future.Take();
EXPECT_EQ(result.data, "move only value");
}
TEST_F(RepeatingTestFutureTest, ShouldStoreValuePassedToCallback) {
RepeatingTestFuture<std::string> future;
RunLater(BindOnce(future.GetCallback(), "value"));
EXPECT_EQ("value", future.Take());
}
TEST_F(RepeatingTestFutureTest, ShouldAllowInvokingCallbackMultipleTimes) {
RepeatingTestFuture<std::string> future;
RunLater(BindLambdaForTesting([callback = future.GetCallback()]() {
callback.Run("first value");
callback.Run("second value");
callback.Run("third value");
}));
EXPECT_EQ("first value", future.Take());
EXPECT_EQ("second value", future.Take());
EXPECT_EQ("third value", future.Take());
}
TEST_F(RepeatingTestFutureTest, ShouldAllowReferenceArgumentsForCallback) {
RepeatingTestFuture<std::string> future;
RepeatingCallback<void(const std::string&)> callback =
future.GetCallback<const std::string&>();
RunLater(BindOnce(std::move(callback), "expected value"));
EXPECT_EQ("expected value", future.Take());
}
TEST_F(RepeatingTestFutureTest, ShouldStoreMultipleValuesInATuple) {
const int expected_int_value = 5;
const std::string expected_string_value = "value";
RepeatingTestFuture<int, std::string> future;
RunLater(BindLambdaForTesting(
[&] { future.AddValue(expected_int_value, expected_string_value); }));
std::tuple<int, std::string> actual = future.Take();
EXPECT_EQ(expected_int_value, std::get<0>(actual));
EXPECT_EQ(expected_string_value, std::get<1>(actual));
}
TEST_F(RepeatingTestFutureTest, ShouldAllowCallbackWithMultipleValues) {
const int expected_int_value = 5;
const std::string expected_string_value = "value";
RepeatingTestFuture<int, std::string> future;
RunLater(BindOnce(future.GetCallback(), expected_int_value,
expected_string_value));
std::tuple<int, std::string> actual = future.Take();
EXPECT_EQ(expected_int_value, std::get<0>(actual));
EXPECT_EQ(expected_string_value, std::get<1>(actual));
}
TEST_F(RepeatingTestFutureTest,
ShouldAllowCallbackWithMultipleReferenceValues) {
const int expected_int_value = 5;
const std::string expected_string_value = "value";
RepeatingTestFuture<int, std::string> future;
RepeatingCallback<void(const int&, std::string)> callback =
future.GetCallback<const int&, std::string>();
RunLater(
BindOnce(std::move(callback), expected_int_value, expected_string_value));
std::tuple<int, std::string> actual = future.Take();
EXPECT_EQ(expected_int_value, std::get<0>(actual));
EXPECT_EQ(expected_string_value, std::get<1>(actual));
}
TEST_F(RepeatingTestFutureTest, ShouldSupportCvRefType) {
std::string expected_value = "value";
RepeatingTestFuture<const std::string&> future;
base::OnceCallback<void(const std::string&)> callback = future.GetCallback();
std::move(callback).Run(expected_value);
std::string actual = future.Take();
EXPECT_EQ(expected_value, actual);
}
TEST_F(RepeatingTestFutureTest, ShouldSupportMultipleCvRefType) {
const int expected_first_value = 5;
std::string expected_second_value = "value";
const long expected_third_value = 10;
RepeatingTestFuture<const int, std::string&, const long&> future;
base::OnceCallback<void(const int, std::string&, const long&)> callback =
future.GetCallback();
std::move(callback).Run(expected_first_value, expected_second_value,
expected_third_value);
std::tuple<int, std::string, long> take_result = future.Take();
EXPECT_EQ(expected_first_value, std::get<0>(take_result));
EXPECT_EQ(expected_second_value, std::get<1>(take_result));
EXPECT_EQ(expected_third_value, std::get<2>(take_result));
}
} // namespace base::test
|