|
Event-Driven Programming in C++ 1.0
A case study on event-driven programming in C++
|
Unit test for the I/O and Sockets demonstration. More...
#include "io_and_sockets/io_and_sockets.hpp"#include <cassert>#include <sstream>#include <iostream>#include <thread>#include <atomic>#include <arpa/inet.h>#include <sys/socket.h>#include <netinet/in.h>#include <unistd.h>Macros | |
| #define | PORT 12345 |
Functions | |
| void | clientThreadFunction () |
| Client thread function that attempts to connect to the server. | |
| int | main () |
Variables | |
| std::atomic< bool > | clientConnected {false} |
Unit test for the I/O and Sockets demonstration.
This test verifies that the I/O and Sockets demonstration (implemented in the io_and_sockets module) terminates automatically when simulated input is provided and correctly accepts a client socket connection.
In test mode (with TEST_MODE defined), the demonstration bypasses the Windows-specific _kbhit() polling and uses std::getline() to read console input. The test simulates input by redirecting std::cin and provides a string ("client\nquit\n") that first allows a client connection and then instructs the demo to terminate.
Additionally, a separate client thread is spawned to connect to the server. If the connection succeeds, a flag is set. The test asserts that runDemo() returns 0 and that the client connection was successfully established.
| #define PORT 12345 |
| void clientThreadFunction | ( | ) |
Client thread function that attempts to connect to the server.
The function waits briefly to allow the server to start up, then creates a client socket and attempts to connect to localhost on PORT. If the connection succeeds, the atomic flag is set.
| int main | ( | ) |
| std::atomic<bool> clientConnected {false} |