Skip to content

Commit

Permalink
add scheduler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daronenko committed Oct 22, 2024
1 parent 4c6634a commit 8e9659c
Show file tree
Hide file tree
Showing 4 changed files with 447 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/sched/run_loop/intrusive.cpp
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);
}
106 changes: 106 additions & 0 deletions tests/sched/run_loop/unit.cpp
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);
}
40 changes: 40 additions & 0 deletions tests/sched/thread_pool/intrusive.cpp
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();
}
Loading

0 comments on commit 8e9659c

Please sign in to comment.