soup-message-io: keep cancellable alive for duration of io_run_until
[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 <ctype.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <signal.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15
16 #include <glib.h>
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>
23
24 #include "test-utils.h"
25
26 static char *base_uri;
27
28 typedef struct {
29         SoupServer *server;
30         SoupMessage *msg;
31         GSource *timeout;
32 } SlowData;
33
34 static void
35 request_failed (SoupMessage *msg, gpointer data)
36 {
37         SlowData *sd = data;
38
39         if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code))
40                 g_source_destroy (sd->timeout);
41         g_free (sd);
42 }
43
44 static gboolean
45 add_body_chunk (gpointer data)
46 {
47         SlowData *sd = data;
48
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);
54
55         return FALSE;
56 }
57
58 static void
59 server_callback (SoupServer *server, SoupMessage *msg,
60                  const char *path, GHashTable *query,
61                  SoupClientContext *context, gpointer data)
62 {
63         SlowData *sd;
64
65         if (msg->method != SOUP_METHOD_GET) {
66                 soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
67                 return;
68         }
69
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);
74                 return;
75         }
76
77         soup_message_headers_set_encoding (msg->response_headers,
78                                            SOUP_ENCODING_CHUNKED);
79         g_object_ref (msg);
80         soup_server_pause_message (server, msg);
81
82         sd = g_new (SlowData, 1);
83         sd->server = server;
84         sd->msg = msg;
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);
90 }
91
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
94  * loop is stopped.
95  */
96
97 static gboolean idle_start_test1_thread (gpointer loop);
98 static gpointer test1_thread (gpointer user_data);
99
100 static GCond test1_cond;
101 static GMutex test1_mutex;
102 static GMainLoop *test1_loop;
103
104 static void
105 do_test1 (int n, gboolean use_thread_context)
106 {
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");
110         else
111                 debug_printf (1, "(Using SOUP_SESSION_ASYNC_CONTEXT)\n");
112
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);
117 }
118
119 static gboolean
120 idle_start_test1_thread (gpointer use_thread_context)
121 {
122         guint64 time;
123         GThread *thread;
124
125         g_mutex_lock (&test1_mutex);
126         thread = g_thread_new ("test1_thread", test1_thread, use_thread_context);
127
128         time = g_get_monotonic_time () + 5000000;
129         if (g_cond_wait_until (&test1_cond, &test1_mutex, time))
130                 g_thread_join (thread);
131         else {
132                 debug_printf (1, "  timeout!\n");
133                 g_thread_unref (thread);
134                 errors++;
135         }
136
137         g_mutex_unlock (&test1_mutex);
138         g_main_loop_quit (test1_loop);
139         return FALSE;
140 }
141
142 static void
143 test1_finished (SoupSession *session, SoupMessage *msg, gpointer loop)
144 {
145         g_main_loop_quit (loop);
146 }
147
148 static gpointer
149 test1_thread (gpointer use_thread_context)
150 {
151         SoupSession *session;
152         GMainContext *async_context;
153         char *uri;
154         SoupMessage *msg;
155         GMainLoop *loop;
156
157         /* Wait for main thread to be waiting on test1_cond */
158         g_mutex_lock (&test1_mutex);
159         g_mutex_unlock (&test1_mutex);
160
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,
166                                                  NULL);
167         } else {
168                 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
169                                                  SOUP_SESSION_ASYNC_CONTEXT, async_context,
170                                                  NULL);
171         }
172         g_main_context_unref (async_context);
173
174         uri = g_build_filename (base_uri, "slow", NULL);
175
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);
182                 errors++;
183         }
184         g_object_unref (msg);
185
186         debug_printf (1, "  queue_message\n");
187         msg = soup_message_new ("GET", uri);
188         loop = g_main_loop_new (async_context, FALSE);
189         g_object_ref (msg);
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);
196                 errors++;
197         }
198         g_object_unref (msg);
199
200         soup_test_session_abort_unref (session);
201         g_free (uri);
202
203         g_cond_signal (&test1_cond);
204
205         if (use_thread_context)
206                 g_main_context_pop_thread_default (async_context);
207         return NULL;
208 }
209
210 /* Test 2: An async session in the main thread with its own
211  * async_context runs independently of the default main loop.
212  */
213
214 static gboolean idle_test2_fail (gpointer user_data);
215
216 static void
217 do_test2 (int n, gboolean use_thread_context)
218 {
219         guint idle;
220         GMainContext *async_context;
221         SoupSession *session;
222         char *uri;
223         SoupMessage *msg;
224
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");
228         else
229                 debug_printf (1, "(Using SOUP_SESSION_ASYNC_CONTEXT)\n");
230
231         idle = g_idle_add_full (G_PRIORITY_HIGH, idle_test2_fail, NULL, NULL);
232
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,
238                                                  NULL);
239         } else {
240                 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
241                                                  SOUP_SESSION_ASYNC_CONTEXT, async_context,
242                                                  NULL);
243         }
244         g_main_context_unref (async_context);
245
246         uri = g_build_filename (base_uri, "slow", NULL);
247
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);
254                 errors++;
255         }
256         g_object_unref (msg);
257
258         soup_test_session_abort_unref (session);
259         g_free (uri);
260
261         g_source_remove (idle);
262
263         if (use_thread_context)
264                 g_main_context_pop_thread_default (async_context);
265 }
266
267 static gboolean
268 idle_test2_fail (gpointer user_data)
269 {
270         debug_printf (1, "  idle ran!\n");
271         errors++;
272         return FALSE;
273 }
274
275 static void
276 multi_request_started (SoupSession *session, SoupMessage *msg,
277                        SoupSocket *socket, gpointer user_data)
278 {
279         g_object_set_data (G_OBJECT (msg), "started", GUINT_TO_POINTER (TRUE));
280 }
281
282 static void
283 msg1_got_headers (SoupMessage *msg, gpointer user_data)
284 {
285         GMainLoop *loop = user_data;
286
287         g_main_loop_quit (loop);
288 }
289
290 static void
291 multi_msg_finished (SoupSession *session, SoupMessage *msg, gpointer user_data)
292 {
293         GMainLoop *loop = user_data;
294
295         g_object_set_data (G_OBJECT (msg), "finished", GUINT_TO_POINTER (TRUE));
296         g_main_loop_quit (loop);
297 }
298
299 static void
300 do_multicontext_test (int n)
301 {
302         SoupSession *session;
303         SoupMessage *msg1, *msg2;
304         GMainContext *context1, *context2;
305         GMainLoop *loop1, *loop2;
306
307         debug_printf (1, "\nTest %d: Using multiple async contexts\n", n);
308
309         session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
310                                          SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
311                                          NULL);
312         g_signal_connect (session, "request-started",
313                           G_CALLBACK (multi_request_started), NULL);
314
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);
319
320         g_main_context_push_thread_default (context1);
321         msg1 = soup_message_new ("GET", base_uri);
322         g_object_ref (msg1);
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);
328
329         g_main_context_push_thread_default (context2);
330         msg2 = soup_message_new ("GET", base_uri);
331         g_object_ref (msg2);
332         soup_session_queue_message (session, msg2, multi_msg_finished, loop2);
333         g_main_context_pop_thread_default (context2);
334
335         g_main_context_push_thread_default (context1);
336         g_main_loop_run (loop1);
337         g_main_context_pop_thread_default (context1);
338
339         if (!g_object_get_data (G_OBJECT (msg1), "started")) {
340                 debug_printf (1, "  msg1 not started??\n");
341                 errors++;
342         }
343         if (g_object_get_data (G_OBJECT (msg2), "started")) {
344                 debug_printf (1, "  msg2 started while loop1 was running!\n");
345                 errors++;
346         }
347
348         g_main_context_push_thread_default (context2);
349         g_main_loop_run (loop2);
350         g_main_context_pop_thread_default (context2);
351
352         if (g_object_get_data (G_OBJECT (msg1), "finished")) {
353                 debug_printf (1, "  msg1 finished while loop2 was running!\n");
354                 errors++;
355         }
356         if (!g_object_get_data (G_OBJECT (msg2), "finished")) {
357                 debug_printf (1, "  msg2 not finished??\n");
358                 errors++;
359         }
360
361         g_main_context_push_thread_default (context1);
362         g_main_loop_run (loop1);
363         g_main_context_pop_thread_default (context1);
364
365         if (!g_object_get_data (G_OBJECT (msg1), "finished")) {
366                 debug_printf (1, "  msg1 not finished??\n");
367                 errors++;
368         }
369
370         g_object_unref (msg1);
371         g_object_unref (msg2);
372
373         soup_test_session_abort_unref (session);
374
375         g_main_loop_unref (loop1);
376         g_main_loop_unref (loop2);
377         g_main_context_unref (context1);
378         g_main_context_unref (context2);
379 }
380
381 int
382 main (int argc, char **argv)
383 {
384         SoupServer *server;
385
386         test_init (argc, argv, NULL);
387
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));
392
393         do_test1 (1, FALSE);
394         do_test1 (2, TRUE);
395         do_test2 (3, FALSE);
396         do_test2 (4, TRUE);
397         do_multicontext_test (5);
398
399         g_free (base_uri);
400         soup_test_server_quit_unref (server);
401
402         test_cleanup ();
403         return errors != 0;
404 }