1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "perfetto/ext/base/threading/future.h"
18
19 #include <memory>
20
21 #include "perfetto/base/flat_set.h"
22 #include "perfetto/base/platform_handle.h"
23 #include "test/gtest_and_gmock.h"
24
25 namespace perfetto {
26 namespace base {
27 namespace {
28
29 using testing::_;
30 using testing::Return;
31
32 template <typename T>
33 class MockPollable : public FuturePollable<T> {
34 public:
35 MOCK_METHOD(FuturePollResult<T>, Poll, (PollContext*), (override));
36 };
37
38 class FutureUnittest : public ::testing::Test {
39 public:
40 base::FlatSet<base::PlatformHandle> interested_;
41 base::FlatSet<base::PlatformHandle> ready_;
42 PollContext ctx_{&interested_, &ready_};
43 };
44
TEST_F(FutureUnittest,PollableImmediateResult)45 TEST_F(FutureUnittest, PollableImmediateResult) {
46 std::unique_ptr<MockPollable<int>> int_pollable(new MockPollable<int>());
47 EXPECT_CALL(*int_pollable, Poll(_))
48 .WillOnce(Return(FuturePollResult<int>(0)));
49
50 base::Future<int> future(std::move(int_pollable));
51 auto res = future.Poll(&ctx_);
52 ASSERT_FALSE(res.IsPending());
53 ASSERT_EQ(res.item(), 0);
54 }
55
TEST_F(FutureUnittest,PollablePendingThenResult)56 TEST_F(FutureUnittest, PollablePendingThenResult) {
57 std::unique_ptr<MockPollable<int>> int_pollable(new MockPollable<int>());
58 EXPECT_CALL(*int_pollable, Poll(_))
59 .WillOnce(Return(PendingPollResult()))
60 .WillOnce(Return(FuturePollResult<int>(1)));
61
62 base::Future<int> future(std::move(int_pollable));
63 ASSERT_TRUE(future.Poll(&ctx_).IsPending());
64 ASSERT_EQ(future.Poll(&ctx_).item(), 1);
65 }
66
TEST_F(FutureUnittest,ImmediateFuture)67 TEST_F(FutureUnittest, ImmediateFuture) {
68 base::Future<int> future(100);
69 ASSERT_EQ(future.Poll(&ctx_).item(), 100);
70 }
71
TEST_F(FutureUnittest,ContinueWithBothImmediate)72 TEST_F(FutureUnittest, ContinueWithBothImmediate) {
73 auto future = base::Future<int>(100).ContinueWith(
74 [](int res) -> Future<int> { return res * 2; });
75 ASSERT_EQ(future.Poll(&ctx_).item(), 200);
76 }
77
TEST_F(FutureUnittest,ImmediateContinueWithPending)78 TEST_F(FutureUnittest, ImmediateContinueWithPending) {
79 auto future = base::Future<int>(100).ContinueWith([](int res) {
80 std::unique_ptr<MockPollable<int>> pollable(new MockPollable<int>());
81 EXPECT_CALL(*pollable, Poll(_))
82 .WillOnce(Return(PendingPollResult()))
83 .WillOnce(Return(FuturePollResult<int>(res * 2)));
84 return Future<int>(std::move(pollable));
85 });
86 ASSERT_TRUE(future.Poll(&ctx_).IsPending());
87 ASSERT_EQ(future.Poll(&ctx_).item(), 200);
88 }
89
TEST_F(FutureUnittest,PendingContinueWithImmediate)90 TEST_F(FutureUnittest, PendingContinueWithImmediate) {
91 std::unique_ptr<MockPollable<int>> pollable(new MockPollable<int>());
92 EXPECT_CALL(*pollable, Poll(_))
93 .WillOnce(Return(PendingPollResult()))
94 .WillOnce(Return(FuturePollResult<int>(100)));
95 auto future =
96 base::Future<int>(std::move(pollable))
97 .ContinueWith([](int res) -> Future<int> { return res * 2; });
98 ASSERT_TRUE(future.Poll(&ctx_).IsPending());
99 ASSERT_EQ(future.Poll(&ctx_).item(), 200);
100 }
101
102 } // namespace
103 } // namespace base
104 } // namespace perfetto
105