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:
+93
-30
@@ -5,6 +5,7 @@
|
||||
#include "system.h"
|
||||
#include "chunk_server.h"
|
||||
#include "metadata_server.h"
|
||||
#include "simulation_client.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define NATIVE_HANDLE HANDLE
|
||||
@@ -131,6 +132,12 @@ typedef struct {
|
||||
int line;
|
||||
} Allocation;
|
||||
|
||||
typedef enum {
|
||||
PROCESS_TYPE_METADATA_SERVER,
|
||||
PROCESS_TYPE_CHUNK_SERVER,
|
||||
PROCESS_TYPE_CLIENT,
|
||||
} ProcessType;
|
||||
|
||||
struct Process {
|
||||
|
||||
int num_desc;
|
||||
@@ -141,10 +148,11 @@ struct Process {
|
||||
|
||||
Time wakeup_time;
|
||||
|
||||
bool leader;
|
||||
ProcessType type;
|
||||
union {
|
||||
ChunkServer chunk_server;
|
||||
MetadataServer metadata_server;
|
||||
ChunkServer chunk_server;
|
||||
MetadataServer metadata_server;
|
||||
SimulationClient simulation_client;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -153,6 +161,14 @@ static Process *processes[MAX_PROCESSES];
|
||||
static Process *current_process = NULL;
|
||||
static uint64_t current_time = 1;
|
||||
|
||||
#ifndef _WIN32
|
||||
// Simulated time for deterministic clock_gettime behavior
|
||||
static struct timespec simulated_time = {0, 0};
|
||||
#else
|
||||
// On Windows, simulated_time is used for QueryPerformanceCounter
|
||||
static struct timespec simulated_time = {0, 0};
|
||||
#endif
|
||||
|
||||
// Helper to set socket errors correctly on Windows vs Linux
|
||||
#ifdef _WIN32
|
||||
#define SET_SOCKET_ERROR(err) WSASetLastError(err)
|
||||
@@ -228,6 +244,14 @@ static bool is_leader(int argc, char **argv)
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool is_client(int argc, char **argv)
|
||||
{
|
||||
for (int i = 0; i < argc; i++)
|
||||
if (!strcmp("--client", argv[i]) || !strcmp("-c", argv[i]))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
#define MAX_ARGS 128
|
||||
|
||||
static bool is_space(char c)
|
||||
@@ -275,12 +299,21 @@ int spawn_simulated_process(char *args)
|
||||
}
|
||||
|
||||
bool leader = is_leader(argc, argv);
|
||||
bool client = is_client(argc, argv);
|
||||
|
||||
Process *process = malloc(sizeof(Process));
|
||||
if (process == NULL)
|
||||
return -1;
|
||||
|
||||
process->leader = leader;
|
||||
// Determine process type
|
||||
if (client) {
|
||||
process->type = PROCESS_TYPE_CLIENT;
|
||||
} else if (leader) {
|
||||
process->type = PROCESS_TYPE_METADATA_SERVER;
|
||||
} else {
|
||||
process->type = PROCESS_TYPE_CHUNK_SERVER;
|
||||
}
|
||||
|
||||
process->num_desc = 0;
|
||||
process->num_allocs = 0;
|
||||
|
||||
@@ -292,41 +325,64 @@ int spawn_simulated_process(char *args)
|
||||
int num_polled;
|
||||
|
||||
current_process = process;
|
||||
int timeout = -1;
|
||||
if (leader) {
|
||||
num_polled = metadata_server_init(&process->metadata_server, argc, argv, contexts, polled, &timeout);
|
||||
} else {
|
||||
num_polled = chunk_server_init(&process->chunk_server, argc, argv, contexts, polled, &timeout);
|
||||
|
||||
switch (process->type) {
|
||||
case PROCESS_TYPE_METADATA_SERVER:
|
||||
num_polled = metadata_server_init(&process->metadata_server, argc, argv, contexts, polled);
|
||||
break;
|
||||
case PROCESS_TYPE_CHUNK_SERVER:
|
||||
num_polled = chunk_server_init(&process->chunk_server, argc, argv, contexts, polled);
|
||||
break;
|
||||
case PROCESS_TYPE_CLIENT:
|
||||
{
|
||||
int timeout = -1;
|
||||
num_polled = simulation_client_init(&process->simulation_client, argc, argv, contexts, polled, &timeout);
|
||||
if (timeout < 0) {
|
||||
process->wakeup_time = INVALID_TIME;
|
||||
} else {
|
||||
process->wakeup_time = current_time + timeout;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
num_polled = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
current_process = NULL;
|
||||
if (num_polled < 0) {
|
||||
// TODO
|
||||
}
|
||||
if (timeout < 0) {
|
||||
process->wakeup_time = INVALID_TIME;
|
||||
} else {
|
||||
process->wakeup_time = current_time + timeout;
|
||||
}
|
||||
|
||||
process_poll_array(process, contexts, polled, num_polled);
|
||||
|
||||
processes[num_processes++] = process;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void free_process(Process *process)
|
||||
{
|
||||
if (process->leader) {
|
||||
metadata_server_free(&process->metadata_server);
|
||||
} else {
|
||||
chunk_server_free(&process->chunk_server);
|
||||
switch (process->type) {
|
||||
case PROCESS_TYPE_METADATA_SERVER:
|
||||
metadata_server_free(&process->metadata_server);
|
||||
break;
|
||||
case PROCESS_TYPE_CHUNK_SERVER:
|
||||
chunk_server_free(&process->chunk_server);
|
||||
break;
|
||||
case PROCESS_TYPE_CLIENT:
|
||||
simulation_client_free(&process->simulation_client);
|
||||
break;
|
||||
}
|
||||
free(process);
|
||||
}
|
||||
|
||||
void cleanup_simulation(void)
|
||||
{
|
||||
for (int i = 0; i < num_processes; i++)
|
||||
for (int i = 0; i < num_processes; i++) {
|
||||
current_process = processes[i];
|
||||
free_process(processes[i]);
|
||||
current_process = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static bool addr_eql_2(DescriptorAddress a, DescriptorAddress b)
|
||||
@@ -490,23 +546,30 @@ void update_simulation(void)
|
||||
}
|
||||
}
|
||||
|
||||
int timeout = -1;
|
||||
if (current_process->leader) {
|
||||
num_polled = metadata_server_step(¤t_process->metadata_server, contexts, polled, num_polled, &timeout);
|
||||
} else {
|
||||
num_polled = chunk_server_step(¤t_process->chunk_server, contexts, polled, num_polled, &timeout);
|
||||
switch (current_process->type) {
|
||||
case PROCESS_TYPE_METADATA_SERVER:
|
||||
num_polled = metadata_server_step(¤t_process->metadata_server, contexts, polled, num_polled);
|
||||
break;
|
||||
case PROCESS_TYPE_CHUNK_SERVER:
|
||||
num_polled = chunk_server_step(¤t_process->chunk_server, contexts, polled, num_polled);
|
||||
break;
|
||||
case PROCESS_TYPE_CLIENT:
|
||||
{
|
||||
int timeout = -1;
|
||||
num_polled = simulation_client_step(¤t_process->simulation_client, contexts, polled, num_polled, &timeout);
|
||||
if (timeout < 0) {
|
||||
current_process->wakeup_time = INVALID_TIME;
|
||||
} else {
|
||||
current_process->wakeup_time = current_time + timeout;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (num_polled < 0) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
if (timeout < 0) {
|
||||
current_process->wakeup_time = INVALID_TIME;
|
||||
} else {
|
||||
current_process->wakeup_time = current_time + timeout;
|
||||
}
|
||||
|
||||
process_poll_array(current_process, contexts, polled, num_polled);
|
||||
|
||||
current_process = NULL;
|
||||
|
||||
Reference in New Issue
Block a user