1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Red Hat, Inc.
6 #include "test-utils.h"
17 request_failed (SoupMessage *msg, gpointer data)
21 if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code))
22 g_source_destroy (sd->timeout);
27 add_body_chunk (gpointer data)
31 soup_message_body_append (sd->msg->response_body,
32 SOUP_MEMORY_STATIC, "OK\r\n", 4);
33 soup_message_body_complete (sd->msg->response_body);
34 soup_server_unpause_message (sd->server, sd->msg);
35 g_object_unref (sd->msg);
41 server_callback (SoupServer *server, SoupMessage *msg,
42 const char *path, GHashTable *query,
43 SoupClientContext *context, gpointer data)
47 if (msg->method != SOUP_METHOD_GET) {
48 soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
52 soup_message_set_status (msg, SOUP_STATUS_OK);
53 if (!strcmp (path, "/fast")) {
54 soup_message_set_response (msg, "text/plain",
55 SOUP_MEMORY_STATIC, "OK\r\n", 4);
59 soup_message_headers_set_encoding (msg->response_headers,
60 SOUP_ENCODING_CHUNKED);
62 soup_server_pause_message (server, msg);
64 sd = g_new (SlowData, 1);
67 sd->timeout = soup_add_timeout (
68 soup_server_get_async_context (server),
69 200, add_body_chunk, sd);
70 g_signal_connect (msg, "finished",
71 G_CALLBACK (request_failed), sd);
74 /* Test 1: An async session in another thread with its own
75 * async_context can complete a request while the main thread's main
79 static gboolean idle_start_test1_thread (gpointer loop);
80 static gpointer test1_thread (gpointer user_data);
82 static GCond test1_cond;
83 static GMutex test1_mutex;
84 static GMainLoop *test1_loop;
87 do_test1 (gconstpointer data)
89 gboolean use_thread_context = GPOINTER_TO_INT (data);
91 test1_loop = g_main_loop_new (NULL, FALSE);
92 g_idle_add (idle_start_test1_thread, GINT_TO_POINTER (use_thread_context));
93 g_main_loop_run (test1_loop);
94 g_main_loop_unref (test1_loop);
98 idle_start_test1_thread (gpointer use_thread_context)
103 g_mutex_lock (&test1_mutex);
104 thread = g_thread_new ("test1_thread", test1_thread, use_thread_context);
106 time = g_get_monotonic_time () + 5000000;
107 if (g_cond_wait_until (&test1_cond, &test1_mutex, time))
108 g_thread_join (thread);
110 soup_test_assert (FALSE, "timeout");
111 g_thread_unref (thread);
114 g_mutex_unlock (&test1_mutex);
115 g_main_loop_quit (test1_loop);
120 test1_finished (SoupSession *session, SoupMessage *msg, gpointer loop)
122 g_main_loop_quit (loop);
126 test1_thread (gpointer use_thread_context)
128 SoupSession *session;
129 GMainContext *async_context;
134 /* Wait for main thread to be waiting on test1_cond */
135 g_mutex_lock (&test1_mutex);
136 g_mutex_unlock (&test1_mutex);
138 async_context = g_main_context_new ();
139 if (use_thread_context) {
140 g_main_context_push_thread_default (async_context);
141 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
142 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
145 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
146 SOUP_SESSION_ASYNC_CONTEXT, async_context,
149 g_main_context_unref (async_context);
151 uri = g_build_filename (base_uri, "slow", NULL);
153 debug_printf (1, " send_message\n");
154 msg = soup_message_new ("GET", uri);
155 soup_session_send_message (session, msg);
156 soup_test_assert_message_status (msg, SOUP_STATUS_OK);
157 g_object_unref (msg);
159 debug_printf (1, " queue_message\n");
160 msg = soup_message_new ("GET", uri);
161 loop = g_main_loop_new (async_context, FALSE);
163 soup_session_queue_message (session, msg, test1_finished, loop);
164 g_main_loop_run (loop);
165 g_main_loop_unref (loop);
166 soup_test_assert_message_status (msg, SOUP_STATUS_OK);
167 g_object_unref (msg);
169 soup_test_session_abort_unref (session);
172 g_cond_signal (&test1_cond);
174 if (use_thread_context)
175 g_main_context_pop_thread_default (async_context);
179 /* Test 2: An async session in the main thread with its own
180 * async_context runs independently of the default main loop.
183 static gboolean idle_test2_fail (gpointer user_data);
186 do_test2 (gconstpointer data)
188 gboolean use_thread_context = GPOINTER_TO_INT (data);
190 GMainContext *async_context;
191 SoupSession *session;
195 idle = g_idle_add_full (G_PRIORITY_HIGH, idle_test2_fail, NULL, NULL);
197 async_context = g_main_context_new ();
198 if (use_thread_context) {
199 g_main_context_push_thread_default (async_context);
200 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
201 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
204 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
205 SOUP_SESSION_ASYNC_CONTEXT, async_context,
208 g_main_context_unref (async_context);
210 uri = g_build_filename (base_uri, "slow", NULL);
212 debug_printf (1, " send_message\n");
213 msg = soup_message_new ("GET", uri);
214 soup_session_send_message (session, msg);
215 soup_test_assert_message_status (msg, SOUP_STATUS_OK);
216 g_object_unref (msg);
218 soup_test_session_abort_unref (session);
221 g_source_remove (idle);
223 if (use_thread_context)
224 g_main_context_pop_thread_default (async_context);
228 idle_test2_fail (gpointer user_data)
230 soup_test_assert (FALSE, "idle ran");
235 multi_request_started (SoupSession *session, SoupMessage *msg,
236 SoupSocket *socket, gpointer user_data)
238 g_object_set_data (G_OBJECT (msg), "started", GUINT_TO_POINTER (TRUE));
242 msg1_got_headers (SoupMessage *msg, gpointer user_data)
244 GMainLoop *loop = user_data;
246 g_main_loop_quit (loop);
250 multi_msg_finished (SoupSession *session, SoupMessage *msg, gpointer user_data)
252 GMainLoop *loop = user_data;
254 g_object_set_data (G_OBJECT (msg), "finished", GUINT_TO_POINTER (TRUE));
255 g_main_loop_quit (loop);
259 do_multicontext_test (void)
261 SoupSession *session;
262 SoupMessage *msg1, *msg2;
263 GMainContext *context1, *context2;
264 GMainLoop *loop1, *loop2;
266 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
267 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
269 g_signal_connect (session, "request-started",
270 G_CALLBACK (multi_request_started), NULL);
272 context1 = g_main_context_new ();
273 loop1 = g_main_loop_new (context1, FALSE);
274 context2 = g_main_context_new ();
275 loop2 = g_main_loop_new (context2, FALSE);
277 g_main_context_push_thread_default (context1);
278 msg1 = soup_message_new ("GET", base_uri);
280 soup_session_queue_message (session, msg1, multi_msg_finished, loop1);
281 g_signal_connect (msg1, "got-headers",
282 G_CALLBACK (msg1_got_headers), loop1);
283 g_object_set_data (G_OBJECT (msg1), "session", session);
284 g_main_context_pop_thread_default (context1);
286 g_main_context_push_thread_default (context2);
287 msg2 = soup_message_new ("GET", base_uri);
289 soup_session_queue_message (session, msg2, multi_msg_finished, loop2);
290 g_main_context_pop_thread_default (context2);
292 g_main_context_push_thread_default (context1);
293 g_main_loop_run (loop1);
294 g_main_context_pop_thread_default (context1);
296 if (!g_object_get_data (G_OBJECT (msg1), "started"))
297 soup_test_assert (FALSE, "msg1 not started");
298 if (g_object_get_data (G_OBJECT (msg2), "started"))
299 soup_test_assert (FALSE, "msg2 started while loop1 was running");
301 g_main_context_push_thread_default (context2);
302 g_main_loop_run (loop2);
303 g_main_context_pop_thread_default (context2);
305 if (g_object_get_data (G_OBJECT (msg1), "finished"))
306 soup_test_assert (FALSE, "msg1 finished while loop2 was running");
307 if (!g_object_get_data (G_OBJECT (msg2), "finished"))
308 soup_test_assert (FALSE, "msg2 not finished");
310 g_main_context_push_thread_default (context1);
311 g_main_loop_run (loop1);
312 g_main_context_pop_thread_default (context1);
314 if (!g_object_get_data (G_OBJECT (msg1), "finished"))
315 soup_test_assert (FALSE, "msg1 not finished");
317 g_object_unref (msg1);
318 g_object_unref (msg2);
320 soup_test_session_abort_unref (session);
322 g_main_loop_unref (loop1);
323 g_main_loop_unref (loop2);
324 g_main_context_unref (context1);
325 g_main_context_unref (context2);
329 main (int argc, char **argv)
334 test_init (argc, argv, NULL);
336 server = soup_test_server_new (TRUE);
337 soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
338 base_uri = g_strdup_printf ("http://127.0.0.1:%u/",
339 soup_server_get_port (server));
341 g_test_add_data_func ("/context/blocking/explicit", GINT_TO_POINTER (FALSE), do_test1);
342 g_test_add_data_func ("/context/blocking/thread-default", GINT_TO_POINTER (TRUE), do_test1);
343 g_test_add_data_func ("/context/nested/explicit", GINT_TO_POINTER (FALSE), do_test2);
344 g_test_add_data_func ("/context/nested/thread-default", GINT_TO_POINTER (TRUE), do_test2);
345 g_test_add_func ("/context/multiple", do_multicontext_test);
350 soup_test_server_quit_unref (server);