2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 18:19:42 +00:00
bind/bin/tests/timer_test.c

149 lines
3.5 KiB
C
Raw Normal View History

1998-10-16 20:20:31 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
1998-10-22 01:33:20 +00:00
#include <isc/assertions.h>
1998-12-11 20:38:46 +00:00
#include <isc/mem.h>
1998-10-16 20:20:31 +00:00
#include <isc/task.h>
#include <isc/thread.h>
#include <isc/result.h>
#include <isc/timer.h>
1998-10-21 22:01:08 +00:00
isc_memctx_t mctx = NULL;
1998-10-21 02:26:57 +00:00
isc_task_t t1, t2, t3;
1998-10-21 01:57:35 +00:00
isc_timer_t ti1, ti2, ti3;
1998-10-16 20:20:31 +00:00
int tick_count = 0;
1998-10-21 01:13:50 +00:00
static isc_boolean_t
1998-10-21 02:26:57 +00:00
shutdown_task(isc_task_t task, isc_event_t event) {
1998-10-16 20:20:31 +00:00
char *name = event->arg;
1998-10-21 01:57:35 +00:00
printf("task %p shutdown %s\n", task, name);
isc_event_free(&event);
1998-10-21 01:13:50 +00:00
return (ISC_TRUE);
1998-10-16 20:20:31 +00:00
}
1998-10-21 01:13:50 +00:00
static isc_boolean_t
1998-10-21 02:26:57 +00:00
tick(isc_task_t task, isc_event_t event)
1998-10-16 20:20:31 +00:00
{
char *name = event->arg;
1998-10-21 01:57:35 +00:00
INSIST(event->type == ISC_TIMEREVENT_TICK);
1998-10-16 20:20:31 +00:00
printf("task %s (%p) tick\n", name, task);
tick_count++;
if (ti3 != NULL && tick_count % 3 == 0)
1998-10-21 01:57:35 +00:00
isc_timer_touch(ti3);
1998-10-16 20:20:31 +00:00
if (ti3 != NULL && tick_count == 7) {
1998-10-23 23:00:40 +00:00
struct isc_time expires, now;
struct isc_interval interval;
1998-10-16 20:20:31 +00:00
1998-10-22 01:33:20 +00:00
(void)isc_time_get(&now);
1998-10-23 23:00:40 +00:00
isc_interval_set(&interval, 5, 0);
isc_time_add(&now, &interval, &expires);
isc_interval_set(&interval, 4, 0);
1998-10-16 20:20:31 +00:00
printf("*** resetting ti3 ***\n");
1998-10-22 01:33:20 +00:00
INSIST(isc_timer_reset(ti3, isc_timertype_once, &expires,
&interval, ISC_TRUE)
1998-10-16 20:20:31 +00:00
== ISC_R_SUCCESS);
}
isc_event_free(&event);
1998-10-21 01:13:50 +00:00
return (ISC_FALSE);
1998-10-16 20:20:31 +00:00
}
1998-10-21 01:13:50 +00:00
static isc_boolean_t
1998-10-21 02:26:57 +00:00
timeout(isc_task_t task, isc_event_t event)
1998-10-16 20:20:31 +00:00
{
char *name = event->arg;
char *type;
1998-10-21 01:57:35 +00:00
INSIST(event->type == ISC_TIMEREVENT_IDLE ||
event->type == ISC_TIMEREVENT_LIFE);
1998-10-16 20:20:31 +00:00
1998-10-21 01:57:35 +00:00
if (event->type == ISC_TIMEREVENT_IDLE)
1998-10-16 20:20:31 +00:00
type = "idle";
else
type = "life";
printf("task %s (%p) %s timeout\n", name, task, type);
if (strcmp(name, "3") == 0) {
printf("*** saving task 3 ***\n");
isc_event_free(&event);
1998-10-21 01:13:50 +00:00
return (ISC_FALSE);
1998-10-16 20:20:31 +00:00
}
isc_event_free(&event);
1998-10-21 01:13:50 +00:00
return (ISC_TRUE);
1998-10-16 20:20:31 +00:00
}
void
main(int argc, char *argv[]) {
1998-10-21 02:26:57 +00:00
isc_taskmgr_t manager = NULL;
1998-10-21 01:57:35 +00:00
isc_timermgr_t timgr = NULL;
1998-10-16 20:20:31 +00:00
unsigned int workers;
1998-10-23 23:00:40 +00:00
struct isc_time expires, now;
struct isc_interval interval;
1998-10-16 20:20:31 +00:00
if (argc > 1)
workers = atoi(argv[1]);
else
workers = 2;
printf("%d workers\n", workers);
1998-10-21 22:01:08 +00:00
INSIST(isc_memctx_create(0, 0, &mctx) == ISC_R_SUCCESS);
1998-10-22 01:33:20 +00:00
INSIST(isc_taskmgr_create(mctx, workers, 0, &manager) ==
ISC_R_SUCCESS);
INSIST(isc_task_create(manager, shutdown_task, "1", 0, &t1) ==
ISC_R_SUCCESS);
INSIST(isc_task_create(manager, shutdown_task, "2", 0, &t2) ==
ISC_R_SUCCESS);
INSIST(isc_task_create(manager, shutdown_task, "3", 0, &t3) ==
ISC_R_SUCCESS);
1998-10-21 01:57:35 +00:00
INSIST(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS);
1998-10-16 20:20:31 +00:00
printf("task 1: %p\n", t1);
printf("task 2: %p\n", t2);
printf("task 3: %p\n", t3);
1998-10-22 01:33:20 +00:00
(void)isc_time_get(&now);
1998-10-16 20:20:31 +00:00
1998-10-23 23:00:40 +00:00
isc_time_settoepoch(&expires);
isc_interval_set(&interval, 2, 0);
1998-10-22 01:33:20 +00:00
INSIST(isc_timer_create(timgr, isc_timertype_once, &expires, &interval,
1998-10-21 01:57:35 +00:00
t2, timeout, "2", &ti2) == ISC_R_SUCCESS);
1998-10-23 23:00:40 +00:00
isc_time_settoepoch(&expires);
isc_interval_set(&interval, 1, 0);
1998-10-22 01:33:20 +00:00
INSIST(isc_timer_create(timgr, isc_timertype_ticker,
&expires, &interval,
1998-10-21 01:57:35 +00:00
t1, tick, "1", &ti1) == ISC_R_SUCCESS);
1998-10-23 23:00:40 +00:00
isc_interval_set(&interval, 10, 0);
isc_time_add(&now, &interval, &expires);
isc_interval_set(&interval, 2, 0);
1998-10-22 01:33:20 +00:00
INSIST(isc_timer_create(timgr, isc_timertype_once, &expires, &interval,
1998-10-21 01:57:35 +00:00
t3, timeout, "3", &ti3) == ISC_R_SUCCESS);
1998-10-16 20:20:31 +00:00
1998-10-21 02:26:57 +00:00
isc_task_detach(&t1);
isc_task_detach(&t2);
isc_task_detach(&t3);
1998-10-16 20:20:31 +00:00
sleep(15);
printf("destroy\n");
1998-10-21 01:57:35 +00:00
isc_timer_detach(&ti1);
isc_timer_detach(&ti2);
isc_timer_detach(&ti3);
1998-10-16 20:20:31 +00:00
sleep(2);
1998-10-21 01:57:35 +00:00
isc_timermgr_destroy(&timgr);
1998-10-21 02:26:57 +00:00
isc_taskmgr_destroy(&manager);
1998-10-16 20:20:31 +00:00
printf("destroyed\n");
1998-10-21 22:01:08 +00:00
isc_mem_stats(mctx, stdout);
isc_memctx_destroy(&mctx);
1998-10-16 20:20:31 +00:00
}