Utilities for testing network functionality.
More...
#include <NetworkTesting.hpp>
|
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...
|
|
Utilities for testing network functionality.
◆ 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
-
start | the start port to try |
count | the number of sequential ports to try |
- Returns
- a TCP port number that was free at the time of the call
- Exceptions
-
NetworkException | if 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
-
code | the code to execute that contains a call to getFreeTcpPort |
attempts | the number of attempts to try (default = 10) |
- Returns
- the TCP port number that was used in the supplied code
- Exceptions
-
NetworkException | if all the attempts to get a free port failed |
The documentation for this struct was generated from the following file: