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/>.
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);
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};
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
);
62 for (i
=0; i
<10; i
++) {
63 ret
= hash_count_increment(hc
, key
);
69 ret
= talloc_get_size(mem_ctx
);
75 static void test2_handler(TDB_DATA key
, uint64_t counter
, void *private_data
)
77 uint64_t *count
= (uint64_t *)private_data
;
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};
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
);
97 ret
= hash_count_increment(hc
, key
);
101 hash_count_expire(hc
, &ret
);
104 ret
= hash_count_increment(hc
, key
);
110 ret
= hash_count_increment(hc
, key
);
116 hash_count_expire(hc
, &ret
);
120 ret
= talloc_get_size(mem_ctx
);
123 talloc_free(mem_ctx
);