123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- /*
- * 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, NULL, ao2_cleanup);
- RAII_VAR(struct test_struct *, 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;
- container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 13, NULL, NULL, NULL);
- object = ao2_alloc(sizeof(*object), NULL);
- 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");
|