Fix the retry-on-broken-connection codepath for SoupRequest
[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 (int n, gboolean use_thread_context)
88 {
89         debug_printf (1, "\nTest %d: blocking the main thread does not block other thread\n", n);
90         if (use_thread_context)
91                 debug_printf (1, "(Using g_main_context_push_thread_default())\n");
92         else
93                 debug_printf (1, "(Using SOUP_SESSION_ASYNC_CONTEXT)\n");
94
95         test1_loop = g_main_loop_new (NULL, FALSE);
96         g_idle_add (idle_start_test1_thread, GINT_TO_POINTER (use_thread_context));
97         g_main_loop_run (test1_loop);
98         g_main_loop_unref (test1_loop);
99 }
100
101 static gboolean
102 idle_start_test1_thread (gpointer use_thread_context)
103 {
104         guint64 time;
105         GThread *thread;
106
107         g_mutex_lock (&test1_mutex);
108         thread = g_thread_new ("test1_thread", test1_thread, use_thread_context);
109
110         time = g_get_monotonic_time () + 5000000;
111         if (g_cond_wait_until (&test1_cond, &test1_mutex, time))
112                 g_thread_join (thread);
113         else {
114                 debug_printf (1, "  timeout!\n");
115                 g_thread_unref (thread);
116                 errors++;
117         }
118
119         g_mutex_unlock (&test1_mutex);
120         g_main_loop_quit (test1_loop);
121         return FALSE;
122 }
123
124 static void
125 test1_finished (SoupSession *session, SoupMessage *msg, gpointer loop)
126 {
127         g_main_loop_quit (loop);
128 }
129
130 static gpointer
131 test1_thread (gpointer use_thread_context)
132 {
133         SoupSession *session;
134         GMainContext *async_context;
135         char *uri;
136         SoupMessage *msg;
137         GMainLoop *loop;
138
139         /* Wait for main thread to be waiting on test1_cond */
140         g_mutex_lock (&test1_mutex);
141         g_mutex_unlock (&test1_mutex);
142
143         async_context = g_main_context_new ();
144         if (use_thread_context) {
145                 g_main_context_push_thread_default (async_context);
146                 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
147                                                  SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
148                                                  NULL);
149         } else {
150                 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
151                                                  SOUP_SESSION_ASYNC_CONTEXT, async_context,
152                                                  NULL);
153         }
154         g_main_context_unref (async_context);
155
156         uri = g_build_filename (base_uri, "slow", NULL);
157
158         debug_printf (1, "  send_message\n");
159         msg = soup_message_new ("GET", uri);
160         soup_session_send_message (session, msg);
161         if (msg->status_code != SOUP_STATUS_OK) {
162                 debug_printf (1, "    unexpected status: %d %s\n",
163                               msg->status_code, msg->reason_phrase);
164                 errors++;
165         }
166         g_object_unref (msg);
167
168         debug_printf (1, "  queue_message\n");
169         msg = soup_message_new ("GET", uri);
170         loop = g_main_loop_new (async_context, FALSE);
171         g_object_ref (msg);
172         soup_session_queue_message (session, msg, test1_finished, loop);
173         g_main_loop_run (loop);
174         g_main_loop_unref (loop);
175         if (msg->status_code != SOUP_STATUS_OK) {
176                 debug_printf (1, "    unexpected status: %d %s\n",
177                               msg->status_code, msg->reason_phrase);
178                 errors++;
179         }
180         g_object_unref (msg);
181
182         soup_test_session_abort_unref (session);
183         g_free (uri);
184
185         g_cond_signal (&test1_cond);
186
187         if (use_thread_context)
188                 g_main_context_pop_thread_default (async_context);
189         return NULL;
190 }
191
192 /* Test 2: An async session in the main thread with its own
193  * async_context runs independently of the default main loop.
194  */
195
196 static gboolean idle_test2_fail (gpointer user_data);
197
198 static void
199 do_test2 (int n, gboolean use_thread_context)
200 {
201         guint idle;
202         GMainContext *async_context;
203         SoupSession *session;
204         char *uri;
205         SoupMessage *msg;
206
207         debug_printf (1, "\nTest %d: a session with its own context is independent of the main loop.\n", n);
208         if (use_thread_context)
209                 debug_printf (1, "(Using g_main_context_push_thread_default())\n");
210         else
211                 debug_printf (1, "(Using SOUP_SESSION_ASYNC_CONTEXT)\n");
212
213         idle = g_idle_add_full (G_PRIORITY_HIGH, idle_test2_fail, NULL, NULL);
214
215         async_context = g_main_context_new ();
216         if (use_thread_context) {
217                 g_main_context_push_thread_default (async_context);
218                 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
219                                                  SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
220                                                  NULL);
221         } else {
222                 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
223                                                  SOUP_SESSION_ASYNC_CONTEXT, async_context,
224                                                  NULL);
225         }
226         g_main_context_unref (async_context);
227
228         uri = g_build_filename (base_uri, "slow", NULL);
229
230         debug_printf (1, "  send_message\n");
231         msg = soup_message_new ("GET", uri);
232         soup_session_send_message (session, msg);
233         if (msg->status_code != SOUP_STATUS_OK) {
234                 debug_printf (1, "    unexpected status: %d %s\n",
235                               msg->status_code, msg->reason_phrase);
236                 errors++;
237         }
238         g_object_unref (msg);
239
240         soup_test_session_abort_unref (session);
241         g_free (uri);
242
243         g_source_remove (idle);
244
245         if (use_thread_context)
246                 g_main_context_pop_thread_default (async_context);
247 }
248
249 static gboolean
250 idle_test2_fail (gpointer user_data)
251 {
252         debug_printf (1, "  idle ran!\n");
253         errors++;
254         return FALSE;
255 }
256
257 static void
258 multi_request_started (SoupSession *session, SoupMessage *msg,
259                        SoupSocket *socket, gpointer user_data)
260 {
261         g_object_set_data (G_OBJECT (msg), "started", GUINT_TO_POINTER (TRUE));
262 }
263
264 static void
265 msg1_got_headers (SoupMessage *msg, gpointer user_data)
266 {
267         GMainLoop *loop = user_data;
268
269         g_main_loop_quit (loop);
270 }
271
272 static void
273 multi_msg_finished (SoupSession *session, SoupMessage *msg, gpointer user_data)
274 {
275         GMainLoop *loop = user_data;
276
277         g_object_set_data (G_OBJECT (msg), "finished", GUINT_TO_POINTER (TRUE));
278         g_main_loop_quit (loop);
279 }
280
281 static void
282 do_multicontext_test (int n)
283 {
284         SoupSession *session;
285         SoupMessage *msg1, *msg2;
286         GMainContext *context1, *context2;
287         GMainLoop *loop1, *loop2;
288
289         debug_printf (1, "\nTest %d: Using multiple async contexts\n", n);
290
291         session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
292                                          SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
293                                          NULL);
294         g_signal_connect (session, "request-started",
295                           G_CALLBACK (multi_request_started), NULL);
296
297         context1 = g_main_context_new ();
298         loop1 = g_main_loop_new (context1, FALSE);
299         context2 = g_main_context_new ();
300         loop2 = g_main_loop_new (context2, FALSE);
301
302         g_main_context_push_thread_default (context1);
303         msg1 = soup_message_new ("GET", base_uri);
304         g_object_ref (msg1);
305         soup_session_queue_message (session, msg1, multi_msg_finished, loop1);
306         g_signal_connect (msg1, "got-headers",
307                           G_CALLBACK (msg1_got_headers), loop1);
308         g_object_set_data (G_OBJECT (msg1), "session", session);
309         g_main_context_pop_thread_default (context1);
310
311         g_main_context_push_thread_default (context2);
312         msg2 = soup_message_new ("GET", base_uri);
313         g_object_ref (msg2);
314         soup_session_queue_message (session, msg2, multi_msg_finished, loop2);
315         g_main_context_pop_thread_default (context2);
316
317         g_main_context_push_thread_default (context1);
318         g_main_loop_run (loop1);
319         g_main_context_pop_thread_default (context1);
320
321         if (!g_object_get_data (G_OBJECT (msg1), "started")) {
322                 debug_printf (1, "  msg1 not started??\n");
323                 errors++;
324         }
325         if (g_object_get_data (G_OBJECT (msg2), "started")) {
326                 debug_printf (1, "  msg2 started while loop1 was running!\n");
327                 errors++;
328         }
329
330         g_main_context_push_thread_default (context2);
331         g_main_loop_run (loop2);
332         g_main_context_pop_thread_default (context2);
333
334         if (g_object_get_data (G_OBJECT (msg1), "finished")) {
335                 debug_printf (1, "  msg1 finished while loop2 was running!\n");
336                 errors++;
337         }
338         if (!g_object_get_data (G_OBJECT (msg2), "finished")) {
339                 debug_printf (1, "  msg2 not finished??\n");
340                 errors++;
341         }
342
343         g_main_context_push_thread_default (context1);
344         g_main_loop_run (loop1);
345         g_main_context_pop_thread_default (context1);
346
347         if (!g_object_get_data (G_OBJECT (msg1), "finished")) {
348                 debug_printf (1, "  msg1 not finished??\n");
349                 errors++;
350         }
351
352         g_object_unref (msg1);
353         g_object_unref (msg2);
354
355         soup_test_session_abort_unref (session);
356
357         g_main_loop_unref (loop1);
358         g_main_loop_unref (loop2);
359         g_main_context_unref (context1);
360         g_main_context_unref (context2);
361 }
362
363 int
364 main (int argc, char **argv)
365 {
366         SoupServer *server;
367
368         test_init (argc, argv, NULL);
369
370         server = soup_test_server_new (TRUE);
371         soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
372         base_uri = g_strdup_printf ("http://127.0.0.1:%u/",
373                                     soup_server_get_port (server));
374
375         do_test1 (1, FALSE);
376         do_test1 (2, TRUE);
377         do_test2 (3, FALSE);
378         do_test2 (4, TRUE);
379         do_multicontext_test (5);
380
381         g_free (base_uri);
382         soup_test_server_quit_unref (server);
383
384         test_cleanup ();
385         return errors != 0;
386 }