|
| 1 | +#include <memory> |
| 2 | +#include <async/post-ack.hpp> |
| 3 | +#include <gtest/gtest.h> |
| 4 | + |
| 5 | +TEST(PostAck, IntType) { |
| 6 | + async::post_ack_mechanism<int> mech; |
| 7 | + |
| 8 | + int ok1_ctr = 0, ok2_ctr = 0; |
| 9 | + |
| 10 | + auto producer = [] (async::post_ack_mechanism<int> &mech) -> async::detached { |
| 11 | + co_await mech.post(1); |
| 12 | + co_await mech.post(2); |
| 13 | + }; |
| 14 | + |
| 15 | + auto consumer = [&] (async::post_ack_mechanism<int> &mech) -> async::detached { |
| 16 | + async::post_ack_agent<int> agent; |
| 17 | + agent.attach(&mech); |
| 18 | + |
| 19 | + auto handle = co_await agent.poll(); |
| 20 | + if (*handle == 1) ok1_ctr++; |
| 21 | + handle.ack(); |
| 22 | + |
| 23 | + handle = co_await agent.poll(); |
| 24 | + if (*handle == 2) ok2_ctr++; |
| 25 | + handle.ack(); |
| 26 | + |
| 27 | + agent.detach(); |
| 28 | + }; |
| 29 | + |
| 30 | + consumer(mech); |
| 31 | + consumer(mech); |
| 32 | + consumer(mech); |
| 33 | + |
| 34 | + producer(mech); |
| 35 | + |
| 36 | + ASSERT_EQ(ok1_ctr, 3); |
| 37 | + ASSERT_EQ(ok2_ctr, 3); |
| 38 | +} |
| 39 | + |
| 40 | +TEST(PostAck, ImmovableType) { |
| 41 | + async::post_ack_mechanism<std::unique_ptr<int>> mech; |
| 42 | + |
| 43 | + int ok1_ctr = 0, ok2_ctr = 0; |
| 44 | + |
| 45 | + auto producer = [] (async::post_ack_mechanism<std::unique_ptr<int>> &mech) -> async::detached { |
| 46 | + co_await mech.post(std::make_unique<int>(1)); |
| 47 | + co_await mech.post(std::make_unique<int>(2)); |
| 48 | + }; |
| 49 | + |
| 50 | + auto consumer = [&] (async::post_ack_mechanism<std::unique_ptr<int>> &mech) -> async::detached { |
| 51 | + async::post_ack_agent<std::unique_ptr<int>> agent; |
| 52 | + agent.attach(&mech); |
| 53 | + |
| 54 | + auto handle = co_await agent.poll(); |
| 55 | + if (*handle != nullptr) ok1_ctr++; |
| 56 | + if (**handle == 1) ok1_ctr++; |
| 57 | + handle.ack(); |
| 58 | + |
| 59 | + handle = co_await agent.poll(); |
| 60 | + if (*handle != nullptr) ok2_ctr++; |
| 61 | + if (**handle == 2) ok2_ctr++; |
| 62 | + handle.ack(); |
| 63 | + |
| 64 | + agent.detach(); |
| 65 | + }; |
| 66 | + |
| 67 | + consumer(mech); |
| 68 | + consumer(mech); |
| 69 | + consumer(mech); |
| 70 | + consumer(mech); |
| 71 | + |
| 72 | + producer(mech); |
| 73 | + |
| 74 | + ASSERT_EQ(ok1_ctr, 8); |
| 75 | + ASSERT_EQ(ok2_ctr, 8); |
| 76 | +} |
0 commit comments