Skip to content

Commit 8aa99e0

Browse files
Add exception (#2)
1 parent d2bbd01 commit 8aa99e0

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/signalrclient/hub_connection_impl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -613,17 +613,17 @@ namespace signalr
613613
{
614614
if (connection->get_connection_state() == connection_state::connected)
615615
{
616+
auto error_msg = std::string("server timeout (")
617+
.append(std::to_string(connection->m_signalr_client_config.get_server_timeout().count()))
618+
.append(" ms) elapsed without receiving a message from the server.");
616619
if (connection->m_logger.is_enabled(trace_level::warning))
617620
{
618-
connection->m_logger.log(trace_level::warning, std::string("server timeout (")
619-
.append(std::to_string(timeNowmSeconds - connection->m_nextActivationServerTimeout.load()))
620-
.append(" ms) elapsed without receiving a message from the server."));
621+
connection->m_logger.log(trace_level::warning, error_msg);
621622
}
622623

623624
connection->m_connection->stop([](std::exception_ptr)
624625
{
625-
///TODO:
626-
}, nullptr);
626+
}, std::make_exception_ptr(signalr_exception(error_msg)));
627627
}
628628
}
629629

test/signalrclienttests/hub_connection_tests.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,7 +1740,7 @@ TEST(config, can_replace_scheduler)
17401740

17411741
// 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
17421742
// handshake timeout timer can trigger more than once if test takes more than 1 second
1743-
ASSERT_GE(8, scheduler->schedule_count);
1743+
ASSERT_GE(scheduler->schedule_count, 8);
17441744
}
17451745

17461746
class throw_hub_protocol : public hub_protocol
@@ -1820,14 +1820,19 @@ TEST(keepalive, sends_ping_messages)
18201820
signalr_client_config config;
18211821
config.set_keepalive_interval(std::chrono::seconds(1));
18221822
config.set_server_timeout(std::chrono::seconds(3));
1823+
auto ping_mre = manual_reset_event<void>();
18231824
auto messages = std::make_shared<std::deque<std::string>>();
18241825
auto websocket_client = create_test_websocket_client(
1825-
/* send function */ [messages](const std::string& msg, std::function<void(std::exception_ptr)> callback)
1826+
/* send function */ [messages, &ping_mre](const std::string& msg, std::function<void(std::exception_ptr)> callback)
18261827
{
18271828
if (messages->size() < 3)
18281829
{
18291830
messages->push_back(msg);
18301831
}
1832+
if (messages->size() == 3)
1833+
{
1834+
ping_mre.set();
1835+
}
18311836
callback(nullptr);
18321837
},
18331838
[](const std::string&, std::function<void(std::exception_ptr)> callback) { callback(nullptr); },
@@ -1848,7 +1853,7 @@ TEST(keepalive, sends_ping_messages)
18481853

18491854
mre.get();
18501855

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

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

18871892
mre.get();
18881893

1889-
disconnect_mre.get();
1894+
try
1895+
{
1896+
disconnect_mre.get();
1897+
ASSERT_TRUE(false);
1898+
}
1899+
catch (const std::exception& ex)
1900+
{
1901+
ASSERT_STREQ("server timeout (1000 ms) elapsed without receiving a message from the server.", ex.what());
1902+
}
18901903
ASSERT_EQ(connection_state::disconnected, hub_connection.get_connection_state());
18911904
}
18921905

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

1925-
disconnect_mre.get();
1938+
try
1939+
{
1940+
disconnect_mre.get();
1941+
ASSERT_TRUE(false);
1942+
}
1943+
catch (const std::exception& ex)
1944+
{
1945+
ASSERT_STREQ("server timeout (1000 ms) elapsed without receiving a message from the server.", ex.what());
1946+
}
19261947
ASSERT_EQ(connection_state::disconnected, hub_connection.get_connection_state());
19271948
}

0 commit comments

Comments
 (0)