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_finished (SoupMessage *msg, gpointer data)
21 g_source_destroy (sd->timeout);
22 g_source_unref (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 g_main_context_get_thread_default (),
69 200, add_body_chunk, sd);
70 g_source_ref (sd->timeout);
71 g_signal_connect (msg, "finished",
72 G_CALLBACK (request_finished), sd);
75 /* Test 1: An async session in another thread with its own
76 * async_context can complete a request while the main thread's main
80 static gboolean idle_start_test1_thread (gpointer loop);
81 static gpointer test1_thread (gpointer user_data);
83 static GCond test1_cond;
84 static GMutex test1_mutex;
85 static GMainLoop *test1_loop;
88 do_test1 (gconstpointer data)
90 gboolean use_thread_context = GPOINTER_TO_INT (data);
92 test1_loop = g_main_loop_new (NULL, FALSE);
93 g_idle_add (idle_start_test1_thread, GINT_TO_POINTER (use_thread_context));
94 g_main_loop_run (test1_loop);
95 g_main_loop_unref (test1_loop);
99 idle_start_test1_thread (gpointer use_thread_context)
104 g_mutex_lock (&test1_mutex);
105 thread = g_thread_new ("test1_thread", test1_thread, use_thread_context);
107 time = g_get_monotonic_time () + 5000000;
108 if (g_cond_wait_until (&test1_cond, &test1_mutex, time))
109 g_thread_join (thread);
111 soup_test_assert (FALSE, "timeout");
112 g_thread_unref (thread);
115 g_mutex_unlock (&test1_mutex);
116 g_main_loop_quit (test1_loop);
121 test1_finished (SoupSession *session, SoupMessage *msg, gpointer loop)
123 g_main_loop_quit (loop);
127 test1_thread (gpointer use_thread_context)
129 SoupSession *session;
130 GMainContext *async_context;
135 /* Wait for main thread to be waiting on test1_cond */
136 g_mutex_lock (&test1_mutex);
137 g_mutex_unlock (&test1_mutex);
139 async_context = g_main_context_new ();
140 if (use_thread_context) {
141 g_main_context_push_thread_default (async_context);
142 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
143 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
146 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
147 SOUP_SESSION_ASYNC_CONTEXT, async_context,
150 g_main_context_unref (async_context);
152 uri = g_build_filename (base_uri, "slow", NULL);
154 debug_printf (1, " send_message\n");
155 msg = soup_message_new ("GET", uri);
156 soup_session_send_message (session, msg);
157 soup_test_assert_message_status (msg, SOUP_STATUS_OK);
158 g_object_unref (msg);
160 debug_printf (1, " queue_message\n");
161 msg = soup_message_new ("GET", uri);
162 loop = g_main_loop_new (async_context, FALSE);
164 soup_session_queue_message (session, msg, test1_finished, loop);
165 g_main_loop_run (loop);
166 g_main_loop_unref (loop);
167 soup_test_assert_message_status (msg, SOUP_STATUS_OK);
168 g_object_unref (msg);
170 soup_test_session_abort_unref (session);
173 g_cond_signal (&test1_cond);
175 if (use_thread_context)
176 g_main_context_pop_thread_default (async_context);
180 /* Test 2: An async session in the main thread with its own
181 * async_context runs independently of the default main loop.
184 static gboolean idle_test2_fail (gpointer user_data);
187 do_test2 (gconstpointer data)
189 gboolean use_thread_context = GPOINTER_TO_INT (data);
191 GMainContext *async_context;
192 SoupSession *session;
196 idle = g_idle_add_full (G_PRIORITY_HIGH, idle_test2_fail, NULL, NULL);
198 async_context = g_main_context_new ();
199 if (use_thread_context) {
200 g_main_context_push_thread_default (async_context);
201 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
202 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
205 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
206 SOUP_SESSION_ASYNC_CONTEXT, async_context,
209 g_main_context_unref (async_context);
211 uri = g_build_filename (base_uri, "slow", NULL);
213 debug_printf (1, " send_message\n");
214 msg = soup_message_new ("GET", uri);
215 soup_session_send_message (session, msg);
216 soup_test_assert_message_status (msg, SOUP_STATUS_OK);
217 g_object_unref (msg);
219 soup_test_session_abort_unref (session);
222 g_source_remove (idle);
224 if (use_thread_context)
225 g_main_context_pop_thread_default (async_context);
229 idle_test2_fail (gpointer user_data)
231 soup_test_assert (FALSE, "idle ran");
236 multi_request_started (SoupSession *session, SoupMessage *msg,
237 SoupSocket *socket, gpointer user_data)
239 g_object_set_data (G_OBJECT (msg), "started", GUINT_TO_POINTER (TRUE));
243 msg1_got_headers (SoupMessage *msg, gpointer user_data)
245 GMainLoop *loop = user_data;
247 g_main_loop_quit (loop);
251 multi_msg_finished (SoupSession *session, SoupMessage *msg, gpointer user_data)
253 GMainLoop *loop = user_data;
255 g_object_set_data (G_OBJECT (msg), "finished", GUINT_TO_POINTER (TRUE));
256 g_main_loop_quit (loop);
260 do_multicontext_test (void)
262 SoupSession *session;
263 SoupMessage *msg1, *msg2;
264 GMainContext *context1, *context2;
265 GMainLoop *loop1, *loop2;
267 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
268 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
270 g_signal_connect (session, "request-started",
271 G_CALLBACK (multi_request_started), NULL);
273 context1 = g_main_context_new ();
274 loop1 = g_main_loop_new (context1, FALSE);
275 context2 = g_main_context_new ();
276 loop2 = g_main_loop_new (context2, FALSE);
278 g_main_context_push_thread_default (context1);
279 msg1 = soup_message_new ("GET", base_uri);
281 soup_session_queue_message (session, msg1, multi_msg_finished, loop1);
282 g_signal_connect (msg1, "got-headers",
283 G_CALLBACK (msg1_got_headers), loop1);
284 g_object_set_data (G_OBJECT (msg1), "session", session);
285 g_main_context_pop_thread_default (context1);
287 g_main_context_push_thread_default (context2);
288 msg2 = soup_message_new ("GET", base_uri);
290 soup_session_queue_message (session, msg2, multi_msg_finished, loop2);
291 g_main_context_pop_thread_default (context2);
293 g_main_context_push_thread_default (context1);
294 g_main_loop_run (loop1);
295 g_main_context_pop_thread_default (context1);
297 if (!g_object_get_data (G_OBJECT (msg1), "started"))
298 soup_test_assert (FALSE, "msg1 not started");
299 if (g_object_get_data (G_OBJECT (msg2), "started"))
300 soup_test_assert (FALSE, "msg2 started while loop1 was running");
302 g_main_context_push_thread_default (context2);
303 g_main_loop_run (loop2);
304 g_main_context_pop_thread_default (context2);
306 if (g_object_get_data (G_OBJECT (msg1), "finished"))
307 soup_test_assert (FALSE, "msg1 finished while loop2 was running");
308 if (!g_object_get_data (G_OBJECT (msg2), "finished"))
309 soup_test_assert (FALSE, "msg2 not finished");
311 g_main_context_push_thread_default (context1);
312 g_main_loop_run (loop1);
313 g_main_context_pop_thread_default (context1);
315 if (!g_object_get_data (G_OBJECT (msg1), "finished"))
316 soup_test_assert (FALSE, "msg1 not finished");
318 g_object_unref (msg1);
319 g_object_unref (msg2);
321 soup_test_session_abort_unref (session);
323 g_main_loop_unref (loop1);
324 g_main_loop_unref (loop2);
325 g_main_context_unref (context1);
326 g_main_context_unref (context2);
330 main (int argc, char **argv)
336 test_init (argc, argv, NULL);
338 server = soup_test_server_new (SOUP_TEST_SERVER_IN_THREAD);
339 soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
340 uri = soup_test_server_get_uri (server, "http", NULL);
341 base_uri = soup_uri_to_string (uri, FALSE);
344 g_test_add_data_func ("/context/blocking/explicit", GINT_TO_POINTER (FALSE), do_test1);
345 g_test_add_data_func ("/context/blocking/thread-default", GINT_TO_POINTER (TRUE), do_test1);
346 g_test_add_data_func ("/context/nested/explicit", GINT_TO_POINTER (FALSE), do_test2);
347 g_test_add_data_func ("/context/nested/thread-default", GINT_TO_POINTER (TRUE), do_test2);
348 g_test_add_func ("/context/multiple", do_multicontext_test);
353 soup_test_server_quit_unref (server);