| 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 |
#include "asterisk.h" |
| 33 |
#include <signal.h> |
| 34 |
|
| 35 |
#include "asterisk/test.h" |
| 36 |
#include "asterisk/utils.h" |
| 37 |
#include "asterisk/module.h" |
| 38 |
#include "asterisk/lock.h" |
| 39 |
#include "asterisk/named_locks.h" |
| 40 |
|
| 41 |
static void *lock_thread(void *data) |
| 42 |
{ |
| 43 |
struct ast_named_lock *lock = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "lock_test", data); |
| 44 |
|
| 45 |
if (!lock) { |
| 46 |
return NULL; |
| 47 |
} |
| 48 |
|
| 49 |
ao2_lock(lock); |
| 50 |
usleep(3000000); |
| 51 |
ao2_unlock(lock); |
| 52 |
|
| 53 |
ast_named_lock_put(lock); |
| 54 |
|
| 55 |
return NULL; |
| 56 |
} |
| 57 |
|
| 58 |
AST_TEST_DEFINE(named_lock_test) |
| 59 |
{ |
| 60 |
enum ast_test_result_state res = AST_TEST_FAIL; |
| 61 |
struct ast_named_lock *lock1 = NULL; |
| 62 |
struct ast_named_lock *lock2 = NULL; |
| 63 |
pthread_t thread1; |
| 64 |
pthread_t thread2; |
| 65 |
struct timeval start_time; |
| 66 |
int64_t duration; |
| 67 |
|
| 68 |
switch(cmd) { |
| 69 |
case TEST_INIT: |
| 70 |
info->name = "named_lock_test"; |
| 71 |
info->category = "/main/lock/"; |
| 72 |
info->summary = "Named Lock test"; |
| 73 |
info->description = |
| 74 |
"Tests that named locks operate as expected"; |
| 75 |
return AST_TEST_NOT_RUN; |
| 76 |
case TEST_EXECUTE: |
| 77 |
break; |
| 78 |
} |
| 79 |
|
| 80 |
ast_test_status_update(test, "This test should take about 3 seconds\n"); |
| 81 |
|
| 82 |
|
| 83 |
ast_pthread_create(&thread1, NULL, lock_thread, "lock_1"); |
| 84 |
ast_pthread_create(&thread2, NULL, lock_thread, "lock_2"); |
| 85 |
|
| 86 |
lock1 = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "lock_test", "lock_1"); |
| 87 |
ast_test_validate_cleanup(test, lock1 != NULL, res, fail); |
| 88 |
|
| 89 |
lock2 = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "lock_test", "lock_2"); |
| 90 |
ast_test_validate_cleanup(test, lock2 != NULL, res, fail); |
| 91 |
|
| 92 |
usleep(1000000); |
| 93 |
|
| 94 |
|
| 95 |
if (!ao2_trylock(lock1)) { |
| 96 |
ast_test_status_update(test, "ao2_trylock on lock1 succeeded when it should have failed\n"); |
| 97 |
ao2_unlock(lock1); |
| 98 |
goto fail; |
| 99 |
} |
| 100 |
|
| 101 |
if (!ao2_trylock(lock2)) { |
| 102 |
ast_test_status_update(test, "ao2_trylock on lock2 succeeded when it should have failed\n"); |
| 103 |
ao2_unlock(lock2); |
| 104 |
goto fail; |
| 105 |
} |
| 106 |
|
| 107 |
start_time = ast_tvnow(); |
| 108 |
|
| 109 |
|
| 110 |
if (ao2_lock(lock1)) { |
| 111 |
ast_test_status_update(test, "ao2_lock on lock1 failed\n"); |
| 112 |
goto fail; |
| 113 |
} |
| 114 |
ao2_unlock(lock1); |
| 115 |
|
| 116 |
if (ao2_lock(lock2)) { |
| 117 |
ast_test_status_update(test, "ao2_lock on lock2 failed\n"); |
| 118 |
goto fail; |
| 119 |
} |
| 120 |
ao2_unlock(lock2); |
| 121 |
|
| 122 |
duration = ast_tvdiff_ms(ast_tvnow(), start_time); |
| 123 |
ast_test_validate_cleanup(test, duration > 1500 && duration < 3500, res, fail); |
| 124 |
|
| 125 |
res = AST_TEST_PASS; |
| 126 |
|
| 127 |
fail: |
| 128 |
|
| 129 |
ast_named_lock_put(lock1); |
| 130 |
ast_named_lock_put(lock2); |
| 131 |
|
| 132 |
pthread_join(thread1, NULL); |
| 133 |
pthread_join(thread2, NULL); |
| 134 |
|
| 135 |
return res; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
static int unload_module(void) |
| 140 |
{ |
| 141 |
AST_TEST_UNREGISTER(named_lock_test); |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
static int load_module(void) |
| 146 |
{ |
| 147 |
AST_TEST_REGISTER(named_lock_test); |
| 148 |
return AST_MODULE_LOAD_SUCCESS; |
| 149 |
} |
| 150 |
|
| 151 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Named Lock test module"); |