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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/coro/lazy.h>
#include <vespa/vespalib/coro/sync_wait.h>
#include <vespa/vespalib/util/require.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <mutex>
#include <thread>
using vespalib::coro::Lazy;
using vespalib::coro::sync_wait;
std::mutex thread_lock;
std::vector<std::thread> threads;
struct JoinThreads {
~JoinThreads() {
for (auto &thread: threads) {
thread.join();
}
threads.clear();
}
};
auto run_in_other_thread() {
struct awaiter {
bool await_ready() const noexcept { return false; }
void await_suspend(std::coroutine_handle<> handle) const {
auto guard = std::lock_guard(thread_lock);
threads.push_back(std::thread(handle));
}
void await_resume() const noexcept {}
};
return awaiter();
}
Lazy<int> make_lazy(int value) {
co_return value;
}
Lazy<int> async_add_values(int a, int b) {
auto lazy_a = make_lazy(a);
auto lazy_b = make_lazy(b);
co_return (co_await lazy_a + co_await lazy_b);
}
Lazy<int> async_sum(Lazy<int> a, Lazy<int> b) {
co_return (co_await a + co_await b);
}
Lazy<std::unique_ptr<int>> move_only_int() {
co_return std::make_unique<int>(123);
}
Lazy<int> extract_rvalue() {
auto res = co_await move_only_int();
co_return *res;
}
Lazy<int> will_throw() {
REQUIRE_FAILED("failed on purpose");
co_return 123;
}
template<typename T>
Lazy<T> forward_value(Lazy<T> value) {
co_return co_await std::move(value);
}
template <typename T>
Lazy<T> switch_thread(Lazy<T> value) {
std::cerr << "switching from thread " << std::this_thread::get_id() << std::endl;
co_await run_in_other_thread();
std::cerr << "........... to thread " << std::this_thread::get_id() << std::endl;
co_return co_await value;
}
TEST(LazyTest, simple_lazy_value) {
auto lazy = make_lazy(42);
auto result = sync_wait(lazy);
EXPECT_EQ(result, 42);
}
TEST(LazyTest, async_sum_of_async_values) {
auto lazy = async_add_values(10, 20);
auto result = sync_wait(lazy);
EXPECT_EQ(result, 30);
}
TEST(LazyTest, async_sum_of_external_async_values) {
auto a = make_lazy(100);
auto b = make_lazy(200);
auto lazy = async_sum(std::move(a), std::move(b));
auto result = sync_wait(lazy);
EXPECT_EQ(result, 300);
}
TEST(LazyTest, extract_rvalue_from_lazy_in_coroutine) {
auto lazy = extract_rvalue();
auto result = sync_wait(lazy);
EXPECT_EQ(result, 123);
}
TEST(LazyTest, extract_rvalue_from_lazy_in_sync_wait) {
auto result = sync_wait(move_only_int());
EXPECT_EQ(*result, 123);
}
TEST(LazyTest, calculate_result_in_another_thread) {
JoinThreads thread_guard;
auto result = sync_wait(switch_thread(make_lazy(7)));
EXPECT_EQ(result, 7);
}
TEST(LazyTest, exceptions_are_propagated) {
JoinThreads thread_guard;
auto lazy = switch_thread(forward_value(will_throw()));
EXPECT_THROW(sync_wait(lazy), vespalib::RequireFailedException);
}
GTEST_MAIN_RUN_ALL_TESTS()
|