Implement simulation client for deterministic testing

Added a simulation client that can run inside the TinyDFS simulation
framework, enabling fully deterministic end-to-end testing of the
distributed file system.

**New Files:**
- src/simulation_client.h - Header defining SimulationClient structure
  and API (init, step, free functions)
- src/simulation_client.c - Implementation of simulation client with
  test operations (create dir, create file, write, read, list, delete)

**Key Changes:**

**System Framework (src/system.c):**
- Added ProcessType enum (METADATA_SERVER, CHUNK_SERVER, CLIENT)
  to distinguish different simulated process types
- Extended Process union to include SimulationClient
- Modified spawn_simulated_process() to detect --client flag and
  initialize client processes using simulation_client_init()
- Updated update_simulation() to call simulation_client_step() for
  client processes, handling timeout management
- Fixed cleanup_simulation() to properly set current_process before
  freeing, preventing NULL pointer dereferences in mock_close()
- Added simulated_time static variable (struct timespec) for
  deterministic clock mocking
- Added helper function is_client() to detect client processes

**API Updates (src/system.h):**
- Exported startup_simulation() function for proper initialization
- Added function declarations for simulation management

**Test Updates (src/main_test.c):**
- Added startup_simulation() call for proper initialization
- Spawns simulation client process with "--client" flag
- Increased iteration limit to 100,000 to allow operations to complete
- Added progress logging every 10,000 iterations
- Added explicit error checking and debug output

**Simulation Client Features:**
- Non-blocking operation model using tinydfs_isdone() and
  tinydfs_process_events()
- State machine for sequential test operations
- Comprehensive test scenario:
  1. Create directory (/test_dir)
  2. Create file (/test_dir/test_file.txt)
  3. Write data to file
  4. Read data back
  5. List directory contents
  6. Delete file
- Proper integration with TinyDFS client library
- Returns poll descriptors from tinydfs_process_events()
- Timeout management for simulation scheduling

**Bug Fixes:**
- Fixed missing return statement in spawn_simulated_process()
- Fixed NULL pointer dereference in cleanup by setting current_process
- Added missing stdlib.h include for atoi()

The simulation client successfully initializes and integrates into the
simulation loop, demonstrating the framework's ability to run client
code deterministically alongside servers.
This commit is contained in:
Claude
2025-11-02 12:05:53 +00:00
parent d267c0760e
commit 38fdab24d3
5 changed files with 459 additions and 32 deletions
+46
View File
@@ -0,0 +1,46 @@
#ifndef SIMULATION_CLIENT_INCLUDED
#define SIMULATION_CLIENT_INCLUDED
#include <stdint.h>
#include <stdbool.h>
#ifdef _WIN32
#include <winsock2.h>
#else
#include <poll.h>
#endif
#include "TinyDFS.h"
typedef enum {
CLIENT_STATE_INIT,
CLIENT_STATE_RUNNING,
CLIENT_STATE_DONE,
} ClientState;
typedef struct {
TinyDFS *tdfs;
ClientState state;
// Track operations
int create_dir_op;
int create_file_op;
int write_op;
int read_op;
int list_op;
int delete_op;
// Read buffer
char read_buffer[1024];
// Test step counter
int step;
} SimulationClient;
int simulation_client_init(SimulationClient *client, int argc, char **argv,
void **contexts, struct pollfd *polled, int *timeout);
int simulation_client_step(SimulationClient *client, void **contexts,
struct pollfd *polled, int num_polled, int *timeout);
void simulation_client_free(SimulationClient *client);
#endif // SIMULATION_CLIENT_INCLUDED