Skip to content

Commit

Permalink
tests: Add basic tests for post-ack
Browse files Browse the repository at this point in the history
  • Loading branch information
qookei committed Feb 7, 2024
1 parent 3350640 commit a1a0100
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ sources = files(
'race.cpp',
'algorithm.cpp',
'promise.cpp',
'sequenced.cpp'
'sequenced.cpp',
'post-ack.cpp'
)

exe = executable('gtests',
Expand Down
76 changes: 76 additions & 0 deletions tests/post-ack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <memory>
#include <async/post-ack.hpp>
#include <gtest/gtest.h>

TEST(PostAck, IntType) {
async::post_ack_mechanism<int> mech;

int ok1_ctr = 0, ok2_ctr = 0;

auto producer = [] (async::post_ack_mechanism<int> &mech) -> async::detached {
co_await mech.post(1);
co_await mech.post(2);
};

auto consumer = [&] (async::post_ack_mechanism<int> &mech) -> async::detached {
async::post_ack_agent<int> 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<std::unique_ptr<int>> mech;

int ok1_ctr = 0, ok2_ctr = 0;

auto producer = [] (async::post_ack_mechanism<std::unique_ptr<int>> &mech) -> async::detached {
co_await mech.post(std::make_unique<int>(1));
co_await mech.post(std::make_unique<int>(2));
};

auto consumer = [&] (async::post_ack_mechanism<std::unique_ptr<int>> &mech) -> async::detached {
async::post_ack_agent<std::unique_ptr<int>> 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);
}

0 comments on commit a1a0100

Please sign in to comment.