1 /* 2 hash_count tests 3 4 Copyright (C) Amitay Isaacs 2017 5 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. 10 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. 15 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/>. 18 */ 19 20 #include "replace.h" 21 22 #include <assert.h> 23 24 #include "common/db_hash.c" 25 #include "common/hash_count.c" 26 27 #define KEY "this_is_a_test_key" 28 29 static void test1_handler(TDB_DATA key, uint64_t counter, void *private_data) 30 { 31 int *count = (int *)private_data; 32 33 assert(key.dsize == strlen(KEY)); 34 assert(strcmp((char *)key.dptr, KEY) == 0); 35 assert(counter > 0); 36 37 (*count) += 1; 38 } 39 40 static void do_test1(void) 41 { 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; 48 49 key.dptr = (uint8_t *)discard_const(KEY); glock_loop_send(TALLOC_CTX * mem_ctx,struct tevent_context * ev,struct ctdb_client_context * client,struct ctdb_db_context * db,int num_nodes,int timelimit)50 key.dsize = strlen(KEY); 51 52 ret = hash_count_increment(hc, key); 53 assert(ret == EINVAL); 54 55 ret = hash_count_init(mem_ctx, interval, NULL, NULL, &hc); 56 assert(ret == EINVAL); 57 58 ret = hash_count_init(mem_ctx, interval, test1_handler, &count, &hc); 59 assert(ret == 0); 60 assert(hc != NULL); 61 62 for (i=0; i<10; i++) { 63 ret = hash_count_increment(hc, key); 64 assert(ret == 0); 65 assert(count == i+1); 66 } 67 68 talloc_free(hc); 69 ret = talloc_get_size(mem_ctx); 70 assert(ret == 0); 71 72 talloc_free(mem_ctx); 73 } 74 75 static void test2_handler(TDB_DATA key, uint64_t counter, void *private_data) 76 { 77 uint64_t *count = (uint64_t *)private_data; 78 79 *count = counter; 80 } 81 82 static void do_test2(void) 83 { 84 struct hash_count_context *hc; 85 TALLOC_CTX *mem_ctx = talloc_new(NULL); glock_loop_start(struct tevent_req * subreq)86 struct timeval interval = {1, 0}; 87 TDB_DATA key; 88 uint64_t count = 0; 89 int ret; 90 91 key.dptr = (uint8_t *)discard_const(KEY); 92 key.dsize = strlen(KEY); 93 94 ret = hash_count_init(mem_ctx, interval, test2_handler, &count, &hc); 95 assert(ret == 0); 96 97 ret = hash_count_increment(hc, key); 98 assert(ret == 0); 99 assert(count == 1); 100 101 hash_count_expire(hc, &ret); 102 assert(ret == 0); 103 104 ret = hash_count_increment(hc, key); 105 assert(ret == 0); 106 assert(count == 2); 107 108 sleep(2); 109 110 ret = hash_count_increment(hc, key); 111 assert(ret == 0); 112 assert(count == 1); 113 114 sleep(2); 115 116 hash_count_expire(hc, &ret); 117 assert(ret == 1); 118 glock_loop_locked(struct tevent_req * subreq)119 talloc_free(hc); 120 ret = talloc_get_size(mem_ctx); 121 assert(ret == 0); 122 123 talloc_free(mem_ctx); 124 } 125 126 int main(void) 127 { 128 do_test1(); 129 do_test2(); 130 131 return 0; 132 } 133