-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
447 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <fiber/sched/run_loop.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
#include <fmt/core.h> | ||
|
||
using namespace fiber; // NOLINT | ||
|
||
class RunLoopTest : public ::testing::Test {}; | ||
|
||
TEST_F(RunLoopTest, JustWorksIntrusive) { | ||
sched::RunLoop loop; | ||
|
||
bool flag = false; | ||
|
||
class SetFlag : public sched::task::TaskBase { | ||
public: | ||
explicit SetFlag(bool& flag) | ||
: flag_(flag) { | ||
} | ||
|
||
void Run() noexcept override { | ||
flag_ = true; | ||
} | ||
|
||
private: | ||
bool& flag_; | ||
}; | ||
|
||
{ | ||
SetFlag set{flag}; | ||
loop.Submit(&set); | ||
loop.RunNext(); | ||
} | ||
|
||
EXPECT_TRUE(flag); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#include <fiber/sched/run_loop.hpp> | ||
#include <fiber/sched/task/submit.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
#include <fmt/core.h> | ||
|
||
using namespace fiber; // NOLINT | ||
|
||
class RunLoopTest : public ::testing::Test {}; | ||
|
||
TEST_F(RunLoopTest, JustWorks) { | ||
sched::RunLoop loop; | ||
|
||
size_t step = 0; | ||
|
||
EXPECT_FALSE(loop.NonEmpty()); | ||
|
||
EXPECT_FALSE(loop.RunNext()); | ||
EXPECT_EQ(loop.RunAtMost(99), 0); | ||
|
||
sched::task::Submit(loop, [&] { | ||
step = 1; | ||
}); | ||
|
||
EXPECT_TRUE(loop.NonEmpty()); | ||
EXPECT_EQ(step, 0); | ||
|
||
sched::task::Submit(loop, [&] { | ||
step = 2; | ||
}); | ||
|
||
EXPECT_EQ(step, 0); | ||
EXPECT_TRUE(loop.RunNext()); | ||
EXPECT_EQ(step, 1); | ||
EXPECT_TRUE(loop.NonEmpty()); | ||
|
||
sched::task::Submit(loop, [&] { | ||
step = 3; | ||
}); | ||
|
||
EXPECT_EQ(loop.RunAtMost(99), 2); | ||
EXPECT_EQ(step, 3); | ||
EXPECT_FALSE(loop.NonEmpty()); | ||
EXPECT_FALSE(loop.RunNext()); | ||
} | ||
|
||
TEST_F(RunLoopTest, Empty) { | ||
sched::RunLoop loop; | ||
|
||
EXPECT_FALSE(loop.RunNext()); | ||
EXPECT_EQ(loop.RunAtMost(7), 0); | ||
EXPECT_EQ(loop.Run(), 0); | ||
} | ||
|
||
void Countdown(sched::RunLoop& loop, size_t k) { | ||
if (k > 0) { | ||
sched::task::Submit(loop, [&loop, k] { | ||
Countdown(loop, k - 1); | ||
}); | ||
} | ||
} | ||
|
||
TEST_F(RunLoopTest, RunAtMost) { | ||
sched::RunLoop loop; | ||
|
||
Countdown(loop, 256); | ||
|
||
size_t tasks = 0; | ||
do { | ||
tasks += loop.RunAtMost(7); | ||
} while (loop.NonEmpty()); | ||
|
||
fmt::println("{}", tasks); | ||
|
||
EXPECT_EQ(tasks, 256); | ||
} | ||
|
||
TEST_F(RunLoopTest, RunAtMostNewTasks) { | ||
sched::RunLoop loop; | ||
|
||
sched::task::Submit(loop, [&]() { | ||
sched::task::Submit(loop, []() {}); | ||
}); | ||
|
||
EXPECT_EQ(loop.RunAtMost(2), 2); | ||
} | ||
|
||
TEST_F(RunLoopTest, Run) { | ||
sched::RunLoop loop; | ||
|
||
Countdown(loop, 117); | ||
|
||
EXPECT_EQ(loop.Run(), 117); | ||
} | ||
|
||
TEST_F(RunLoopTest, RunTwice) { | ||
sched::RunLoop loop; | ||
|
||
Countdown(loop, 11); | ||
|
||
EXPECT_EQ(loop.Run(), 11); | ||
|
||
Countdown(loop, 7); | ||
|
||
EXPECT_EQ(loop.Run(), 7); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <fiber/sched/thread_pool.hpp> | ||
#include <fiber/sync/wait_group.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
#include <fmt/core.h> | ||
|
||
using namespace fiber; // NOLINT | ||
|
||
class ThreadPoolTest : public ::testing::Test {}; | ||
|
||
TEST_F(ThreadPoolTest, JustWorksIntrusive) { | ||
sched::ThreadPool pool{4}; | ||
pool.Start(); | ||
|
||
class DoWork : public sched::task::TaskBase { | ||
public: | ||
explicit DoWork(sync::WaitGroup& wg) | ||
: wg_(wg) { | ||
} | ||
|
||
void Run() noexcept override { | ||
wg_.Done(); | ||
} | ||
|
||
private: | ||
sync::WaitGroup& wg_; | ||
}; | ||
|
||
{ | ||
sync::WaitGroup wg; | ||
|
||
DoWork work{wg}; | ||
|
||
wg.Add(1); | ||
pool.Submit(&work); | ||
wg.Wait(); | ||
} | ||
|
||
pool.Stop(); | ||
} |
Oops, something went wrong.