87 lines
2.6 KiB
C
87 lines
2.6 KiB
C
#include "u_async.h"
|
|
#include <stdio.h>
|
|
|
|
/* Test statistics */
|
|
static struct {
|
|
int timer_callbacks;
|
|
int immediate_timeouts;
|
|
} test_stats = {0};
|
|
|
|
typedef struct {
|
|
int callback_count;
|
|
int timeout_ms;
|
|
} test_context_t;
|
|
|
|
/* Timer callback for testing */
|
|
static void test_timer_callback(void* arg) {
|
|
test_context_t* ctx = (test_context_t*)arg;
|
|
ctx->callback_count++;
|
|
test_stats.timer_callbacks++;
|
|
|
|
printf("Callback fired: ctx->timeout_ms=%d, immediate_timeouts before=%d", ctx->timeout_ms, test_stats.immediate_timeouts);
|
|
|
|
if (ctx->timeout_ms == 0) {
|
|
test_stats.immediate_timeouts++;
|
|
printf(" -> INCREMENTED to %d\n", test_stats.immediate_timeouts);
|
|
} else {
|
|
printf(" -> NOT incremented\n");
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
printf("=== Test 1: Basic timers (non-zero timeouts) ===\n");
|
|
printf("Before test 1: immediate_timeouts = %d\n", test_stats.immediate_timeouts);
|
|
|
|
uasync_t* ua1 = uasync_create();
|
|
test_context_t ctx1 = {.timeout_ms = 10};
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
uasync_set_timeout(ua1, 10 + i*10, &ctx1, test_timer_callback, "test_d1");
|
|
}
|
|
|
|
int polls = 0;
|
|
while (ctx1.callback_count < 3 && polls < 10) {
|
|
uasync_poll(ua1, 10);
|
|
polls++;
|
|
}
|
|
|
|
printf("After test 1: immediate_timeouts = %d\n", test_stats.immediate_timeouts);
|
|
uasync_destroy(ua1);
|
|
|
|
printf("\n=== Test 2: Immediate timeouts (zero timeouts) ===\n");
|
|
printf("Before test 2: immediate_timeouts = %d\n", test_stats.immediate_timeouts);
|
|
|
|
uasync_t* ua2 = uasync_create();
|
|
test_context_t ctx2 = {.timeout_ms = 0};
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
uasync_set_timeout(ua2, 0, &ctx2, test_timer_callback, "test_d2");
|
|
}
|
|
|
|
printf("Before poll: ctx2.callback_count = %d, immediate_timeouts = %d\n", ctx2.callback_count, test_stats.immediate_timeouts);
|
|
|
|
uasync_poll(ua2, 1);
|
|
|
|
printf("After poll: ctx2.callback_count = %d, immediate_timeouts = %d\n", ctx2.callback_count, test_stats.immediate_timeouts);
|
|
|
|
uasync_destroy(ua2);
|
|
|
|
printf("\n=== Test 3: Another immediate timeout test ===\n");
|
|
printf("Before test 3: immediate_timeouts = %d\n", test_stats.immediate_timeouts);
|
|
|
|
uasync_t* ua3 = uasync_create();
|
|
test_context_t ctx3 = {.timeout_ms = 0};
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
uasync_set_timeout(ua3, 0, &ctx3, test_timer_callback, "test_d3");
|
|
}
|
|
|
|
uasync_poll(ua3, 1);
|
|
|
|
printf("After test 3: immediate_timeouts = %d\n", test_stats.immediate_timeouts);
|
|
|
|
uasync_destroy(ua3);
|
|
|
|
return 0;
|
|
}
|