Skip to content

Commit

Permalink
make pub-sub test use multiple subscribers (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 authored Feb 28, 2025
1 parent a9406fa commit 2962e51
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions tests/universal/network/pub_sub.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ void put_sub_fifo_channel(Talloc& alloc) {

std::this_thread::sleep_for(1s);

auto subscriber = session2.declare_subscriber(ke, channels::FifoChannel(16));
std::vector<Subscriber<channels::FifoChannel::HandlerType<Sample>>> subscribers;
subscribers.push_back(session2.declare_subscriber(ke, channels::FifoChannel(16)));
subscribers.push_back(session2.declare_subscriber(ke, channels::FifoChannel(16)));

std::this_thread::sleep_for(1s);

Expand All @@ -128,24 +130,28 @@ void put_sub_fifo_channel(Talloc& alloc) {

std::this_thread::sleep_for(1s);

auto res = subscriber.handler().recv();
assert(std::holds_alternative<Sample>(res));
assert(std::get<Sample>(res).get_keyexpr() == "zenoh/test");
assert(std::get<Sample>(res).get_payload().as_string() == "first");
res = subscriber.handler().try_recv();
assert(std::holds_alternative<Sample>(res));
assert(std::get<Sample>(res).get_keyexpr() == "zenoh/test");
assert(std::get<Sample>(res).get_payload().as_string() == "second");

res = subscriber.handler().try_recv();
assert(std::holds_alternative<channels::RecvError>(res));
assert(std::get<channels::RecvError>(res) == channels::RecvError::Z_NODATA);
for (const auto& subscriber : subscribers) {
auto res = subscriber.handler().recv();
assert(std::holds_alternative<Sample>(res));
assert(std::get<Sample>(res).get_keyexpr() == "zenoh/test");
assert(std::get<Sample>(res).get_payload().as_string() == "first");
res = subscriber.handler().try_recv();
assert(std::holds_alternative<Sample>(res));
assert(std::get<Sample>(res).get_keyexpr() == "zenoh/test");
assert(std::get<Sample>(res).get_payload().as_string() == "second");

res = subscriber.handler().try_recv();
assert(std::holds_alternative<channels::RecvError>(res));
assert(std::get<channels::RecvError>(res) == channels::RecvError::Z_NODATA);
}

/// after session close subscriber handler should become disconnected
session2.close();
res = subscriber.handler().recv();
assert(std::holds_alternative<channels::RecvError>(res));
assert(std::get<channels::RecvError>(res) == channels::RecvError::Z_DISCONNECTED);
for (const auto& subscriber : subscribers) {
auto res = subscriber.handler().recv();
assert(std::holds_alternative<channels::RecvError>(res));
assert(std::get<channels::RecvError>(res) == channels::RecvError::Z_DISCONNECTED);
}
}

template <typename Talloc>
Expand All @@ -156,7 +162,9 @@ void put_sub_ring_channel(Talloc& alloc) {

std::this_thread::sleep_for(1s);

auto subscriber = session2.declare_subscriber(ke, channels::RingChannel(1));
std::vector<Subscriber<channels::RingChannel::HandlerType<Sample>>> subscribers;
subscribers.push_back(session2.declare_subscriber(ke, channels::RingChannel(1)));
subscribers.push_back(session2.declare_subscriber(ke, channels::RingChannel(1)));

std::this_thread::sleep_for(1s);

Expand All @@ -165,20 +173,24 @@ void put_sub_ring_channel(Talloc& alloc) {

std::this_thread::sleep_for(1s);

auto res = subscriber.handler().recv();
assert(std::holds_alternative<Sample>(res));
assert(std::get<Sample>(res).get_keyexpr() == "zenoh/test");
assert(std::get<Sample>(res).get_payload().as_string() == "second");
for (const auto& subscriber : subscribers) {
auto res = subscriber.handler().recv();
assert(std::holds_alternative<Sample>(res));
assert(std::get<Sample>(res).get_keyexpr() == "zenoh/test");
assert(std::get<Sample>(res).get_payload().as_string() == "second");

res = subscriber.handler().try_recv();
assert(std::holds_alternative<channels::RecvError>(res));
assert(std::get<channels::RecvError>(res) == channels::RecvError::Z_NODATA);
res = subscriber.handler().try_recv();
assert(std::holds_alternative<channels::RecvError>(res));
assert(std::get<channels::RecvError>(res) == channels::RecvError::Z_NODATA);
}

/// after session close subscriber handler should become disconnected
session2.close();
res = subscriber.handler().recv();
assert(std::holds_alternative<channels::RecvError>(res));
assert(std::get<channels::RecvError>(res) == channels::RecvError::Z_DISCONNECTED);
for (const auto& subscriber : subscribers) {
auto res = subscriber.handler().recv();
assert(std::holds_alternative<channels::RecvError>(res));
assert(std::get<channels::RecvError>(res) == channels::RecvError::Z_DISCONNECTED);
}
}

template <typename Talloc, bool share_alloc = true>
Expand Down

0 comments on commit 2962e51

Please sign in to comment.