Files
SPHLite/tests/app/test_worker_ipc.c
2026-07-05 21:41:34 +08:00

127 lines
4.3 KiB
C

#include "sph_worker_ipc.h"
#include <stdio.h>
#include <string.h>
static int fail(const char *message)
{
fprintf(stderr, "%s\n", message);
return 1;
}
static int check_int(int actual, int expected, const char *label)
{
if (actual != expected) {
fprintf(stderr, "%s: expected %d, got %d\n", label, expected, actual);
return 1;
}
return 0;
}
static int check_string(const char *actual, const char *expected,
const char *label)
{
if (strcmp(actual, expected) != 0) {
fprintf(stderr, "%s: expected %s, got %s\n", label, expected, actual);
return 1;
}
return 0;
}
int main(void)
{
sphlite_worker_ipc_t ipc;
int error_code = -1;
if (sphlite_worker_ipc_init(&ipc, "case-a", 0, 0) != 0) {
return fail("default IPC init failed");
}
if (check_string(ipc.command_block, "case-a.command",
"default command block") != 0) return 1;
if (check_string(ipc.data_prefix, "case-a.data",
"default data prefix") != 0) return 1;
if (check_string(ipc.command_event, "case-a.command_event",
"command event") != 0) return 1;
if (check_string(ipc.done_event, "case-a.done_event",
"done event") != 0) return 1;
if (sphlite_worker_ipc_init(&ipc, "case-b", "cmd", "data") != 0) {
return fail("explicit IPC init failed");
}
if (check_string(ipc.command_block, "cmd",
"explicit command block") != 0) return 1;
if (check_string(ipc.data_prefix, "data",
"explicit data prefix") != 0) return 1;
if (sphlite_worker_ipc_open(&ipc, 1, &error_code) != 0 ||
error_code != SPHLITE_WORKER_ERROR_OK ||
ipc.platform != SPHLITE_WORKER_IPC_PLATFORM_MOCK) {
return fail("mock IPC open failed");
}
if (check_string(sphlite_worker_ipc_platform_name(ipc.platform), "MOCK",
"mock platform") != 0) return 1;
if (sphlite_worker_ipc_write_ready(&ipc, &error_code) != 0 ||
error_code != SPHLITE_WORKER_ERROR_OK) {
return fail("ready write failed");
}
if (sphlite_worker_ipc_wait_command(&ipc, 100, &error_code) != 0 ||
error_code != SPHLITE_WORKER_ERROR_OK) {
return fail("wait command failed");
}
if (sphlite_worker_ipc_write_done(&ipc, 7, SPHLITE_WORKER_STATE_DONE,
SPHLITE_WORKER_ERROR_OK) != 0) {
return fail("done write failed");
}
if (sphlite_worker_ipc_signal_done(&ipc, &error_code) != 0 ||
error_code != SPHLITE_WORKER_ERROR_OK) {
return fail("done signal failed");
}
if (check_int(ipc.ready_written, 1, "ready count") != 0 ||
check_int(ipc.command_event_waits, 1, "wait count") != 0 ||
check_int(ipc.done_written, 1, "done count") != 0 ||
check_int(ipc.done_event_signals, 1, "signal count") != 0 ||
check_int(ipc.command_sequence, 7, "ack sequence") != 0) {
return 1;
}
{
int sequence = 0;
int command = 0;
int step_id = 0;
double dt = 0.0;
if (sphlite_worker_ipc_read_command(&ipc, &sequence, &command,
&dt, &step_id, &error_code) == 0 ||
error_code != SPHLITE_WORKER_ERROR_UNSUPPORTED) {
return fail("mock command read did not report unsupported");
}
}
if (sphlite_worker_ipc_reset_event(&ipc, &error_code) == 0 ||
error_code != SPHLITE_WORKER_ERROR_CONFLICT ||
ipc.reset_attempts != 1) {
return fail("event reset was not rejected");
}
if (sphlite_worker_ipc_wait_command(&ipc, 0, &error_code) == 0 ||
error_code != SPHLITE_WORKER_ERROR_TIMEOUT) {
return fail("wait timeout path failed");
}
if (sphlite_worker_ipc_close(&ipc) != 0 || ipc.opened != 0) {
return fail("IPC close failed");
}
if (sphlite_worker_ipc_write_ready(&ipc, &error_code) == 0 ||
error_code != SPHLITE_WORKER_ERROR_IPC) {
return fail("ready write after close did not fail");
}
if (sphlite_worker_ipc_init(&ipc, "", 0, 0) == 0) {
return fail("empty IPC name was accepted");
}
if (sphlite_worker_ipc_init(&ipc, "case-c", 0, 0) != 0) {
return fail("real IPC init failed");
}
if (sphlite_worker_ipc_open(&ipc, 0, &error_code) == 0 ||
error_code == SPHLITE_WORKER_ERROR_OK) {
return fail("real IPC open did not report an explicit error");
}
return 0;
}