From 9a2eda6f8e435ecf0d84502c84b1e75e2f5e9220 Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Wed, 12 May 1999 09:48:12 +0000 Subject: [PATCH] add shutdown_test --- bin/tests/.cvsignore | 1 + bin/tests/Makefile.in | 4 + bin/tests/shutdown_test.c | 200 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 bin/tests/shutdown_test.c diff --git a/bin/tests/.cvsignore b/bin/tests/.cvsignore index 5d4efaeb75..d63fafe99b 100644 --- a/bin/tests/.cvsignore +++ b/bin/tests/.cvsignore @@ -6,6 +6,7 @@ sock_test sym_test lex_test task_test +shutdown_test timer_test wire_test rdata_test diff --git a/bin/tests/Makefile.in b/bin/tests/Makefile.in index 4dbdd92ac6..8b1c0af174 100644 --- a/bin/tests/Makefile.in +++ b/bin/tests/Makefile.in @@ -36,6 +36,7 @@ TARGETS = lex_test \ sock_test \ sym_test \ task_test \ + shutdown_test \ timer_test \ rbt_test \ rdata_test \ @@ -62,6 +63,9 @@ sym_test: sym_test.o ../../lib/isc/libisc.a task_test: task_test.o ../../lib/isc/libisc.a ../../lib/dns/libdns.a ${CC} -o $@ task_test.o ${LIBS} +shutdown_test: shutdown_test.o ../../lib/isc/libisc.a ../../lib/dns/libdns.a + ${CC} -o $@ shutdown_test.o ${LIBS} + timer_test: timer_test.o ../../lib/isc/libisc.a ../../lib/dns/libdns.a ${CC} -o $@ timer_test.o ${LIBS} diff --git a/bin/tests/shutdown_test.c b/bin/tests/shutdown_test.c new file mode 100644 index 0000000000..cb445368a9 --- /dev/null +++ b/bin/tests/shutdown_test.c @@ -0,0 +1,200 @@ +/* + * Copyright (C) 1998, 1999 Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + */ + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct { + isc_mem_t * mctx; + isc_task_t * task; + isc_timer_t * timer; + unsigned int ticks; + char name[16]; + isc_boolean_t exiting; + isc_task_t * peer; +} t_info; + +#define MAX_TASKS 2 +#define T2_SHUTDOWNOK (ISC_EVENTCLASS(1024) + 0) +#define T2_SHUTDOWNDONE (ISC_EVENTCLASS(1024) + 1) + +static t_info tasks[MAX_TASKS]; +static unsigned int task_count; +static isc_taskmgr_t * task_manager; +static isc_timermgr_t * timer_manager; + +static void +t1_shutdown(isc_task_t *task, isc_event_t *event) { + t_info *info = event->arg; + + printf("task %s (%p) t1_shutdown\n", info->name, task); + isc_task_allowdone(info->task, ISC_TRUE); + isc_task_detach(&info->task); + isc_event_free(&event); +} + +static void +t2_shutdown(isc_task_t *task, isc_event_t *event) { + t_info *info = event->arg; + + printf("task %s (%p) t2_shutdown\n", info->name, task); + info->exiting = ISC_TRUE; + isc_event_free(&event); +} + +static void +shutdown_action(isc_task_t *task, isc_event_t *event) { + t_info *info = event->arg; + isc_event_t *nevent; + + INSIST(event->type == ISC_TASKEVENT_SHUTDOWN); + + printf("task %s (%p) shutdown\n", info->name, task); + if (strcmp(info->name, "0") == 0) { + isc_timer_detach(&info->timer); + nevent = isc_event_allocate(info->mctx, info, T2_SHUTDOWNOK, + t2_shutdown, &tasks[1], + sizeof *event); + RUNTIME_CHECK(nevent != NULL); + info->exiting = ISC_TRUE; + isc_task_send(info->peer, &nevent); + isc_task_detach(&info->peer); + } + isc_event_free(&event); +} + +static void +tick(isc_task_t *task, isc_event_t *event) +{ + t_info *info = event->arg; + isc_event_t *nevent; + + INSIST(event->type == ISC_TIMEREVENT_TICK); + + printf("task %s (%p) tick\n", info->name, task); + + info->ticks++; + if (strcmp(info->name, "1") == 0) { + if (info->ticks == 10) { + RUNTIME_CHECK(isc_app_shutdown() == ISC_R_SUCCESS); + } else if (info->ticks >= 15 && info->exiting) { + isc_timer_detach(&info->timer); + isc_task_allowdone(info->task, ISC_TRUE); + isc_task_detach(&info->task); + nevent = isc_event_allocate(info->mctx, info, + T2_SHUTDOWNDONE, + t1_shutdown, &tasks[0], + sizeof *event); + RUNTIME_CHECK(nevent != NULL); + isc_task_send(info->peer, &nevent); + isc_task_detach(&info->peer); + } + } + + isc_event_free(&event); +} + +static t_info * +new_task(isc_mem_t *mctx) { + t_info *ti; + isc_time_t expires; + isc_interval_t interval; + + RUNTIME_CHECK(task_count < MAX_TASKS); + ti = &tasks[task_count]; + ti->mctx = mctx; + ti->task = NULL; + ti->timer = NULL; + ti->ticks = 0; + sprintf(ti->name, "%d", task_count); + RUNTIME_CHECK(isc_task_create(task_manager, mctx, 0, &ti->task) == + ISC_R_SUCCESS); + RUNTIME_CHECK(isc_task_onshutdown(ti->task, shutdown_action, ti) == + ISC_R_SUCCESS); + + isc_time_settoepoch(&expires); + isc_interval_set(&interval, 1, 0); + RUNTIME_CHECK(isc_timer_create(timer_manager, isc_timertype_ticker, + &expires, &interval, ti->task, + tick, ti, &ti->timer) == + ISC_R_SUCCESS); + + task_count++; + + return (ti); +} + +void +main(int argc, char *argv[]) { + unsigned int workers; + t_info *t1, *t2; + isc_mem_t *mctx, *mctx2; + + RUNTIME_CHECK(isc_app_start() == ISC_R_SUCCESS); + + if (argc > 1) + workers = atoi(argv[1]); + else + workers = 2; + printf("%d workers\n", workers); + + mctx = NULL; + RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS); + mctx2 = NULL; + RUNTIME_CHECK(isc_mem_create(0, 0, &mctx2) == ISC_R_SUCCESS); + RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, &task_manager) == + ISC_R_SUCCESS); + RUNTIME_CHECK(isc_timermgr_create(mctx, &timer_manager) == + ISC_R_SUCCESS); + + t1 = new_task(mctx); + t2 = new_task(mctx2); + isc_task_attach(t2->task, &t1->peer); + isc_task_attach(t1->task, &t2->peer); + isc_task_allowdone(t1->task, ISC_FALSE); + isc_task_allowdone(t2->task, ISC_FALSE); + + RUNTIME_CHECK(isc_app_run() == ISC_R_SUCCESS); + + isc_taskmgr_destroy(&task_manager); + isc_timermgr_destroy(&timer_manager); + + printf("Statistics for mctx:\n"); + isc_mem_stats(mctx, stdout); + isc_mem_destroy(&mctx); + printf("Statistics for mctx2:\n"); + isc_mem_stats(mctx2, stdout); + isc_mem_destroy(&mctx2); + + isc_app_finish(); +}