From dad2b64e15a4aaac75ca62bc46dcfd0349fd103a Mon Sep 17 00:00:00 2001 From: Kumamon38 Date: Mon, 13 May 2019 18:08:46 +0200 Subject: [PATCH] save timepoints after connect and not in contructor, adjusted tests (#72) * save timepoints after connect and not in contructor, adjusted tests * move call into setReadyState * more time to detect client close in test --- ixwebsocket/IXWebSocketTransport.cpp | 26 +++++++++++++++++++++----- ixwebsocket/IXWebSocketTransport.h | 2 ++ test/IXWebSocketPingTest.cpp | 8 ++++---- test/IXWebSocketPingTimeoutTest.cpp | 6 +++--- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 503b0c91..415b6360 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -134,11 +134,6 @@ namespace ix { _pingIntervalOrTimeoutGCDSecs = pingIntervalSecs; } - - if (_pingIntervalOrTimeoutGCDSecs > 0) - { - _nextGCDTimePoint = std::chrono::steady_clock::now() + std::chrono::seconds(_pingIntervalOrTimeoutGCDSecs); - } } // Client @@ -225,6 +220,10 @@ namespace ix _closeWireSize = 0; _closeRemote = false; } + else if (readyState == ReadyState::OPEN) + { + initTimePointsAndGCDAfterConnect(); + } _readyState = readyState; } @@ -234,6 +233,23 @@ namespace ix _onCloseCallback = onCloseCallback; } + void WebSocketTransport::initTimePointsAndGCDAfterConnect() + { + { + std::lock_guard lock(_lastSendPingTimePointMutex); + _lastSendPingTimePoint = std::chrono::steady_clock::now(); + } + { + std::lock_guard lock(_lastReceivePongTimePointMutex); + _lastReceivePongTimePoint = std::chrono::steady_clock::now(); + } + + if (_pingIntervalOrTimeoutGCDSecs > 0) + { + _nextGCDTimePoint = std::chrono::steady_clock::now() + std::chrono::seconds(_pingIntervalOrTimeoutGCDSecs); + } + } + // Only consider send PING time points for that computation. bool WebSocketTransport::pingIntervalExceeded() { diff --git a/ixwebsocket/IXWebSocketTransport.h b/ixwebsocket/IXWebSocketTransport.h index db569967..22e87c45 100644 --- a/ixwebsocket/IXWebSocketTransport.h +++ b/ixwebsocket/IXWebSocketTransport.h @@ -220,6 +220,8 @@ namespace ix // after calling close(), if no CLOSE frame answer is received back from the remote, we should close the connexion bool closingDelayExceeded(); + void initTimePointsAndGCDAfterConnect(); + void sendCloseFrame(uint16_t code, const std::string& reason); void closeSocketAndSwitchToClosedState(uint16_t code, diff --git a/test/IXWebSocketPingTest.cpp b/test/IXWebSocketPingTest.cpp index c30a99bd..cfcfabdd 100644 --- a/test/IXWebSocketPingTest.cpp +++ b/test/IXWebSocketPingTest.cpp @@ -413,13 +413,13 @@ TEST_CASE("Websocket_ping_no_data_sent_setHeartBeatPeriod", "[setHeartBeatPeriod REQUIRE(server.getClients().size() == 1); - ix::msleep(1850); + ix::msleep(1900); webSocketClient.stop(); // Here we test ping interval - // -> expected ping messages == 1 as 1850 seconds, 1 ping sent every second + // -> expected ping messages == 1 as 1900 seconds, 1 ping sent every second REQUIRE(serverReceivedPingMessages == 1); // Give us 500ms for the server to notice that clients went away @@ -460,7 +460,7 @@ TEST_CASE("Websocket_ping_data_sent_setHeartBeatPeriod", "[setHeartBeatPeriod]") webSocketClient.sendMessage("hello world"); ix::msleep(900); webSocketClient.sendMessage("hello world"); - ix::msleep(900); + ix::msleep(1100); webSocketClient.stop(); @@ -469,7 +469,7 @@ TEST_CASE("Websocket_ping_data_sent_setHeartBeatPeriod", "[setHeartBeatPeriod]") // Here we test ping interval // client has sent data, but ping should have been sent no matter what - // -> expected ping messages == 2 as 900+900+900 = 2700 seconds, 1 ping sent every second + // -> expected ping messages == 2 as 900+900+1100 = 2900 seconds, 1 ping sent every second REQUIRE(serverReceivedPingMessages == 2); // Give us 500ms for the server to notice that clients went away diff --git a/test/IXWebSocketPingTimeoutTest.cpp b/test/IXWebSocketPingTimeoutTest.cpp index 4b744b20..d7eaf8e0 100644 --- a/test/IXWebSocketPingTimeoutTest.cpp +++ b/test/IXWebSocketPingTimeoutTest.cpp @@ -350,7 +350,7 @@ TEST_CASE("Websocket_no_ping_but_timeout", "[setPingTimeout]") REQUIRE(server.getClients().size() == 1); - ix::msleep(2700); + ix::msleep(2900); // Here we test ping timeout, no timeout yet REQUIRE(serverReceivedPingMessages == 0); @@ -359,7 +359,7 @@ TEST_CASE("Websocket_no_ping_but_timeout", "[setPingTimeout]") REQUIRE(webSocketClient.isClosed() == false); REQUIRE(webSocketClient.closedDueToPingTimeout() == false); - ix::msleep(400); + ix::msleep(200); // Here we test ping timeout, timeout REQUIRE(serverReceivedPingMessages == 0); @@ -410,7 +410,7 @@ TEST_CASE("Websocket_ping_timeout", "[setPingTimeout]") REQUIRE(serverReceivedPingMessages == 1); REQUIRE(webSocketClient.getReceivedPongMessages() == 0); - ix::msleep(1000); + ix::msleep(1100); // Here we test ping timeout, timeout REQUIRE(serverReceivedPingMessages == 1);