summaryrefslogtreecommitdiff
path: root/tests/context-test.c
blob: 4bb3e60f3b52a5c6d72c08d911aa1fdf4e5735dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2007 Red Hat, Inc.
 */

#include "test-utils.h"
#include "soup-misc.h"

static char *base_uri;

typedef struct {
	SoupServerMessage *msg;
	GSource *timeout;
} SlowData;

static void
request_finished (SoupServerMessage *msg,
		  gpointer           data)
{
	SlowData *sd = data;

	g_source_destroy (sd->timeout);
	g_source_unref (sd->timeout);
	g_free (sd);
}

static gboolean
add_body_chunk (gpointer data)
{
	SlowData *sd = data;
	SoupMessageBody *response_body;

	response_body = soup_server_message_get_response_body (sd->msg);
	soup_message_body_append (response_body,
				  SOUP_MEMORY_STATIC, "OK\r\n", 4);
	soup_message_body_complete (response_body);
	soup_server_message_unpause (sd->msg);
	g_object_unref (sd->msg);

	return FALSE;
}

static void
server_callback (SoupServer        *server,
		 SoupServerMessage *msg,
		 const char        *path,
		 GHashTable        *query,
		 gpointer           data)
{
	SlowData *sd;
	SoupMessageHeaders *response_headers;

	if (soup_server_message_get_method (msg) != SOUP_METHOD_GET) {
		soup_server_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED, NULL);
		return;
	}

	soup_server_message_set_status (msg, SOUP_STATUS_OK, NULL);
	if (!strcmp (path, "/fast")) {
		soup_server_message_set_response (msg, "text/plain",
						  SOUP_MEMORY_STATIC, "OK\r\n", 4);
		return;
	}

	response_headers = soup_server_message_get_response_headers (msg);
	soup_message_headers_set_encoding (response_headers,
					   SOUP_ENCODING_CHUNKED);
	g_object_ref (msg);
	soup_server_message_pause (msg);

	sd = g_new (SlowData, 1);
	sd->msg = msg;
	sd->timeout = soup_add_timeout (
		g_main_context_get_thread_default (),
		200, add_body_chunk, sd);
	g_signal_connect (msg, "finished",
			  G_CALLBACK (request_finished), sd);
}

/* Test 1: An async session in another thread with its own
 * async_context can complete a request while the main thread's main
 * loop is stopped.
 */

static gboolean idle_start_test1_thread (gpointer loop);
static gpointer test1_thread (gpointer user_data);

static GCond test1_cond;
static GMutex test1_mutex;
static GMainLoop *test1_loop;

static void
do_test1 (void)
{
        if (g_getenv ("ASAN_OPTIONS")) {
                g_test_skip ("Flaky timing with ASAN enabled");
                return;
        }

	test1_loop = g_main_loop_new (NULL, FALSE);
	g_idle_add (idle_start_test1_thread, NULL);
	g_main_loop_run (test1_loop);
	g_main_loop_unref (test1_loop);
}

static gboolean
idle_start_test1_thread (gpointer user_data)
{
	guint64 time;
	GThread *thread;

	g_mutex_lock (&test1_mutex);
	thread = g_thread_new ("test1_thread", test1_thread, NULL);

	time = g_get_monotonic_time () + 5000000;
	if (g_cond_wait_until (&test1_cond, &test1_mutex, time))
		g_thread_join (thread);
	else {
		soup_test_assert (FALSE, "timeout");
		g_thread_unref (thread);
	}

	g_mutex_unlock (&test1_mutex);
	g_main_loop_quit (test1_loop);
	return FALSE;
}

static void
message_send_cb (SoupSession  *session,
		 GAsyncResult *result,
		 GMainContext *async_context)
{
        GBytes *body;

	g_assert_true (async_context == g_main_context_get_thread_default ());
	body = soup_session_send_and_read_finish (session, result, NULL);
	g_clear_pointer (&body, g_bytes_unref);
}

static void
message_finished (SoupMessage *msg,
		GMainLoop   *loop)
{
	g_main_loop_quit (loop);
}

static gpointer
test1_thread (gpointer user_data)
{
	SoupSession *session;
	GMainContext *async_context;
	char *uri;
	SoupMessage *msg;
	GInputStream *stream;
	GMainLoop *loop;

	/* Wait for main thread to be waiting on test1_cond */
	g_mutex_lock (&test1_mutex);
	g_mutex_unlock (&test1_mutex);

        async_context = g_main_context_new ();
        g_main_context_push_thread_default (async_context);
        session = soup_test_session_new (NULL);
	g_main_context_unref (async_context);

	uri = g_build_filename (base_uri, "slow", NULL);

	debug_printf (1, "  send_message\n");
	msg = soup_message_new ("GET", uri);
	stream = soup_session_send (session, msg, NULL, NULL);
	soup_test_assert_message_status (msg, SOUP_STATUS_OK);
	g_object_unref (stream);
	g_object_unref (msg);

	debug_printf (1, "  queue_message\n");
	msg = soup_message_new ("GET", uri);
	loop = g_main_loop_new (async_context, FALSE);
	g_signal_connect (msg, "finished", G_CALLBACK (message_finished), loop);
        soup_session_send_and_read_async (session, msg, G_PRIORITY_DEFAULT, NULL,
                                          (GAsyncReadyCallback)message_send_cb,
                                          async_context);
	g_main_loop_run (loop);
	/* We need one more iteration, because SoupMessage::finished is emitted
         * right before the message is unqueued.
         */
        g_main_context_iteration (async_context, TRUE);
	g_main_loop_unref (loop);
	soup_test_assert_message_status (msg, SOUP_STATUS_OK);
	g_object_unref (msg);

	soup_test_session_abort_unref (session);
	g_free (uri);

	g_cond_signal (&test1_cond);

	g_main_context_pop_thread_default (async_context);
	return NULL;
}

/* Test 2: An async session in the main thread with its own
 * async_context runs independently of the default main loop.
 */
static gboolean
idle_test2_fail (gpointer user_data)
{
	soup_test_assert (FALSE, "idle ran");
	return FALSE;
}

static void
do_test2 (void)
{
	guint idle;
	GMainContext *async_context;
	SoupSession *session;
	char *uri;
	SoupMessage *msg;
        GBytes *body;
	GMainLoop *loop;

	idle = g_idle_add_full (G_PRIORITY_HIGH, idle_test2_fail, NULL, NULL);

	async_context = g_main_context_new ();
        g_main_context_push_thread_default (async_context);
        session = soup_test_session_new (NULL);
	g_main_context_unref (async_context);

	uri = g_build_filename (base_uri, "slow", NULL);

	debug_printf (1, "  send_message\n");
	msg = soup_message_new ("GET", uri);
	body = soup_session_send_and_read (session, msg, NULL, NULL);
	soup_test_assert_message_status (msg, SOUP_STATUS_OK);
	g_bytes_unref (body);
	g_object_unref (msg);

	debug_printf (1, "  queue_message\n");
	msg = soup_message_new ("GET", uri);
	loop = g_main_loop_new (async_context, FALSE);
	g_signal_connect (msg, "finished", G_CALLBACK (message_finished), loop);
	soup_session_send_and_read_async (session, msg, G_PRIORITY_DEFAULT, NULL,
                                          (GAsyncReadyCallback)message_send_cb,
                                          async_context);
	g_main_loop_run (loop);
	/* We need one more iteration, because SoupMessage::finished is emitted
         * right before the message is unqueued.
         */
        g_main_context_iteration (async_context, TRUE);
	g_main_loop_unref (loop);
	soup_test_assert_message_status (msg, SOUP_STATUS_OK);
	g_object_unref (msg);

	soup_test_session_abort_unref (session);
	g_free (uri);

	g_source_remove (idle);

	g_main_context_pop_thread_default (async_context);
}

int
main (int argc, char **argv)
{
	SoupServer *server;
	GUri *uri;
	int ret;

	test_init (argc, argv, NULL);

	server = soup_test_server_new (SOUP_TEST_SERVER_IN_THREAD);
	soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
	uri = soup_test_server_get_uri (server, "http", NULL);
	base_uri = g_uri_to_string (uri);
	g_uri_unref (uri);

	g_test_add_func ("/context/blocking/thread-default", do_test1);
	g_test_add_func ("/context/nested/thread-default", do_test2);

	ret = g_test_run ();

	g_free (base_uri);
	soup_test_server_quit_unref (server);

	test_cleanup ();
	return ret;
}