@@ -1740,7 +1740,7 @@ TEST(config, can_replace_scheduler)
1740
1740
1741
1741
// 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
1742
1742
// 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 );
1744
1744
}
1745
1745
1746
1746
class throw_hub_protocol : public hub_protocol
@@ -1820,14 +1820,19 @@ TEST(keepalive, sends_ping_messages)
1820
1820
signalr_client_config config;
1821
1821
config.set_keepalive_interval (std::chrono::seconds (1 ));
1822
1822
config.set_server_timeout (std::chrono::seconds (3 ));
1823
+ auto ping_mre = manual_reset_event<void >();
1823
1824
auto messages = std::make_shared<std::deque<std::string>>();
1824
1825
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)
1826
1827
{
1827
1828
if (messages->size () < 3 )
1828
1829
{
1829
1830
messages->push_back (msg);
1830
1831
}
1832
+ if (messages->size () == 3 )
1833
+ {
1834
+ ping_mre.set ();
1835
+ }
1831
1836
callback (nullptr );
1832
1837
},
1833
1838
[](const std::string&, std::function<void (std::exception_ptr)> callback) { callback (nullptr ); },
@@ -1848,7 +1853,7 @@ TEST(keepalive, sends_ping_messages)
1848
1853
1849
1854
mre.get ();
1850
1855
1851
- std::this_thread::sleep_for (config. get_keepalive_interval () + std::chrono::milliseconds ( 500 ) );
1856
+ ping_mre. get ( );
1852
1857
1853
1858
ASSERT_EQ (3 , messages->size ());
1854
1859
ASSERT_EQ (" {\" protocol\" :\" json\" ,\" version\" :1}\x1e " , (*messages)[0 ]);
@@ -1886,7 +1891,15 @@ TEST(keepalive, server_timeout_on_no_ping_from_server)
1886
1891
1887
1892
mre.get ();
1888
1893
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
+ }
1890
1903
ASSERT_EQ (connection_state::disconnected, hub_connection.get_connection_state ());
1891
1904
}
1892
1905
@@ -1922,6 +1935,14 @@ TEST(keepalive, resets_server_timeout_timer_on_any_message_from_server)
1922
1935
std::this_thread::sleep_for (std::chrono::seconds (1 ));
1923
1936
ASSERT_EQ (connection_state::connected, hub_connection.get_connection_state ());
1924
1937
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
+ }
1926
1947
ASSERT_EQ (connection_state::disconnected, hub_connection.get_connection_state ());
1927
1948
}
0 commit comments