Asterisk - The Open Source Telephony Project  18.5.0
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 = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .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.

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(), sip_to_pjsip::info(), lock_thread(), NULL, TEST_EXECUTE, and TEST_INIT.

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 
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 }
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
#define ao2_unlock(a)
Definition: astobj2.h:730
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:98
#define NULL
Definition: resample.c:96
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
#define ao2_lock(a)
Definition: astobj2.h:718
static void * lock_thread(void *data)
#define ao2_trylock(a)
Definition: astobj2.h:740
def info(msg)
#define ast_pthread_create(a, b, c, d)
Definition: utils.h:559
ast_test_result_state
Definition: test.h:200
#define ast_named_lock_get(lock_type, keyspace, key)
Geta named lock handle.
Definition: named_locks.h:83
#define ast_named_lock_put(lock)
Put a named lock handle away.
Definition: named_locks.h:93

◆ load_module()

static int load_module ( void  )
static

Definition at line 145 of file test_named_lock.c.

References AST_MODULE_LOAD_SUCCESS, and AST_TEST_REGISTER.

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

◆ lock_thread()

static void* lock_thread ( void *  data)
static

Definition at line 41 of file test_named_lock.c.

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().

42 {
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 }
#define ao2_unlock(a)
Definition: astobj2.h:730
#define NULL
Definition: resample.c:96
ast_mutex_t lock
Definition: app_meetme.c:1091
#define ao2_lock(a)
Definition: astobj2.h:718
#define ast_named_lock_get(lock_type, keyspace, key)
Geta named lock handle.
Definition: named_locks.h:83
#define ast_named_lock_put(lock)
Put a named lock handle away.
Definition: named_locks.h:93

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 139 of file test_named_lock.c.

References AST_TEST_UNREGISTER.

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

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Named Lock test module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .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.