1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Red Hat, Inc.
17 #include <libsoup/soup-address.h>
18 #include <libsoup/soup-message.h>
19 #include <libsoup/soup-misc.h>
20 #include <libsoup/soup-server.h>
21 #include <libsoup/soup-session-async.h>
22 #include <libsoup/soup-session-sync.h>
24 #include "test-utils.h"
26 static char *base_uri;
35 request_failed (SoupMessage *msg, gpointer data)
39 if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code))
40 g_source_destroy (sd->timeout);
45 add_body_chunk (gpointer data)
49 soup_message_body_append (sd->msg->response_body,
50 SOUP_MEMORY_STATIC, "OK\r\n", 4);
51 soup_message_body_complete (sd->msg->response_body);
52 soup_server_unpause_message (sd->server, sd->msg);
53 g_object_unref (sd->msg);
59 server_callback (SoupServer *server, SoupMessage *msg,
60 const char *path, GHashTable *query,
61 SoupClientContext *context, gpointer data)
65 if (msg->method != SOUP_METHOD_GET) {
66 soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
70 soup_message_set_status (msg, SOUP_STATUS_OK);
71 if (!strcmp (path, "/fast")) {
72 soup_message_set_response (msg, "text/plain",
73 SOUP_MEMORY_STATIC, "OK\r\n", 4);
77 soup_message_headers_set_encoding (msg->response_headers,
78 SOUP_ENCODING_CHUNKED);
80 soup_server_pause_message (server, msg);
82 sd = g_new (SlowData, 1);
85 sd->timeout = soup_add_timeout (
86 soup_server_get_async_context (server),
87 200, add_body_chunk, sd);
88 g_signal_connect (msg, "finished",
89 G_CALLBACK (request_failed), sd);
92 /* Test 1: An async session in another thread with its own
93 * async_context can complete a request while the main thread's main
97 static gboolean idle_start_test1_thread (gpointer loop);
98 static gpointer test1_thread (gpointer user_data);
100 static GCond test1_cond;
101 static GMutex test1_mutex;
102 static GMainLoop *test1_loop;
105 do_test1 (int n, gboolean use_thread_context)
107 debug_printf (1, "\nTest %d: blocking the main thread does not block other thread\n", n);
108 if (use_thread_context)
109 debug_printf (1, "(Using g_main_context_push_thread_default())\n");
111 debug_printf (1, "(Using SOUP_SESSION_ASYNC_CONTEXT)\n");
113 test1_loop = g_main_loop_new (NULL, FALSE);
114 g_idle_add (idle_start_test1_thread, GINT_TO_POINTER (use_thread_context));
115 g_main_loop_run (test1_loop);
116 g_main_loop_unref (test1_loop);
120 idle_start_test1_thread (gpointer use_thread_context)
125 g_mutex_lock (&test1_mutex);
126 thread = g_thread_new ("test1_thread", test1_thread, use_thread_context);
128 time = g_get_monotonic_time () + 5000000;
129 if (g_cond_wait_until (&test1_cond, &test1_mutex, time))
130 g_thread_join (thread);
132 debug_printf (1, " timeout!\n");
133 g_thread_unref (thread);
137 g_mutex_unlock (&test1_mutex);
138 g_main_loop_quit (test1_loop);
143 test1_finished (SoupSession *session, SoupMessage *msg, gpointer loop)
145 g_main_loop_quit (loop);
149 test1_thread (gpointer use_thread_context)
151 SoupSession *session;
152 GMainContext *async_context;
157 /* Wait for main thread to be waiting on test1_cond */
158 g_mutex_lock (&test1_mutex);
159 g_mutex_unlock (&test1_mutex);
161 async_context = g_main_context_new ();
162 if (use_thread_context) {
163 g_main_context_push_thread_default (async_context);
164 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
165 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
168 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
169 SOUP_SESSION_ASYNC_CONTEXT, async_context,
172 g_main_context_unref (async_context);
174 uri = g_build_filename (base_uri, "slow", NULL);
176 debug_printf (1, " send_message\n");
177 msg = soup_message_new ("GET", uri);
178 soup_session_send_message (session, msg);
179 if (msg->status_code != SOUP_STATUS_OK) {
180 debug_printf (1, " unexpected status: %d %s\n",
181 msg->status_code, msg->reason_phrase);
184 g_object_unref (msg);
186 debug_printf (1, " queue_message\n");
187 msg = soup_message_new ("GET", uri);
188 loop = g_main_loop_new (async_context, FALSE);
190 soup_session_queue_message (session, msg, test1_finished, loop);
191 g_main_loop_run (loop);
192 g_main_loop_unref (loop);
193 if (msg->status_code != SOUP_STATUS_OK) {
194 debug_printf (1, " unexpected status: %d %s\n",
195 msg->status_code, msg->reason_phrase);
198 g_object_unref (msg);
200 soup_test_session_abort_unref (session);
203 g_cond_signal (&test1_cond);
205 if (use_thread_context)
206 g_main_context_pop_thread_default (async_context);
210 /* Test 2: An async session in the main thread with its own
211 * async_context runs independently of the default main loop.
214 static gboolean idle_test2_fail (gpointer user_data);
217 do_test2 (int n, gboolean use_thread_context)
220 GMainContext *async_context;
221 SoupSession *session;
225 debug_printf (1, "\nTest %d: a session with its own context is independent of the main loop.\n", n);
226 if (use_thread_context)
227 debug_printf (1, "(Using g_main_context_push_thread_default())\n");
229 debug_printf (1, "(Using SOUP_SESSION_ASYNC_CONTEXT)\n");
231 idle = g_idle_add_full (G_PRIORITY_HIGH, idle_test2_fail, NULL, NULL);
233 async_context = g_main_context_new ();
234 if (use_thread_context) {
235 g_main_context_push_thread_default (async_context);
236 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
237 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
240 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
241 SOUP_SESSION_ASYNC_CONTEXT, async_context,
244 g_main_context_unref (async_context);
246 uri = g_build_filename (base_uri, "slow", NULL);
248 debug_printf (1, " send_message\n");
249 msg = soup_message_new ("GET", uri);
250 soup_session_send_message (session, msg);
251 if (msg->status_code != SOUP_STATUS_OK) {
252 debug_printf (1, " unexpected status: %d %s\n",
253 msg->status_code, msg->reason_phrase);
256 g_object_unref (msg);
258 soup_test_session_abort_unref (session);
261 g_source_remove (idle);
263 if (use_thread_context)
264 g_main_context_pop_thread_default (async_context);
268 idle_test2_fail (gpointer user_data)
270 debug_printf (1, " idle ran!\n");
276 multi_request_started (SoupSession *session, SoupMessage *msg,
277 SoupSocket *socket, gpointer user_data)
279 g_object_set_data (G_OBJECT (msg), "started", GUINT_TO_POINTER (TRUE));
283 msg1_got_headers (SoupMessage *msg, gpointer user_data)
285 GMainLoop *loop = user_data;
287 g_main_loop_quit (loop);
291 multi_msg_finished (SoupSession *session, SoupMessage *msg, gpointer user_data)
293 GMainLoop *loop = user_data;
295 g_object_set_data (G_OBJECT (msg), "finished", GUINT_TO_POINTER (TRUE));
296 g_main_loop_quit (loop);
300 do_multicontext_test (int n)
302 SoupSession *session;
303 SoupMessage *msg1, *msg2;
304 GMainContext *context1, *context2;
305 GMainLoop *loop1, *loop2;
307 debug_printf (1, "\nTest %d: Using multiple async contexts\n", n);
309 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
310 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
312 g_signal_connect (session, "request-started",
313 G_CALLBACK (multi_request_started), NULL);
315 context1 = g_main_context_new ();
316 loop1 = g_main_loop_new (context1, FALSE);
317 context2 = g_main_context_new ();
318 loop2 = g_main_loop_new (context2, FALSE);
320 g_main_context_push_thread_default (context1);
321 msg1 = soup_message_new ("GET", base_uri);
323 soup_session_queue_message (session, msg1, multi_msg_finished, loop1);
324 g_signal_connect (msg1, "got-headers",
325 G_CALLBACK (msg1_got_headers), loop1);
326 g_object_set_data (G_OBJECT (msg1), "session", session);
327 g_main_context_pop_thread_default (context1);
329 g_main_context_push_thread_default (context2);
330 msg2 = soup_message_new ("GET", base_uri);
332 soup_session_queue_message (session, msg2, multi_msg_finished, loop2);
333 g_main_context_pop_thread_default (context2);
335 g_main_context_push_thread_default (context1);
336 g_main_loop_run (loop1);
337 g_main_context_pop_thread_default (context1);
339 if (!g_object_get_data (G_OBJECT (msg1), "started")) {
340 debug_printf (1, " msg1 not started??\n");
343 if (g_object_get_data (G_OBJECT (msg2), "started")) {
344 debug_printf (1, " msg2 started while loop1 was running!\n");
348 g_main_context_push_thread_default (context2);
349 g_main_loop_run (loop2);
350 g_main_context_pop_thread_default (context2);
352 if (g_object_get_data (G_OBJECT (msg1), "finished")) {
353 debug_printf (1, " msg1 finished while loop2 was running!\n");
356 if (!g_object_get_data (G_OBJECT (msg2), "finished")) {
357 debug_printf (1, " msg2 not finished??\n");
361 g_main_context_push_thread_default (context1);
362 g_main_loop_run (loop1);
363 g_main_context_pop_thread_default (context1);
365 if (!g_object_get_data (G_OBJECT (msg1), "finished")) {
366 debug_printf (1, " msg1 not finished??\n");
370 g_object_unref (msg1);
371 g_object_unref (msg2);
373 soup_test_session_abort_unref (session);
375 g_main_loop_unref (loop1);
376 g_main_loop_unref (loop2);
377 g_main_context_unref (context1);
378 g_main_context_unref (context2);
382 main (int argc, char **argv)
386 test_init (argc, argv, NULL);
388 server = soup_test_server_new (TRUE);
389 soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
390 base_uri = g_strdup_printf ("http://127.0.0.1:%u/",
391 soup_server_get_port (server));
397 do_multicontext_test (5);
400 soup_test_server_quit_unref (server);