summaryrefslogtreecommitdiff
path: root/tests/test_scoped_lock.c
blob: 1881bce7e135a6d3dd590ab1da33632674c37e0e (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2012, Digium, Inc.
 *
 * Mark Michelson <mmichelson@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*!
 * \file
 * \brief SCOPED_LOCK unit tests
 *
 * \author Mark Michelson <mmichelson@digium.com>
 *
 */

/*** MODULEINFO
	<depend>TEST_FRAMEWORK</depend>
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include "asterisk/test.h"
#include "asterisk/utils.h"
#include "asterisk/module.h"
#include "asterisk/astobj2.h"

static int indicator;
static struct ast_test *current_test;
AST_MUTEX_DEFINE_STATIC(the_lock);

static void lock_it(ast_mutex_t *lock)
{
	indicator = 1;
	ast_mutex_lock(lock);
}

static void unlock_it(ast_mutex_t *lock)
{
	indicator = 0;
	ast_mutex_unlock(lock);
}

AST_TEST_DEFINE(lock_test)
{
	enum ast_test_result_state res = AST_TEST_PASS;
	int i;

	switch(cmd) {
	case TEST_INIT:
		info->name = "lock_test";
		info->category = "/main/lock/";
		info->summary = "SCOPED_LOCK test";
		info->description =
			"Tests that scoped locks are scoped as they are expected to be";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	current_test = test;
	indicator = 0;
	{
		SCOPED_LOCK(lock, &the_lock, lock_it, unlock_it);
		if (indicator != 1) {
			ast_log(LOG_ERROR, "The lock was not acquired via RAII");
			res = AST_TEST_FAIL;
		}
	}
	if (indicator != 0) {
		ast_log(LOG_ERROR, "The lock was not released when the variable went out of scope");
		res = AST_TEST_FAIL;
	}

	for (i = 0; i < 10; ++i) {
		SCOPED_LOCK(lock, &the_lock, lock_it, unlock_it);
		if (indicator != 1) {
			ast_log(LOG_ERROR, "The lock was not acquired via RAII");
			res = AST_TEST_FAIL;
		}
	}

	if (indicator != 0) {
		ast_log(LOG_ERROR, "The lock was not released when the variable went out of scope");
		res = AST_TEST_FAIL;
	}

	return res;
}

struct test_struct
{
	int locked;
	int reffed;
};

/*!
 * \brief lock callback function
 *
 * Locks the object passed in. Only sets the locked
 * flag if the object is reffed. This allows us to check
 * that locking is always occurring after reffing.
 */
static void test_lock(struct test_struct *test)
{
	ast_test_status_update(current_test, "Lock is occurring\n");
	ao2_lock(test);
	if (test->reffed) {
		test->locked = 1;
	}
}

/*!
 * \brief unlock callback function
 *
 * Unlocks the object passed in. Only clears the locked
 * flag if the object is still reffed. This allows us to
 * ensure that unlocking is always occurring before unreffing.
 */
static void test_unlock(struct test_struct *test)
{
	ast_test_status_update(current_test, "Unlock is occurring\n");
	ao2_unlock(test);
	if (test->reffed) {
		test->locked = 0;
	}
}

/*!
 * \brief ref callback function
 *
 * Refs the object passed in. Only sets the reffed flag if
 * the object is not locked. This allows us to ensure that
 * reffing always occurs before locking.
 */
static struct test_struct *test_ref(struct test_struct *test)
{
	ast_test_status_update(current_test, "Ref is occurring\n");
	ao2_ref(test, +1);
	if (!test->locked) {
		test->reffed = 1;
	}
	return test;
}

/*!
 * \brief unref callback function
 *
 * Unrefs the object passed in. Only sets the unreffed flag if
 * the object is not locked. This allows us to ensure that
 * unreffing always occurs after unlocking.
 */
static void test_unref(struct test_struct *test)
{
	ast_test_status_update(current_test, "Unref is occurring\n");
	ao2_ref(test, -1);
	if (!test->locked) {
		test->reffed = 0;
	}
}

/*!
 * \brief wrapper for ao2_iterator_next
 *
 * Grabs the next item in the container and replaces the ref acquired
 * from ao2_iterator_next() with a call to test_ref().
 */
static struct test_struct *test_iterator_next(struct ao2_iterator *iter)
{
	struct test_struct *test = ao2_iterator_next(iter);

	if (!test) {
		return NULL;
	}

	/* Remove ref from ao2_iterator_next() and replace it with
	 * a test_ref() call. The order here is safe since we can guarantee
	 * the container still has a ref to the test structure.
	 */
	ao2_ref(test, -1);
	test_ref(test);

	return test;
}

AST_TEST_DEFINE(cleanup_order)
{
	enum ast_test_result_state res = AST_TEST_PASS;
	struct ao2_iterator iter;
	struct test_struct *object_iter;
	RAII_VAR(struct ao2_container*, container, ao2_container_alloc(13, NULL, NULL), ao2_cleanup);
	RAII_VAR(struct test_struct *, object, ao2_alloc(sizeof(*object), NULL), ao2_cleanup);

	switch(cmd) {
	case TEST_INIT:
		info->name = "cleanup_order_test";
		info->category = "/main/lock/";
		info->summary = "cleanup order test";
		info->description =
			"Tests that variables with cleanup attributes are cleaned up\n"
			"in the reverse order they are declared.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}
	current_test = test;

	if (!object || !container) {
		/* Allocation failure. We can't even pretend to do this test properly */
		return AST_TEST_FAIL;
	}

	{
		/* Purpose of this block is to make sure that the cleanup operations
		 * run in the reverse order that they were created here.
		 */
		RAII_VAR(struct test_struct *, object2, test_ref(object), test_unref);
		SCOPED_LOCK(lock, object, test_lock, test_unlock);
		if (!object->reffed || !object->locked) {
			ast_log(LOG_ERROR, "Test failed due to out of order initializations");
			res = AST_TEST_FAIL;
		}
	}

	if (object->reffed || object->locked) {
		ast_log(LOG_ERROR, "Test failed due to out of order cleanups\n");
		res = AST_TEST_FAIL;
	}

	/* Now link the object into the container for a little experiment ... */
	ao2_link(container, object);

	/* This loop is to ensure that unrefs in a for loop occur after the cleanup
	 * operations of items inside the loop. If we hope to be able to mix scoped locks
	 * and ao2 refs, this is the way to go about it.
	 */
	for (iter = ao2_iterator_init(container, 0);
			(object_iter = test_iterator_next(&iter));
			test_unref(object_iter)) {
		SCOPED_LOCK(lock, object_iter, test_lock, test_unlock);
		if (!object->reffed || !object->locked) {
			ast_log(LOG_ERROR, "Test failed due to out of order initializations");
			res = AST_TEST_FAIL;
		}
	}
	ao2_iterator_destroy(&iter);

	if (object->reffed || object->locked) {
		ast_log(LOG_ERROR, "Test failed due to out of order cleanups\n");
		res = AST_TEST_FAIL;
	}

	return res;
}

static int unload_module(void)
{
	AST_TEST_UNREGISTER(lock_test);
	AST_TEST_UNREGISTER(cleanup_order);
	return 0;
}

static int load_module(void)
{
	AST_TEST_REGISTER(lock_test);
	AST_TEST_REGISTER(cleanup_order);
	return AST_MODULE_LOAD_SUCCESS;
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SCOPED_LOCK test module");