Skip to content

Commit e996139

Browse files
committed
* randomize test port usage
1 parent 4458e97 commit e996139

File tree

6 files changed

+13
-11
lines changed

6 files changed

+13
-11
lines changed

src/server.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ class server {
283283
close_and_free();
284284
continue;
285285
}
286+
_listener_sock->set_reuseaddr(true);
286287
_listener_sock->set_nonblocking(true); // we want to be able to join
287288
int32_t res = ::bind(_listener_sock->get_raw().value(), res_addrinfo->ai_addr, res_addrinfo->ai_addrlen);
288289
if (res < 0) {

tests/main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#define DOCTEST_CONFIG_IMPLEMENT
2+
#include <random>
23
#include "../doctest/doctest/doctest.h"
34

5+
// generate random port for server, which gets used in all tests
6+
std::random_device random_device;
7+
std::mt19937 gen(random_device());
8+
std::uniform_int_distribution<> distr(10000, 65000);
9+
10+
uint16_t test_port = distr(gen);
11+
412
int main(int argc, char **argv)
513
{
614
doctest::Context context;

tests/test_client_server.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ using namespace std::chrono_literals;
1010
static const std::string hello_msg = "hello!";
1111
static const std::vector<uint8_t> client_message = {1, 2, 3};
1212

13-
std::random_device random_device;
14-
std::mt19937 gen(random_device());
15-
std::uniform_int_distribution<> distr(10000, 65000);
16-
static uint16_t test_port = distr(gen);
13+
extern uint16_t test_port;
1714

1815
std::string to_hex_array(const std::vector<uint8_t> &data)
1916
{

tests/test_client_server_disconnect.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
#include "../src/netlib.hpp"
33

44
using namespace std::chrono_literals;
5-
static const uint16_t test_port = 8889;
5+
extern uint16_t test_port;
66

77
TEST_CASE("Test disconnect handling")
88
{
9-
109
netlib::server server;
1110
netlib::client client;
1211
bool client_was_connected = false;

tests/test_large_data_transfer.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
using namespace std::chrono_literals;
66

7-
std::random_device rd;
8-
std::uniform_int_distribution<uint16_t> dist(1000, 65000);
9-
std::mt19937 mt(rd());
10-
uint16_t test_port = dist(rd);
7+
extern uint16_t test_port;
118

129
TEST_CASE("Large data transfer")
1310
{

tests/test_raii.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <atomic>
44

55
using namespace std::chrono_literals;
6-
static const uint16_t test_port = 8888;
6+
extern uint16_t test_port;
77

88
TEST_CASE("RAII test for client and server")
99
{

0 commit comments

Comments
 (0)