Commit fad32186 authored by Marshall Cline's avatar Marshall Cline Committed by Facebook Github Bot

test cases per discussion in D7923362

Summary:
Discussion in D7923362 included validity & behavior of `wait()` vs. fulfillment and `wait()` vs. `then()`.

18 test-cases:

8 cases with `.wait(...)` and fulfillment:

- Future vs. SemiFuture
- wait() vs. wait(dur)
- fulfilled before wait(...) call vs. during wait(...) call

10 cases with `.wait(...)` and `.then(...)`:

- 4 wait() cases: {Future vs. SemiFuture} that gets fulfilled {prior to, during} the wait() call.
- 6 wait(dur) cases: {Future vs. SemiFuture} that gets fulfilled {prior to, during, after} the wait(dur) call.

Reviewed By: yfeldblum

Differential Revision: D8048426

fbshipit-source-id: c79cc337706f08f8fb9c6057a114723320a90820
parent c3ffff44
......@@ -16,6 +16,7 @@
#include <queue>
#include <folly/executors/InlineExecutor.h>
#include <folly/futures/Future.h>
#include <folly/io/async/EventBase.h>
#include <folly/portability/GTest.h>
......@@ -195,6 +196,110 @@ TEST(Wait, waitWithDuration) {
EXPECT_TRUE(f.isReady());
t.join();
}
{
// `Future::wait(Duration) &` when promise is fulfilled during the wait
Promise<int> p;
auto f = p.getFuture();
EXPECT_FALSE(f.isReady());
folly::Baton<> b;
auto t = std::thread([&] {
b.post();
/* sleep override */ std::this_thread::sleep_for(milliseconds(100));
p.setValue(42);
});
b.wait();
f.wait(std::chrono::seconds(10));
EXPECT_TRUE(f.valid());
EXPECT_TRUE(f.isReady());
EXPECT_EQ(f.value(), 42);
t.join();
EXPECT_TRUE(f.isReady());
EXPECT_EQ(f.value(), 42);
}
{
// `Future::wait(Duration) &&` when promise is fulfilled during the wait
Promise<int> p;
auto f1 = p.getFuture();
EXPECT_FALSE(f1.isReady());
folly::Baton<> b;
auto t = std::thread([&] {
b.post();
/* sleep override */ std::this_thread::sleep_for(milliseconds(100));
p.setValue(42);
});
b.wait();
auto f2 = std::move(f1).wait(std::chrono::seconds(10));
EXPECT_FALSE(f1.valid());
EXPECT_TRUE(f2.valid());
EXPECT_TRUE(f2.isReady());
EXPECT_EQ(f2.value(), 42);
t.join();
EXPECT_TRUE(f2.valid());
EXPECT_TRUE(f2.isReady());
EXPECT_EQ(f2.value(), 42);
}
{
// `SemiFuture::wait(Duration) &` when promise is fulfilled during the wait
Promise<int> p;
auto f = p.getSemiFuture();
EXPECT_FALSE(f.isReady());
folly::Baton<> b;
auto t = std::thread([&] {
b.post();
/* sleep override */ std::this_thread::sleep_for(milliseconds(100));
p.setValue(42);
});
b.wait();
f.wait(std::chrono::seconds(10));
EXPECT_TRUE(f.valid());
EXPECT_TRUE(f.isReady());
EXPECT_EQ(f.value(), 42);
t.join();
EXPECT_TRUE(f.isReady());
EXPECT_EQ(f.value(), 42);
}
{
// `SemiFuture::wait(Duration) &&` when promise is fulfilled during the wait
Promise<int> p;
auto f1 = p.getSemiFuture();
EXPECT_FALSE(f1.isReady());
folly::Baton<> b;
auto t = std::thread([&] {
b.post();
/* sleep override */ std::this_thread::sleep_for(milliseconds(100));
p.setValue(42);
});
b.wait();
auto f2 = std::move(f1).wait(std::chrono::seconds(10));
EXPECT_FALSE(f1.valid());
EXPECT_TRUE(f2.valid());
EXPECT_TRUE(f2.isReady());
EXPECT_EQ(f2.value(), 42);
t.join();
EXPECT_TRUE(f2.valid());
EXPECT_TRUE(f2.isReady());
EXPECT_EQ(f2.value(), 42);
}
}
TEST(Wait, multipleWait) {
......@@ -209,3 +314,187 @@ TEST(Wait, multipleWait) {
f.wait();
EXPECT_TRUE(f.isReady());
}
TEST(Wait, WaitPlusThen) {
// Validate expected behavior of `f.wait(...).then([](auto&){...})`.
// There are 10 sub-cases:
// - Future fulfilled {prior to, during} call to wait().
// - Future fulfilled {prior to, during, after} call to wait(dur).
// - then repeat those 5 cases for SemiFuture
{
// Sub-case: Future fulfilled before `wait()` is called.
// Expect call to `.then()` to succeed & continuation to run immediately.
Promise<int> p;
auto f = p.getFuture();
p.setValue(42);
EXPECT_TRUE(f.isReady());
EXPECT_EQ(f.value(), 42);
f.wait();
auto continuation = 0;
EXPECT_NO_THROW(f.then([&](auto& v) { continuation = v; }));
EXPECT_EQ(continuation, 42);
}
{
// Sub-case: Future fulfilled after `wait()` actually has to wait.
// Expect call to `.then()` to fail (throw std::logic_error).
Promise<int> p;
auto f = p.getFuture();
folly::Baton<> b;
auto t = std::thread([&] {
b.post();
/* sleep override */ std::this_thread::sleep_for(milliseconds(100));
p.setValue(42);
});
b.wait();
EXPECT_FALSE(f.isReady()); // deterministically passes in practice
f.wait();
EXPECT_THROW(f.then([](auto&) {}), std::logic_error);
t.join();
}
{
// Sub-case: Future fulfilled before `wait(dur)` is called.
// Expect call to `.then()` to succeed & continuation to run immediately.
Promise<int> p;
auto f = p.getFuture();
p.setValue(42);
EXPECT_TRUE(f.isReady());
EXPECT_EQ(f.value(), 42);
f.wait(std::chrono::seconds(10));
auto continuation = 0;
EXPECT_NO_THROW(f.then([&](auto& v) { continuation = v; }));
EXPECT_EQ(continuation, 42);
}
{
// Sub-case: Future fulfilled after `wait(dur)` actually starts waiting.
// Expect call to `.then()` to succeed & continuation to when result ready.
Promise<int> p;
auto f = p.getFuture();
folly::Baton<> b;
auto t = std::thread([&] {
b.post();
/* sleep override */ std::this_thread::sleep_for(milliseconds(100));
p.setValue(42);
});
b.wait();
EXPECT_FALSE(f.isReady()); // deterministically passes in practice
f.wait(std::chrono::seconds(10));
EXPECT_TRUE(f.isReady()); // deterministically passes in practice
auto continuation = 0;
EXPECT_NO_THROW(f.then([&](auto& v) { continuation = v; }));
EXPECT_EQ(continuation, 42);
t.join();
}
{
// Sub-case: Future not fulfilled - `wait(dur)` times out.
// Expect call to `.then()` to succeed; continuation to not run.
Promise<int> p;
auto f = p.getFuture();
f.wait(milliseconds(1));
auto continuation = 0;
EXPECT_NO_THROW(f.then([&](auto& v) { continuation = v; }));
EXPECT_EQ(continuation, 0);
}
{
// Sub-case: SemiFuture fulfilled before `wait()` is called.
// Expect call to `.then()` to succeed & continuation to run immediately.
Promise<int> p;
auto f = p.getSemiFuture();
p.setValue(42);
EXPECT_TRUE(f.isReady());
EXPECT_EQ(f.value(), 42);
f.wait();
auto continuation = 0;
InlineExecutor e;
auto f2 = std::move(f).via(&e);
EXPECT_NO_THROW(f2.then([&](auto& v) { continuation = v; }));
EXPECT_EQ(continuation, 42);
}
{
// Sub-case: SemiFuture fulfilled after `wait()` actually has to wait.
// Expect call to `.then()` to fail (throw std::logic_error).
Promise<int> p;
auto f = p.getSemiFuture();
folly::Baton<> b;
auto t = std::thread([&] {
b.post();
/* sleep override */ std::this_thread::sleep_for(milliseconds(100));
p.setValue(42);
});
b.wait();
EXPECT_FALSE(f.isReady()); // deterministically passes in practice
f.wait();
InlineExecutor e;
auto f2 = std::move(f).via(&e);
EXPECT_THROW(f2.then([](auto&) {}), std::logic_error);
t.join();
}
{
// Sub-case: SemiFuture fulfilled before `wait(dur)` is called.
// Expect call to `.then()` to succeed & continuation to run immediately.
Promise<int> p;
auto f = p.getSemiFuture();
p.setValue(42);
EXPECT_TRUE(f.isReady());
EXPECT_EQ(f.value(), 42);
f.wait(std::chrono::seconds(10));
auto continuation = 0;
InlineExecutor e;
auto f2 = std::move(f).via(&e);
EXPECT_NO_THROW(f2.then([&](auto& v) { continuation = v; }));
EXPECT_EQ(continuation, 42);
}
{
// Sub-case: SemiFuture fulfilled after `wait(dur)` actually starts waiting.
// Expect call to `.then()` to succeed & continuation to when result ready.
Promise<int> p;
auto f = p.getSemiFuture();
folly::Baton<> b;
auto t = std::thread([&] {
b.post();
/* sleep override */ std::this_thread::sleep_for(milliseconds(100));
p.setValue(42);
});
b.wait();
EXPECT_FALSE(f.isReady()); // deterministically passes in practice
f.wait(std::chrono::seconds(10));
EXPECT_TRUE(f.isReady()); // deterministically passes in practice
auto continuation = 0;
InlineExecutor e;
auto f2 = std::move(f).via(&e);
EXPECT_NO_THROW(f2.then([&](auto& v) { continuation = v; }));
EXPECT_EQ(continuation, 42);
t.join();
}
{
// Sub-case: SemiFuture fulfilled after `wait(dur)` times out.
// Expect call to `.then()` to succeed; continuation runs when fulfilled.
Promise<int> p;
auto f = p.getSemiFuture();
f.wait(milliseconds(1));
auto continuation = 0;
InlineExecutor e;
auto f2 = std::move(f).via(&e);
EXPECT_NO_THROW(f2.then([&](auto& v) { continuation = v; }));
EXPECT_EQ(continuation, 0);
p.setValue(42);
EXPECT_EQ(continuation, 42);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment