mirror of
https://gitlab.com/niansa/libcrosscoro.git
synced 2025-03-06 20:53:32 +01:00
* Update docs on io_scheduler for inline processing Support gcc 10.3.1 (fedora 33 updated) Update ci.yml to run fedora 32,33,34 and support both gcc 10.2.1 and 10.3.1 * fedora 32 -> gcc-c++ drop version * Update ci.yml and test_latch.cpp
110 lines
2.2 KiB
C++
110 lines
2.2 KiB
C++
#include "catch.hpp"
|
|
|
|
#include <coro/coro.hpp>
|
|
|
|
#include <chrono>
|
|
#include <thread>
|
|
|
|
TEST_CASE("latch count=0", "[latch]")
|
|
{
|
|
coro::latch l{0};
|
|
|
|
auto make_task = [&]() -> coro::task<uint64_t> {
|
|
co_await l;
|
|
co_return 42;
|
|
};
|
|
|
|
auto task = make_task();
|
|
|
|
task.resume();
|
|
REQUIRE(task.is_ready()); // The latch never waits due to zero count.
|
|
REQUIRE(task.promise().return_value() == 42);
|
|
}
|
|
|
|
TEST_CASE("latch count=1", "[latch]")
|
|
{
|
|
coro::latch l{1};
|
|
|
|
auto make_task = [&]() -> coro::task<uint64_t> {
|
|
auto workers = l.remaining();
|
|
co_await l;
|
|
co_return workers;
|
|
};
|
|
|
|
auto task = make_task();
|
|
|
|
task.resume();
|
|
REQUIRE_FALSE(task.is_ready());
|
|
|
|
l.count_down();
|
|
REQUIRE(task.is_ready());
|
|
REQUIRE(task.promise().return_value() == 1);
|
|
}
|
|
|
|
TEST_CASE("latch count=1 count_down=5", "[latch]")
|
|
{
|
|
coro::latch l{1};
|
|
|
|
auto make_task = [&]() -> coro::task<uint64_t> {
|
|
auto workers = l.remaining();
|
|
co_await l;
|
|
co_return workers;
|
|
};
|
|
|
|
auto task = make_task();
|
|
|
|
task.resume();
|
|
REQUIRE_FALSE(task.is_ready());
|
|
|
|
l.count_down(5);
|
|
REQUIRE(task.is_ready());
|
|
REQUIRE(task.promise().return_value() == 1);
|
|
}
|
|
|
|
TEST_CASE("latch count=5 count_down=1 x5", "[latch]")
|
|
{
|
|
coro::latch l{5};
|
|
|
|
auto make_task = [&]() -> coro::task<uint64_t> {
|
|
auto workers = l.remaining();
|
|
co_await l;
|
|
co_return workers;
|
|
};
|
|
|
|
auto task = make_task();
|
|
|
|
task.resume();
|
|
REQUIRE_FALSE(task.is_ready());
|
|
|
|
l.count_down(1);
|
|
REQUIRE_FALSE(task.is_ready());
|
|
l.count_down(1);
|
|
REQUIRE_FALSE(task.is_ready());
|
|
l.count_down(1);
|
|
REQUIRE_FALSE(task.is_ready());
|
|
l.count_down(1);
|
|
REQUIRE_FALSE(task.is_ready());
|
|
l.count_down(1);
|
|
REQUIRE(task.is_ready());
|
|
REQUIRE(task.promise().return_value() == 5);
|
|
}
|
|
|
|
TEST_CASE("latch count=5 count_down=5", "[latch]")
|
|
{
|
|
coro::latch l{5};
|
|
|
|
auto make_task = [&]() -> coro::task<uint64_t> {
|
|
auto workers = l.remaining();
|
|
co_await l;
|
|
co_return workers;
|
|
};
|
|
|
|
auto task = make_task();
|
|
|
|
task.resume();
|
|
REQUIRE_FALSE(task.is_ready());
|
|
|
|
l.count_down(5);
|
|
REQUIRE(task.is_ready());
|
|
REQUIRE(task.promise().return_value() == 5);
|
|
}
|