Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test socket boot on simultaneous connect with session.single_socket=true #153

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a failing test due to a length constraint placed on group names.

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
Loading