Add more locks around assertions

Catch2 isn't currently thread safe, so if we hit one of these assertions
while some other thread is doing things such as logging we might
segfault.
This commit is contained in:
Jason Rhinelander 2020-03-21 11:27:01 -03:00
parent 3a120efb79
commit 98b1bd6930
4 changed files with 68 additions and 30 deletions

View File

@ -52,6 +52,7 @@ TEST_CASE("batching many small jobs", "[batch-many]") {
start_big_task(lmq);
auto sum = done.get_future().get();
auto lock = catch_lock();
REQUIRE( sum.first == 1337.0 );
REQUIRE( sum.second == 3 );
}
@ -76,6 +77,7 @@ TEST_CASE("batch exception propagation", "[batch-exceptions]") {
for (int i : {1, 2})
batch.add_job([i]() { if (i == 1) return 42; throw std::domain_error("bad value " + std::to_string(i)); });
batch.completion([&done_promise](auto results) {
auto lock = catch_lock();
REQUIRE( results.size() == 2 );
REQUIRE( results[0].get() == 42 );
REQUIRE_THROWS_MATCHES( results[1].get() == 0, std::domain_error, Message("bad value 2") );
@ -95,6 +97,7 @@ TEST_CASE("batch exception propagation", "[batch-exceptions]") {
throw std::domain_error("bad value " + std::to_string(i));
});
batch.completion([&done_promise,&forty_two](auto results) {
auto lock = catch_lock();
REQUIRE( results.size() == 2 );
auto& r = results[0].get();
REQUIRE( &r == &forty_two );
@ -111,6 +114,7 @@ TEST_CASE("batch exception propagation", "[batch-exceptions]") {
for (int i : {1, 2})
batch.add_job([i]() { if (i != 1) throw std::domain_error("bad value " + std::to_string(i)); });
batch.completion([&done_promise](auto results) {
auto lock = catch_lock();
REQUIRE( results.size() == 2 );
REQUIRE_NOTHROW( results[0].get() );
REQUIRE_THROWS_MATCHES( results[1].get(), std::domain_error, Message("bad value 2") );

View File

@ -55,25 +55,34 @@ TEST_CASE("basic commands", "[commands]") {
break;
std::this_thread::sleep_for(50ms);
}
REQUIRE( connected.load() );
REQUIRE( i <= 1 ); // should be fast
REQUIRE( !failed.load() );
REQUIRE( to_hex(pubkey) == to_hex(server.get_pubkey()) );
{
auto lock = catch_lock();
REQUIRE( connected.load() );
REQUIRE( i <= 1 ); // should be fast
REQUIRE( !failed.load() );
REQUIRE( to_hex(pubkey) == to_hex(server.get_pubkey()) );
}
client.send(c, "public.hello");
client.send(c, "public.client.pubkey");
std::this_thread::sleep_for(50ms);
REQUIRE( hellos == 1 );
REQUIRE( his == 1 );
REQUIRE( to_hex(client_pubkey) == to_hex(client.get_pubkey()) );
{
auto lock = catch_lock();
REQUIRE( hellos == 1 );
REQUIRE( his == 1 );
REQUIRE( to_hex(client_pubkey) == to_hex(client.get_pubkey()) );
}
for (int i = 0; i < 50; i++)
client.send(c, "public.hello");
std::this_thread::sleep_for(100ms);
REQUIRE( hellos == 51 );
REQUIRE( his == 26 );
{
auto lock = catch_lock();
REQUIRE( hellos == 51 );
REQUIRE( his == 26 );
}
}
TEST_CASE("outgoing auth level", "[commands][auth]") {
@ -117,7 +126,10 @@ TEST_CASE("outgoing auth level", "[commands][auth]") {
client.send(public_c, "public.reflect", "public.hi");
std::this_thread::sleep_for(50ms);
REQUIRE( public_hi == 1 );
{
auto lock = catch_lock();
REQUIRE( public_hi == 1 );
}
client.send(basic_c, "public.reflect", "basic.hi");
client.send(admin_c, "public.reflect", "admin.hi");
@ -127,9 +139,12 @@ TEST_CASE("outgoing auth level", "[commands][auth]") {
client.send(basic_c, "public.reflect", "basic.hi");
std::this_thread::sleep_for(50ms);
REQUIRE( admin_hi == 3 );
REQUIRE( basic_hi == 2 );
REQUIRE( public_hi == 2 );
{
auto lock = catch_lock();
REQUIRE( admin_hi == 3 );
REQUIRE( basic_hi == 2 );
REQUIRE( public_hi == 2 );
}
admin_hi = 0;
basic_hi = 0;
@ -146,6 +161,7 @@ TEST_CASE("outgoing auth level", "[commands][auth]") {
client.send(admin_c, "public.reflect", "public.hi");
std::this_thread::sleep_for(50ms);
auto lock = catch_lock();
REQUIRE( admin_hi == 1 );
REQUIRE( basic_hi == 2 );
REQUIRE( public_hi == 3 );

View File

@ -39,13 +39,17 @@ TEST_CASE("connections with curve authentication", "[curve][connect]") {
break;
std::this_thread::sleep_for(50ms);
}
REQUIRE( i <= 1 );
REQUIRE( connected.load() );
{
auto lock = catch_lock();
REQUIRE( i <= 1 );
REQUIRE( connected.load() );
}
bool success = false;
std::vector<std::string> parts;
client.request(server_conn, "public.hello", [&](auto success_, auto parts_) { success = success_; parts = parts_; });
std::this_thread::sleep_for(50ms);
auto lock = catch_lock();
REQUIRE( success );
}
@ -79,6 +83,7 @@ TEST_CASE("self-connection SN optimization", "[connect][self]") {
std::this_thread::sleep_for(50ms);
sn.send(pubkey, "a.b", "my data");
std::this_thread::sleep_for(50ms);
auto lock = catch_lock();
REQUIRE(invoked);
}
@ -111,16 +116,18 @@ TEST_CASE("plain-text connections", "[plaintext][connect]") {
break;
std::this_thread::sleep_for(50ms);
}
REQUIRE( i <= 1 );
REQUIRE( connected.load() );
{
auto lock = catch_lock();
REQUIRE( i <= 1 );
REQUIRE( connected.load() );
}
bool success = false;
std::vector<std::string> parts;
client.request(c, "public.hello", [&](auto success_, auto parts_) { success = success_; parts = parts_; });
std::this_thread::sleep_for(50ms);
auto lock = catch_lock();
REQUIRE( success );
}
TEST_CASE("SN disconnections", "[connect][disconnect]") {
@ -162,5 +169,6 @@ TEST_CASE("SN disconnections", "[connect][disconnect]") {
lmq[0]->send(pubkey[2], "sn.hi");
std::this_thread::sleep_for(50ms);
auto lock = catch_lock();
REQUIRE(his == 5);
}

View File

@ -42,10 +42,13 @@ TEST_CASE("basic requests", "[requests]") {
break;
std::this_thread::sleep_for(50ms);
}
REQUIRE( connected.load() );
REQUIRE( !failed.load() );
REQUIRE( i <= 1 );
REQUIRE( to_hex(pubkey) == to_hex(server.get_pubkey()) );
{
auto lock = catch_lock();
REQUIRE( connected.load() );
REQUIRE( !failed.load() );
REQUIRE( i <= 1 );
REQUIRE( to_hex(pubkey) == to_hex(server.get_pubkey()) );
}
std::atomic<bool> got_reply{false};
bool success;
@ -57,6 +60,7 @@ TEST_CASE("basic requests", "[requests]") {
});
std::this_thread::sleep_for(50ms);
auto lock = catch_lock();
REQUIRE( got_reply.load() );
REQUIRE( success );
REQUIRE( data == std::vector<std::string>{{"123"}} );
@ -100,10 +104,13 @@ TEST_CASE("request from server to client", "[requests]") {
break;
std::this_thread::sleep_for(50ms);
}
REQUIRE( connected.load() );
REQUIRE( !failed.load() );
REQUIRE( i <= 1 );
REQUIRE( to_hex(pubkey) == to_hex(server.get_pubkey()) );
{
auto lock = catch_lock();
REQUIRE( connected.load() );
REQUIRE( !failed.load() );
REQUIRE( i <= 1 );
REQUIRE( to_hex(pubkey) == to_hex(server.get_pubkey()) );
}
std::atomic<bool> got_reply{false};
bool success;
@ -115,7 +122,10 @@ TEST_CASE("request from server to client", "[requests]") {
});
std::this_thread::sleep_for(50ms);
REQUIRE( got_reply.load() );
REQUIRE( success );
REQUIRE( data == std::vector<std::string>{{"123"}} );
{
auto lock = catch_lock();
REQUIRE( got_reply.load() );
REQUIRE( success );
REQUIRE( data == std::vector<std::string>{{"123"}} );
}
}