diff --git a/tests/meson.build b/tests/meson.build index e24ffd7..b37a5e9 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -20,7 +20,8 @@ sources = files( 'race.cpp', 'algorithm.cpp', 'promise.cpp', - 'sequenced.cpp' + 'sequenced.cpp', + 'post-ack.cpp' ) exe = executable('gtests', diff --git a/tests/post-ack.cpp b/tests/post-ack.cpp new file mode 100644 index 0000000..97ddb1e --- /dev/null +++ b/tests/post-ack.cpp @@ -0,0 +1,76 @@ +#include +#include +#include + +TEST(PostAck, IntType) { + async::post_ack_mechanism mech; + + int ok1_ctr = 0, ok2_ctr = 0; + + auto producer = [] (async::post_ack_mechanism &mech) -> async::detached { + co_await mech.post(1); + co_await mech.post(2); + }; + + auto consumer = [&] (async::post_ack_mechanism &mech) -> async::detached { + async::post_ack_agent agent; + agent.attach(&mech); + + auto handle = co_await agent.poll(); + if (*handle == 1) ok1_ctr++; + handle.ack(); + + handle = co_await agent.poll(); + if (*handle == 2) ok2_ctr++; + handle.ack(); + + agent.detach(); + }; + + consumer(mech); + consumer(mech); + consumer(mech); + + producer(mech); + + ASSERT_EQ(ok1_ctr, 3); + ASSERT_EQ(ok2_ctr, 3); +} + +TEST(PostAck, ImmovableType) { + async::post_ack_mechanism> mech; + + int ok1_ctr = 0, ok2_ctr = 0; + + auto producer = [] (async::post_ack_mechanism> &mech) -> async::detached { + co_await mech.post(std::make_unique(1)); + co_await mech.post(std::make_unique(2)); + }; + + auto consumer = [&] (async::post_ack_mechanism> &mech) -> async::detached { + async::post_ack_agent> agent; + agent.attach(&mech); + + auto handle = co_await agent.poll(); + if (*handle != nullptr) ok1_ctr++; + if (**handle == 1) ok1_ctr++; + handle.ack(); + + handle = co_await agent.poll(); + if (*handle != nullptr) ok2_ctr++; + if (**handle == 2) ok2_ctr++; + handle.ack(); + + agent.detach(); + }; + + consumer(mech); + consumer(mech); + consumer(mech); + consumer(mech); + + producer(mech); + + ASSERT_EQ(ok1_ctr, 8); + ASSERT_EQ(ok2_ctr, 8); +}