~ubuntu-branches/debian/stretch/libnice/stretch

« back to all changes in this revision

Viewing changes to tests/test-io-stream-pollable.c

  • Committer: Package Import Robot
  • Author(s): Simon McVittie
  • Date: 2014-05-14 12:00:13 UTC
  • mfrom: (1.2.9) (5.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20140514120013-fi5mh9bexrjnwnd8
Tags: 0.1.7-1
* New upstream release 0.1.6, 0.1.7
  - fixes various compiler warnings that were mistakenly fatal in 0.1.5
    (Closes: #743232, #743233)
  - update symbols file for new API
* Explicitly disable -Werror, even if we package a non-release in future
* Don't run tests during the build. We were ignoring failures already,
  and they sometimes hang until the buildd terminates them.
  Upstream (Olivier Crête) says they are stable enough to be useful
  for developers, but not for integration testing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include "test-io-stream-common.h"
 
41
 
 
42
#include <stdlib.h>
 
43
#include <string.h>
 
44
#ifndef G_OS_WIN32
 
45
#include <unistd.h>
 
46
#endif
 
47
 
 
48
typedef struct {
 
49
  GMainLoop *read_loop;  /* unowned */
 
50
 
 
51
  gsize recv_count;
 
52
  gsize *other_recv_count;
 
53
 
 
54
  gsize send_count;
 
55
  gsize *other_send_count;
 
56
} ThreadData;
 
57
 
 
58
static gboolean
 
59
read_stream_cb (GObject *pollable_stream, gpointer _user_data)
 
60
{
 
61
  TestIOStreamThreadData *data = _user_data;
 
62
  ThreadData *user_data = data->user_data;
 
63
  gchar expected_data[MESSAGE_SIZE];
 
64
  GError *error = NULL;
 
65
  guint8 buf[MESSAGE_SIZE];
 
66
  gssize len;
 
67
 
 
68
  /* Try to receive some data. */
 
69
  len = g_pollable_input_stream_read_nonblocking (
 
70
      G_POLLABLE_INPUT_STREAM (pollable_stream), buf, sizeof (buf), NULL,
 
71
      &error);
 
72
 
 
73
  if (len == -1) {
 
74
    g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
 
75
    return TRUE;
 
76
  }
 
77
 
 
78
  g_assert_cmpint (len, ==, MESSAGE_SIZE);
 
79
 
 
80
  memset (expected_data, user_data->recv_count + '1', sizeof (expected_data));
 
81
  g_assert (memcmp (buf, expected_data, sizeof (expected_data)) == 0);
 
82
 
 
83
  user_data->recv_count++;
 
84
 
 
85
  if (user_data->recv_count == 10) {
 
86
    g_main_loop_quit (user_data->read_loop);
 
87
    return FALSE;
 
88
  }
 
89
 
 
90
  return TRUE;
 
91
}
 
92
 
 
93
static void
 
94
read_thread_cb (GInputStream *input_stream, TestIOStreamThreadData *data)
 
95
{
 
96
  GMainContext *main_context;
 
97
  GMainLoop *main_loop;
 
98
  GSource *stream_source;
 
99
  ThreadData *user_data = data->user_data;
 
100
 
 
101
  main_context = g_main_context_new ();
 
102
  main_loop = g_main_loop_new (main_context, FALSE);
 
103
  g_main_context_push_thread_default (main_context);
 
104
 
 
105
  stream_source =
 
106
      g_pollable_input_stream_create_source (
 
107
          G_POLLABLE_INPUT_STREAM (input_stream), NULL);
 
108
 
 
109
  g_source_set_callback (stream_source, (GSourceFunc) read_stream_cb,
 
110
      data, NULL);
 
111
  g_source_attach (stream_source, main_context);
 
112
  g_source_unref (stream_source);
 
113
 
 
114
  /* Run the main loop. */
 
115
  user_data->read_loop = main_loop;
 
116
  g_main_loop_run (main_loop);
 
117
 
 
118
  g_main_context_pop_thread_default (main_context);
 
119
  g_main_loop_unref (main_loop);
 
120
  g_main_context_unref (main_context);
 
121
 
 
122
  check_for_termination (data, &user_data->recv_count,
 
123
      user_data->other_recv_count, &user_data->send_count, 10);
 
124
}
 
125
 
 
126
static void
 
127
write_thread_cb (GOutputStream *output_stream, TestIOStreamThreadData *data)
 
128
{
 
129
  ThreadData *user_data = data->user_data;
 
130
  guint8 buf[MESSAGE_SIZE];
 
131
 
 
132
  for (user_data->send_count = 0;
 
133
       user_data->send_count < 10;
 
134
       user_data->send_count++) {
 
135
    GError *error = NULL;
 
136
 
 
137
    memset (buf, user_data->send_count + '1', MESSAGE_SIZE);
 
138
 
 
139
    g_pollable_output_stream_write_nonblocking (
 
140
        G_POLLABLE_OUTPUT_STREAM (output_stream), buf, sizeof (buf), NULL,
 
141
        &error);
 
142
    g_assert_no_error (error);
 
143
  }
 
144
}
 
145
 
 
146
int main (void)
 
147
{
 
148
  ThreadData *l_data, *r_data;
 
149
 
 
150
  const TestIOStreamCallbacks callbacks = {
 
151
    read_thread_cb,
 
152
    write_thread_cb,
 
153
    NULL,
 
154
    NULL,
 
155
  };
 
156
 
 
157
#ifdef G_OS_WIN32
 
158
  WSADATA w;
 
159
  WSAStartup (0x0202, &w);
 
160
#endif
 
161
  g_type_init ();
 
162
  g_thread_init (NULL);
 
163
 
 
164
  l_data = g_malloc0 (sizeof (ThreadData));
 
165
  r_data = g_malloc0 (sizeof (ThreadData));
 
166
 
 
167
  l_data->recv_count = 0;
 
168
  l_data->send_count = 0;
 
169
  l_data->other_recv_count = &r_data->recv_count;
 
170
  l_data->other_send_count = &r_data->send_count;
 
171
 
 
172
  r_data->recv_count = 0;
 
173
  r_data->send_count = 0;
 
174
  r_data->other_recv_count = &l_data->recv_count;
 
175
  r_data->other_send_count = &l_data->send_count;
 
176
 
 
177
  run_io_stream_test (30, TRUE, &callbacks, l_data, NULL, r_data, NULL);
 
178
 
 
179
  g_free (r_data);
 
180
  g_free (l_data);
 
181
 
 
182
#ifdef G_OS_WIN32
 
183
  WSACleanup ();
 
184
#endif
 
185
  return 0;
 
186
}