Skip to content

Commit

Permalink
[tests] Added +/-30 ms confidence interval for connection timeout check
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsharabayko authored and rndi committed Feb 6, 2019
1 parent 795efd2 commit 785e2ca
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/test_connection_timeout.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <gtest/gtest.h>
#include <chrono>

#ifdef _WIN32
#define _WINSOCKAPI_ // to include Winsock2.h instead of Winsock.h from windows.h
Expand All @@ -17,6 +18,16 @@ extern "C" {
#include "srt.h"


/**
* The test creates a socket and tries to connect to a localhost port 5555
* in a non-blocking mode. This means we wait on epoll for a notification
* about SRT_EPOLL_OUT | SRT_EPOLL_ERR events on the socket calling srt_epoll_wait(...).
* The test expects a connection timeout to happen within the time,
* set with SRTO_CONNTIMEO (500 ms).
* The expected behavior is to return from srt_epoll_wait(...)
*
* @remarks Inspired by Max Tomilov (maxtomilov) in issue #468
*/
TEST(Core, ConnectionTimeout) {
ASSERT_EQ(srt_startup(), 0);

Expand Down Expand Up @@ -64,6 +75,9 @@ TEST(Core, ConnectionTimeout) {
int wlen = 2;
SRTSOCKET write[2];

using namespace std;
const chrono::steady_clock::time_point chrono_ts_start = chrono::steady_clock::now();

// Here we check the connection timeout.
// Epoll timeout is set 100 ms greater than socket's TTL
EXPECT_EQ(srt_epoll_wait(pollid, read, &rlen,
Expand All @@ -74,6 +88,13 @@ TEST(Core, ConnectionTimeout) {
* sockets with exceptions are returned to both read and write sets.
*/
, 2);
// Check the actual timeout
const chrono::steady_clock::time_point chrono_ts_end = chrono::steady_clock::now();
const auto delta_ms = chrono::duration_cast<chrono::milliseconds>(chrono_ts_end - chrono_ts_start).count();
// Confidence interval border : +/-30 ms
EXPECT_LT(delta_ms, connection_timeout_ms + 30);
EXPECT_GT(delta_ms, connection_timeout_ms - 30);
cerr << "Timeout was: " << delta_ms << "\n";

EXPECT_EQ(rlen, 1);
EXPECT_EQ(read[0], client_sock);
Expand Down

0 comments on commit 785e2ca

Please sign in to comment.