Skip to content

transports_test.cpp

file tests/io/transports_test.cpp · Test suite

Tests of the nmeasim::io transports and of network interface enumeration.

Source tests/io/transports_test.cpp

Covers nmeasim::io::TcpServerTransport, nmeasim::io::TcpClientTransport, nmeasim::io::UdpTransport, nmeasim::io::WebSocketServerTransport, nmeasim::io::FileTransport, nmeasim::io::SerialTransport and nmeasim::io::LogTransport: opening and closing, state changes and errors, the bytes each one delivers, client counts, reconnection, and the nmeasim::io::to_string names of transport states and UDP modes. It also checks nmeasim::io::ipv4_interfaces and nmeasim::io::find_ipv4_interface.

The network tests (tagged [integration]) talk to Qt sockets over the loopback interface and bind to port 0, so that the operating system picks a free port and parallel test runs do not collide. Files are written to temporary directories; the serial test uses a device name that does not exist. The file reads no fixture.

Test cases

16 test cases, in source order.

Test case Tags
TCP server delivers lines to every client and tracks disconnects [io][transport][integration]
TCP server reports a port that is already in use [io][transport][integration]
TCP client connects, sends and reconnects [io][transport][integration]
TCP client reports a refused connection and keeps trying [io][transport][integration]
UDP unicast sends one datagram per line [io][transport][integration]
UDP broadcast without an address falls back to the limited broadcast address [io][transport]
UDP rejects an invalid destination [io][transport]
UDP rejects an IPv6 destination [io][transport]
UDP multicast finds its interface by system or descriptive name [io][transport][integration]
WebSocket server greets new clients and sends lines as text frames [io][transport][integration]
file transport appends lines and flushes immediately [io][transport]
file transport writes the bytes as given and truncates only on its first open [io][transport]
serial transport fails cleanly on a missing device [io][transport]
network interface enumeration includes loopback [io][network]
transport states have names [io][transport]
log transport writes a header once and timestamps every line [io][transport]

Summary

File-local functions

Name Description
wait_until() Pumps the Qt event loop until a condition holds or a timeout expires.

File-local variables

Name Description
kLine The line the transport tests write: a valid GLL sentence with its CR LF terminator.

File-local functions

wait_until()

bool wait_until(const std::function<bool()>& condition, int timeout_ms = 3000)

Pumps the Qt event loop until a condition holds or a timeout expires.

Evaluates condition first, so a condition that already holds returns at once without processing any event. Otherwise it processes pending events for up to 20 milliseconds at a time and evaluates condition again after each round. Passing a condition that never holds turns the call into a wait of timeout_ms during which events are still delivered.

Parameters

Name Type Description
condition const std::function<bool()>& Predicate evaluated on the calling thread between rounds of event processing; it must be callable repeatedly and should be cheap.
timeout_ms int Wall-clock time in milliseconds after which the wait gives up; 3000 ms by default. The timeout is checked between rounds of event processing, so the call can overrun it by one round.

Returns bool: True as soon as condition returns true; false when it still returns false after timeout_ms milliseconds have elapsed.

Note

Events are delivered only for the calling thread, which in this suite is the thread of the QCoreApplication created in main.

Declared in tests/io/transports_test.cpp:43 · defined in tests/io/event_loop.hpp:36

File-local variables

kLine

const QByteArray kLine{"$GPGLL,4916.45,N,12311.12,W,225444,A,*1D\r\n"}

The line the transport tests write: a valid GLL sentence with its CR LF terminator.

It reports 49 degrees 16.45 minutes north, 123 degrees 11.12 minutes west at 22:54:44 UTC. Its checksum 1D is the XOR of the characters between $ and *.

Declared in tests/io/transports_test.cpp:51