1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2007 Red Hat, Inc.
*/
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <glib.h>
#include <libsoup/soup-address.h>
#include <libsoup/soup-message.h>
#include <libsoup/soup-misc.h>
#include <libsoup/soup-server.h>
#include <libsoup/soup-session-async.h>
#include <libsoup/soup-session-sync.h>
#include "test-utils.h"
static char *base_uri;
typedef struct {
SoupServer *server;
SoupMessage *msg;
GSource *timeout;
} SlowData;
static void
request_failed (SoupMessage *msg, gpointer data)
{
SlowData *sd = data;
if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code))
g_source_destroy (sd->timeout);
g_free (sd);
}
static gboolean
add_body_chunk (gpointer data)
{
SlowData *sd = data;
soup_message_body_append (sd->msg->response_body,
SOUP_MEMORY_STATIC, "OK\r\n", 4);
soup_message_body_complete (sd->msg->response_body);
soup_server_unpause_message (sd->server, sd->msg);
g_object_unref (sd->msg);
return FALSE;
}
static void
server_callback (SoupServer *server, SoupMessage *msg,
const char *path, GHashTable *query,
SoupClientContext *context, gpointer data)
{
SlowData *sd;
if (msg->method != SOUP_METHOD_GET) {
soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
return;
}
soup_message_set_status (msg, SOUP_STATUS_OK);
if (!strcmp (path, "/fast")) {
soup_message_set_response (msg, "text/plain",
SOUP_MEMORY_STATIC, "OK\r\n", 4);
return;
}
soup_message_headers_set_encoding (msg->response_headers,
SOUP_ENCODING_CHUNKED);
g_object_ref (msg);
soup_server_pause_message (server, msg);
sd = g_new (SlowData, 1);
sd->server = server;
sd->msg = msg;
sd->timeout = soup_add_timeout (
soup_server_get_async_context (server),
200, add_body_chunk, sd);
g_signal_connect (msg, "finished",
G_CALLBACK (request_failed), sd);
}
/* Test 1: An async session in another thread with its own
* async_context can complete a request while the main thread's main
* loop is stopped.
*/
static gboolean idle_start_test1_thread (gpointer loop);
static gpointer test1_thread (gpointer user_data);
static GCond *test1_cond;
static GMutex *test1_mutex;
static GMainLoop *test1_loop;
static void
do_test1 (int n, gboolean use_thread_context)
{
debug_printf (1, "\nTest %d: blocking the main thread does not block other thread\n", n);
if (use_thread_context)
debug_printf (1, "(Using g_main_context_push_thread_default())\n");
else
debug_printf (1, "(Using SOUP_SESSION_ASYNC_CONTEXT)\n");
test1_cond = g_cond_new ();
test1_mutex = g_mutex_new ();
test1_loop = g_main_loop_new (NULL, FALSE);
g_idle_add (idle_start_test1_thread, GINT_TO_POINTER (use_thread_context));
g_main_loop_run (test1_loop);
g_main_loop_unref (test1_loop);
g_mutex_free (test1_mutex);
g_cond_free (test1_cond);
}
static gboolean
idle_start_test1_thread (gpointer use_thread_context)
{
GTimeVal time;
GThread *thread;
g_mutex_lock (test1_mutex);
thread = g_thread_create (test1_thread, use_thread_context, TRUE, NULL);
g_get_current_time (&time);
time.tv_sec += 5;
if (g_cond_timed_wait (test1_cond, test1_mutex, &time))
g_thread_join (thread);
else {
debug_printf (1, " timeout!\n");
errors++;
}
g_mutex_unlock (test1_mutex);
g_main_loop_quit (test1_loop);
return FALSE;
}
static void
test1_finished (SoupSession *session, SoupMessage *msg, gpointer loop)
{
g_main_loop_quit (loop);
}
static gpointer
test1_thread (gpointer use_thread_context)
{
SoupSession *session;
GMainContext *async_context;
char *uri;
SoupMessage *msg;
GMainLoop *loop;
/* Wait for main thread to be waiting on test1_cond */
g_mutex_lock (test1_mutex);
g_mutex_unlock (test1_mutex);
async_context = g_main_context_new ();
if (use_thread_context) {
g_main_context_push_thread_default (async_context);
session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
NULL);
} else {
session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
SOUP_SESSION_ASYNC_CONTEXT, async_context,
NULL);
}
g_main_context_unref (async_context);
uri = g_build_filename (base_uri, "slow", NULL);
debug_printf (1, " send_message\n");
msg = soup_message_new ("GET", uri);
soup_session_send_message (session, msg);
if (msg->status_code != SOUP_STATUS_OK) {
debug_printf (1, " unexpected status: %d %s\n",
msg->status_code, msg->reason_phrase);
errors++;
}
g_object_unref (msg);
debug_printf (1, " queue_message\n");
msg = soup_message_new ("GET", uri);
loop = g_main_loop_new (async_context, FALSE);
g_object_ref (msg);
soup_session_queue_message (session, msg, test1_finished, loop);
g_main_loop_run (loop);
g_main_loop_unref (loop);
if (msg->status_code != SOUP_STATUS_OK) {
debug_printf (1, " unexpected status: %d %s\n",
msg->status_code, msg->reason_phrase);
errors++;
}
g_object_unref (msg);
soup_test_session_abort_unref (session);
g_free (uri);
g_cond_signal (test1_cond);
if (use_thread_context)
g_main_context_pop_thread_default (async_context);
return NULL;
}
/* Test 2: An async session in the main thread with its own
* async_context runs independently of the default main loop.
*/
static gboolean idle_test2_fail (gpointer user_data);
static void
do_test2 (int n, gboolean use_thread_context)
{
guint idle;
GMainContext *async_context;
SoupSession *session;
char *uri;
SoupMessage *msg;
debug_printf (1, "\nTest %d: a session with its own context is independent of the main loop.\n", n);
if (use_thread_context)
debug_printf (1, "(Using g_main_context_push_thread_default())\n");
else
debug_printf (1, "(Using SOUP_SESSION_ASYNC_CONTEXT)\n");
idle = g_idle_add_full (G_PRIORITY_HIGH, idle_test2_fail, NULL, NULL);
async_context = g_main_context_new ();
if (use_thread_context) {
g_main_context_push_thread_default (async_context);
session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
NULL);
} else {
session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
SOUP_SESSION_ASYNC_CONTEXT, async_context,
NULL);
}
g_main_context_unref (async_context);
uri = g_build_filename (base_uri, "slow", NULL);
debug_printf (1, " send_message\n");
msg = soup_message_new ("GET", uri);
soup_session_send_message (session, msg);
if (msg->status_code != SOUP_STATUS_OK) {
debug_printf (1, " unexpected status: %d %s\n",
msg->status_code, msg->reason_phrase);
errors++;
}
g_object_unref (msg);
soup_test_session_abort_unref (session);
g_free (uri);
g_source_remove (idle);
if (use_thread_context)
g_main_context_pop_thread_default (async_context);
}
static gboolean
idle_test2_fail (gpointer user_data)
{
debug_printf (1, " idle ran!\n");
errors++;
return FALSE;
}
static void
multi_request_started (SoupSession *session, SoupMessage *msg,
SoupSocket *socket, gpointer user_data)
{
g_object_set_data (G_OBJECT (msg), "started", GUINT_TO_POINTER (TRUE));
}
static void
msg1_got_headers (SoupMessage *msg, gpointer user_data)
{
GMainLoop *loop = user_data;
g_main_loop_quit (loop);
}
static void
multi_msg_finished (SoupSession *session, SoupMessage *msg, gpointer user_data)
{
GMainLoop *loop = user_data;
g_object_set_data (G_OBJECT (msg), "finished", GUINT_TO_POINTER (TRUE));
g_main_loop_quit (loop);
}
static void
do_multicontext_test (int n)
{
SoupSession *session;
SoupMessage *msg1, *msg2;
GMainContext *context1, *context2;
GMainLoop *loop1, *loop2;
debug_printf (1, "\nTest %d: Using multiple async contexts\n", n);
session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
NULL);
g_signal_connect (session, "request-started",
G_CALLBACK (multi_request_started), NULL);
context1 = g_main_context_new ();
loop1 = g_main_loop_new (context1, FALSE);
context2 = g_main_context_new ();
loop2 = g_main_loop_new (context2, FALSE);
g_main_context_push_thread_default (context1);
msg1 = soup_message_new ("GET", base_uri);
g_object_ref (msg1);
soup_session_queue_message (session, msg1, multi_msg_finished, loop1);
g_signal_connect (msg1, "got-headers",
G_CALLBACK (msg1_got_headers), loop1);
g_object_set_data (G_OBJECT (msg1), "session", session);
g_main_context_pop_thread_default (context1);
g_main_context_push_thread_default (context2);
msg2 = soup_message_new ("GET", base_uri);
g_object_ref (msg2);
soup_session_queue_message (session, msg2, multi_msg_finished, loop2);
g_main_context_pop_thread_default (context2);
g_main_context_push_thread_default (context1);
g_main_loop_run (loop1);
g_main_context_pop_thread_default (context1);
if (!g_object_get_data (G_OBJECT (msg1), "started")) {
debug_printf (1, " msg1 not started??\n");
errors++;
}
if (g_object_get_data (G_OBJECT (msg2), "started")) {
debug_printf (1, " msg2 started while loop1 was running!\n");
errors++;
}
g_main_context_push_thread_default (context2);
g_main_loop_run (loop2);
g_main_context_pop_thread_default (context2);
if (g_object_get_data (G_OBJECT (msg1), "finished")) {
debug_printf (1, " msg1 finished while loop2 was running!\n");
errors++;
}
if (!g_object_get_data (G_OBJECT (msg2), "finished")) {
debug_printf (1, " msg2 not finished??\n");
errors++;
}
g_main_context_push_thread_default (context1);
g_main_loop_run (loop1);
g_main_context_pop_thread_default (context1);
if (!g_object_get_data (G_OBJECT (msg1), "finished")) {
debug_printf (1, " msg1 not finished??\n");
errors++;
}
g_object_unref (msg1);
g_object_unref (msg2);
soup_test_session_abort_unref (session);
g_main_loop_unref (loop1);
g_main_loop_unref (loop2);
g_main_context_unref (context1);
g_main_context_unref (context2);
}
int
main (int argc, char **argv)
{
SoupServer *server;
test_init (argc, argv, NULL);
server = soup_test_server_new (TRUE);
soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
base_uri = g_strdup_printf ("http://127.0.0.1:%u/",
soup_server_get_port (server));
do_test1 (1, FALSE);
do_test1 (2, TRUE);
do_test2 (3, FALSE);
do_test2 (4, TRUE);
do_multicontext_test (5);
g_free (base_uri);
soup_test_server_quit_unref (server);
test_cleanup ();
return errors != 0;
}
|