Asterisk - The Open Source Telephony Project  18.5.0
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 <[email protected]>
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 <[email protected]>
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 */
51  size_t max_messages;
52  /*! Current number of messages in messages field. */
53  size_t num_messages;
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 
95 typedef 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 timeout_millis Number of milliseconds to wait.
105  * \return Index of the matching message.
106  * \return Negative for no match.
107  */
108 int stasis_message_sink_wait_for(struct stasis_message_sink *sink, int start,
109  stasis_wait_cb cmp_cb, const void *data, int timeout_millis);
110 
111 /*!
112  * \brief Ensures that no new messages are received.
113  *
114  * The optional timeout prevents complete deadlock in a test.
115  *
116  * \param sink Sink to wait on.
117  * \param num_messages expecte \a sink->num_messages.
118  * \param timeout_millis Number of milliseconds to wait for.
119  * \return Actual sink->num_messages value at return.
120  * If this is < \a num_messages, then the timeout expired.
121  */
123  int num_messages, int timeout_millis);
124 
125 /*! \addtogroup StasisTopicsAndMessages
126  * @{
127  */
128 
129 /*!
130  * \brief Creates a test message.
131  */
133 
134 /*!
135  * \brief Gets the type of messages created by stasis_test_message_create().
136  */
138 
139 /*!
140  * @}
141  */
142 
143 #endif /* _ASTERISK_STASIS_TEST_H */
Structure that collects messages from a topic.
Definition: stasis_test.h:44
Asterisk locking-related definitions:
struct stasis_message ** messages
Definition: stasis_test.h:57
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.
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
struct stasis_message_type * stasis_test_message_type(void)
Gets the type of messages created by stasis_test_message_create().
int stasis_message_sink_wait_for_count(struct stasis_message_sink *sink, int num_messages, int timeout_millis)
Wait for a sink&#39;s num_messages field to reach a certain level.
struct stasis_message_sink * stasis_message_sink_create(void)
Create a message sink.
struct stasis_message * stasis_test_message_create(void)
Creates a test message.
pthread_cond_t ast_cond_t
Definition: lock.h:176
stasis_subscription_cb stasis_message_sink_cb(void)
Topic callback to receive messages.
void(* stasis_subscription_cb)(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Callback function type for Stasis subscriptions.
Definition: stasis.h:615
int stasis_message_sink_should_stay(struct stasis_message_sink *sink, int num_messages, int timeout_millis)
Ensures that no new messages are received.
ast_mutex_t lock
Definition: stasis_test.h:46
int(* stasis_wait_cb)(struct stasis_message *msg, const void *data)
Definition: stasis_test.h:95
Structure for mutex and tracking information.
Definition: lock.h:135