summaryrefslogtreecommitdiffstats
path: root/ctdb/tests/src/hash_count_test.c
blob: 6ddde084cff0d50d28e958e8dcc77e7fe97b4725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
   hash_count tests

   Copyright (C) Amitay Isaacs  2017

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"

#include <assert.h>

#include "common/db_hash.c"
#include "common/hash_count.c"

#define KEY	"this_is_a_test_key"

static void test1_handler(TDB_DATA key, uint64_t counter, void *private_data)
{
	int *count = (int *)private_data;

	assert(key.dsize == strlen(KEY));
	assert(strcmp((char *)key.dptr, KEY) == 0);
	assert(counter > 0);

	(*count) += 1;
}

static void do_test1(void)
{
	struct hash_count_context *hc = NULL;
	TALLOC_CTX *mem_ctx = talloc_new(NULL);
	struct timeval interval = {1, 0};
	TDB_DATA key;
	int count = 0;
	int ret, i;

	key.dptr = (uint8_t *)discard_const(KEY);
	key.dsize = strlen(KEY);

	ret = hash_count_increment(hc, key);
	assert(ret == EINVAL);

	ret = hash_count_init(mem_ctx, interval, NULL, NULL, &hc);
	assert(ret == EINVAL);

	ret = hash_count_init(mem_ctx, interval, test1_handler, &count, &hc);
	assert(ret == 0);
	assert(hc != NULL);

	for (i=0; i<10; i++) {
		ret = hash_count_increment(hc, key);
		assert(ret == 0);
		assert(count == i+1);
	}

	talloc_free(hc);
	ret = talloc_get_size(mem_ctx);
	assert(ret == 0);

	talloc_free(mem_ctx);
}

static void test2_handler(TDB_DATA key, uint64_t counter, void *private_data)
{
	uint64_t *count = (uint64_t *)private_data;

	*count = counter;
}

static void do_test2(void)
{
	struct hash_count_context *hc;
	TALLOC_CTX *mem_ctx = talloc_new(NULL);
	struct timeval interval = {1, 0};
	TDB_DATA key;
	uint64_t count = 0;
	int ret;

	key.dptr = (uint8_t *)discard_const(KEY);
	key.dsize = strlen(KEY);

	ret = hash_count_init(mem_ctx, interval, test2_handler, &count, &hc);
	assert(ret == 0);

	ret = hash_count_increment(hc, key);
	assert(ret == 0);
	assert(count == 1);

	hash_count_expire(hc, &ret);
	assert(ret == 0);

	ret = hash_count_increment(hc, key);
	assert(ret == 0);
	assert(count == 2);

	sleep(2);

	ret = hash_count_increment(hc, key);
	assert(ret == 0);
	assert(count == 1);

	sleep(2);

	hash_count_expire(hc, &ret);
	assert(ret == 1);

	talloc_free(hc);
	ret = talloc_get_size(mem_ctx);
	assert(ret == 0);

	talloc_free(mem_ctx);
}

int main(void)
{
	do_test1();
	do_test2();

	return 0;
}