Skip to content

Commit

Permalink
network: add additional tests for exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasDebrunner committed Feb 29, 2024
1 parent a2e5a3f commit 75e8cef
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion tests/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,24 @@ TEST_CASE("Create network runtime") {
CHECK_THROWS_AS(auto message = connection->receive(expectation, 100), TimeoutException);
}

SUBCASE("Receive throws a NetworkError if the interface fails") {
SUBCASE("Receive throws a NetworkError if the interface fails, error callback gets called") {
interface.reset();

// add a callback using the callback API. The error should then call the error callback
auto error_callback_called_promise = std::promise<void>();
connection->addMessageCallback([](const Message &message) {
// do nothing
}, [&error_callback_called_promise](const std::exception_ptr& exception) {
error_callback_called_promise.set_value();
CHECK_THROWS_AS(std::rethrow_exception(exception), NetworkError);
});

auto expectation = connection->expect("TEST_MESSAGE");
interface.makeFailOnNextReceive();
// Receive on the sync api. The receive should then throw an exception
CHECK_THROWS_AS(auto message = connection->receive(expectation), NetworkError);
CHECK((error_callback_called_promise.get_future().wait_for(std::chrono::seconds(2)) != std::future_status::timeout));
connection->removeAllCallbacks();
}

SUBCASE("Connection recycled on recover after fail") {
Expand Down Expand Up @@ -266,6 +279,22 @@ TEST_CASE("Create network runtime") {
CHECK(found);
}

SUBCASE("Correct callback called when message is received") {
interface.reset();
std::promise<void> callback_called_promise;
auto callback_called_future = callback_called_promise.get_future();

connection->addMessageCallback([&callback_called_promise](const Message &message) {
if (message.name() == "TEST_MESSAGE") {
callback_called_promise.set_value();
}
});

interface.addToReceiveQueue("\xfd\x10\x00\x00\x01\x61\x61\xbc\x26\x00\x2a\x00\x00\x00\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21\x53\xd9"s, interface_partner);
CHECK((callback_called_future.wait_for(std::chrono::seconds(2)) != std::future_status::timeout));
connection->removeAllCallbacks();
}

SUBCASE("Callbacks are cleaned up on receive timeout") {
interface.reset();
for (int i = 0; i < 10; i++) {
Expand Down

0 comments on commit 75e8cef

Please sign in to comment.