| 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 |
#include "asterisk.h" |
| 35 |
|
| 36 |
#include "asterisk/astobj2.h" |
| 37 |
#include "asterisk/channel.h" |
| 38 |
#include "asterisk/endpoints.h" |
| 39 |
#include "asterisk/module.h" |
| 40 |
#include "asterisk/stasis_channels.h" |
| 41 |
#include "asterisk/stasis_endpoints.h" |
| 42 |
#include "asterisk/stasis_test.h" |
| 43 |
#include "asterisk/test.h" |
| 44 |
|
| 45 |
static const char *test_category = "/stasis/endpoints/"; |
| 46 |
|
| 47 |
|
| 48 |
static int cache_update(struct stasis_message *msg, const void *data) { |
| 49 |
struct stasis_cache_update *update; |
| 50 |
struct ast_endpoint_snapshot *snapshot; |
| 51 |
const char *name = data; |
| 52 |
|
| 53 |
if (stasis_cache_update_type() != stasis_message_type(msg)) { |
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
update = stasis_message_data(msg); |
| 58 |
if (ast_endpoint_snapshot_type() != update->type) { |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
snapshot = stasis_message_data(update->old_snapshot); |
| 63 |
if (!snapshot) { |
| 64 |
snapshot = stasis_message_data(update->new_snapshot); |
| 65 |
} |
| 66 |
|
| 67 |
return 0 == strcmp(name, snapshot->resource); |
| 68 |
} |
| 69 |
|
| 70 |
AST_TEST_DEFINE(state_changes) |
| 71 |
{ |
| 72 |
RAII_VAR(struct ast_endpoint *, uut, NULL, ast_endpoint_shutdown); |
| 73 |
RAII_VAR(struct ast_channel *, chan, NULL, ast_hangup); |
| 74 |
RAII_VAR(struct stasis_message_sink *, sink, NULL, ao2_cleanup); |
| 75 |
RAII_VAR(struct stasis_subscription *, sub, NULL, stasis_unsubscribe); |
| 76 |
struct stasis_message *msg; |
| 77 |
struct stasis_message_type *type; |
| 78 |
struct ast_endpoint_snapshot *actual_snapshot; |
| 79 |
int actual_count; |
| 80 |
|
| 81 |
switch (cmd) { |
| 82 |
case TEST_INIT: |
| 83 |
info->name = __func__; |
| 84 |
info->category = test_category; |
| 85 |
info->summary = "Test endpoint updates as its state changes"; |
| 86 |
info->description = |
| 87 |
"Test endpoint updates as its state changes"; |
| 88 |
return AST_TEST_NOT_RUN; |
| 89 |
case TEST_EXECUTE: |
| 90 |
break; |
| 91 |
} |
| 92 |
|
| 93 |
uut = ast_endpoint_create("TEST", __func__); |
| 94 |
ast_test_validate(test, NULL != uut); |
| 95 |
|
| 96 |
sink = stasis_message_sink_create(); |
| 97 |
ast_test_validate(test, NULL != sink); |
| 98 |
|
| 99 |
sub = stasis_subscribe(ast_endpoint_topic(uut), |
| 100 |
stasis_message_sink_cb(), sink); |
| 101 |
ast_test_validate(test, NULL != sub); |
| 102 |
|
| 103 |
ast_endpoint_set_state(uut, AST_ENDPOINT_OFFLINE); |
| 104 |
actual_count = stasis_message_sink_wait_for_count(sink, 1, |
| 105 |
STASIS_SINK_DEFAULT_WAIT); |
| 106 |
ast_test_validate(test, 1 == actual_count); |
| 107 |
msg = sink->messages[0]; |
| 108 |
type = stasis_message_type(msg); |
| 109 |
ast_test_validate(test, ast_endpoint_snapshot_type() == type); |
| 110 |
actual_snapshot = stasis_message_data(msg); |
| 111 |
ast_test_validate(test, AST_ENDPOINT_OFFLINE == actual_snapshot->state); |
| 112 |
|
| 113 |
ast_endpoint_set_max_channels(uut, 8675309); |
| 114 |
actual_count = stasis_message_sink_wait_for_count(sink, 2, |
| 115 |
STASIS_SINK_DEFAULT_WAIT); |
| 116 |
ast_test_validate(test, 2 == actual_count); |
| 117 |
msg = sink->messages[1]; |
| 118 |
type = stasis_message_type(msg); |
| 119 |
ast_test_validate(test, ast_endpoint_snapshot_type() == type); |
| 120 |
actual_snapshot = stasis_message_data(msg); |
| 121 |
ast_test_validate(test, 8675309 == actual_snapshot->max_channels); |
| 122 |
|
| 123 |
return AST_TEST_PASS; |
| 124 |
} |
| 125 |
|
| 126 |
AST_TEST_DEFINE(cache_clear) |
| 127 |
{ |
| 128 |
RAII_VAR(struct ast_endpoint *, uut, NULL, ast_endpoint_shutdown); |
| 129 |
RAII_VAR(struct ast_channel *, chan, NULL, ast_hangup); |
| 130 |
RAII_VAR(struct stasis_message_sink *, sink, NULL, ao2_cleanup); |
| 131 |
RAII_VAR(struct stasis_subscription *, sub, NULL, stasis_unsubscribe); |
| 132 |
struct stasis_message *msg; |
| 133 |
struct stasis_message_type *type; |
| 134 |
struct ast_endpoint_snapshot *actual_snapshot; |
| 135 |
struct stasis_cache_update *update; |
| 136 |
int message_index; |
| 137 |
|
| 138 |
switch (cmd) { |
| 139 |
case TEST_INIT: |
| 140 |
info->name = __func__; |
| 141 |
info->category = test_category; |
| 142 |
info->summary = "Test endpoint state change messages"; |
| 143 |
info->description = "Test endpoint state change messages"; |
| 144 |
return AST_TEST_NOT_RUN; |
| 145 |
case TEST_EXECUTE: |
| 146 |
break; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
sink = stasis_message_sink_create(); |
| 151 |
ast_test_validate(test, NULL != sink); |
| 152 |
|
| 153 |
sub = stasis_subscribe( |
| 154 |
ast_endpoint_topic_all_cached(), |
| 155 |
stasis_message_sink_cb(), sink); |
| 156 |
ast_test_validate(test, NULL != sub); |
| 157 |
|
| 158 |
uut = ast_endpoint_create("TEST", __func__); |
| 159 |
ast_test_validate(test, NULL != uut); |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
message_index = stasis_message_sink_wait_for(sink, 0, |
| 167 |
cache_update, __func__, STASIS_SINK_DEFAULT_WAIT); |
| 168 |
ast_test_validate(test, 0 <= message_index); |
| 169 |
|
| 170 |
|
| 171 |
msg = sink->messages[message_index]; |
| 172 |
type = stasis_message_type(msg); |
| 173 |
ast_test_validate(test, stasis_cache_update_type() == type); |
| 174 |
update = stasis_message_data(msg); |
| 175 |
ast_test_validate(test, ast_endpoint_snapshot_type() == update->type); |
| 176 |
ast_test_validate(test, NULL == update->old_snapshot); |
| 177 |
actual_snapshot = stasis_message_data(update->new_snapshot); |
| 178 |
ast_test_validate(test, 0 == strcmp("TEST", actual_snapshot->tech)); |
| 179 |
ast_test_validate(test, |
| 180 |
0 == strcmp(__func__, actual_snapshot->resource)); |
| 181 |
|
| 182 |
ast_endpoint_shutdown(uut); |
| 183 |
uut = NULL; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
message_index = stasis_message_sink_wait_for(sink, message_index + 2, |
| 188 |
cache_update, __func__, STASIS_SINK_DEFAULT_WAIT); |
| 189 |
ast_test_validate(test, 0 <= message_index); |
| 190 |
|
| 191 |
|
| 192 |
msg = sink->messages[message_index]; |
| 193 |
type = stasis_message_type(msg); |
| 194 |
ast_test_validate(test, stasis_cache_update_type() == type); |
| 195 |
update = stasis_message_data(msg); |
| 196 |
ast_test_validate(test, ast_endpoint_snapshot_type() == update->type); |
| 197 |
actual_snapshot = stasis_message_data(update->old_snapshot); |
| 198 |
ast_test_validate(test, 0 == strcmp("TEST", actual_snapshot->tech)); |
| 199 |
ast_test_validate(test, |
| 200 |
0 == strcmp(__func__, actual_snapshot->resource)); |
| 201 |
ast_test_validate(test, NULL == update->new_snapshot); |
| 202 |
|
| 203 |
return AST_TEST_PASS; |
| 204 |
} |
| 205 |
|
| 206 |
AST_TEST_DEFINE(channel_messages) |
| 207 |
{ |
| 208 |
RAII_VAR(struct ast_endpoint *, uut, NULL, ast_endpoint_shutdown); |
| 209 |
RAII_VAR(struct ast_channel *, chan, NULL, ast_hangup); |
| 210 |
RAII_VAR(struct stasis_message_sink *, sink, NULL, ao2_cleanup); |
| 211 |
RAII_VAR(struct stasis_subscription *, sub, NULL, stasis_unsubscribe); |
| 212 |
struct stasis_message *msg; |
| 213 |
struct stasis_message_type *type; |
| 214 |
struct ast_endpoint_snapshot *actual_snapshot; |
| 215 |
int expected_count; |
| 216 |
int actual_count; |
| 217 |
int i; |
| 218 |
int channel_index = -1; |
| 219 |
int endpoint_index = -1; |
| 220 |
|
| 221 |
switch (cmd) { |
| 222 |
case TEST_INIT: |
| 223 |
info->name = __func__; |
| 224 |
info->category = test_category; |
| 225 |
info->summary = "Test channel messages on an endpoint topic"; |
| 226 |
info->description = |
| 227 |
"Test channel messages on an endpoint topic"; |
| 228 |
return AST_TEST_NOT_RUN; |
| 229 |
case TEST_EXECUTE: |
| 230 |
break; |
| 231 |
} |
| 232 |
|
| 233 |
uut = ast_endpoint_create("TEST", __func__); |
| 234 |
ast_test_validate(test, NULL != uut); |
| 235 |
|
| 236 |
sink = stasis_message_sink_create(); |
| 237 |
ast_test_validate(test, NULL != sink); |
| 238 |
|
| 239 |
sub = stasis_subscribe(ast_endpoint_topic(uut), |
| 240 |
stasis_message_sink_cb(), sink); |
| 241 |
ast_test_validate(test, NULL != sub); |
| 242 |
|
| 243 |
chan = ast_channel_alloc(0, AST_STATE_DOWN, "100", __func__, "100", |
| 244 |
"100", "default", NULL, NULL, 0, "TEST/test_res"); |
| 245 |
ast_test_validate(test, NULL != chan); |
| 246 |
|
| 247 |
ast_endpoint_add_channel(uut, chan); |
| 248 |
|
| 249 |
actual_count = stasis_message_sink_wait_for_count(sink, 1, |
| 250 |
STASIS_SINK_DEFAULT_WAIT); |
| 251 |
ast_test_validate(test, 1 == actual_count); |
| 252 |
|
| 253 |
msg = sink->messages[0]; |
| 254 |
type = stasis_message_type(msg); |
| 255 |
ast_test_validate(test, ast_endpoint_snapshot_type() == type); |
| 256 |
actual_snapshot = stasis_message_data(msg); |
| 257 |
ast_test_validate(test, 1 == actual_snapshot->num_channels); |
| 258 |
|
| 259 |
ast_endpoint_remove_channel(uut, chan); |
| 260 |
|
| 261 |
ast_hangup(chan); |
| 262 |
chan = NULL; |
| 263 |
|
| 264 |
expected_count = 3; |
| 265 |
actual_count = stasis_message_sink_wait_for_count(sink, expected_count, |
| 266 |
STASIS_SINK_DEFAULT_WAIT); |
| 267 |
ast_test_validate(test, expected_count == actual_count); |
| 268 |
|
| 269 |
for (i = 0; i < expected_count; i++) { |
| 270 |
msg = sink->messages[i]; |
| 271 |
type = stasis_message_type(msg); |
| 272 |
if (type == ast_channel_snapshot_type()) { |
| 273 |
channel_index = i; |
| 274 |
} |
| 275 |
if (type == ast_endpoint_snapshot_type()) { |
| 276 |
endpoint_index = i; |
| 277 |
} |
| 278 |
} |
| 279 |
ast_test_validate(test, channel_index >= 0 && endpoint_index >= 0); |
| 280 |
actual_snapshot = stasis_message_data(sink->messages[endpoint_index]); |
| 281 |
ast_test_validate(test, 0 == actual_snapshot->num_channels); |
| 282 |
|
| 283 |
return AST_TEST_PASS; |
| 284 |
} |
| 285 |
|
| 286 |
static int unload_module(void) |
| 287 |
{ |
| 288 |
AST_TEST_UNREGISTER(state_changes); |
| 289 |
AST_TEST_UNREGISTER(cache_clear); |
| 290 |
AST_TEST_UNREGISTER(channel_messages); |
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
static int load_module(void) |
| 295 |
{ |
| 296 |
AST_TEST_REGISTER(state_changes); |
| 297 |
AST_TEST_REGISTER(cache_clear); |
| 298 |
AST_TEST_REGISTER(channel_messages); |
| 299 |
return AST_MODULE_LOAD_SUCCESS; |
| 300 |
} |
| 301 |
|
| 302 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Endpoint stasis-related testing", |
| 303 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 304 |
.load = load_module, |
| 305 |
.unload = unload_module, |
| 306 |
.requires = "res_stasis_test", |
| 307 |
); |