1 /*	$NetBSD: ratelimiter_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1999-2001  Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 /* Id: ratelimiter_test.c,v 1.18 2007/06/19 23:46:59 tbox Exp  */
21 
22 #include <config.h>
23 
24 #include <isc/app.h>
25 #include <isc/mem.h>
26 #include <isc/task.h>
27 #include <isc/time.h>
28 #include <isc/timer.h>
29 #include <isc/ratelimiter.h>
30 #include <isc/util.h>
31 
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;
37 
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);
41 
42 typedef struct {
43 	int milliseconds;
44 	void (*fun)(isc_task_t *, isc_event_t *);
45 } schedule_t;
46 
47 schedule_t schedule[] = {
48 	{   100, utick },
49 	{   200, utick },
50 	{   300, utick },
51 	{  3000, utick },
52 	{  3100, utick },
53 	{  3200, utick },
54 	{  3300, shutdown_rl },
55 	{  5000, utick },
56 	{  6000, shutdown_all }
57 };
58 
59 #define NEVENTS (int)(sizeof(schedule)/sizeof(schedule[0]))
60 
61 isc_timer_t *timers[NEVENTS];
62 
63 static void
ltick(isc_task_t * task,isc_event_t * event)64 ltick(isc_task_t *task, isc_event_t *event) {
65 	UNUSED(task);
66 	printf("** ltick%s **\n",
67 	       (event->ev_attributes & ISC_EVENTATTR_CANCELED) != 0 ?
68 	       " (canceled)" : "");
69 	isc_event_free(&event);
70 }
71 
72 static void
utick(isc_task_t * task,isc_event_t * event)73 utick(isc_task_t *task, isc_event_t *event) {
74 	isc_result_t result;
75 	UNUSED(task);
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");
81 }
82 
83 static void
shutdown_rl(isc_task_t * task,isc_event_t * event)84 shutdown_rl(isc_task_t *task, isc_event_t *event) {
85 	UNUSED(task);
86 	UNUSED(event);
87 	printf("shutdown ratelimiter\n");
88 	isc_ratelimiter_shutdown(rlim);
89 }
90 
91 static void
shutdown_all(isc_task_t * task,isc_event_t * event)92 shutdown_all(isc_task_t *task, isc_event_t *event) {
93 	int i;
94 	UNUSED(task);
95 	UNUSED(event);
96 	printf("shutdown all\n");
97 	for (i = 0; i < NEVENTS; i++) {
98 		isc_timer_detach(&timers[i]);
99 	}
100 
101 	isc_app_shutdown();
102 }
103 
104 int
main(int argc,char * argv[])105 main(int argc, char *argv[]) {
106 	isc_interval_t linterval;
107 	int i;
108 
109 	UNUSED(argc);
110 	UNUSED(argv);
111 
112 	isc_app_start();
113 	isc_interval_set(&linterval, 1, 0);
114 
115 	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
116 	RUNTIME_CHECK(isc_taskmgr_create(mctx, 3, 0, &taskmgr) ==
117 		      ISC_R_SUCCESS);
118 	RUNTIME_CHECK(isc_timermgr_create(mctx, &timermgr) ==
119 		      ISC_R_SUCCESS);
120 	RUNTIME_CHECK(isc_task_create(taskmgr, 0, &g_task) ==
121 		      ISC_R_SUCCESS);
122 
123 	RUNTIME_CHECK(isc_ratelimiter_create(mctx, timermgr, g_task,
124 					     &rlim) == ISC_R_SUCCESS);
125 
126 	RUNTIME_CHECK(isc_ratelimiter_setinterval(rlim, &linterval) ==
127 		      ISC_R_SUCCESS);
128 
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);
134 		timers[i] = NULL;
135 		RUNTIME_CHECK(isc_timer_create(timermgr,
136 					       isc_timertype_once, NULL,
137 					       &uinterval,
138 					       g_task, schedule[i].fun, NULL,
139 					       &timers[i]) == ISC_R_SUCCESS);
140 	}
141 
142 	isc_app_run();
143 
144 	isc_task_destroy(&g_task);
145 
146 	isc_ratelimiter_detach(&rlim);
147 
148 	isc_timermgr_destroy(&timermgr);
149 	isc_taskmgr_destroy(&taskmgr);
150 
151 	isc_mem_stats(mctx, stdout);
152 
153 	isc_app_finish();
154 	return (0);
155 }
156