Skip to content

Commit

Permalink
Test socket boot on simultaneous connect with session.single_socket=t…
Browse files Browse the repository at this point in the history
…rue (#153)
  • Loading branch information
Luke Gehorsam authored Jan 26, 2024
1 parent 5e68c4a commit 98cce48
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion test/src/realtime/test_chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace Nakama {
NSessionPtr session = test.client->authenticateCustomAsync(TestGuid::newGuid(), std::string(), true).get();
bool createStatus = false;
test.rtClient->connectAsync(session, createStatus, NTest::RtProtocol).get();
const NGroup group = test.client->createGroupAsync(session, "group chat " + session->getAuthToken(), "a group for chatting", "", "", false, {}).get();
const NGroup group = test.client->createGroupAsync(session, TestGuid::newGuid(), "a group for chatting", "", "", false, {}).get();
const NChannelPtr channelPtr = test.rtClient->joinChatAsync(group.id, NChannelType::GROUP, {}, {}).get();
test.stopTest(true);
}
Expand Down
45 changes: 45 additions & 0 deletions test/src/realtime/test_lifecycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,51 @@ namespace Nakama {
test.stopTest(connected);
}

// requires session.single_socket to be true.
void test_rt_simultaneous_connect()
{
bool threadedTick = true;

NTest test(__func__, threadedTick);
test.runTest();

NSessionPtr session = test.client->authenticateCustomAsync(TestGuid::newGuid(), std::string(), true).get();

test.rtClient->connectAsync(session, true).get();

bool disconnected = false;
std::mutex disconnectedMtx;
std::condition_variable disconnectedCV;

test.listener.setDisconnectCallback([&](const NRtClientDisconnectInfo& info) {
std::unique_lock<std::mutex> lock(disconnectedMtx);
disconnected = true;
disconnectedCV.notify_one();
});

std::atomic<bool> rtClient2Tick(true);
auto rtClient2 = test.client->createRtClient();
rtClient2->connect(session, true);

std::thread tickThread([&]() {
while (rtClient2Tick.load()) {
rtClient2->tick();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
});

{
std::unique_lock<std::mutex> lock(disconnectedMtx);
disconnectedCV.wait(lock, [&](){ return disconnected; }); // Wait until rtClient1 disconnects due to new connect.
}

std::this_thread::sleep_for(std::chrono::milliseconds(2000)); // see if any tick() from rtClient2 causes exceptions to get thrown.
rtClient2Tick.store(false);
tickThread.join();

test.stopTest(true);
}

void test_connectivity_loss()
{
bool threadedTick = true;
Expand Down
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,8 @@ void test_rt_reconnect();
void test_rt_connect_callback();
void test_rt_double_connect();
void test_rt_double_connect_async();
void test_rt_simultaneous_connect();

void test_connectivity_loss();

void run_realtime_tests()
Expand All @@ -58,6 +60,8 @@ void test_realtime()
test_rt_connect_callback();
test_rt_double_connect();
test_rt_double_connect_async();
// optional test. requires session.single_socket to be true in server configuration.
// test_rt_simultaneous_connect();

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

0 comments on commit 98cce48

Please sign in to comment.