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

« back to all changes in this revision

Viewing changes to tests/test-io-stream-cancelling.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
  GCancellable *cancellable;  /* owned */
 
50
 
 
51
  GCond cond;
 
52
  GMutex mutex;
 
53
  gboolean blocking;  /* protected by @mutex */
 
54
} CancellationData;
 
55
 
 
56
static gpointer
 
57
cancellation_thread_cb (gpointer user_data)
 
58
{
 
59
  CancellationData *data = user_data;
 
60
 
 
61
  /* Wait to be signalled from read_thread_cb(). */
 
62
  g_mutex_lock (&data->mutex);
 
63
  while (!data->blocking)
 
64
    g_cond_wait (&data->cond, &data->mutex);
 
65
  g_mutex_unlock (&data->mutex);
 
66
 
 
67
  /* Try and ensure we cancel part-way through the read, rather than before the
 
68
   * read function is called. */
 
69
  g_usleep (100000);
 
70
 
 
71
  g_cancellable_cancel (data->cancellable);
 
72
 
 
73
  return NULL;
 
74
}
 
75
 
 
76
static void
 
77
read_thread_cb (GInputStream *input_stream, TestIOStreamThreadData *data)
 
78
{
 
79
  CancellationData *user_data = data->user_data;
 
80
  GError *error = NULL;
 
81
  guint8 buf[MESSAGE_SIZE];
 
82
  gssize len;
 
83
 
 
84
  /* Block on receiving some data or cancellation. */
 
85
  g_mutex_lock (&user_data->mutex);
 
86
  user_data->blocking = TRUE;
 
87
  g_cond_signal (&user_data->cond);
 
88
  g_mutex_unlock (&user_data->mutex);
 
89
 
 
90
  len = g_input_stream_read (input_stream, buf, sizeof (buf),
 
91
      user_data->cancellable, &error);
 
92
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
 
93
  g_assert (len == -1);
 
94
 
 
95
  g_main_loop_quit (data->error_loop);
 
96
}
 
97
 
 
98
int main (void)
 
99
{
 
100
  GThread *l_cancellation_thread, *r_cancellation_thread;
 
101
  CancellationData *l_data, *r_data;
 
102
 
 
103
  const TestIOStreamCallbacks callbacks = {
 
104
    read_thread_cb,
 
105
    NULL,
 
106
    NULL,
 
107
    NULL,
 
108
  };
 
109
 
 
110
#ifdef G_OS_WIN32
 
111
  WSADATA w;
 
112
  WSAStartup (0x0202, &w);
 
113
#endif
 
114
  g_type_init ();
 
115
  g_thread_init (NULL);
 
116
 
 
117
  l_data = g_malloc0 (sizeof (CancellationData));
 
118
  l_data->cancellable = g_cancellable_new ();
 
119
  l_data->blocking = FALSE;
 
120
 
 
121
  r_data = g_malloc0 (sizeof (CancellationData));
 
122
  r_data->cancellable = g_cancellable_new ();
 
123
  r_data->blocking = FALSE;
 
124
 
 
125
  l_cancellation_thread = spawn_thread ("libnice L cancel",
 
126
      cancellation_thread_cb, l_data);
 
127
  r_cancellation_thread = spawn_thread ("libnice R cancel",
 
128
      cancellation_thread_cb, r_data);
 
129
 
 
130
  run_io_stream_test (30, TRUE, &callbacks, l_data, NULL, r_data, NULL);
 
131
 
 
132
  g_thread_join (l_cancellation_thread);
 
133
  g_thread_join (r_cancellation_thread);
 
134
 
 
135
  /* Free things. */
 
136
  g_object_unref (r_data->cancellable);
 
137
  g_free (r_data);
 
138
  g_object_unref (l_data->cancellable);
 
139
  g_free (l_data);
 
140
 
 
141
#ifdef G_OS_WIN32
 
142
  WSACleanup ();
 
143
#endif
 
144
 
 
145
  return 0;
 
146
}