pool_test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /* $Id$ */
00018 
00019 /*! \file */
00020 
00021 #include <config.h>
00022 
00023 #include <atf-c.h>
00024 
00025 #include <unistd.h>
00026 
00027 #include <isc/mem.h>
00028 #include <isc/pool.h>
00029 
00030 #include "isctest.h"
00031 
00032 static isc_result_t
00033 poolinit(void **target, void *arg) {
00034         isc_result_t result;
00035 
00036         isc_taskmgr_t *mgr = (isc_taskmgr_t *) arg;
00037         isc_task_t *task = NULL;
00038         result = isc_task_create(mgr, 0, &task);
00039         if (result != ISC_R_SUCCESS)
00040                 return (result);
00041 
00042         *target = (void *) task;
00043         return (ISC_R_SUCCESS);
00044 }
00045 
00046 static void
00047 poolfree(void **target) {
00048         isc_task_t *task = *(isc_task_t **) target;
00049         isc_task_destroy(&task);
00050         *target = NULL;
00051 }
00052 
00053 /*
00054  * Individual unit tests
00055  */
00056 
00057 /* Create a pool */
00058 ATF_TC(create_pool);
00059 ATF_TC_HEAD(create_pool, tc) {
00060         atf_tc_set_md_var(tc, "descr", "create a pool");
00061 }
00062 ATF_TC_BODY(create_pool, tc) {
00063         isc_result_t result;
00064         isc_pool_t *pool = NULL;
00065 
00066         UNUSED(tc);
00067 
00068         result = isc_test_begin(NULL, ISC_TRUE);
00069         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00070 
00071         result = isc_pool_create(mctx, 8, poolfree, poolinit, taskmgr, &pool);
00072         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00073         ATF_REQUIRE_EQ(isc_pool_count(pool), 8);
00074 
00075         isc_pool_destroy(&pool);
00076         ATF_REQUIRE_EQ(pool, NULL);
00077 
00078         isc_test_end();
00079 }
00080 
00081 /* Resize a pool */
00082 ATF_TC(expand_pool);
00083 ATF_TC_HEAD(expand_pool, tc) {
00084         atf_tc_set_md_var(tc, "descr", "expand a pool");
00085 }
00086 ATF_TC_BODY(expand_pool, tc) {
00087         isc_result_t result;
00088         isc_pool_t *pool1 = NULL, *pool2 = NULL, *hold = NULL;
00089 
00090         UNUSED(tc);
00091 
00092         result = isc_test_begin(NULL, ISC_TRUE);
00093         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00094 
00095         result = isc_pool_create(mctx, 10, poolfree, poolinit, taskmgr, &pool1);
00096         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00097         ATF_REQUIRE_EQ(isc_pool_count(pool1), 10);
00098 
00099         /* resizing to a smaller size should have no effect */
00100         hold = pool1;
00101         result = isc_pool_expand(&pool1, 5, &pool2);
00102         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00103         ATF_REQUIRE_EQ(isc_pool_count(pool2), 10);
00104         ATF_REQUIRE_EQ(pool2, hold);
00105         ATF_REQUIRE_EQ(pool1, NULL);
00106         pool1 = pool2;
00107         pool2 = NULL;
00108 
00109         /* resizing to the same size should have no effect */
00110         hold = pool1;
00111         result = isc_pool_expand(&pool1, 10, &pool2);
00112         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00113         ATF_REQUIRE_EQ(isc_pool_count(pool2), 10);
00114         ATF_REQUIRE_EQ(pool2, hold);
00115         ATF_REQUIRE_EQ(pool1, NULL);
00116         pool1 = pool2;
00117         pool2 = NULL;
00118 
00119         /* resizing to larger size should make a new pool */
00120         hold = pool1;
00121         result = isc_pool_expand(&pool1, 20, &pool2);
00122         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00123         ATF_REQUIRE_EQ(isc_pool_count(pool2), 20);
00124         ATF_REQUIRE(pool2 != hold);
00125         ATF_REQUIRE_EQ(pool1, NULL);
00126 
00127         isc_pool_destroy(&pool2);
00128         ATF_REQUIRE_EQ(pool2, NULL);
00129 
00130         isc_test_end();
00131 }
00132 
00133 /* Get objects */
00134 ATF_TC(get_objects);
00135 ATF_TC_HEAD(get_objects, tc) {
00136         atf_tc_set_md_var(tc, "descr", "get objects");
00137 }
00138 ATF_TC_BODY(get_objects, tc) {
00139         isc_result_t result;
00140         isc_pool_t *pool = NULL;
00141         void *item;
00142         isc_task_t *task1 = NULL, *task2 = NULL, *task3 = NULL;
00143 
00144         UNUSED(tc);
00145 
00146         result = isc_test_begin(NULL, ISC_TRUE);
00147         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00148 
00149         result = isc_pool_create(mctx, 2, poolfree, poolinit, taskmgr, &pool);
00150         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00151         ATF_REQUIRE_EQ(isc_pool_count(pool), 2);
00152 
00153         item = isc_pool_get(pool);
00154         ATF_REQUIRE(item != NULL);
00155         isc_task_attach((isc_task_t *) item, &task1);
00156 
00157         item = isc_pool_get(pool);
00158         ATF_REQUIRE(item != NULL);
00159         isc_task_attach((isc_task_t *) item, &task2);
00160 
00161         item = isc_pool_get(pool);
00162         ATF_REQUIRE(item != NULL);
00163         isc_task_attach((isc_task_t *) item, &task3);
00164 
00165         isc_task_detach(&task1);
00166         isc_task_detach(&task2);
00167         isc_task_detach(&task3);
00168 
00169         isc_pool_destroy(&pool);
00170         ATF_REQUIRE_EQ(pool, NULL);
00171 
00172         isc_test_end();
00173 }
00174 
00175 
00176 /*
00177  * Main
00178  */
00179 ATF_TP_ADD_TCS(tp) {
00180         ATF_TP_ADD_TC(tp, create_pool);
00181         ATF_TP_ADD_TC(tp, expand_pool);
00182         ATF_TP_ADD_TC(tp, get_objects);
00183 
00184         return (atf_no_error());
00185 }
00186 

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha