Files
lotherk 4e97d5930d dtchat: add the poll and timer event loop (T1.2)
loop.c: one poll(2) per run, monotonic one-shot timers (sorted, FIFO on equal deadlines), non-blocking wakeup pipe and safe fd dispatch when a callback removes another fd. test_loop covers timer order/cancel, timeout, wakeup, pipe, delete-in-callback, timer-added-timer, monotonicity and invalid arguments. make mcde-host exit=0; test_json, test_frame and test_loop all ok.
2026-11-16 17:00:50 +00:00

278 lines
5.2 KiB
C

/*
* test_loop.c -- unit tests for the core's poll/timer event loop
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*/
#include "dc_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
static int fails;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
#cond); \
fails++; \
} \
} while (0)
static long long
now_ms(void)
{
return dc_loop_now_ms();
}
struct Order {
int values[8];
int n;
};
struct TimerCtx {
struct Order *order;
int code;
int fired;
};
static void
timer_cb(void *arg)
{
struct TimerCtx *ctx = arg;
ctx->fired++;
if (ctx->order != NULL && ctx->order->n < 8)
ctx->order->values[ctx->order->n++] = ctx->code;
}
static void
test_timer_order(DcLoop *loop)
{
struct Order order = { { 0 }, 0 };
struct TimerCtx a = { &order, 1, 0 };
struct TimerCtx b = { &order, 2, 0 };
struct TimerCtx c = { &order, 3, 0 };
long long start = now_ms();
CHECK(dc_loop_add_timer(loop, 60, timer_cb, &a) > 0);
CHECK(dc_loop_add_timer(loop, 20, timer_cb, &b) > 0);
CHECK(dc_loop_add_timer(loop, 40, timer_cb, &c) > 0);
while (order.n < 3 && now_ms() - start < 500)
(void)dc_loop_run(loop, 10);
CHECK(order.n == 3);
CHECK(a.fired == 1 && b.fired == 1 && c.fired == 1);
CHECK(order.values[0] == 2);
CHECK(order.values[1] == 3);
CHECK(order.values[2] == 1);
}
static void
test_timer_cancel(DcLoop *loop)
{
struct TimerCtx ctx = { NULL, 4, 0 };
int id = dc_loop_add_timer(loop, 20, timer_cb, &ctx);
CHECK(id > 0);
CHECK(dc_loop_del_timer(loop, id) == 0);
(void)dc_loop_run(loop, 60);
CHECK(ctx.fired == 0);
CHECK(dc_loop_del_timer(loop, 99999) == -1);
}
static void
test_timeout(DcLoop *loop)
{
long long start = now_ms();
int rc = dc_loop_run(loop, 40);
long long elapsed = now_ms() - start;
CHECK(rc == 0);
CHECK(elapsed >= 30);
CHECK(elapsed < 500);
}
static void
test_wakeup(DcLoop *loop)
{
long long start;
CHECK(dc_loop_wakeup_fd(loop) >= 0);
CHECK(dc_loop_wake(loop) == 0);
start = now_ms();
CHECK(dc_loop_run(loop, 1000) >= 1);
CHECK(now_ms() - start < 200);
}
struct PipeCtx {
int called;
char c;
};
static void
pipe_cb(int fd, short revents, void *arg)
{
struct PipeCtx *ctx = arg;
if (revents & POLLIN) {
char c;
if (read(fd, &c, 1) == 1) {
ctx->called++;
ctx->c = c;
}
}
}
static void
test_fd_pipe(DcLoop *loop)
{
int fds[2];
int id;
struct PipeCtx ctx = { 0, 0 };
CHECK(pipe(fds) == 0);
id = dc_loop_add_fd(loop, fds[0], POLLIN, pipe_cb, &ctx);
CHECK(id > 0);
CHECK(write(fds[1], "Z", 1) == 1);
CHECK(dc_loop_run(loop, 1000) >= 1);
CHECK(ctx.called == 1);
CHECK(ctx.c == 'Z');
CHECK(dc_loop_del_fd(loop, id) == 0);
CHECK(write(fds[1], "Q", 1) == 1);
(void)dc_loop_run(loop, 50);
CHECK(ctx.called == 1);
close(fds[0]);
close(fds[1]);
}
struct DelCtx {
DcLoop *loop;
int id2;
int called1;
int called2;
};
static void
del_cb1(int fd, short revents, void *arg)
{
struct DelCtx *ctx = arg;
(void)fd;
(void)revents;
ctx->called1++;
CHECK(dc_loop_del_fd(ctx->loop, ctx->id2) == 0);
}
static void
del_cb2(int fd, short revents, void *arg)
{
struct DelCtx *ctx = arg;
(void)fd;
(void)revents;
ctx->called2++;
}
static void
test_delete_in_callback(DcLoop *loop)
{
int p1[2], p2[2], id1;
struct DelCtx ctx;
memset(&ctx, 0, sizeof(ctx));
ctx.loop = loop;
CHECK(pipe(p1) == 0);
CHECK(pipe(p2) == 0);
id1 = dc_loop_add_fd(loop, p1[0], POLLIN, del_cb1, &ctx);
CHECK(id1 > 0);
ctx.id2 = dc_loop_add_fd(loop, p2[0], POLLIN, del_cb2, &ctx);
CHECK(ctx.id2 > 0);
CHECK(write(p1[1], "x", 1) == 1);
CHECK(write(p2[1], "y", 1) == 1);
(void)dc_loop_run(loop, 1000);
CHECK(ctx.called1 == 1);
CHECK(ctx.called2 == 0);
CHECK(dc_loop_del_fd(loop, id1) == 0);
close(p1[0]);
close(p1[1]);
close(p2[0]);
close(p2[1]);
}
struct ChainCtx {
DcLoop *loop;
int fired;
};
static void
chain_cb(void *arg)
{
struct ChainCtx *ctx = arg;
ctx->fired++;
if (ctx->fired == 1)
CHECK(dc_loop_add_timer(ctx->loop, 10, chain_cb, ctx) > 0);
}
static void
test_timer_adds_timer(DcLoop *loop)
{
struct ChainCtx ctx = { loop, 0 };
long long start = now_ms();
CHECK(dc_loop_add_timer(loop, 10, chain_cb, &ctx) > 0);
while (ctx.fired < 2 && now_ms() - start < 500)
(void)dc_loop_run(loop, 10);
CHECK(ctx.fired == 2);
}
static void
test_monotonic(void)
{
struct timespec ts = { 0, 20 * 1000 * 1000 };
long long a = now_ms();
(void)nanosleep(&ts, NULL);
CHECK(now_ms() >= a + 10);
}
static void
test_invalid(DcLoop *loop)
{
CHECK(dc_loop_add_timer(loop, -1, timer_cb, NULL) == -1);
CHECK(dc_loop_add_fd(loop, -1, POLLIN, pipe_cb, NULL) == -1);
CHECK(dc_loop_add_fd(loop, 0, 0, pipe_cb, NULL) == -1);
}
int
main(void)
{
DcLoop *loop = dc_loop_new();
if (loop == NULL) {
perror("dc_loop_new");
return 1;
}
test_timer_order(loop);
test_timer_cancel(loop);
test_timeout(loop);
test_wakeup(loop);
test_fd_pipe(loop);
test_delete_in_callback(loop);
test_timer_adds_timer(loop);
test_monotonic();
test_invalid(loop);
dc_loop_free(loop);
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
}
printf("test_loop: all ok\n");
return 0;
}