You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
2.9 KiB
114 lines
2.9 KiB
/** |
|
* Simple test for lib race condition fix |
|
*/ |
|
|
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include <assert.h> |
|
#include <unistd.h> |
|
|
|
#include "u_async.h" |
|
|
|
static int callback_count = 0; |
|
|
|
static void simple_timer_callback(void* arg) { |
|
int* count = (int*)arg; |
|
(*count)++; |
|
printf("Timer fired! Count: %d\n", *count); |
|
} |
|
|
|
int main(void) { |
|
printf("=== Simple lib race condition test ===\n"); |
|
|
|
uasync_t* ua = uasync_create(); |
|
if (!ua) { |
|
printf("Failed to create uasync\n"); |
|
return 1; |
|
} |
|
|
|
printf("Created uasync instance\n"); |
|
|
|
/* Test 1: Basic timer functionality */ |
|
printf("Test 1: Basic timer...\n"); |
|
callback_count = 0; |
|
|
|
void* timer1 = uasync_set_timeout(ua, 10, &callback_count, simple_timer_callback); |
|
if (!timer1) { |
|
printf("Failed to set timer\n"); |
|
uasync_destroy(ua); |
|
return 1; |
|
} |
|
|
|
printf("Set timer, polling...\n"); |
|
uasync_poll(ua, 20); /* 2ms poll */ |
|
|
|
if (callback_count != 1) { |
|
printf("Timer didn't fire correctly. Count: %d\n", callback_count); |
|
uasync_destroy(ua); |
|
return 1; |
|
} |
|
|
|
printf("Timer fired correctly!\n"); |
|
|
|
/* Test 2: Timer cancellation */ |
|
printf("Test 2: Timer cancellation...\n"); |
|
callback_count = 0; |
|
|
|
void* timer2 = uasync_set_timeout(ua, 50, &callback_count, simple_timer_callback); /* 5ms */ |
|
if (!timer2) { |
|
printf("Failed to set timer 2\n"); |
|
uasync_destroy(ua); |
|
return 1; |
|
} |
|
|
|
printf("Cancelling timer immediately...\n"); |
|
err_t result = uasync_cancel_timeout(ua, timer2); |
|
if (result != ERR_OK) { |
|
printf("Failed to cancel timer: %d\n", result); |
|
uasync_destroy(ua); |
|
return 1; |
|
} |
|
|
|
printf("Polling after cancellation...\n"); |
|
uasync_poll(ua, 20); /* 2ms poll - should not fire */ |
|
|
|
if (callback_count != 0) { |
|
printf("Cancelled timer fired anyway! Count: %d\n", callback_count); |
|
uasync_destroy(ua); |
|
return 1; |
|
} |
|
|
|
printf("Timer cancellation works correctly!\n"); |
|
|
|
/* Test 3: Multiple timers */ |
|
printf("Test 3: Multiple timers...\n"); |
|
callback_count = 0; |
|
|
|
void* timer3 = uasync_set_timeout(ua, 5, &callback_count, simple_timer_callback); /* 0.5ms */ |
|
void* timer4 = uasync_set_timeout(ua, 15, &callback_count, simple_timer_callback); /* 1.5ms */ |
|
|
|
if (!timer3 || !timer4) { |
|
printf("Failed to set multiple timers\n"); |
|
uasync_destroy(ua); |
|
return 1; |
|
} |
|
|
|
printf("Polling multiple timers...\n"); |
|
uasync_poll(ua, 30); /* 3ms poll */ |
|
|
|
if (callback_count != 2) { |
|
printf("Multiple timers didn't fire correctly. Count: %d\n", callback_count); |
|
uasync_destroy(ua); |
|
return 1; |
|
} |
|
|
|
printf("Multiple timers work correctly!\n"); |
|
|
|
/* Cleanup */ |
|
printf("Cleaning up...\n"); |
|
uasync_destroy(ua); |
|
|
|
printf("=== All tests passed! ===\n"); |
|
return 0; |
|
} |