1 /*
2 * Copyright 2020 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 <ftl/future.h>
18 #include <gtest/gtest.h>
19
20 #include <algorithm>
21 #include <future>
22 #include <string>
23 #include <thread>
24 #include <vector>
25
26 namespace android::test {
27
28 // Keep in sync with example usage in header file.
TEST(Future,Example)29 TEST(Future, Example) {
30 {
31 auto future = ftl::defer([](int x) { return x + 1; }, 99);
32 EXPECT_EQ(future.get(), 100);
33 }
34 {
35 auto future = ftl::yield(42);
36 EXPECT_EQ(future.get(), 42);
37 }
38 {
39 auto ptr = std::make_unique<char>('!');
40 auto future = ftl::yield(std::move(ptr));
41 EXPECT_EQ(*future.get(), '!');
42 }
43 {
44 auto future = ftl::yield(123);
45 ftl::Future<char> futures[] = {ftl::yield('a'), ftl::yield('b')};
46
47 ftl::Future<char> chain = ftl::Future(std::move(future))
48 .then([](int x) { return static_cast<size_t>(x % 2); })
49 .then([&futures](size_t i) { return std::move(futures[i]); });
50
51 EXPECT_EQ(chain.get(), 'b');
52 }
53 }
54
55 namespace {
56
57 using ByteVector = std::vector<uint8_t>;
58
decrement(ByteVector bytes)59 ByteVector decrement(ByteVector bytes) {
60 std::transform(bytes.begin(), bytes.end(), bytes.begin(), [](auto b) { return b - 1; });
61 return bytes;
62 }
63
64 } // namespace
65
TEST(Future,Chain)66 TEST(Future, Chain) {
67 std::packaged_task<const char*()> fetch_string([] { return "ifmmp-"; });
68
69 std::packaged_task<ByteVector(std::string)> append_string([](std::string str) {
70 str += "!xpsme";
71 return ByteVector{str.begin(), str.end()};
72 });
73
74 std::packaged_task<ftl::Future<ByteVector>(ByteVector)> decrement_bytes(
75 [](ByteVector bytes) { return ftl::defer(decrement, std::move(bytes)); });
76
77 auto fetch = fetch_string.get_future();
78 std::thread fetch_thread(std::move(fetch_string));
79
80 std::thread append_thread, decrement_thread;
81
82 EXPECT_EQ(
83 "hello, world",
84 ftl::Future(std::move(fetch))
85 .then([](const char* str) { return std::string(str); })
86 .then([&](std::string str) {
87 auto append = append_string.get_future();
88 append_thread = std::thread(std::move(append_string), std::move(str));
89 return append;
90 })
91 .then([&](ByteVector bytes) {
92 auto decrement = decrement_bytes.get_future();
93 decrement_thread = std::thread(std::move(decrement_bytes), std::move(bytes));
94 return decrement;
95 })
96 .then([](ftl::Future<ByteVector> bytes) { return bytes; })
97 .then([](const ByteVector& bytes) { return std::string(bytes.begin(), bytes.end()); })
98 .get());
99
100 fetch_thread.join();
101 append_thread.join();
102 decrement_thread.join();
103 }
104
TEST(Future,WaitFor)105 TEST(Future, WaitFor) {
106 using namespace std::chrono_literals;
107 {
108 auto future = ftl::yield(42);
109 // Check that we can wait_for multiple times without invalidating the future
110 EXPECT_EQ(future.wait_for(1s), std::future_status::ready);
111 EXPECT_EQ(future.wait_for(1s), std::future_status::ready);
112 EXPECT_EQ(future.get(), 42);
113 }
114
115 {
116 std::condition_variable cv;
117 std::mutex m;
118 bool ready = false;
119
120 std::packaged_task<int32_t()> get_int([&] {
121 std::unique_lock lk(m);
122 cv.wait(lk, [&] { return ready; });
123 return 24;
124 });
125
126 auto get_future = ftl::Future(get_int.get_future());
127 std::thread get_thread(std::move(get_int));
128
129 EXPECT_EQ(get_future.wait_for(0s), std::future_status::timeout);
130 {
131 std::unique_lock lk(m);
132 ready = true;
133 }
134 cv.notify_one();
135
136 EXPECT_EQ(get_future.wait_for(1s), std::future_status::ready);
137 EXPECT_EQ(get_future.get(), 24);
138
139 get_thread.join();
140 }
141 }
142
143 } // namespace android::test
144