1 /* $NetBSD: ratelimiter_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */
4 * Copyright (C) 2004, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: ratelimiter_test.c,v 1.18 2007/06/19 23:46:59 tbox Exp */
28 #include <isc/timer.h>
29 #include <isc/ratelimiter.h>
32 isc_ratelimiter_t
*rlim
= NULL
;
33 isc_taskmgr_t
*taskmgr
= NULL
;
34 isc_timermgr_t
*timermgr
= NULL
;
35 isc_task_t
*g_task
= NULL
;
36 isc_mem_t
*mctx
= NULL
;
38 static void utick(isc_task_t
*task
, isc_event_t
*event
);
39 static void shutdown_rl(isc_task_t
*task
, isc_event_t
*event
);
40 static void shutdown_all(isc_task_t
*task
, isc_event_t
*event
);
44 void (*fun
)(isc_task_t
*, isc_event_t
*);
47 schedule_t schedule
[] = {
54 { 3300, shutdown_rl
},
56 { 6000, shutdown_all
}
59 #define NEVENTS (int)(sizeof(schedule)/sizeof(schedule[0]))
61 isc_timer_t
*timers
[NEVENTS
];
64 ltick(isc_task_t
*task
, isc_event_t
*event
) {
66 printf("** ltick%s **\n",
67 (event
->ev_attributes
& ISC_EVENTATTR_CANCELED
) != 0 ?
69 isc_event_free(&event
);
73 utick(isc_task_t
*task
, isc_event_t
*event
) {
76 event
->ev_action
= ltick
;
77 event
->ev_sender
= NULL
;
78 result
= isc_ratelimiter_enqueue(rlim
, g_task
, &event
);
79 printf("enqueue: %s\n",
80 result
== ISC_R_SUCCESS
? "ok" : "failed");
84 shutdown_rl(isc_task_t
*task
, isc_event_t
*event
) {
87 printf("shutdown ratelimiter\n");
88 isc_ratelimiter_shutdown(rlim
);
92 shutdown_all(isc_task_t
*task
, isc_event_t
*event
) {
96 printf("shutdown all\n");
97 for (i
= 0; i
< NEVENTS
; i
++) {
98 isc_timer_detach(&timers
[i
]);
105 main(int argc
, char *argv
[]) {
106 isc_interval_t linterval
;
113 isc_interval_set(&linterval
, 1, 0);
115 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx
) == ISC_R_SUCCESS
);
116 RUNTIME_CHECK(isc_taskmgr_create(mctx
, 3, 0, &taskmgr
) ==
118 RUNTIME_CHECK(isc_timermgr_create(mctx
, &timermgr
) ==
120 RUNTIME_CHECK(isc_task_create(taskmgr
, 0, &g_task
) ==
123 RUNTIME_CHECK(isc_ratelimiter_create(mctx
, timermgr
, g_task
,
124 &rlim
) == ISC_R_SUCCESS
);
126 RUNTIME_CHECK(isc_ratelimiter_setinterval(rlim
, &linterval
) ==
129 for (i
= 0; i
< NEVENTS
; i
++) {
130 isc_interval_t uinterval
;
131 int ms
= schedule
[i
].milliseconds
;
132 isc_interval_set(&uinterval
, ms
/ 1000,
133 (ms
% 1000) * 1000000);
135 RUNTIME_CHECK(isc_timer_create(timermgr
,
136 isc_timertype_once
, NULL
,
138 g_task
, schedule
[i
].fun
, NULL
,
139 &timers
[i
]) == ISC_R_SUCCESS
);
144 isc_task_destroy(&g_task
);
146 isc_ratelimiter_detach(&rlim
);
148 isc_timermgr_destroy(&timermgr
);
149 isc_taskmgr_destroy(&taskmgr
);
151 isc_mem_stats(mctx
, stdout
);