TEST(LoopTest, Async) { Queue<int> queue; Promise<int> promise1; Promise<string> promise2; Future<string> future = loop( [&]() { return queue.get(); }, [&](int i) { promise1.set(i); return promise2.future() .then([](const string& s) -> ControlFlow<string> { return Break(s); }); }); EXPECT_TRUE(future.isPending()); queue.put(1); AWAIT_EQ(1, promise1.future()); EXPECT_TRUE(future.isPending()); string s = "Hello world!"; promise2.set(s); AWAIT_EQ(s, future); }
TEST(LoopTest, DiscardIterate) { Promise<int> promise; promise.future().onDiscard([&]() { promise.discard(); }); Future<Nothing> future = loop( [&]() { return promise.future(); }, [&](int i) -> ControlFlow<Nothing> { return Break(); }); EXPECT_TRUE(future.isPending()); future.discard(); AWAIT_DISCARDED(future); EXPECT_TRUE(promise.future().hasDiscard()); }
TEST(LoopTest, Sync) { std::atomic_int value = ATOMIC_VAR_INIT(1); Future<Nothing> future = loop( [&]() { return value.load(); }, [](int i) -> ControlFlow<Nothing> { if (i != 0) { return Continue(); } return Break(); }); EXPECT_TRUE(future.isPending()); value.store(0); AWAIT_READY(future); }