Skip to content

Commit

Permalink
Fix folly::coro unit tests to not use InlineExecutor
Browse files Browse the repository at this point in the history
Reviewed By: yfeldblum, lewissbaker

Differential Revision: D18052431

fbshipit-source-id: 9fb1b3faba25093cf67bdf9c6cbe5cc70794deee
  • Loading branch information
andriigrynenko authored and facebook-github-bot committed Oct 22, 2019
1 parent f54c24f commit 8f2abf7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
14 changes: 10 additions & 4 deletions folly/experimental/coro/test/BatonTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#if FOLLY_HAS_COROUTINES

#include <folly/executors/InlineExecutor.h>
#include <folly/executors/ManualExecutor.h>
#include <folly/experimental/coro/Baton.h>
#include <folly/experimental/coro/Task.h>
#include <folly/portability/GTest.h>
Expand Down Expand Up @@ -59,12 +59,15 @@ TEST(Baton, AwaitBaton) {
CHECK(!reachedBeforeAwait);
CHECK(!reachedAfterAwait);

auto f = std::move(t).scheduleOn(&InlineExecutor::instance()).start();
ManualExecutor executor;
auto f = std::move(t).scheduleOn(&executor).start();
executor.drain();

CHECK(reachedBeforeAwait);
CHECK(!reachedAfterAwait);

baton.post();
executor.drain();

CHECK(reachedAfterAwait);
}
Expand Down Expand Up @@ -92,15 +95,18 @@ TEST(Baton, MultiAwaitBaton) {
coro::Task<void> t1 = makeTask1();
coro::Task<void> t2 = makeTask2();

auto f1 = std::move(t1).scheduleOn(&InlineExecutor::instance()).start();
auto f2 = std::move(t2).scheduleOn(&InlineExecutor::instance()).start();
ManualExecutor executor;
auto f1 = std::move(t1).scheduleOn(&executor).start();
auto f2 = std::move(t2).scheduleOn(&executor).start();
executor.drain();

CHECK(reachedBeforeAwait1);
CHECK(reachedBeforeAwait2);
CHECK(!reachedAfterAwait1);
CHECK(!reachedAfterAwait2);

baton.post();
executor.drain();

CHECK(f1.isReady());
CHECK(f2.isReady());
Expand Down
2 changes: 1 addition & 1 deletion folly/experimental/coro/test/CoroTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ TEST(Coro, TaskOfMoveOnly) {
co_return std::make_unique<int>(123);
};

auto p = coro::blockingWait(f().scheduleOn(&InlineExecutor::instance()));
auto p = coro::blockingWait(f());
EXPECT_TRUE(p);
EXPECT_EQ(123, *p);
}
Expand Down
21 changes: 14 additions & 7 deletions folly/experimental/coro/test/MutexTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#if FOLLY_HAS_COROUTINES

#include <folly/executors/CPUThreadPoolExecutor.h>
#include <folly/executors/InlineExecutor.h>
#include <folly/executors/ManualExecutor.h>
#include <folly/experimental/coro/Baton.h>
#include <folly/experimental/coro/BlockingWait.h>
Expand Down Expand Up @@ -71,25 +70,29 @@ TEST(Mutex, LockAsync) {
m.unlock();
};

auto& inlineExecutor = InlineExecutor::instance();
ManualExecutor executor;

auto f1 = makeTask(b1).scheduleOn(&inlineExecutor).start();
auto f1 = makeTask(b1).scheduleOn(&executor).start();
executor.drain();
CHECK_EQ(1, value);
CHECK(!m.try_lock());

auto f2 = makeTask(b2).scheduleOn(&inlineExecutor).start();
auto f2 = makeTask(b2).scheduleOn(&executor).start();
executor.drain();
CHECK_EQ(1, value);

// This will resume f1 coroutine and let it release the
// lock. This will in turn resume f2 which was suspended
// at co_await m.lockAsync() which will then increment the value
// before becoming blocked on
b1.post();
executor.drain();

CHECK_EQ(3, value);
CHECK(!m.try_lock());

b2.post();
executor.drain();
CHECK_EQ(4, value);
CHECK(m.try_lock());
}
Expand All @@ -108,25 +111,29 @@ TEST(Mutex, ScopedLockAsync) {
++value;
};

auto& inlineExecutor = InlineExecutor::instance();
ManualExecutor executor;

auto f1 = makeTask(b1).scheduleOn(&inlineExecutor).start();
auto f1 = makeTask(b1).scheduleOn(&executor).start();
executor.drain();
CHECK_EQ(1, value);
CHECK(!m.try_lock());

auto f2 = makeTask(b2).scheduleOn(&inlineExecutor).start();
auto f2 = makeTask(b2).scheduleOn(&executor).start();
executor.drain();
CHECK_EQ(1, value);

// This will resume f1 coroutine and let it release the
// lock. This will in turn resume f2 which was suspended
// at co_await m.lockAsync() which will then increment the value
// before becoming blocked on b2.
b1.post();
executor.drain();

CHECK_EQ(3, value);
CHECK(!m.try_lock());

b2.post();
executor.drain();
CHECK_EQ(4, value);
CHECK(m.try_lock());
}
Expand Down
16 changes: 13 additions & 3 deletions folly/experimental/coro/test/SharedMutexTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#if FOLLY_HAS_COROUTINES

#include <folly/executors/CPUThreadPoolExecutor.h>
#include <folly/executors/InlineExecutor.h>
#include <folly/executors/ManualExecutor.h>
#include <folly/experimental/coro/Baton.h>
#include <folly/experimental/coro/BlockingWait.h>
Expand Down Expand Up @@ -72,7 +71,7 @@ TEST(SharedMutex, ManualLockAsync) {
mutex.unlock();
};

auto& executor = InlineExecutor::instance();
ManualExecutor executor;

{
coro::Baton b1;
Expand All @@ -86,22 +85,28 @@ TEST(SharedMutex, ManualLockAsync) {
auto w1 = makeWriterTask(b3).scheduleOn(&executor).start();
auto w2 = makeWriterTask(b4).scheduleOn(&executor).start();
auto r3 = makeReaderTask(b5).scheduleOn(&executor).start();
executor.drain();

b1.post();
executor.drain();
CHECK_EQ(0, std::move(r1).get());

b2.post();
executor.drain();
CHECK_EQ(0, std::move(r2).get());

b3.post();
executor.drain();
CHECK_EQ(1, value);

b4.post();
executor.drain();
CHECK_EQ(2, value);

// This reader should have had to wait for the prior two write locks
// to complete before it acquired the read-lock.
b5.post();
executor.drain();
CHECK_EQ(2, std::move(r3).get());
}
}
Expand All @@ -122,7 +127,7 @@ TEST(SharedMutex, ScopedLockAsync) {
value += 1;
};

auto& executor = InlineExecutor::instance();
ManualExecutor executor;

{
coro::Baton b1;
Expand All @@ -138,20 +143,25 @@ TEST(SharedMutex, ScopedLockAsync) {
auto r3 = makeReaderTask(b5).scheduleOn(&executor).start();

b1.post();
executor.drain();
CHECK_EQ(0, std::move(r1).get());

b2.post();
executor.drain();
CHECK_EQ(0, std::move(r2).get());

b3.post();
executor.drain();
CHECK_EQ(1, value);

b4.post();
executor.drain();
CHECK_EQ(2, value);

// This reader should have had to wait for the prior two write locks
// to complete before it acquired the read-lock.
b5.post();
executor.drain();
CHECK_EQ(2, std::move(r3).get());
}
}
Expand Down

0 comments on commit 8f2abf7

Please # to comment.