aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/coro/lazy/lazy_test.cpp
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@yahooinc.com>2022-10-04 12:03:32 +0000
committerHåvard Pettersen <havardpe@yahooinc.com>2022-10-06 14:11:25 +0000
commit2743ff3bc794ccdd276800a0f98050fbe8f346d3 (patch)
tree5071d3528dcf05358c6fff1b4c0c3dd7d4711324 /vespalib/src/tests/coro/lazy/lazy_test.cpp
parent51e61ab17c38d01981a08527ad5a698beeb14411 (diff)
experiment with coroutines
Diffstat (limited to 'vespalib/src/tests/coro/lazy/lazy_test.cpp')
-rw-r--r--vespalib/src/tests/coro/lazy/lazy_test.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/vespalib/src/tests/coro/lazy/lazy_test.cpp b/vespalib/src/tests/coro/lazy/lazy_test.cpp
new file mode 100644
index 00000000000..a715e473aaf
--- /dev/null
+++ b/vespalib/src/tests/coro/lazy/lazy_test.cpp
@@ -0,0 +1,44 @@
+// 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/gtest/gtest.h>
+
+using vespalib::coro::Lazy;
+using vespalib::coro::sync_wait;
+
+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);
+}
+
+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);
+}
+
+GTEST_MAIN_RUN_ALL_TESTS()