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