1 /* $NetBSD: pool_test.c,v 1.1.1.3 2014/12/10 03:34:44 christos Exp $ */
2
3 /*
4 * Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 /* Id */
20
21 /*! \file */
22
23 #include <config.h>
24
25 #include <atf-c.h>
26
27 #include <unistd.h>
28
29 #include <isc/mem.h>
30 #include <isc/pool.h>
31
32 #include "isctest.h"
33
34 static isc_result_t
poolinit(void ** target,void * arg)35 poolinit(void **target, void *arg) {
36 isc_result_t result;
37
38 isc_taskmgr_t *mgr = (isc_taskmgr_t *) arg;
39 isc_task_t *task = NULL;
40 result = isc_task_create(mgr, 0, &task);
41 if (result != ISC_R_SUCCESS)
42 return (result);
43
44 *target = (void *) task;
45 return (ISC_R_SUCCESS);
46 }
47
48 static void
poolfree(void ** target)49 poolfree(void **target) {
50 isc_task_t *task = *(isc_task_t **) target;
51 isc_task_destroy(&task);
52 *target = NULL;
53 }
54
55 /*
56 * Individual unit tests
57 */
58
59 /* Create a pool */
60 ATF_TC(create_pool);
ATF_TC_HEAD(create_pool,tc)61 ATF_TC_HEAD(create_pool, tc) {
62 atf_tc_set_md_var(tc, "descr", "create a pool");
63 }
ATF_TC_BODY(create_pool,tc)64 ATF_TC_BODY(create_pool, tc) {
65 isc_result_t result;
66 isc_pool_t *pool = NULL;
67
68 UNUSED(tc);
69
70 result = isc_test_begin(NULL, ISC_TRUE);
71 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
72
73 result = isc_pool_create(mctx, 8, poolfree, poolinit, taskmgr, &pool);
74 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
75 ATF_REQUIRE_EQ(isc_pool_count(pool), 8);
76
77 isc_pool_destroy(&pool);
78 ATF_REQUIRE_EQ(pool, NULL);
79
80 isc_test_end();
81 }
82
83 /* Resize a pool */
84 ATF_TC(expand_pool);
ATF_TC_HEAD(expand_pool,tc)85 ATF_TC_HEAD(expand_pool, tc) {
86 atf_tc_set_md_var(tc, "descr", "expand a pool");
87 }
ATF_TC_BODY(expand_pool,tc)88 ATF_TC_BODY(expand_pool, tc) {
89 isc_result_t result;
90 isc_pool_t *pool1 = NULL, *pool2 = NULL, *hold = NULL;
91
92 UNUSED(tc);
93
94 result = isc_test_begin(NULL, ISC_TRUE);
95 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
96
97 result = isc_pool_create(mctx, 10, poolfree, poolinit, taskmgr, &pool1);
98 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
99 ATF_REQUIRE_EQ(isc_pool_count(pool1), 10);
100
101 /* resizing to a smaller size should have no effect */
102 hold = pool1;
103 result = isc_pool_expand(&pool1, 5, &pool2);
104 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
105 ATF_REQUIRE_EQ(isc_pool_count(pool2), 10);
106 ATF_REQUIRE_EQ(pool2, hold);
107 ATF_REQUIRE_EQ(pool1, NULL);
108 pool1 = pool2;
109 pool2 = NULL;
110
111 /* resizing to the same size should have no effect */
112 hold = pool1;
113 result = isc_pool_expand(&pool1, 10, &pool2);
114 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
115 ATF_REQUIRE_EQ(isc_pool_count(pool2), 10);
116 ATF_REQUIRE_EQ(pool2, hold);
117 ATF_REQUIRE_EQ(pool1, NULL);
118 pool1 = pool2;
119 pool2 = NULL;
120
121 /* resizing to larger size should make a new pool */
122 hold = pool1;
123 result = isc_pool_expand(&pool1, 20, &pool2);
124 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
125 ATF_REQUIRE_EQ(isc_pool_count(pool2), 20);
126 ATF_REQUIRE(pool2 != hold);
127 ATF_REQUIRE_EQ(pool1, NULL);
128
129 isc_pool_destroy(&pool2);
130 ATF_REQUIRE_EQ(pool2, NULL);
131
132 isc_test_end();
133 }
134
135 /* Get objects */
136 ATF_TC(get_objects);
ATF_TC_HEAD(get_objects,tc)137 ATF_TC_HEAD(get_objects, tc) {
138 atf_tc_set_md_var(tc, "descr", "get objects");
139 }
ATF_TC_BODY(get_objects,tc)140 ATF_TC_BODY(get_objects, tc) {
141 isc_result_t result;
142 isc_pool_t *pool = NULL;
143 void *item;
144 isc_task_t *task1 = NULL, *task2 = NULL, *task3 = NULL;
145
146 UNUSED(tc);
147
148 result = isc_test_begin(NULL, ISC_TRUE);
149 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
150
151 result = isc_pool_create(mctx, 2, poolfree, poolinit, taskmgr, &pool);
152 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
153 ATF_REQUIRE_EQ(isc_pool_count(pool), 2);
154
155 item = isc_pool_get(pool);
156 ATF_REQUIRE(item != NULL);
157 isc_task_attach((isc_task_t *) item, &task1);
158
159 item = isc_pool_get(pool);
160 ATF_REQUIRE(item != NULL);
161 isc_task_attach((isc_task_t *) item, &task2);
162
163 item = isc_pool_get(pool);
164 ATF_REQUIRE(item != NULL);
165 isc_task_attach((isc_task_t *) item, &task3);
166
167 isc_task_detach(&task1);
168 isc_task_detach(&task2);
169 isc_task_detach(&task3);
170
171 isc_pool_destroy(&pool);
172 ATF_REQUIRE_EQ(pool, NULL);
173
174 isc_test_end();
175 }
176
177
178 /*
179 * Main
180 */
ATF_TP_ADD_TCS(tp)181 ATF_TP_ADD_TCS(tp) {
182 ATF_TP_ADD_TC(tp, create_pool);
183 ATF_TP_ADD_TC(tp, expand_pool);
184 ATF_TP_ADD_TC(tp, get_objects);
185
186 return (atf_no_error());
187 }
188
189