• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
request_finished(SoupMessage * msg,gpointer data)17 request_finished (SoupMessage *msg, gpointer data)
18 {
19 	SlowData *sd = data;
20 
21 	g_source_destroy (sd->timeout);
22 	g_source_unref (sd->timeout);
23 	g_free (sd);
24 }
25 
26 static gboolean
add_body_chunk(gpointer data)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
server_callback(SoupServer * server,SoupMessage * msg,const char * path,GHashTable * query,SoupClientContext * context,gpointer data)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 		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);
73 }
74 
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
77  * loop is stopped.
78  */
79 
80 static gboolean idle_start_test1_thread (gpointer loop);
81 static gpointer test1_thread (gpointer user_data);
82 
83 static GCond test1_cond;
84 static GMutex test1_mutex;
85 static GMainLoop *test1_loop;
86 
87 static void
do_test1(gconstpointer data)88 do_test1 (gconstpointer data)
89 {
90 	gboolean use_thread_context = GPOINTER_TO_INT (data);
91 
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);
96 }
97 
98 static gboolean
idle_start_test1_thread(gpointer use_thread_context)99 idle_start_test1_thread (gpointer use_thread_context)
100 {
101 	guint64 time;
102 	GThread *thread;
103 
104 	g_mutex_lock (&test1_mutex);
105 	thread = g_thread_new ("test1_thread", test1_thread, use_thread_context);
106 
107 	time = g_get_monotonic_time () + 5000000;
108 	if (g_cond_wait_until (&test1_cond, &test1_mutex, time))
109 		g_thread_join (thread);
110 	else {
111 		soup_test_assert (FALSE, "timeout");
112 		g_thread_unref (thread);
113 	}
114 
115 	g_mutex_unlock (&test1_mutex);
116 	g_main_loop_quit (test1_loop);
117 	return FALSE;
118 }
119 
120 static void
test1_finished(SoupSession * session,SoupMessage * msg,gpointer loop)121 test1_finished (SoupSession *session, SoupMessage *msg, gpointer loop)
122 {
123 	g_main_loop_quit (loop);
124 }
125 
126 static gpointer
test1_thread(gpointer use_thread_context)127 test1_thread (gpointer use_thread_context)
128 {
129 	SoupSession *session;
130 	GMainContext *async_context;
131 	char *uri;
132 	SoupMessage *msg;
133 	GMainLoop *loop;
134 
135 	/* Wait for main thread to be waiting on test1_cond */
136 	g_mutex_lock (&test1_mutex);
137 	g_mutex_unlock (&test1_mutex);
138 
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,
144 						 NULL);
145 	} else {
146 		session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
147 						 SOUP_SESSION_ASYNC_CONTEXT, async_context,
148 						 NULL);
149 	}
150 	g_main_context_unref (async_context);
151 
152 	uri = g_build_filename (base_uri, "slow", NULL);
153 
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);
159 
160 	debug_printf (1, "  queue_message\n");
161 	msg = soup_message_new ("GET", uri);
162 	loop = g_main_loop_new (async_context, FALSE);
163 	g_object_ref (msg);
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);
169 
170 	soup_test_session_abort_unref (session);
171 	g_free (uri);
172 
173 	g_cond_signal (&test1_cond);
174 
175 	if (use_thread_context)
176 		g_main_context_pop_thread_default (async_context);
177 	return NULL;
178 }
179 
180 /* Test 2: An async session in the main thread with its own
181  * async_context runs independently of the default main loop.
182  */
183 
184 static gboolean idle_test2_fail (gpointer user_data);
185 
186 static void
do_test2(gconstpointer data)187 do_test2 (gconstpointer data)
188 {
189 	gboolean use_thread_context = GPOINTER_TO_INT (data);
190 	guint idle;
191 	GMainContext *async_context;
192 	SoupSession *session;
193 	char *uri;
194 	SoupMessage *msg;
195 
196 	idle = g_idle_add_full (G_PRIORITY_HIGH, idle_test2_fail, NULL, NULL);
197 
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,
203 						 NULL);
204 	} else {
205 		session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
206 						 SOUP_SESSION_ASYNC_CONTEXT, async_context,
207 						 NULL);
208 	}
209 	g_main_context_unref (async_context);
210 
211 	uri = g_build_filename (base_uri, "slow", NULL);
212 
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);
218 
219 	soup_test_session_abort_unref (session);
220 	g_free (uri);
221 
222 	g_source_remove (idle);
223 
224 	if (use_thread_context)
225 		g_main_context_pop_thread_default (async_context);
226 }
227 
228 static gboolean
idle_test2_fail(gpointer user_data)229 idle_test2_fail (gpointer user_data)
230 {
231 	soup_test_assert (FALSE, "idle ran");
232 	return FALSE;
233 }
234 
235 static void
multi_request_started(SoupSession * session,SoupMessage * msg,SoupSocket * socket,gpointer user_data)236 multi_request_started (SoupSession *session, SoupMessage *msg,
237 		       SoupSocket *socket, gpointer user_data)
238 {
239 	g_object_set_data (G_OBJECT (msg), "started", GUINT_TO_POINTER (TRUE));
240 }
241 
242 static void
msg1_got_headers(SoupMessage * msg,gpointer user_data)243 msg1_got_headers (SoupMessage *msg, gpointer user_data)
244 {
245 	GMainLoop *loop = user_data;
246 
247 	g_main_loop_quit (loop);
248 }
249 
250 static void
multi_msg_finished(SoupSession * session,SoupMessage * msg,gpointer user_data)251 multi_msg_finished (SoupSession *session, SoupMessage *msg, gpointer user_data)
252 {
253 	GMainLoop *loop = user_data;
254 
255 	g_object_set_data (G_OBJECT (msg), "finished", GUINT_TO_POINTER (TRUE));
256 	g_main_loop_quit (loop);
257 }
258 
259 static void
do_multicontext_test(void)260 do_multicontext_test (void)
261 {
262 	SoupSession *session;
263 	SoupMessage *msg1, *msg2;
264 	GMainContext *context1, *context2;
265 	GMainLoop *loop1, *loop2;
266 
267 	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
268 					 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
269 					 NULL);
270 	g_signal_connect (session, "request-started",
271 			  G_CALLBACK (multi_request_started), NULL);
272 
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);
277 
278 	g_main_context_push_thread_default (context1);
279 	msg1 = soup_message_new ("GET", base_uri);
280 	g_object_ref (msg1);
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);
286 
287 	g_main_context_push_thread_default (context2);
288 	msg2 = soup_message_new ("GET", base_uri);
289 	g_object_ref (msg2);
290 	soup_session_queue_message (session, msg2, multi_msg_finished, loop2);
291 	g_main_context_pop_thread_default (context2);
292 
293 	g_main_context_push_thread_default (context1);
294 	g_main_loop_run (loop1);
295 	g_main_context_pop_thread_default (context1);
296 
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");
301 
302 	g_main_context_push_thread_default (context2);
303 	g_main_loop_run (loop2);
304 	g_main_context_pop_thread_default (context2);
305 
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");
310 
311 	g_main_context_push_thread_default (context1);
312 	g_main_loop_run (loop1);
313 	g_main_context_pop_thread_default (context1);
314 
315 	if (!g_object_get_data (G_OBJECT (msg1), "finished"))
316 		soup_test_assert (FALSE, "msg1 not finished");
317 
318 	g_object_unref (msg1);
319 	g_object_unref (msg2);
320 
321 	soup_test_session_abort_unref (session);
322 
323 	g_main_loop_unref (loop1);
324 	g_main_loop_unref (loop2);
325 	g_main_context_unref (context1);
326 	g_main_context_unref (context2);
327 }
328 
329 int
main(int argc,char ** argv)330 main (int argc, char **argv)
331 {
332 	SoupServer *server;
333 	SoupURI *uri;
334 	int ret;
335 
336 	test_init (argc, argv, NULL);
337 
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);
342 	soup_uri_free (uri);
343 
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);
349 
350 	ret = g_test_run ();
351 
352 	g_free (base_uri);
353 	soup_test_server_quit_unref (server);
354 
355 	test_cleanup ();
356 	return ret;
357 }
358