gencache: Simplify gencache_stabilize
[Samba.git] / ctdb / tests / src / hash_count_test.c
blob6ddde084cff0d50d28e958e8dcc77e7fe97b4725
1 /*
2 hash_count tests
4 Copyright (C) Amitay Isaacs 2017
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "replace.h"
22 #include <assert.h>
24 #include "common/db_hash.c"
25 #include "common/hash_count.c"
27 #define KEY "this_is_a_test_key"
29 static void test1_handler(TDB_DATA key, uint64_t counter, void *private_data)
31 int *count = (int *)private_data;
33 assert(key.dsize == strlen(KEY));
34 assert(strcmp((char *)key.dptr, KEY) == 0);
35 assert(counter > 0);
37 (*count) += 1;
40 static void do_test1(void)
42 struct hash_count_context *hc = NULL;
43 TALLOC_CTX *mem_ctx = talloc_new(NULL);
44 struct timeval interval = {1, 0};
45 TDB_DATA key;
46 int count = 0;
47 int ret, i;
49 key.dptr = (uint8_t *)discard_const(KEY);
50 key.dsize = strlen(KEY);
52 ret = hash_count_increment(hc, key);
53 assert(ret == EINVAL);
55 ret = hash_count_init(mem_ctx, interval, NULL, NULL, &hc);
56 assert(ret == EINVAL);
58 ret = hash_count_init(mem_ctx, interval, test1_handler, &count, &hc);
59 assert(ret == 0);
60 assert(hc != NULL);
62 for (i=0; i<10; i++) {
63 ret = hash_count_increment(hc, key);
64 assert(ret == 0);
65 assert(count == i+1);
68 talloc_free(hc);
69 ret = talloc_get_size(mem_ctx);
70 assert(ret == 0);
72 talloc_free(mem_ctx);
75 static void test2_handler(TDB_DATA key, uint64_t counter, void *private_data)
77 uint64_t *count = (uint64_t *)private_data;
79 *count = counter;
82 static void do_test2(void)
84 struct hash_count_context *hc;
85 TALLOC_CTX *mem_ctx = talloc_new(NULL);
86 struct timeval interval = {1, 0};
87 TDB_DATA key;
88 uint64_t count = 0;
89 int ret;
91 key.dptr = (uint8_t *)discard_const(KEY);
92 key.dsize = strlen(KEY);
94 ret = hash_count_init(mem_ctx, interval, test2_handler, &count, &hc);
95 assert(ret == 0);
97 ret = hash_count_increment(hc, key);
98 assert(ret == 0);
99 assert(count == 1);
101 hash_count_expire(hc, &ret);
102 assert(ret == 0);
104 ret = hash_count_increment(hc, key);
105 assert(ret == 0);
106 assert(count == 2);
108 sleep(2);
110 ret = hash_count_increment(hc, key);
111 assert(ret == 0);
112 assert(count == 1);
114 sleep(2);
116 hash_count_expire(hc, &ret);
117 assert(ret == 1);
119 talloc_free(hc);
120 ret = talloc_get_size(mem_ctx);
121 assert(ret == 0);
123 talloc_free(mem_ctx);
126 int main(void)
128 do_test1();
129 do_test2();
131 return 0;