-
Notifications
You must be signed in to change notification settings - Fork 74
Add support for pinging #77
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0e71f02
Add keepalive interval and the server timeout.
6ec24f6
added tests for keep alive feature
1c652de
fixed using weak_ptrs and coding style
4cecd14
assert returned
8df7195
undo changes in README
6a46b05
fixed code formating
00635e4
fixed code formating
765c136
fixed code formating
0482fda
fixed code style
d2bbd01
fixup tests (#1)
BrennanConroy 8aa99e0
Add exception (#2)
BrennanConroy 86ebf61
Apply suggestions from code review
BrennanConroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1738,9 +1738,9 @@ TEST(config, can_replace_scheduler) | |
|
||
mre.get(); | ||
|
||
// http_client->send (negotiate), websocket_client->start, handshake timeout timer, websocket_client->send, websocket_client->send, websocket_client->stop | ||
// 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(6, scheduler->schedule_count); | ||
ASSERT_GE(scheduler->schedule_count, 8); | ||
} | ||
|
||
class throw_hub_protocol : public hub_protocol | ||
|
@@ -1814,3 +1814,135 @@ TEST(send, throws_if_protocol_fails) | |
|
||
ASSERT_EQ(connection_state::connected, hub_connection->get_connection_state()); | ||
} | ||
|
||
TEST(keepalive, sends_ping_messages) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should have a test that a ping isn't sent if other messages are being sent from the hub_connection. |
||
{ | ||
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, &ping_mre](const std::string& msg, std::function<void(std::exception_ptr)> callback) | ||
{ | ||
if (messages->size() < 3) | ||
gonzo-coder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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); }, | ||
[](std::function<void(std::exception_ptr)> callback) { callback(nullptr); }, | ||
false); | ||
auto hub_connection = create_hub_connection(websocket_client); | ||
hub_connection.set_client_config(config); | ||
|
||
auto mre = manual_reset_event<void>(); | ||
hub_connection.start([&mre](std::exception_ptr exception) | ||
{ | ||
mre.set(exception); | ||
}); | ||
|
||
ASSERT_FALSE(websocket_client->receive_loop_started.wait(5000)); | ||
ASSERT_FALSE(websocket_client->handshake_sent.wait(5000)); | ||
websocket_client->receive_message("{}\x1e"); | ||
|
||
mre.get(); | ||
|
||
ping_mre.get(); | ||
|
||
ASSERT_EQ(3, messages->size()); | ||
ASSERT_EQ("{\"protocol\":\"json\",\"version\":1}\x1e", (*messages)[0]); | ||
ASSERT_EQ("{\"type\":6}\x1e", (*messages)[1]); | ||
ASSERT_EQ("{\"type\":6}\x1e", (*messages)[2]); | ||
ASSERT_EQ(connection_state::connected, hub_connection.get_connection_state()); | ||
} | ||
|
||
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(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; | ||
|
||
auto disconnect_mre = manual_reset_event<void>(); | ||
hub_connection.set_disconnected([&disconnected_called, &disconnect_mre](std::exception_ptr ex) | ||
{ | ||
disconnect_mre.set(ex); | ||
}); | ||
|
||
auto mre = manual_reset_event<void>(); | ||
hub_connection.start([&mre](std::exception_ptr exception) | ||
{ | ||
mre.set(exception); | ||
}); | ||
|
||
ASSERT_FALSE(websocket_client->receive_loop_started.wait(5000)); | ||
ASSERT_FALSE(websocket_client->handshake_sent.wait(5000)); | ||
websocket_client->receive_message("{}\x1e"); | ||
|
||
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()); | ||
} | ||
|
||
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(1)); | ||
auto websocket_client = create_test_websocket_client(); | ||
auto hub_connection = create_hub_connection(websocket_client); | ||
hub_connection.set_client_config(config); | ||
|
||
auto disconnect_mre = manual_reset_event<void>(); | ||
hub_connection.set_disconnected([&disconnect_mre](std::exception_ptr ex) | ||
{ | ||
disconnect_mre.set(ex); | ||
}); | ||
|
||
auto mre = manual_reset_event<void>(); | ||
hub_connection.start([&mre](std::exception_ptr exception) | ||
{ | ||
mre.set(exception); | ||
}); | ||
|
||
ASSERT_FALSE(websocket_client->receive_loop_started.wait(5000)); | ||
ASSERT_FALSE(websocket_client->handshake_sent.wait(5000)); | ||
websocket_client->receive_message("{}\x1e"); | ||
|
||
mre.get(); | ||
|
||
std::this_thread::sleep_for(config.get_server_timeout() - std::chrono::milliseconds(500)); | ||
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()); | ||
|
||
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()); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.