gio/tests: fix leaks
[platform/upstream/glib.git] / gio / tests / contexts.c
1 #include <gio/gio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #define TEST_FILE (SRCDIR "/Makefile.am")
6 char *test_file_buffer;
7 gsize test_file_size;
8 static char async_read_buffer[8192];
9
10 static void
11 read_data (GObject *source, GAsyncResult *result, gpointer loop)
12 {
13   GInputStream *in = G_INPUT_STREAM (source);
14   GError *error = NULL;
15   gssize nread;
16
17   nread = g_input_stream_read_finish (in, result, &error);
18   g_assert_no_error (error);
19
20   g_assert_cmpint (nread, >, 0);
21   g_assert_cmpint (nread, <=, MIN(sizeof (async_read_buffer), test_file_size));
22   g_assert (memcmp (async_read_buffer, test_file_buffer, nread) == 0);
23
24   g_main_loop_quit (loop);
25 }
26
27 static void
28 opened_for_read (GObject *source, GAsyncResult *result, gpointer loop)
29 {
30   GFile *file = G_FILE (source);
31   GFileInputStream *in;
32   GError *error = NULL;
33
34   in = g_file_read_finish (file, result, &error);
35   g_assert_no_error (error);
36
37   memset (async_read_buffer, 0, sizeof (async_read_buffer));
38   g_input_stream_read_async (G_INPUT_STREAM (in),
39                              async_read_buffer, sizeof (async_read_buffer),
40                              G_PRIORITY_DEFAULT, NULL,
41                              read_data, loop);
42
43   g_object_unref (in);
44 }
45
46 /* Test 1: Async I/O started in a thread with a thread-default context
47  * will stick to that thread, and will complete even if the default
48  * main loop is blocked. (NB: the last part would not be true if we
49  * were testing GFileMonitor!)
50  */
51
52 static gboolean idle_start_test1_thread (gpointer loop);
53 static gpointer test1_thread (gpointer user_data);
54
55 static gboolean test1_done;
56 static GCond test1_cond;
57 static GMutex test1_mutex;
58
59 static void
60 test_thread_independence (void)
61 {
62   GMainLoop *loop;
63
64   loop = g_main_loop_new (NULL, FALSE);
65   g_idle_add (idle_start_test1_thread, loop);
66   g_main_loop_run (loop);
67   g_main_loop_unref (loop);
68 }
69
70 static gboolean
71 idle_start_test1_thread (gpointer loop)
72 {
73   gint64 time;
74   GThread *thread;
75   gboolean io_completed;
76
77   g_mutex_lock (&test1_mutex);
78   thread = g_thread_new ("test1", test1_thread, NULL);
79
80   time = g_get_monotonic_time () + 2 * G_TIME_SPAN_SECOND;
81   while (!test1_done)
82     {
83       io_completed = g_cond_wait_until (&test1_cond, &test1_mutex, time);
84       g_assert (io_completed);
85     }
86   g_thread_join (thread);
87
88   g_mutex_unlock (&test1_mutex);
89   g_main_loop_quit (loop);
90   return G_SOURCE_REMOVE;
91 }
92
93 static gpointer
94 test1_thread (gpointer user_data)
95 {
96   GMainContext *context;
97   GMainLoop *loop;
98   GFile *file;
99
100   /* Wait for main thread to be waiting on test1_cond */
101   g_mutex_lock (&test1_mutex);
102
103   context = g_main_context_new ();
104   g_assert (g_main_context_get_thread_default () == NULL);
105   g_main_context_push_thread_default (context);
106   g_assert (g_main_context_get_thread_default () == context);
107
108   file = g_file_new_for_path (TEST_FILE);
109   g_assert (g_file_supports_thread_contexts (file));
110
111   loop = g_main_loop_new (context, FALSE);
112   g_file_read_async (file, G_PRIORITY_DEFAULT, NULL,
113                      opened_for_read, loop);
114   g_object_unref (file);
115   g_main_loop_run (loop);
116   g_main_loop_unref (loop);
117
118   test1_done = TRUE;
119   g_cond_signal (&test1_cond);
120   g_mutex_unlock (&test1_mutex);
121
122   g_main_context_pop_thread_default (context);
123   g_main_context_unref (context);
124
125   return NULL;
126 }
127
128 /* Test 2: If we push a thread-default context in the main thread, we
129  * can run async ops in that context without running the default
130  * context.
131  */
132
133 static gboolean test2_fail (gpointer user_data);
134
135 static void
136 test_context_independence (void)
137 {
138   GMainContext *context;
139   GMainLoop *loop;
140   GFile *file;
141   guint default_timeout;
142   GSource *thread_default_timeout;
143
144   context = g_main_context_new ();
145   g_assert (g_main_context_get_thread_default () == NULL);
146   g_main_context_push_thread_default (context);
147   g_assert (g_main_context_get_thread_default () == context);
148
149   file = g_file_new_for_path (TEST_FILE);
150   g_assert (g_file_supports_thread_contexts (file));
151
152   /* Add a timeout to the main loop, to fail immediately if it gets run */
153   default_timeout = g_timeout_add_full (G_PRIORITY_HIGH, 0,
154                                         test2_fail, NULL, NULL);
155   /* Add a timeout to the alternate loop, to fail if the I/O *doesn't* run */
156   thread_default_timeout = g_timeout_source_new_seconds (2);
157   g_source_set_callback (thread_default_timeout, test2_fail, NULL, NULL);
158   g_source_attach (thread_default_timeout, context);
159
160   loop = g_main_loop_new (context, FALSE);
161   g_file_read_async (file, G_PRIORITY_DEFAULT, NULL,
162                      opened_for_read, loop);
163   g_object_unref (file);
164   g_main_loop_run (loop);
165   g_main_loop_unref (loop);
166
167   g_source_remove (default_timeout);
168   g_source_destroy (thread_default_timeout);
169   g_source_unref (thread_default_timeout);
170
171   g_main_context_pop_thread_default (context);
172   g_main_context_unref (context);
173 }
174
175 static gboolean
176 test2_fail (gpointer user_data)
177 {
178   g_assert_not_reached ();
179   return FALSE;
180 }
181
182 int
183 main (int argc, char **argv)
184 {
185   GError *error = NULL;
186   int ret;
187
188   g_type_init ();
189   g_test_init (&argc, &argv, NULL);
190
191   g_file_get_contents (TEST_FILE, &test_file_buffer,
192                        &test_file_size, &error);
193   g_assert_no_error (error);
194
195   g_test_add_func ("/gio/contexts/thread-independence", test_thread_independence);
196   g_test_add_func ("/gio/contexts/context-independence", test_context_independence);
197
198   ret = g_test_run();
199
200   g_free (test_file_buffer);
201
202   return ret;
203 }