Event-Driven Programming in C++ 1.0
A case study on event-driven programming in C++
Loading...
Searching...
No Matches
io_and_sockets_test.cpp File Reference

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}
 

Detailed Description

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.

Note
This test uses the TEST_MODE macro. Ensure that your implementation of runDemo() (in io_and_sockets.cpp) is modified so that when TEST_MODE is defined, it reads input using std::getline() rather than using _kbhit() on Windows.

Macro Definition Documentation

◆ PORT

#define PORT   12345

Function Documentation

◆ clientThreadFunction()

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.

◆ main()

int main ( )

Variable Documentation

◆ clientConnected

std::atomic<bool> clientConnected {false}