Skip to content

Commit

Permalink
catch errors arising from std::future::set_exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Gehorsam committed Nov 17, 2023
1 parent 392fe51 commit b6b7648
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
9 changes: 7 additions & 2 deletions core/core-rt/NRtClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,16 @@ void NRtClient::onTransportError(const std::string& description)
_listener->onError(error);
}

bool futureCompleted = _connectPromise->get_future().wait_for(std::chrono::seconds(0)) == std::future_status::ready;
if (!futureCompleted)
try
{
_connectPromise->set_exception(std::make_exception_ptr<NRtException>(NRtException(NRtError(RtErrorCode::CONNECT_ERROR, "An error occurred while connecting."))));
}
catch
{
// expected to throw an exception if we are already completed. no way to check if std::future is completed, it cannot be double-retrieved with get_future().
}


}

void NRtClient::onTransportMessage(const NBytes & data)
Expand Down
10 changes: 10 additions & 0 deletions test/src/realtime/test_lifecycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,15 @@ namespace Nakama {

test.stopTest(connected);
}

void test_connectivity_loss()
{
bool threadedTick = true;
NTest test(__func__, threadedTick);
test.setTestTimeoutMs(60 * 1000);
test.runTest();
NSessionPtr session = test.client->authenticateDeviceAsync("mytestdevice0001", opt::nullopt, opt::nullopt, {}).get();
test.rtClient->connect(session, true);
}
}
}
4 changes: 4 additions & 0 deletions test/src/realtime/test_realtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void test_rt_reconnect();
void test_rt_connect_callback();
void test_rt_double_connect();
void test_rt_double_connect_async();
void test_connectivity_loss();

void run_realtime_tests()
{
Expand All @@ -58,6 +59,9 @@ void test_realtime()
test_rt_double_connect();
test_rt_double_connect_async();

// optional "test". run websocket for a full minute. useful for testing connection loss with network link conditioner.
// test_connectivity_loss();

/// change to 10 iterations to trigger https://github.com/microsoft/libHttpClient/issues/698 bug
for (int i = 0; i < 1; i++) {
test_rt_reconnect();
Expand Down

0 comments on commit b6b7648

Please sign in to comment.