86f5dd0bc44967ccaecffb43550143f66eb2060d
[platform/upstream/libnice.git] / tests / test-io-stream-common.h
1 /*
2  * This file is part of the Nice GLib ICE library.
3  *
4  * (C) 2014 Collabora Ltd.
5  *  Contact: Philip Withnall
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is the Nice GLib ICE library.
18  *
19  * The Initial Developers of the Original Code are Collabora Ltd and Nokia
20  * Corporation. All Rights Reserved.
21  *
22  * Contributors:
23  *   Philip Withnall, Collabora Ltd.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
27  * case the provisions of LGPL are applicable instead of those above. If you
28  * wish to allow use of your version of this file only under the terms of the
29  * LGPL and not to allow others to use your version of this file under the
30  * MPL, indicate your decision by deleting the provisions above and replace
31  * them with the notice and other provisions required by the LGPL. If you do
32  * not delete the provisions above, a recipient may use your version of this
33  * file under either the MPL or the LGPL.
34  */
35 #ifdef HAVE_CONFIG_H
36 # include <config.h>
37 #endif
38
39 #include "agent.h"
40
41 #include <stdlib.h>
42 #include <string.h>
43 #ifndef G_OS_WIN32
44 #include <unistd.h>
45 #endif
46
47 #if !GLIB_CHECK_VERSION(2, 58, 0)
48 #define G_SOURCE_FUNC(f) ((GSourceFunc) (void (*)(void)) (f))
49 #endif
50
51 /* Make the message sufficiently large to not hit Nagle’s algorithm in the
52  * pseudo-TCP implementation, and hence run really slowly. */
53 #define MESSAGE_SIZE 1284 /* bytes */
54
55 typedef struct _TestIOStreamThreadData TestIOStreamThreadData;
56
57 typedef struct {
58   void (*read_thread) (GInputStream *input_stream,
59       TestIOStreamThreadData *data);
60   void (*write_thread) (GOutputStream *output_stream,
61       TestIOStreamThreadData *data);
62   void (*reliable_transport_writable) (GOutputStream *output_stream,
63       NiceAgent *agent, guint stream_id, guint component_id,
64       TestIOStreamThreadData *data);
65   void (*new_selected_pair) (NiceAgent *agent, guint stream_id,
66       guint component_id, gchar *lfoundation, gchar *rfoundation,
67       TestIOStreamThreadData *data);
68   void (*wait_transmission_cb) (NiceAgent *agent);
69 } TestIOStreamCallbacks;
70
71 struct _TestIOStreamThreadData {
72   NiceAgent *agent;
73   GIOStream *io_stream;
74
75   gboolean reliable;
76
77   GMainLoop *main_loop;
78   GMainLoop *error_loop;
79
80   GMainContext *main_context;
81   GMainContext *write_context;
82   GMainContext *read_context;
83
84   gpointer user_data;
85   GDestroyNotify user_data_free;
86
87   TestIOStreamThreadData *other;
88
89   /*< private >*/
90   const TestIOStreamCallbacks *callbacks;
91
92   gboolean done;
93
94   /* Condition signalling for the stream being open/writeable. */
95   gboolean stream_open;
96   gboolean stream_ready;
97   GCond write_cond;
98   GMutex write_mutex;
99
100   GMutex *start_mutex;
101   GCond *start_cond;
102   guint *start_count;
103 };
104
105 GThread *spawn_thread (const gchar *thread_name, GThreadFunc thread_func,
106     gpointer user_data);
107 void run_io_stream_test (guint deadlock_timeout, gboolean reliable,
108     const TestIOStreamCallbacks *callbacks,
109     gpointer l_user_data, GDestroyNotify l_user_data_free,
110     gpointer r_user_data, GDestroyNotify r_user_data_free);
111 void check_for_termination (TestIOStreamThreadData *data, gsize *recv_count,
112     gsize *other_recv_count, volatile gsize *send_count, gsize expected_recv_count);
113
114 void stop_main_loop (GMainLoop *loop);