add missing slash in %configure
[platform/upstream/libsoup.git] / tests / context-test.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 Red Hat, Inc.
4  */
5
6 #include "test-utils.h"
7
8 static char *base_uri;
9
10 typedef struct {
11         SoupServer *server;
12         SoupMessage *msg;
13         GSource *timeout;
14 } SlowData;
15
16 static void
17 request_failed (SoupMessage *msg, gpointer data)
18 {
19         SlowData *sd = data;
20
21         if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code))
22                 g_source_destroy (sd->timeout);
23         g_free (sd);
24 }
25
26 static gboolean
27 add_body_chunk (gpointer data)
28 {
29         SlowData *sd = data;
30
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);
36
37         return FALSE;
38 }
39
40 static void
41 server_callback (SoupServer *server, SoupMessage *msg,
42                  const char *path, GHashTable *query,
43                  SoupClientContext *context, gpointer data)
44 {
45         SlowData *sd;
46
47         if (msg->method != SOUP_METHOD_GET) {
48                 soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
49                 return;
50         }
51
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);
56                 return;
57         }
58
59         soup_message_headers_set_encoding (msg->response_headers,
60                                            SOUP_ENCODING_CHUNKED);
61         g_object_ref (msg);
62         soup_server_pause_message (server, msg);
63
64         sd = g_new (SlowData, 1);
65         sd->server = server;
66         sd->msg = msg;
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);
72 }
73
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
76  * loop is stopped.
77  */
78
79 static gboolean idle_start_test1_thread (gpointer loop);
80 static gpointer test1_thread (gpointer user_data);
81
82 static GCond test1_cond;
83 static GMutex test1_mutex;
84 static GMainLoop *test1_loop;
85
86 static void
87 do_test1 (gconstpointer data)
88 {
89         gboolean use_thread_context = GPOINTER_TO_INT (data);
90
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);
95 }
96
97 static gboolean
98 idle_start_test1_thread (gpointer use_thread_context)
99 {
100         guint64 time;
101         GThread *thread;
102
103         g_mutex_lock (&test1_mutex);
104         thread = g_thread_new ("test1_thread", test1_thread, use_thread_context);
105
106         time = g_get_monotonic_time () + 5000000;
107         if (g_cond_wait_until (&test1_cond, &test1_mutex, time))
108                 g_thread_join (thread);
109         else {
110                 soup_test_assert (FALSE, "timeout");
111                 g_thread_unref (thread);
112         }
113
114         g_mutex_unlock (&test1_mutex);
115         g_main_loop_quit (test1_loop);
116         return FALSE;
117 }
118
119 static void
120 test1_finished (SoupSession *session, SoupMessage *msg, gpointer loop)
121 {
122         g_main_loop_quit (loop);
123 }
124
125 static gpointer
126 test1_thread (gpointer use_thread_context)
127 {
128         SoupSession *session;
129         GMainContext *async_context;
130         char *uri;
131         SoupMessage *msg;
132         GMainLoop *loop;
133
134         /* Wait for main thread to be waiting on test1_cond */
135         g_mutex_lock (&test1_mutex);
136         g_mutex_unlock (&test1_mutex);
137
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,
143                                                  NULL);
144         } else {
145                 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
146                                                  SOUP_SESSION_ASYNC_CONTEXT, async_context,
147                                                  NULL);
148         }
149         g_main_context_unref (async_context);
150
151         uri = g_build_filename (base_uri, "slow", NULL);
152
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);
158
159         debug_printf (1, "  queue_message\n");
160         msg = soup_message_new ("GET", uri);
161         loop = g_main_loop_new (async_context, FALSE);
162         g_object_ref (msg);
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);
168
169         soup_test_session_abort_unref (session);
170         g_free (uri);
171
172         g_cond_signal (&test1_cond);
173
174         if (use_thread_context)
175                 g_main_context_pop_thread_default (async_context);
176         return NULL;
177 }
178
179 /* Test 2: An async session in the main thread with its own
180  * async_context runs independently of the default main loop.
181  */
182
183 static gboolean idle_test2_fail (gpointer user_data);
184
185 static void
186 do_test2 (gconstpointer data)
187 {
188         gboolean use_thread_context = GPOINTER_TO_INT (data);
189         guint idle;
190         GMainContext *async_context;
191         SoupSession *session;
192         char *uri;
193         SoupMessage *msg;
194
195         idle = g_idle_add_full (G_PRIORITY_HIGH, idle_test2_fail, NULL, NULL);
196
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,
202                                                  NULL);
203         } else {
204                 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
205                                                  SOUP_SESSION_ASYNC_CONTEXT, async_context,
206                                                  NULL);
207         }
208         g_main_context_unref (async_context);
209
210         uri = g_build_filename (base_uri, "slow", NULL);
211
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);
217
218         soup_test_session_abort_unref (session);
219         g_free (uri);
220
221         g_source_remove (idle);
222
223         if (use_thread_context)
224                 g_main_context_pop_thread_default (async_context);
225 }
226
227 static gboolean
228 idle_test2_fail (gpointer user_data)
229 {
230         soup_test_assert (FALSE, "idle ran");
231         return FALSE;
232 }
233
234 static void
235 multi_request_started (SoupSession *session, SoupMessage *msg,
236                        SoupSocket *socket, gpointer user_data)
237 {
238         g_object_set_data (G_OBJECT (msg), "started", GUINT_TO_POINTER (TRUE));
239 }
240
241 static void
242 msg1_got_headers (SoupMessage *msg, gpointer user_data)
243 {
244         GMainLoop *loop = user_data;
245
246         g_main_loop_quit (loop);
247 }
248
249 static void
250 multi_msg_finished (SoupSession *session, SoupMessage *msg, gpointer user_data)
251 {
252         GMainLoop *loop = user_data;
253
254         g_object_set_data (G_OBJECT (msg), "finished", GUINT_TO_POINTER (TRUE));
255         g_main_loop_quit (loop);
256 }
257
258 static void
259 do_multicontext_test (void)
260 {
261         SoupSession *session;
262         SoupMessage *msg1, *msg2;
263         GMainContext *context1, *context2;
264         GMainLoop *loop1, *loop2;
265
266         session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
267                                          SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
268                                          NULL);
269         g_signal_connect (session, "request-started",
270                           G_CALLBACK (multi_request_started), NULL);
271
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);
276
277         g_main_context_push_thread_default (context1);
278         msg1 = soup_message_new ("GET", base_uri);
279         g_object_ref (msg1);
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);
285
286         g_main_context_push_thread_default (context2);
287         msg2 = soup_message_new ("GET", base_uri);
288         g_object_ref (msg2);
289         soup_session_queue_message (session, msg2, multi_msg_finished, loop2);
290         g_main_context_pop_thread_default (context2);
291
292         g_main_context_push_thread_default (context1);
293         g_main_loop_run (loop1);
294         g_main_context_pop_thread_default (context1);
295
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");
300
301         g_main_context_push_thread_default (context2);
302         g_main_loop_run (loop2);
303         g_main_context_pop_thread_default (context2);
304
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");
309
310         g_main_context_push_thread_default (context1);
311         g_main_loop_run (loop1);
312         g_main_context_pop_thread_default (context1);
313
314         if (!g_object_get_data (G_OBJECT (msg1), "finished"))
315                 soup_test_assert (FALSE, "msg1 not finished");
316
317         g_object_unref (msg1);
318         g_object_unref (msg2);
319
320         soup_test_session_abort_unref (session);
321
322         g_main_loop_unref (loop1);
323         g_main_loop_unref (loop2);
324         g_main_context_unref (context1);
325         g_main_context_unref (context2);
326 }
327
328 int
329 main (int argc, char **argv)
330 {
331         SoupServer *server;
332         int ret;
333
334         test_init (argc, argv, NULL);
335
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));
340
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);
346
347         ret = g_test_run ();
348
349         g_free (base_uri);
350         soup_test_server_quit_unref (server);
351
352         test_cleanup ();
353         return ret;
354 }