Asterisk - The Open Source Telephony Project GIT-master-b023714
Loading...
Searching...
No Matches
Functions | Variables
test_named_lock.c File Reference

Named Lock unit tests. More...

#include "asterisk.h"
#include <signal.h>
#include "asterisk/test.h"
#include "asterisk/utils.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/named_locks.h"
Include dependency graph for test_named_lock.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (named_lock_test)
 
static int load_module (void)
 
static void * lock_thread (void *data)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Named Lock test module" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

Named Lock unit tests.

Author
George Joseph georg.nosp@m.e.jo.nosp@m.seph@.nosp@m.fair.nosp@m.view5.nosp@m..com

Definition in file test_named_lock.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 151 of file test_named_lock.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 151 of file test_named_lock.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 151 of file test_named_lock.c.

◆ AST_TEST_DEFINE()

AST_TEST_DEFINE ( named_lock_test  )

Definition at line 58 of file test_named_lock.c.

59{
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 /* 2 locks/threads to make sure they're independent */
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 /* These should both fail */
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 /* These should both succeed eventually */
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
127fail:
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}
#define ao2_unlock(a)
Definition astobj2.h:729
#define ao2_lock(a)
Definition astobj2.h:717
#define ao2_trylock(a)
Definition astobj2.h:739
#define ast_named_lock_put(lock)
Put a named lock handle away.
Definition named_locks.h:93
#define ast_named_lock_get(lock_type, keyspace, key)
Geta named lock handle.
Definition named_locks.h:83
@ AST_NAMED_LOCK_TYPE_MUTEX
Definition named_locks.h:59
#define NULL
Definition resample.c:96
@ TEST_INIT
Definition test.h:200
@ TEST_EXECUTE
Definition test.h:201
#define ast_test_status_update(a, b, c...)
Definition test.h:129
ast_test_result_state
Definition test.h:193
@ AST_TEST_PASS
Definition test.h:195
@ AST_TEST_FAIL
Definition test.h:196
@ AST_TEST_NOT_RUN
Definition test.h:194
static void * lock_thread(void *data)
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition time.h:107
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition time.h:159
#define ast_pthread_create(a, b, c, d)
Definition utils.h:621

References ao2_lock, ao2_trylock, ao2_unlock, ast_named_lock_get, ast_named_lock_put, AST_NAMED_LOCK_TYPE_MUTEX, ast_pthread_create, AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, ast_tvdiff_ms(), ast_tvnow(), lock_thread(), NULL, TEST_EXECUTE, and TEST_INIT.

◆ load_module()

static int load_module ( void  )
static

Definition at line 145 of file test_named_lock.c.

146{
147 AST_TEST_REGISTER(named_lock_test);
149}
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
#define AST_TEST_REGISTER(cb)
Definition test.h:127

References AST_MODULE_LOAD_SUCCESS, and AST_TEST_REGISTER.

◆ lock_thread()

static void * lock_thread ( void *  data)
static

Definition at line 41 of file test_named_lock.c.

42{
44
45 if (!lock) {
46 return NULL;
47 }
48
50 usleep(3000000);
52
54
55 return NULL;
56}
ast_mutex_t lock
Definition app_sla.c:337

References ao2_lock, ao2_unlock, ast_named_lock_get, ast_named_lock_put, AST_NAMED_LOCK_TYPE_MUTEX, lock, and NULL.

Referenced by AST_TEST_DEFINE().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 139 of file test_named_lock.c.

140{
141 AST_TEST_UNREGISTER(named_lock_test);
142 return 0;
143}
#define AST_TEST_UNREGISTER(cb)
Definition test.h:128

References AST_TEST_UNREGISTER.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Named Lock test module" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 151 of file test_named_lock.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 151 of file test_named_lock.c.