Static Public Member Functions | List of all members
NetworkTesting Struct Referencefinal

Utilities for testing network functionality. More...

#include <NetworkTesting.hpp>

Static Public Member Functions

static unsigned short getFreeTcpPort (unsigned short start=1025, unsigned short count=65535)
 Get a TCP port number that is free at the time of the call. More...
 
static unsigned short initialiseWithFreeTcpPort (std::function< unsigned short()> code, size_t attempts=10)
 Repeatedly execute the supplied code until there is no network exception due to an in use port. More...
 

Detailed Description

Utilities for testing network functionality.

Member Function Documentation

◆ getFreeTcpPort()

static unsigned short getFreeTcpPort ( unsigned short  start = 1025,
unsigned short  count = 65535 
)
static

Get a TCP port number that is free at the time of the call.

The best use of this function is to call it in a loop, via the , until the component being tested stops complaining about a used port. Hopefully this will happen on the first iteration, but in exceptional circumstances, it may fail once or twice due to the unavoidable race condition.

Parameters
startthe start port to try
countthe number of sequential ports to try
Returns
a TCP port number that was free at the time of the call
Exceptions
NetworkExceptionif the attempts to get a free port failed

◆ initialiseWithFreeTcpPort()

static unsigned short initialiseWithFreeTcpPort ( std::function< unsigned short()>  code,
size_t  attempts = 10 
)
static

Repeatedly execute the supplied code until there is no network exception due to an in use port.

This function mitigates the inherent race condition that exists between obtaining a free port and using it.

The supplied code must contain a call to getFreeTcpPort in order to obtain a free test port.

Parameters
codethe code to execute that contains a call to getFreeTcpPort
attemptsthe number of attempts to try (default = 10)
Returns
the TCP port number that was used in the supplied code
Exceptions
NetworkExceptionif all the attempts to get a free port failed

The documentation for this struct was generated from the following file: