From 8f2abf7724b54d8961d368944de2db98b2a1d5d3 Mon Sep 17 00:00:00 2001 From: Andrii Grynenko Date: Tue, 22 Oct 2019 11:03:43 -0700 Subject: [PATCH] Fix folly::coro unit tests to not use InlineExecutor Reviewed By: yfeldblum, lewissbaker Differential Revision: D18052431 fbshipit-source-id: 9fb1b3faba25093cf67bdf9c6cbe5cc70794deee --- folly/experimental/coro/test/BatonTest.cpp | 14 +++++++++---- folly/experimental/coro/test/CoroTest.cpp | 2 +- folly/experimental/coro/test/MutexTest.cpp | 21 ++++++++++++------- .../coro/test/SharedMutexTest.cpp | 16 +++++++++++--- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/folly/experimental/coro/test/BatonTest.cpp b/folly/experimental/coro/test/BatonTest.cpp index dfeba9e2ae1..a2fc39cb779 100644 --- a/folly/experimental/coro/test/BatonTest.cpp +++ b/folly/experimental/coro/test/BatonTest.cpp @@ -18,7 +18,7 @@ #if FOLLY_HAS_COROUTINES -#include +#include #include #include #include @@ -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); } @@ -92,8 +95,10 @@ TEST(Baton, MultiAwaitBaton) { coro::Task t1 = makeTask1(); coro::Task 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); @@ -101,6 +106,7 @@ TEST(Baton, MultiAwaitBaton) { CHECK(!reachedAfterAwait2); baton.post(); + executor.drain(); CHECK(f1.isReady()); CHECK(f2.isReady()); diff --git a/folly/experimental/coro/test/CoroTest.cpp b/folly/experimental/coro/test/CoroTest.cpp index 3ec36e73960..e18c3a8c071 100644 --- a/folly/experimental/coro/test/CoroTest.cpp +++ b/folly/experimental/coro/test/CoroTest.cpp @@ -101,7 +101,7 @@ TEST(Coro, TaskOfMoveOnly) { co_return std::make_unique(123); }; - auto p = coro::blockingWait(f().scheduleOn(&InlineExecutor::instance())); + auto p = coro::blockingWait(f()); EXPECT_TRUE(p); EXPECT_EQ(123, *p); } diff --git a/folly/experimental/coro/test/MutexTest.cpp b/folly/experimental/coro/test/MutexTest.cpp index 22ec0a24e2a..f959bf862ee 100644 --- a/folly/experimental/coro/test/MutexTest.cpp +++ b/folly/experimental/coro/test/MutexTest.cpp @@ -19,7 +19,6 @@ #if FOLLY_HAS_COROUTINES #include -#include #include #include #include @@ -71,13 +70,15 @@ 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 @@ -85,11 +86,13 @@ TEST(Mutex, LockAsync) { // 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()); } @@ -108,13 +111,15 @@ 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 @@ -122,11 +127,13 @@ TEST(Mutex, ScopedLockAsync) { // 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()); } diff --git a/folly/experimental/coro/test/SharedMutexTest.cpp b/folly/experimental/coro/test/SharedMutexTest.cpp index 717ecb49387..64fe23c9633 100644 --- a/folly/experimental/coro/test/SharedMutexTest.cpp +++ b/folly/experimental/coro/test/SharedMutexTest.cpp @@ -19,7 +19,6 @@ #if FOLLY_HAS_COROUTINES #include -#include #include #include #include @@ -72,7 +71,7 @@ TEST(SharedMutex, ManualLockAsync) { mutex.unlock(); }; - auto& executor = InlineExecutor::instance(); + ManualExecutor executor; { coro::Baton b1; @@ -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()); } } @@ -122,7 +127,7 @@ TEST(SharedMutex, ScopedLockAsync) { value += 1; }; - auto& executor = InlineExecutor::instance(); + ManualExecutor executor; { coro::Baton b1; @@ -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()); } }