sled/src/futures/promise_test.cc

54 lines
1.4 KiB
C++
Raw Normal View History

2024-03-23 18:18:15 +08:00
#include <gtest/gtest.h>
#include <sled/futures/promise.h>
TEST(Promise, Basic)
{
auto p = sled::Promise<int>();
auto v = p.Then([](int v) {
EXPECT_EQ(v, 1);
return v + 10;
})
.Tap([](int v) {
EXPECT_EQ(v, 11);
// no effect
return v + 1;
})
.Then([](int v) {
EXPECT_EQ(v, 11);
return v + 10;
});
p.Resolve(1);
}
2024-03-23 19:00:52 +08:00
TEST(Future, Basic)
{
auto p = sled::Promise<int>();
auto future = p.GetFuture()
.Then([](int v) {
EXPECT_EQ(v, 1);
return v + 10;
})
.Then([](int v) {
EXPECT_EQ(v, 11);
return v + 10;
});
p.Resolve(1);
EXPECT_EQ(future.Get(), 21);
}
2024-03-25 14:00:37 +08:00
TEST(Future, Except)
{
auto p = sled::Promise<int>();
p.Resolve(1);
p.GetFuture()
.Then([](int) { throw std::runtime_error("test"); })
.Except([](std::exception_ptr e) {
try {
std::rethrow_exception(e);
} catch (const std::exception &e) {
EXPECT_STREQ(e.what(), "test");
}
})
.Get();
}