Skip to content

Commit bc77f01

Browse files
authored
Merge pull request #467 from evoskuil/master
Fix broadcaster test race.
2 parents 841960c + f184bf9 commit bc77f01

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

test/net/broadcaster.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,34 @@ BOOST_AUTO_TEST_CASE(broadcaster__subscribe__stop__expected_code)
4747
constexpr auto expected_ec = error::invalid_magic;
4848
auto result = true;
4949

50-
std::promise<code> promise;
50+
// Subscription will capture message and stop notifications.
51+
std::promise<code> sub_promise;
52+
std::promise<code> ping_promise;
53+
5154
boost::asio::post(strand, [&]() NOEXCEPT
5255
{
53-
BOOST_REQUIRE(!instance.subscribe(
56+
sub_promise.set_value(instance.subscribe(
5457
[&](const code& ec, const messages::ping::cptr& ping, broadcaster::channel_id id) NOEXCEPT
5558
{
5659
// Stop notification has nullptr message, zero id, and specified code.
5760
result &= is_null(ping);
5861
result &= is_zero(id);
59-
promise.set_value(ec);
62+
ping_promise.set_value(ec);
6063
return true;
6164
}, channel_id));
6265
});
6366

67+
// Wait on successful subscription.
68+
BOOST_REQUIRE(!sub_promise.get_future().get());
69+
6470
boost::asio::post(strand, [&]() NOEXCEPT
6571
{
6672
instance.stop(expected_ec);
6773
});
6874

6975
pool.stop();
7076
BOOST_REQUIRE(pool.join());
71-
BOOST_REQUIRE_EQUAL(promise.get_future().get(), expected_ec);
77+
BOOST_REQUIRE_EQUAL(ping_promise.get_future().get(), expected_ec);
7278
BOOST_REQUIRE(result);
7379
}
7480

@@ -83,16 +89,17 @@ BOOST_AUTO_TEST_CASE(broadcaster__notify__valid_nonced_ping__expected_notificati
8389
auto result = true;
8490

8591
// Subscription will capture message and stop notifications.
86-
std::promise<code> promise;
92+
std::promise<code> sub_promise;
93+
std::promise<code> ping_promise;
8794
boost::asio::post(strand, [&]() NOEXCEPT
8895
{
89-
BOOST_REQUIRE(!instance.subscribe(
96+
sub_promise.set_value(instance.subscribe(
9097
[&](const code& ec, const messages::ping::cptr& ping, broadcaster::channel_id id) NOEXCEPT
9198
{
9299
// Handle stop notification (unavoidable test condition).
93100
if (!ping)
94101
{
95-
promise.set_value(ec);
102+
ping_promise.set_value(ec);
96103
return true;
97104
}
98105

@@ -104,6 +111,9 @@ BOOST_AUTO_TEST_CASE(broadcaster__notify__valid_nonced_ping__expected_notificati
104111
}, channel_id));
105112
});
106113

114+
// Wait on successful subscription.
115+
BOOST_REQUIRE(!sub_promise.get_future().get());
116+
107117
// Move vs. emplace required on some platforms, possibly due to default constructor.
108118
const auto ping = std::make_shared<messages::ping>(messages::ping{ expected_nonce });
109119
boost::asio::post(strand, [&]() NOEXCEPT
@@ -118,7 +128,7 @@ BOOST_AUTO_TEST_CASE(broadcaster__notify__valid_nonced_ping__expected_notificati
118128

119129
pool.stop();
120130
BOOST_REQUIRE(pool.join());
121-
BOOST_REQUIRE_EQUAL(promise.get_future().get(), expected_ec);
131+
BOOST_REQUIRE_EQUAL(ping_promise.get_future().get(), expected_ec);
122132
BOOST_REQUIRE(result);
123133
}
124134

0 commit comments

Comments
 (0)