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
This commit is contained in:
		
				
					committed by
					
						
						Benjamin Sergeant
					
				
			
			
				
	
			
			
			
						parent
						
							22a806ca6f
						
					
				
				
					commit
					4b96632a69
				
			@@ -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<std::mutex> lock(_lastSendPingTimePointMutex);
 | 
			
		||||
            _lastSendPingTimePoint = std::chrono::steady_clock::now();
 | 
			
		||||
        } 
 | 
			
		||||
        {
 | 
			
		||||
            std::lock_guard<std::mutex> 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()
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user