Skip to content

How to test in mock mode

How to Test – Mock Socket Workflow

PhoenixSocket makes it easy to test socket communication without a real network by using the mock backend. The mock backend simulates socket behavior by reading from and writing to files, allowing you to verify your logic in a controlled environment.

Example Test Architecture

In a typical test, you have two roles: - Sender socket (e.g., "Alice"): sends data/messages. - Receiver socket (e.g., "Bob"): receives data/messages.

The mock backend uses files to simulate data exchange between these sockets.

Workflow Diagram

+-------------------+         RESULT_DATA (e.g., INT=42)         +---------------------------------------+
|   Sender Socket   |------------------------------------------->|  Mock File (Alice_localhost_3390)     |
|    ("Alice")      |                                            |  (written by sender, read by receiver)|
+-------------------+                                            +---------------------------------------+
        |                                                                 |
        |                                                                 |
        |                                                                 |
+-------------------+                                            +---------------------------------------+
|  Receiver Socket  |<-------------------------------------------|  Mock File (Bob_localhost_3390)       |
|    ("Bob")        |         (reads data, can send STOP)        |  (written by receiver, read by sender)|
+-------------------+                                            +---------------------------------------+
  • The sender writes messages (e.g., integers, custom messages) to a mock file.
  • The receiver reads these messages from the same mock file.
  • Control messages (like STOP) can be sent in the opposite direction, using another mock file.

How It Works in Practice

  • Each socket (server/client) is created in mock mode using the PGenericSocketManager.
  • When the sender calls sendData or sendMsg, data is written to a mock file.
  • The receiver calls recvData or recvMsg to read data from the mock file.
  • This setup allows you to run tests without any real network connection.

Example Code Snippet

// Sender (Alice)
SocketManager manager(PSocketMode::MOCK_RECORD);
manager.addServerSocket("Alice", PSocketParam{"localhost", 3390}, Backend::server(), "", Mock::server());
manager.sendData("Alice", 42); // Writes 42 to the mock file

// Receiver (Bob)
SocketManager manager(PSocketMode::MOCK);
manager.addClientSocket("Bob", PSocketParam{"localhost", 3390}, Backend::client(), "", Mock::client());
size_t value = 0;
manager.recvData("Bob", value); // Reads 42 from the mock file

Benefits

  • No real network required: All communication is simulated via files.
  • Deterministic and reproducible: Test data is stored and can be replayed.
  • Easy debugging: Inspect mock files to see exactly what was sent and received.

By following this approach, you can thoroughly test your socket logic in isolation, ensuring robust and reliable code before deploying with real backends.