Skip to content

Add exception #2

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
Mar 8, 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
10 changes: 5 additions & 5 deletions src/signalrclient/hub_connection_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,17 +613,17 @@ namespace signalr
{
if (connection->get_connection_state() == connection_state::connected)
{
auto error_msg = std::string("server timeout (")
.append(std::to_string(connection->m_signalr_client_config.get_server_timeout().count()))
.append(" ms) elapsed without receiving a message from the server.");
if (connection->m_logger.is_enabled(trace_level::warning))
{
connection->m_logger.log(trace_level::warning, std::string("server timeout (")
.append(std::to_string(timeNowmSeconds - connection->m_nextActivationServerTimeout.load()))
.append(" ms) elapsed without receiving a message from the server."));
connection->m_logger.log(trace_level::warning, error_msg);
}

connection->m_connection->stop([](std::exception_ptr)
{
///TODO:
}, nullptr);
}, std::make_exception_ptr(signalr_exception(error_msg)));
}
}

Expand Down
31 changes: 26 additions & 5 deletions test/signalrclienttests/hub_connection_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1740,7 +1740,7 @@ TEST(config, can_replace_scheduler)

// http_client->send (negotiate), websocket_client->start, handshake timeout timer, websocket_client->send, websocket_client->send, keep alive timer, websocket_client->send ping, websocket_client->stop
// handshake timeout timer can trigger more than once if test takes more than 1 second
ASSERT_GE(8, scheduler->schedule_count);
ASSERT_GE(scheduler->schedule_count, 8);
}

class throw_hub_protocol : public hub_protocol
Expand Down Expand Up @@ -1820,14 +1820,19 @@ TEST(keepalive, sends_ping_messages)
signalr_client_config config;
config.set_keepalive_interval(std::chrono::seconds(1));
config.set_server_timeout(std::chrono::seconds(3));
auto ping_mre = manual_reset_event<void>();
auto messages = std::make_shared<std::deque<std::string>>();
auto websocket_client = create_test_websocket_client(
/* send function */ [messages](const std::string& msg, std::function<void(std::exception_ptr)> callback)
/* send function */ [messages, &ping_mre](const std::string& msg, std::function<void(std::exception_ptr)> callback)
{
if (messages->size() < 3)
{
messages->push_back(msg);
}
if (messages->size() == 3)
{
ping_mre.set();
}
callback(nullptr);
},
[](const std::string&, std::function<void(std::exception_ptr)> callback) { callback(nullptr); },
Expand All @@ -1848,7 +1853,7 @@ TEST(keepalive, sends_ping_messages)

mre.get();

std::this_thread::sleep_for(config.get_keepalive_interval() + std::chrono::milliseconds(500));
ping_mre.get();

ASSERT_EQ(3, messages->size());
ASSERT_EQ("{\"protocol\":\"json\",\"version\":1}\x1e", (*messages)[0]);
Expand Down Expand Up @@ -1886,7 +1891,15 @@ TEST(keepalive, server_timeout_on_no_ping_from_server)

mre.get();

disconnect_mre.get();
try
{
disconnect_mre.get();
ASSERT_TRUE(false);
}
catch (const std::exception& ex)
{
ASSERT_STREQ("server timeout (1000 ms) elapsed without receiving a message from the server.", ex.what());
}
ASSERT_EQ(connection_state::disconnected, hub_connection.get_connection_state());
}

Expand Down Expand Up @@ -1922,6 +1935,14 @@ TEST(keepalive, resets_server_timeout_timer_on_any_message_from_server)
std::this_thread::sleep_for(std::chrono::seconds(1));
ASSERT_EQ(connection_state::connected, hub_connection.get_connection_state());

disconnect_mre.get();
try
{
disconnect_mre.get();
ASSERT_TRUE(false);
}
catch (const std::exception& ex)
{
ASSERT_STREQ("server timeout (1000 ms) elapsed without receiving a message from the server.", ex.what());
}
ASSERT_EQ(connection_state::disconnected, hub_connection.get_connection_state());
}