Files
libakerror/tests/err_pool_refcount.c

61 lines
1.4 KiB
C
Raw Normal View History

#include "akerror.h"
#include "err_capture.h"
/*
* Pool-hygiene regression test. The error pool is a fixed 128-slot static
* array; a single leaked reference per error would silently exhaust it. Run a
* large number of full raise -> catch -> handle cycles and confirm every slot
* is returned to the pool (refcount 0) and the pool can still hand out
* contexts afterwards.
*/
#define ITERATIONS 100000
static int handled_status = 0;
akerr_ErrorContext *boom(void)
{
PREPARE_ERROR(e);
ATTEMPT {
FAIL(e, AKERR_VALUE, "boom %d", 1);
} CLEANUP {
} PROCESS(e) {
} FINISH(e, true);
SUCCEED_RETURN(e);
}
/* One full raise -> catch -> handle cycle. Returns NULL (context released). */
akerr_ErrorContext *one_cycle(void)
{
PREPARE_ERROR(e);
ATTEMPT {
CATCH(e, boom());
} CLEANUP {
} PROCESS(e) {
} HANDLE(e, AKERR_VALUE) {
handled_status = e->status;
} FINISH(e, false);
return e;
}
int main(void)
{
akerr_capture_install();
akerr_init();
AKERR_CHECK(akerr_slots_in_use() == 0);
for ( int iter = 0; iter < ITERATIONS; iter++ ) {
handled_status = 0;
(void)one_cycle();
AKERR_CHECK(handled_status == AKERR_VALUE);
}
AKERR_CHECK(akerr_slots_in_use() == 0);
akerr_ErrorContext *probe = akerr_next_error();
AKERR_CHECK(probe != NULL);
fprintf(stderr, "err_pool_refcount ok (%d cycles, 0 leaked)\n", ITERATIONS);
return 0;
}