Asterisk - The Open Source Telephony Project GIT-master-a358458
stasis_test.h
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * David M. Lee, II <dlee@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19#ifndef _ASTERISK_STASIS_TEST_H
20#define _ASTERISK_STASIS_TEST_H
21
22/*!
23 * \file
24 * \brief Test infrastructure for dealing with Stasis.
25 *
26 * \author David M. Lee, II <dlee@digium.com>
27 *
28 * This file contains some helpful utilities for testing Stasis related topics
29 * and messages. The \ref stasis_message_sink is something you can subscribe to
30 * a topic which will receive all of the messages from the topic. This messages
31 * are accumulated in its \c messages field.
32 *
33 * There are a set of wait functions (stasis_message_sink_wait_for_count(),
34 * stasis_message_sink_wait_for(), etc.) which will block waiting for conditions
35 * to be met in the \ref stasis_message_sink.
36 */
37
38#include "asterisk/lock.h"
39#include "asterisk/stasis.h"
40
41#define STASIS_SINK_DEFAULT_WAIT 5000
42
43/*! \brief Structure that collects messages from a topic */
45 /*! Condition mutex. */
47 /*! Condition to signal state changes */
49 /*! Maximum number of messages messages field can hold without
50 * realloc */
52 /*! Current number of messages in messages field. */
54 /*! Boolean flag to be set when unsubscribe is received */
55 int is_done:1;
56 /*! Ordered array of messages received */
58};
59
60/*!
61 * \brief Create a message sink.
62 *
63 * This is an AO2 managed object, which you ao2_cleanup() when done. The
64 * destructor waits for an unsubscribe message to be received, to ensure the
65 * object isn't disposed of before the topic is finished.
66 */
68
69/*!
70 * \brief Topic callback to receive messages.
71 *
72 * We return a function pointer instead of simply exposing the function because
73 * of the vagaries of dlopen(), \c RTLD_LAZY, and function pointers. See the
74 * comment on the implementation for details why.
75 *
76 * \return Function pointer to \ref stasis_message_sink's message handling
77 * function
78 */
80
81/*!
82 * \brief Wait for a sink's num_messages field to reach a certain level.
83 *
84 * The optional timeout prevents complete deadlock in a test.
85 *
86 * \param sink Sink to wait on.
87 * \param num_messages sink->num_messages value to wait for.
88 * \param timeout_millis Number of milliseconds to wait. -1 to wait forever.
89 * \return Actual sink->num_messages value at return.
90 * If this is < \a num_messages, then the timeout expired.
91 */
93 int num_messages, int timeout_millis);
94
95typedef int (*stasis_wait_cb)(struct stasis_message *msg, const void *data);
96
97/*!
98 * \brief Wait for a message that matches the given criteria.
99 *
100 * \param sink Sink to wait on.
101 * \param start Index of message to start with.
102 * \param cmp_cb comparison function. This returns true (non-zero) on match
103 * and false (zero) on match.
104 * \param data
105 * \param timeout_millis Number of milliseconds to wait.
106 * \return Index of the matching message.
107 * \return Negative for no match.
108 */
109int stasis_message_sink_wait_for(struct stasis_message_sink *sink, int start,
110 stasis_wait_cb cmp_cb, const void *data, int timeout_millis);
111
112/*!
113 * \brief Ensures that no new messages are received.
114 *
115 * The optional timeout prevents complete deadlock in a test.
116 *
117 * \param sink Sink to wait on.
118 * \param num_messages expecte \a sink->num_messages.
119 * \param timeout_millis Number of milliseconds to wait for.
120 * \return Actual sink->num_messages value at return.
121 * If this is < \a num_messages, then the timeout expired.
122 */
124 int num_messages, int timeout_millis);
125
126/*! \addtogroup StasisTopicsAndMessages
127 * @{
128 */
129
130/*!
131 * \brief Creates a test message.
132 */
134
135/*!
136 * \brief Gets the type of messages created by stasis_test_message_create().
137 */
139
140/*!
141 * @}
142 */
143
144#endif /* _ASTERISK_STASIS_TEST_H */
struct stasis_message_type * stasis_test_message_type(void)
Gets the type of messages created by stasis_test_message_create().
struct stasis_message * stasis_test_message_create(void)
Creates a test message.
Asterisk locking-related definitions:
pthread_cond_t ast_cond_t
Definition: lock.h:178
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
void(* stasis_subscription_cb)(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Callback function type for Stasis subscriptions.
Definition: stasis.h:611
int stasis_message_sink_should_stay(struct stasis_message_sink *sink, int num_messages, int timeout_millis)
Ensures that no new messages are received.
int(* stasis_wait_cb)(struct stasis_message *msg, const void *data)
Definition: stasis_test.h:95
int stasis_message_sink_wait_for_count(struct stasis_message_sink *sink, int num_messages, int timeout_millis)
Wait for a sink's num_messages field to reach a certain level.
int stasis_message_sink_wait_for(struct stasis_message_sink *sink, int start, stasis_wait_cb cmp_cb, const void *data, int timeout_millis)
Wait for a message that matches the given criteria.
struct stasis_message_sink * stasis_message_sink_create(void)
Create a message sink.
stasis_subscription_cb stasis_message_sink_cb(void)
Topic callback to receive messages.
Structure for mutex and tracking information.
Definition: lock.h:135
Structure that collects messages from a topic.
Definition: stasis_test.h:44
struct stasis_message ** messages
Definition: stasis_test.h:57
ast_mutex_t lock
Definition: stasis_test.h:46