Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
test_stasis_state.c File Reference
#include "asterisk.h"
#include "asterisk/astobj2.h"
#include "asterisk/conversions.h"
#include "asterisk/module.h"
#include "asterisk/stasis_state.h"
#include "asterisk/test.h"
Include dependency graph for test_stasis_state.c:

Go to the source code of this file.

Data Structures

struct  foo_data
 

Macros

#define MANAGER_TOPIC   "foo"
 
#define test_category   "/stasis/core/state/"
 
#define TOPIC_COUNT   500
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (implicit_publish)
 
 AST_TEST_DEFINE (explicit_publish)
 
 AST_VECTOR (subscriptions, struct stasis_state_subscriber *)
 
 AST_VECTOR (publishers, struct stasis_state_publisher *)
 
static struct stasis_messagecreate_foo_type_message (const char *id)
 
static int explicit_publish_cb (const char *id, struct stasis_message *msg, void *user_data)
 
struct stasis_message_typefoo_type (void)
 
static void foo_type_cb (void *data, struct stasis_subscription *sub, struct stasis_message *message)
 
static void handle_validate (const char *id, struct stasis_state_subscriber *sub)
 
static int implicit_publish_cb (const char *id, struct stasis_message *msg, void *user_data)
 
static int load_module (void)
 
static int publish (struct stasis_state_manager *manager, on_stasis_state cb, void *user_data)
 
static int publishers_create (struct stasis_state_manager *manager, struct publishers *pubs)
 
static int publishers_destroy (struct stasis_state_manager *manager, struct publishers *pubs)
 
 STASIS_MESSAGE_TYPE_DEFN (foo_type)
 
static int subscriptions_create (struct stasis_state_manager *manager, struct subscriptions *subs)
 
static int subscriptions_destroy (struct stasis_state_manager *manager, struct subscriptions *subs)
 
static int unload_module (void)
 
static int validate_data (const char *id, struct foo_data *foo)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Stasis state testing" , .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
 
static int expect_null
 
struct stasis_state_observer foo_observer
 
static size_t running_total
 
static size_t sum_total
 

Macro Definition Documentation

◆ MANAGER_TOPIC

#define MANAGER_TOPIC   "foo"

Definition at line 36 of file test_stasis_state.c.

Referenced by AST_TEST_DEFINE().

◆ test_category

#define test_category   "/stasis/core/state/"

Definition at line 32 of file test_stasis_state.c.

Referenced by AST_TEST_DEFINE().

◆ TOPIC_COUNT

#define TOPIC_COUNT   500

Definition at line 34 of file test_stasis_state.c.

Referenced by publishers_create(), publishers_destroy(), and subscriptions_create().

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 466 of file test_stasis_state.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 466 of file test_stasis_state.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 466 of file test_stasis_state.c.

◆ AST_TEST_DEFINE() [1/2]

AST_TEST_DEFINE ( implicit_publish  )

Definition at line 356 of file test_stasis_state.c.

References ao2_cleanup, ao2_ref, ast_log, AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, cleanup(), implicit_publish_cb(), sip_to_pjsip::info(), LOG_ERROR, MANAGER_TOPIC, NULL, publish(), publishers_destroy(), RAII_VAR, stasis_state_manager_create(), subscriptions_create(), subscriptions_destroy(), test_category, TEST_EXECUTE, and TEST_INIT.

357 {
358  RAII_VAR(struct stasis_state_manager *, manager, NULL, ao2_cleanup);
359  struct subscriptions subs;
360  int rc = AST_TEST_PASS;
361 
362  switch (cmd) {
363  case TEST_INIT:
364  info->name = __func__;
365  info->category = test_category;
366  info->summary = "Test implicit publishing of stasis state";
367  info->description = info->summary;
368  return AST_TEST_NOT_RUN;
369  case TEST_EXECUTE:
370  break;
371  }
372 
374  ast_test_validate(test, manager != NULL);
375 
376  ast_test_validate(test, !subscriptions_create(manager, &subs));
377 
378  ast_test_validate_cleanup(test, !publish(manager, implicit_publish_cb, manager),
379  rc, cleanup);
380 
381 cleanup:
382  if (subscriptions_destroy(manager, &subs) || publishers_destroy(manager, NULL)) {
383  return AST_TEST_FAIL;
384  }
385 
386  /*
387  * State subscriptions add a ref a state. The state in turn adds a ref
388  * to the manager. So if more than one ref is held on the manager before
389  * exiting, there is a ref leak some place.
390  */
391  if (ao2_ref(manager, 0) != 1) {
392  ast_log(LOG_ERROR, "Memory leak - Too many references held on manager\n");
393  return AST_TEST_FAIL;
394  }
395 
396  return rc;
397 }
static int publish(struct stasis_state_manager *manager, on_stasis_state cb, void *user_data)
static int subscriptions_create(struct stasis_state_manager *manager, struct subscriptions *subs)
#define NULL
Definition: resample.c:96
struct stasis_state_manager * stasis_state_manager_create(const char *topic_name)
Create a stasis state manager.
Definition: stasis_state.c:324
static int subscriptions_destroy(struct stasis_state_manager *manager, struct subscriptions *subs)
static int publishers_destroy(struct stasis_state_manager *manager, struct publishers *pubs)
#define ast_log
Definition: astobj2.c:42
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define LOG_ERROR
Definition: logger.h:285
def info(msg)
static int implicit_publish_cb(const char *id, struct stasis_message *msg, void *user_data)
#define MANAGER_TOPIC
static void * cleanup(void *unused)
Definition: pbx_realtime.c:124
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define test_category

◆ AST_TEST_DEFINE() [2/2]

AST_TEST_DEFINE ( explicit_publish  )

Definition at line 399 of file test_stasis_state.c.

References ao2_cleanup, ao2_ref, ast_log, AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, cleanup(), explicit_publish_cb(), sip_to_pjsip::info(), LOG_ERROR, MANAGER_TOPIC, NULL, publish(), publishers, publishers_create(), publishers_destroy(), RAII_VAR, stasis_state_manager_create(), subscriptions_create(), subscriptions_destroy(), test_category, TEST_EXECUTE, and TEST_INIT.

400 {
401  RAII_VAR(struct stasis_state_manager *, manager, NULL, ao2_cleanup);
402  struct subscriptions subs;
403  struct publishers pubs;
404  int rc = AST_TEST_PASS;
405 
406  switch (cmd) {
407  case TEST_INIT:
408  info->name = __func__;
409  info->category = test_category;
410  info->summary = "Test explicit publishing of stasis state";
411  info->description = info->summary;
412  return AST_TEST_NOT_RUN;
413  case TEST_EXECUTE:
414  break;
415  }
416 
418  ast_test_validate(test, manager != NULL);
419 
420  ast_test_validate(test, !subscriptions_create(manager, &subs));
421  ast_test_validate_cleanup(test, !publishers_create(manager, &pubs), rc, cleanup);
422 
423  ast_test_validate_cleanup(test, !publish(manager, explicit_publish_cb, &pubs),
424  rc, cleanup);
425 
426 cleanup:
427  if (subscriptions_destroy(manager, &subs) || publishers_destroy(manager, &pubs)) {
428  return AST_TEST_FAIL;
429  }
430 
431  /*
432  * State subscriptions add a ref a state. The state in turn adds a ref
433  * to the manager. So if more than one ref is held on the manager before
434  * exiting, there is a ref leak some place.
435  */
436  if (ao2_ref(manager, 0) != 1) {
437  ast_log(LOG_ERROR, "Memory leak - Too many references held on manager\n");
438  return AST_TEST_FAIL;
439  }
440 
441  return rc;
442 }
static struct ao2_container * publishers
Container of active outbound extension state publishers.
static int publish(struct stasis_state_manager *manager, on_stasis_state cb, void *user_data)
static int subscriptions_create(struct stasis_state_manager *manager, struct subscriptions *subs)
#define NULL
Definition: resample.c:96
static int publishers_create(struct stasis_state_manager *manager, struct publishers *pubs)
struct stasis_state_manager * stasis_state_manager_create(const char *topic_name)
Create a stasis state manager.
Definition: stasis_state.c:324
static int subscriptions_destroy(struct stasis_state_manager *manager, struct subscriptions *subs)
static int publishers_destroy(struct stasis_state_manager *manager, struct publishers *pubs)
#define ast_log
Definition: astobj2.c:42
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define LOG_ERROR
Definition: logger.h:285
def info(msg)
static int explicit_publish_cb(const char *id, struct stasis_message *msg, void *user_data)
#define MANAGER_TOPIC
static void * cleanup(void *unused)
Definition: pbx_realtime.c:124
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define test_category

◆ AST_VECTOR() [1/2]

AST_VECTOR ( subscriptions  ,
struct stasis_state_subscriber  
)

◆ AST_VECTOR() [2/2]

AST_VECTOR ( publishers  ,
struct stasis_state_publisher  
)

◆ create_foo_type_message()

static struct stasis_message* create_foo_type_message ( const char *  id)
static

Definition at line 246 of file test_stasis_state.c.

References ao2_alloc, ao2_ref, ast_log, ast_str_to_umax(), foo_data::bar, foo_type(), LOG_ERROR, NULL, and stasis_message_create_full().

Referenced by explicit_publish_cb(), and implicit_publish_cb().

247 {
248  struct stasis_message *msg;
249  struct foo_data *foo;
250 
251  foo = ao2_alloc(sizeof(*foo), NULL);
252  if (!foo) {
253  ast_log(LOG_ERROR, "Failed to allocate foo data for '%s'\n", id);
254  return NULL;
255  }
256 
257  if (ast_str_to_umax(id, &foo->bar)) {
258  ast_log(LOG_ERROR, "Unable to convert the state's id '%s' to numeric\n", id);
259  ao2_ref(foo, -1);
260  return NULL;
261  }
262 
264  if (!msg) {
265  ast_log(LOG_ERROR, "Failed to create stasis message for '%s'\n", id);
266  }
267 
268  ao2_ref(foo, -1);
269  return msg;
270 }
int ast_str_to_umax(const char *str, uintmax_t *res)
Convert the given string to an unsigned max size integer.
Definition: conversions.c:119
#define NULL
Definition: resample.c:96
struct stasis_message_type * foo_type(void)
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define LOG_ERROR
Definition: logger.h:285
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
struct stasis_message * stasis_message_create_full(struct stasis_message_type *type, void *data, const struct ast_eid *eid)
Create a new message for an entity.

◆ explicit_publish_cb()

static int explicit_publish_cb ( const char *  id,
struct stasis_message msg,
void *  user_data 
)
static

Definition at line 293 of file test_stasis_state.c.

References ao2_ref, ast_log, AST_VECTOR_GET, AST_VECTOR_SIZE, CMP_STOP, create_foo_type_message(), LOG_ERROR, NULL, publishers, stasis_message_data(), stasis_state_publish(), stasis_state_publisher_id(), and validate_data().

Referenced by AST_TEST_DEFINE().

294 {
295  /* For each state object create and publish new state data */
296  struct publishers *pubs = user_data;
297  struct stasis_state_publisher *pub = NULL;
298  struct foo_data *foo = stasis_message_data(msg);
299  size_t i;
300 
301  if (validate_data(id, foo)) {
302  return CMP_STOP;
303  }
304 
305  msg = create_foo_type_message(id);
306  if (!msg) {
307  return CMP_STOP;
308  }
309 
310  for (i = 0; i < AST_VECTOR_SIZE(pubs); ++i) {
311  if (!strcmp(stasis_state_publisher_id(AST_VECTOR_GET(pubs, i)), id)) {
312  pub = AST_VECTOR_GET(pubs, i);
313  break;
314  }
315  }
316 
317  if (!pub) {
318  ast_log(LOG_ERROR, "Unable to locate publisher for id '%s'\n", id);
319  return CMP_STOP;
320  }
321 
322  stasis_state_publish(pub, msg);
323  ao2_ref(msg, -1);
324 
325  return 0;
326 }
static struct ao2_container * publishers
Container of active outbound extension state publishers.
const char * stasis_state_publisher_id(const struct stasis_state_publisher *pub)
Retrieve the publisher&#39;s underlying state&#39;s unique id.
Definition: stasis_state.c:552
#define NULL
Definition: resample.c:96
static int validate_data(const char *id, struct foo_data *foo)
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define LOG_ERROR
Definition: logger.h:285
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
static struct stasis_message * create_foo_type_message(const char *id)
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:682
void stasis_state_publish(struct stasis_state_publisher *pub, struct stasis_message *msg)
Publish to a managed state (topic) using a publisher.
Definition: stasis_state.c:562
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:611

◆ foo_type()

struct stasis_message_type* foo_type ( void  )

◆ foo_type_cb()

static void foo_type_cb ( void *  data,
struct stasis_subscription sub,
struct stasis_message message 
)
static

Definition at line 108 of file test_stasis_state.c.

Referenced by subscriptions_create().

109 {
110  /* No op since we are not really testing stasis topic handling here */
111 }

◆ handle_validate()

static void handle_validate ( const char *  id,
struct stasis_state_subscriber sub 
)
static

Definition at line 96 of file test_stasis_state.c.

References ao2_cleanup, stasis_state_subscriber_data(), and validate_data().

97 {
98  struct foo_data *foo = stasis_state_subscriber_data(sub);
99  validate_data(id, foo);
100  ao2_cleanup(foo);
101 }
void * stasis_state_subscriber_data(struct stasis_state_subscriber *sub)
Retrieve the last known state stasis message payload for the subscriber.
Definition: stasis_state.c:497
static int validate_data(const char *id, struct foo_data *foo)
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ implicit_publish_cb()

static int implicit_publish_cb ( const char *  id,
struct stasis_message msg,
void *  user_data 
)
static

Definition at line 272 of file test_stasis_state.c.

References ao2_ref, CMP_STOP, create_foo_type_message(), NULL, stasis_message_data(), stasis_state_publish_by_id(), and validate_data().

Referenced by AST_TEST_DEFINE().

273 {
274  /* For each state object create and publish new state data */
275  struct foo_data *foo = stasis_message_data(msg);
276 
277  if (validate_data(id, foo)) {
278  return CMP_STOP;
279  }
280 
281  msg = create_foo_type_message(id);
282  if (!msg) {
283  return CMP_STOP;
284  }
285 
286  /* Now publish it on the managed state object */
287  stasis_state_publish_by_id(user_data, id, NULL, msg);
288  ao2_ref(msg, -1);
289 
290  return 0;
291 }
#define NULL
Definition: resample.c:96
static int validate_data(const char *id, struct foo_data *foo)
#define ao2_ref(o, delta)
Definition: astobj2.h:464
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
void stasis_state_publish_by_id(struct stasis_state_manager *manager, const char *id, const struct ast_eid *eid, struct stasis_message *msg)
Publish to a managed named by id topic, and add an implicit subscriber.
Definition: stasis_state.c:638
static struct stasis_message * create_foo_type_message(const char *id)

◆ load_module()

static int load_module ( void  )
static

Definition at line 454 of file test_stasis_state.c.

References AST_MODULE_LOAD_SUCCESS, AST_TEST_REGISTER, foo_type(), and STASIS_MESSAGE_TYPE_INIT.

455 {
457  return -1;
458  }
459 
460  AST_TEST_REGISTER(implicit_publish);
461  AST_TEST_REGISTER(explicit_publish);
462 
464 }
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
Definition: stasis.h:1501
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
struct stasis_message_type * foo_type(void)

◆ publish()

static int publish ( struct stasis_state_manager manager,
on_stasis_state  cb,
void *  user_data 
)
static

Definition at line 328 of file test_stasis_state.c.

References ast_log, expect_null, LOG_ERROR, running_total, stasis_state_callback_all(), and sum_total.

Referenced by AST_TEST_DEFINE().

330 {
331  /* First time there is no state data */
332  expect_null = 1;
333 
334  running_total = 0;
335  stasis_state_callback_all(manager, cb, user_data);
336 
337  if (running_total != sum_total) {
338  ast_log(LOG_ERROR, "Failed manager_callback (1): running=%zu, sum=%zu\n",
340  return -1;
341  }
342 
343  /* Second time check valid state data exists */
345  stasis_state_callback_all(manager, cb, user_data);
346 
347  if (running_total != sum_total) {
348  ast_log(LOG_ERROR, "Failed manager_callback (2): running=%zu, sum=%zu\n",
350  return -1;
351  }
352 
353  return 0;
354 }
static size_t running_total
#define ast_log
Definition: astobj2.c:42
void stasis_state_callback_all(struct stasis_state_manager *manager, on_stasis_state handler, void *data)
For each managed state call the given handler.
Definition: stasis_state.c:740
static int expect_null
#define LOG_ERROR
Definition: logger.h:285
static size_t sum_total

◆ publishers_create()

static int publishers_create ( struct stasis_state_manager manager,
struct publishers pubs 
)
static

Definition at line 205 of file test_stasis_state.c.

References ao2_ref, ast_log, AST_VECTOR_APPEND, AST_VECTOR_INIT, LOG_ERROR, publishers_destroy(), stasis_state_add_publisher(), and TOPIC_COUNT.

Referenced by AST_TEST_DEFINE().

207 {
208  size_t i;
209 
210  if (AST_VECTOR_INIT(pubs, TOPIC_COUNT)) {
211  return -1;
212  }
213 
214  for (i = 0; i < TOPIC_COUNT; ++i) {
215  struct stasis_state_publisher *pub;
216  char id[32];
217 
218  if (snprintf(id, 10, "%zu", i) == -1) {
219  ast_log(LOG_ERROR, "Unable to convert publisher id to string\n");
220  break;
221  }
222 
223  /* Create the state publisher */
224  pub = stasis_state_add_publisher(manager, id);
225  if (!pub) {
226  ast_log(LOG_ERROR, "Failed to create a state publisher for id '%s'\n", id);
227  break;
228  }
229 
230  if (AST_VECTOR_APPEND(pubs, pub)) {
231  ast_log(LOG_ERROR, "Failed to add to publisher to vector for id '%s'\n", id);
232  ao2_ref(pub, -1);
233  break;
234  }
235  }
236 
237  if (i != TOPIC_COUNT) {
238  ast_log(LOG_ERROR, "Failed to create all publishers: count=%zu\n", i);
239  publishers_destroy(manager, pubs);
240  return -1;
241  }
242 
243  return 0;
244 }
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256
static int publishers_destroy(struct stasis_state_manager *manager, struct publishers *pubs)
#define ast_log
Definition: astobj2.c:42
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition: vector.h:113
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct stasis_state_publisher * stasis_state_add_publisher(struct stasis_state_manager *manager, const char *id)
Add a publisher to the managed state for the given id.
Definition: stasis_state.c:531
#define TOPIC_COUNT
#define LOG_ERROR
Definition: logger.h:285

◆ publishers_destroy()

static int publishers_destroy ( struct stasis_state_manager manager,
struct publishers pubs 
)
static

Definition at line 179 of file test_stasis_state.c.

References ao2_cleanup, ast_log, AST_VECTOR_CALLBACK_VOID, AST_VECTOR_FREE, LOG_ERROR, NULL, stasis_state_remove_publish_by_id(), and TOPIC_COUNT.

Referenced by AST_TEST_DEFINE(), and publishers_create().

180 {
181  size_t i;
182 
183  if (pubs) {
184  /* Remove explicit publishers */
186  AST_VECTOR_FREE(pubs);
187  return 0;
188  }
189 
190  for (i = 0; i < TOPIC_COUNT; ++i) {
191  char id[32];
192 
193  /* Remove implicit publishers */
194  if (snprintf(id, 10, "%zu", i) == -1) {
195  ast_log(LOG_ERROR, "Unable to convert publisher id to string\n");
196  return -1;
197  }
198 
200  }
201 
202  return 0;
203 }
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Definition: vector.h:174
void stasis_state_remove_publish_by_id(struct stasis_state_manager *manager, const char *id, const struct ast_eid *eid, struct stasis_message *msg)
Publish to a managed named by id topic, and remove an implicit publisher.
Definition: stasis_state.c:658
#define NULL
Definition: resample.c:96
#define ast_log
Definition: astobj2.c:42
#define TOPIC_COUNT
#define LOG_ERROR
Definition: logger.h:285
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define AST_VECTOR_CALLBACK_VOID(vec, callback,...)
Execute a callback on every element in a vector disregarding callback return.
Definition: vector.h:865

◆ STASIS_MESSAGE_TYPE_DEFN()

STASIS_MESSAGE_TYPE_DEFN ( foo_type  )

foo stasis message type

◆ subscriptions_create()

static int subscriptions_create ( struct stasis_state_manager manager,
struct subscriptions subs 
)
static

Definition at line 131 of file test_stasis_state.c.

References ao2_ref, ast_log, AST_VECTOR_APPEND, AST_VECTOR_INIT, expect_null, foo_type_cb(), LOG_ERROR, NULL, running_total, stasis_state_add_observer(), stasis_state_subscribe_pool(), sub, subscriptions_destroy(), sum_total, and TOPIC_COUNT.

Referenced by AST_TEST_DEFINE().

133 {
134  size_t i;
135 
136  if (stasis_state_add_observer(manager, &foo_observer) ||
137  AST_VECTOR_INIT(subs, TOPIC_COUNT)) {
138  return -1;
139  }
140 
141  sum_total = running_total = 0;
142  expect_null = 1;
143 
144  for (i = 0; i < TOPIC_COUNT; ++i) {
146  char id[32];
147 
148  if (snprintf(id, 10, "%zu", i) == -1) {
149  ast_log(LOG_ERROR, "Unable to convert subscriber id to string\n");
150  break;
151  }
152 
153  sub = stasis_state_subscribe_pool(manager, id, foo_type_cb, NULL);
154  if (!sub) {
155  ast_log(LOG_ERROR, "Failed to create a state subscriber for id '%s'\n", id);
156  ao2_ref(sub, -1);
157  break;
158  }
159 
160  if (AST_VECTOR_APPEND(subs, sub)) {
161  ast_log(LOG_ERROR, "Failed to add to foo_sub to vector for id '%s'\n", id);
162  ao2_ref(sub, -1);
163  break;
164  }
165 
166  sum_total += i;
167  }
168 
169  if (i != TOPIC_COUNT || running_total != sum_total) {
170  ast_log(LOG_ERROR, "Failed to create all subscriptions: running=%zu, sum=%zu\n",
172  subscriptions_destroy(manager, subs);
173  return -1;
174  }
175 
176  return 0;
177 }
struct stasis_state_subscriber * stasis_state_subscribe_pool(struct stasis_state_manager *manager, const char *id, stasis_subscription_cb callback, void *data)
Add a subscriber, and subscribe to its underlying stasis topic.
Definition: stasis_state.c:446
static void foo_type_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256
struct stasis_state_observer foo_observer
#define NULL
Definition: resample.c:96
int stasis_state_add_observer(struct stasis_state_manager *manager, struct stasis_state_observer *observer)
Add an observer to receive managed state related events.
Definition: stasis_state.c:688
static size_t running_total
static int subscriptions_destroy(struct stasis_state_manager *manager, struct subscriptions *subs)
#define ast_log
Definition: astobj2.c:42
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition: vector.h:113
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static int expect_null
#define TOPIC_COUNT
#define LOG_ERROR
Definition: logger.h:285
static size_t sum_total
struct stasis_forward * sub
Definition: res_corosync.c:240

◆ subscriptions_destroy()

static int subscriptions_destroy ( struct stasis_state_manager manager,
struct subscriptions subs 
)
static

Definition at line 113 of file test_stasis_state.c.

References ast_log, AST_VECTOR_CALLBACK_VOID, AST_VECTOR_FREE, expect_null, LOG_ERROR, running_total, stasis_state_remove_observer(), stasis_state_unsubscribe_and_join(), and sum_total.

Referenced by AST_TEST_DEFINE(), and subscriptions_create().

114 {
116 
118  AST_VECTOR_FREE(subs);
119 
121 
122  if (running_total != sum_total) {
123  ast_log(LOG_ERROR, "Failed to destroy all subscriptions: running=%zu, sum=%zu\n",
125  return -1;
126  }
127 
128  return 0;
129 }
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Definition: vector.h:174
void stasis_state_remove_observer(struct stasis_state_manager *manager, struct stasis_state_observer *observer)
Remove an observer (will no longer receive managed state related events).
Definition: stasis_state.c:700
struct stasis_state_observer foo_observer
void * stasis_state_unsubscribe_and_join(struct stasis_state_subscriber *sub)
Unsubscribe from the stasis topic, block until the final message is received, and then unsubscribe fr...
Definition: stasis_state.c:477
static size_t running_total
#define ast_log
Definition: astobj2.c:42
static int expect_null
#define LOG_ERROR
Definition: logger.h:285
static size_t sum_total
#define AST_VECTOR_CALLBACK_VOID(vec, callback,...)
Execute a callback on every element in a vector disregarding callback return.
Definition: vector.h:865

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 444 of file test_stasis_state.c.

References AST_TEST_UNREGISTER, foo_type(), and STASIS_MESSAGE_TYPE_CLEANUP.

445 {
446  AST_TEST_UNREGISTER(implicit_publish);
447  AST_TEST_UNREGISTER(explicit_publish);
448 
450 
451  return 0;
452 }
#define STASIS_MESSAGE_TYPE_CLEANUP(name)
Boiler-plate messaging macro for cleaning up message types.
Definition: stasis.h:1523
struct stasis_message_type * foo_type(void)
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128

◆ validate_data()

static int validate_data ( const char *  id,
struct foo_data foo 
)
static

Definition at line 63 of file test_stasis_state.c.

References ast_log, ast_str_to_umax(), foo_data::bar, expect_null, LOG_ERROR, and running_total.

Referenced by explicit_publish_cb(), handle_validate(), and implicit_publish_cb().

64 {
65  size_t num;
66 
67  if (ast_str_to_umax(id, &num)) {
68  ast_log(LOG_ERROR, "Unable to convert the state's id '%s' to numeric\n", id);
69  return -1;
70  }
71 
72  running_total += num;
73 
74  if (!foo) {
75  if (expect_null) {
76  return 0;
77  }
78 
79  ast_log(LOG_ERROR, "Expected state data for '%s'\n", id);
80  return -1;
81  }
82 
83  if (expect_null) {
84  ast_log(LOG_ERROR, "Expected NULL state data for '%s'\n", id);
85  return -1;
86  }
87 
88  if (foo->bar != num) {
89  ast_log(LOG_ERROR, "Unexpected state data for '%s'\n", id);
90  return -1;
91  }
92 
93  return 0;
94 }
int ast_str_to_umax(const char *str, uintmax_t *res)
Convert the given string to an unsigned max size integer.
Definition: conversions.c:119
static size_t running_total
#define ast_log
Definition: astobj2.c:42
static int expect_null
#define LOG_ERROR
Definition: logger.h:285

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Stasis state testing" , .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 466 of file test_stasis_state.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 466 of file test_stasis_state.c.

◆ expect_null

int expect_null
static

This value is set to check if state data is NULL before publishing

Definition at line 61 of file test_stasis_state.c.

Referenced by publish(), subscriptions_create(), subscriptions_destroy(), and validate_data().

◆ foo_observer

struct stasis_state_observer foo_observer
Initial value:
= {
.on_subscribe = handle_validate,
.on_unsubscribe = handle_validate
}
static void handle_validate(const char *id, struct stasis_state_subscriber *sub)

Definition at line 103 of file test_stasis_state.c.

◆ running_total

size_t running_total
static

Test variable that tracks the running total of state ids

Definition at line 58 of file test_stasis_state.c.

Referenced by publish(), subscriptions_create(), subscriptions_destroy(), and validate_data().

◆ sum_total

size_t sum_total
static

For testing purposes each subscribed state's id is a number. This value is the summation of all id's.

Definition at line 55 of file test_stasis_state.c.

Referenced by publish(), subscriptions_create(), and subscriptions_destroy().