ratelimiter_test.c revision b3eeac48ea16234be25bacd7cd0c74e112f60ced
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews/*
a094c46640de70bb03a9351211fb17c41b6fbbb5Automatic Updater * Copyright (C) 1999, 2000 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * copyright notice and this permission notice appear in all copies.
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews *
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews */
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
a094c46640de70bb03a9351211fb17c41b6fbbb5Automatic Updater#include <config.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <stdio.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <stdlib.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <unistd.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <isc/assertions.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <isc/error.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <isc/mem.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <isc/task.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <isc/thread.h>
c5387e694299c41361660e54f23e89c7da3ede1dMark Andrews#include <isc/result.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <isc/time.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <isc/timer.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <isc/ratelimiter.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <isc/util.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrewsisc_ratelimiter_t *rlim = NULL;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrewsstatic void
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrewsltick(isc_task_t *task, isc_event_t *event)
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews{
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews (void) task;
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews printf("** ltick **\n");
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews isc_event_free(&event);
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews}
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrewsstatic void
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrewsutick(isc_task_t *task, isc_event_t *event)
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews{
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews (void) task;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews printf("utick\n");
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews event->ev_action = ltick;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews isc_ratelimiter_enqueue(rlim, &event);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews}
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#define N 7
c5387e694299c41361660e54f23e89c7da3ede1dMark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrewsint
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrewsmain(int argc, char *argv[]) {
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews isc_mem_t *mctx = NULL;
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews isc_taskmgr_t *taskmgr = NULL;
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews isc_timermgr_t *timermgr = NULL;
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews isc_task_t *task = NULL;
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews int times[N] = { 1, 2, 3, 10000, 10001, 10002, 11500 };
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews isc_timer_t *timers[N];
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews isc_interval_t linterval;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews int i;
93d6dfaf66258337985427c86181f01fc51f0bb4Mark Andrews
93d6dfaf66258337985427c86181f01fc51f0bb4Mark Andrews UNUSED(argc);
93d6dfaf66258337985427c86181f01fc51f0bb4Mark Andrews UNUSED(argv);
c5387e694299c41361660e54f23e89c7da3ede1dMark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews isc_interval_set(&linterval, 1, 0);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews RUNTIME_CHECK(isc_taskmgr_create(mctx, 3, 0, &taskmgr) ==
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews ISC_R_SUCCESS);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews RUNTIME_CHECK(isc_timermgr_create(mctx, &timermgr) ==
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews ISC_R_SUCCESS);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews RUNTIME_CHECK(isc_task_create(taskmgr, 0, &task) ==
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews ISC_R_SUCCESS);
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews RUNTIME_CHECK(isc_ratelimiter_create(mctx, timermgr, task,
c5387e694299c41361660e54f23e89c7da3ede1dMark Andrews &rlim) == ISC_R_SUCCESS);
c5387e694299c41361660e54f23e89c7da3ede1dMark Andrews
c5387e694299c41361660e54f23e89c7da3ede1dMark Andrews RUNTIME_CHECK(isc_ratelimiter_setinterval(rlim, &linterval) ==
c5387e694299c41361660e54f23e89c7da3ede1dMark Andrews ISC_R_SUCCESS);
c5387e694299c41361660e54f23e89c7da3ede1dMark Andrews
c5387e694299c41361660e54f23e89c7da3ede1dMark Andrews for (i = 0; i < N; i++) {
c5387e694299c41361660e54f23e89c7da3ede1dMark Andrews isc_interval_t uinterval;
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews isc_interval_set(&uinterval, times[i] / 1000,
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews (times[i] % 1000) * 1000000);
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews timers[i] = NULL;
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews RUNTIME_CHECK(isc_timer_create(timermgr,
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews isc_timertype_once, NULL,
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews &uinterval,
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews task, utick, NULL,
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews &timers[i]) == ISC_R_SUCCESS);
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews }
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews sleep(15);
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews printf("destroy\n");
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews for (i = 0; i < N; i++) {
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews isc_timer_detach(&timers[i]);
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews }
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews isc_ratelimiter_destroy(&rlim);
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews isc_task_destroy(&task);
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews isc_timermgr_destroy(&timermgr);
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews isc_taskmgr_destroy(&taskmgr);
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews sleep(2);
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews isc_mem_stats(mctx, stdout);
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews return (0);
c5387e694299c41361660e54f23e89c7da3ede1dMark Andrews}
c5387e694299c41361660e54f23e89c7da3ede1dMark Andrews