Pass full pool list to release_address
[strongswan.git] / src / libcharon / plugins / unit_tester / tests / test_pool.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <time.h>
17
18 #include <library.h>
19 #include <threading/thread.h>
20 #include <hydra.h>
21
22 #define ALLOCS 1000
23 #define THREADS 20
24
25 static void* testing(void *thread)
26 {
27 int i;
28 host_t *addr[ALLOCS];
29 identification_t *id[ALLOCS];
30 linked_list_t *pools;
31
32 /* prepare identities */
33 for (i = 0; i < ALLOCS; i++)
34 {
35 char buf[256];
36
37 snprintf(buf, sizeof(buf), "%d-%d@strongswan.org", (uintptr_t)thread, i);
38 id[i] = identification_create_from_string(buf);
39 }
40
41 pools = linked_list_create();
42 pools->insert_last(pools, "test");
43
44 /* allocate addresses */
45 for (i = 0; i < ALLOCS; i++)
46 {
47 addr[i] = hydra->attributes->acquire_address(hydra->attributes,
48 pools, id[i], NULL);
49 if (!addr[i])
50 {
51 pools->destroy(pools);
52 return (void*)FALSE;
53 }
54 }
55
56 /* release addresses */
57 for (i = 0; i < ALLOCS; i++)
58 {
59 hydra->attributes->release_address(hydra->attributes,
60 pools, addr[i], id[i]);
61 }
62
63 pools->destroy(pools);
64
65 /* cleanup */
66 for (i = 0; i < ALLOCS; i++)
67 {
68 addr[i]->destroy(addr[i]);
69 id[i]->destroy(id[i]);
70 }
71 return (void*)TRUE;
72 }
73
74
75 /*******************************************************************************
76 * SQL pool performance test
77 ******************************************************************************/
78 bool test_pool()
79 {
80 thread_t *threads[THREADS];
81 uintptr_t i;
82
83 for (i = 0; i < THREADS; i++)
84 {
85 if (!(threads[i] = thread_create((thread_main_t)testing, (void*)i)))
86 {
87 return FALSE;
88 }
89 }
90 for (i = 0; i < THREADS; i++)
91 {
92 bool *res = threads[i]->join(threads[i]);
93 if (!res)
94 {
95 return FALSE;
96 }
97 }
98 return TRUE;
99 }
100