Skip to content

Fixup tests #1

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

Merged
merged 1 commit into from
Feb 22, 2022
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
28 changes: 15 additions & 13 deletions test/signalrclienttests/hub_connection_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1829,7 +1829,10 @@ TEST(keepalive, sends_ping_messages)
messages->push_back(msg);
}
callback(nullptr);
});
},
[](const std::string&, std::function<void(std::exception_ptr)> callback) { callback(nullptr); },
[](std::function<void(std::exception_ptr)> callback) { callback(nullptr); },
false);
auto hub_connection = create_hub_connection(websocket_client);
hub_connection.set_client_config(config);

Expand Down Expand Up @@ -1858,15 +1861,17 @@ TEST(keepalive, server_timeout_on_no_ping_from_server)
{
signalr_client_config config;
config.set_keepalive_interval(std::chrono::seconds(1));
config.set_server_timeout(std::chrono::seconds(3));
config.set_server_timeout(std::chrono::seconds(1));
auto websocket_client = create_test_websocket_client();
auto hub_connection = create_hub_connection(websocket_client);
hub_connection.set_client_config(config);

auto disconnected_called = false;
hub_connection.set_disconnected([&disconnected_called](std::exception_ptr ex)

auto disconnect_mre = manual_reset_event<void>();
hub_connection.set_disconnected([&disconnected_called, &disconnect_mre](std::exception_ptr ex)
{
disconnected_called = true;
disconnect_mre.set(ex);
});

auto mre = manual_reset_event<void>();
Expand All @@ -1881,24 +1886,23 @@ TEST(keepalive, server_timeout_on_no_ping_from_server)

mre.get();

std::this_thread::sleep_for(config.get_server_timeout() + std::chrono::milliseconds(500));
disconnect_mre.get();
ASSERT_EQ(connection_state::disconnected, hub_connection.get_connection_state());
ASSERT_TRUE(disconnected_called);
}

TEST(keepalive, resets_server_timeout_timer_on_any_message_from_server)
{
signalr_client_config config;
config.set_keepalive_interval(std::chrono::seconds(1));
config.set_server_timeout(std::chrono::seconds(3));
config.set_server_timeout(std::chrono::seconds(1));
auto websocket_client = create_test_websocket_client();
auto hub_connection = create_hub_connection(websocket_client);
hub_connection.set_client_config(config);

auto disconnected_called = false;
hub_connection.set_disconnected([&disconnected_called](std::exception_ptr ex)
auto disconnect_mre = manual_reset_event<void>();
hub_connection.set_disconnected([&disconnect_mre](std::exception_ptr ex)
{
disconnected_called = true;
disconnect_mre.set(ex);
});

auto mre = manual_reset_event<void>();
Expand All @@ -1917,9 +1921,7 @@ TEST(keepalive, resets_server_timeout_timer_on_any_message_from_server)
websocket_client->receive_message("{\"type\":6}\x1e");
std::this_thread::sleep_for(std::chrono::seconds(1));
ASSERT_EQ(connection_state::connected, hub_connection.get_connection_state());
ASSERT_FALSE(disconnected_called);

std::this_thread::sleep_for(config.get_server_timeout() + std::chrono::milliseconds(500));
disconnect_mre.get();
ASSERT_EQ(connection_state::disconnected, hub_connection.get_connection_state());
ASSERT_TRUE(disconnected_called);
}
14 changes: 11 additions & 3 deletions test/signalrclienttests/test_websocket_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
std::shared_ptr<test_websocket_client> create_test_websocket_client(
std::function<void(const std::string & msg, std::function<void(std::exception_ptr)>)> send_function,
std::function<void(const std::string&, std::function<void(std::exception_ptr)>)> connect_function,
std::function<void(std::function<void(std::exception_ptr)>)> close_function)
std::function<void(std::function<void(std::exception_ptr)>)> close_function,
bool ignore_pings)
{
auto websocket_client = std::make_shared<test_websocket_client>();
websocket_client->set_send_function(send_function);
websocket_client->set_connect_function(connect_function);
websocket_client->set_close_function(close_function);
websocket_client->ignore_pings = ignore_pings;

return websocket_client;
}
Expand All @@ -24,7 +26,7 @@ test_websocket_client::test_websocket_client()
: m_connect_function(std::make_shared<std::function<void(const std::string&, std::function<void(std::exception_ptr)>)>>([](const std::string&, std::function<void(std::exception_ptr)> callback) { callback(nullptr); })),
m_send_function(std::make_shared<std::function<void(const std::string& msg, std::function<void(std::exception_ptr)>)>>([](const std::string msg, std::function<void(std::exception_ptr)> callback) { callback(nullptr); })),
m_close_function(std::make_shared<std::function<void(std::function<void(std::exception_ptr)>)>>([](std::function<void(std::exception_ptr)> callback) { callback(nullptr); })),
m_receive_message_event(), m_receive_message(), m_stopped(true), receive_count(0)
m_receive_message_event(), m_receive_message(), m_stopped(true), receive_count(0), ignore_pings(true)
{
m_receive_loop_not_running.cancel();
}
Expand Down Expand Up @@ -107,8 +109,14 @@ void test_websocket_client::send(const std::string& payload, signalr::transfer_f
{
handshake_sent.cancel();
auto local_copy = m_send_function;
m_scheduler->schedule([payload, callback, local_copy]()
auto l_ignore_pings = ignore_pings;
m_scheduler->schedule([payload, callback, local_copy, l_ignore_pings]()
{
if (l_ignore_pings && payload.find("\"type\":6") != -1)
{
callback(nullptr);
return;
}
(*local_copy)(payload, callback);
});
}
Expand Down
3 changes: 2 additions & 1 deletion test/signalrclienttests/test_websocket_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class test_websocket_client : public websocket_client
cancellation_token_source receive_loop_started;
cancellation_token_source handshake_sent;
int receive_count;
bool ignore_pings;

private:
std::shared_ptr<std::function<void(const std::string&, std::function<void(std::exception_ptr)>)>> m_connect_function;
Expand All @@ -63,4 +64,4 @@ class test_websocket_client : public websocket_client
std::shared_ptr<test_websocket_client> create_test_websocket_client(
std::function<void(const std::string & msg, std::function<void(std::exception_ptr)>)> send_function = [](const std::string&, std::function<void(std::exception_ptr)> callback) { callback(nullptr); },
std::function<void(const std::string&, std::function<void(std::exception_ptr)>)> connect_function = [](const std::string&, std::function<void(std::exception_ptr)> callback) { callback(nullptr); },
std::function<void(std::function<void(std::exception_ptr)>)> close_function = [](std::function<void(std::exception_ptr)> callback) { callback(nullptr); });
std::function<void(std::function<void(std::exception_ptr)>)> close_function = [](std::function<void(std::exception_ptr)> callback) { callback(nullptr); }, bool ignore_pings = true);