Initial commit
[platform/upstream/glib2.0.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
44 /* Test 1: Async I/O started in a thread with a thread-default context
45  * will stick to that thread, and will complete even if the default
46  * main loop is blocked. (NB: the last part would not be true if we
47  * were testing GFileMonitor!)
48  */
49
50 static gboolean idle_start_test1_thread (gpointer loop);
51 static gpointer test1_thread (gpointer user_data);
52
53 static GCond *test1_cond;
54 static GMutex *test1_mutex;
55
56 static void
57 test_thread_independence (void)
58 {
59   GMainLoop *loop;
60
61   test1_cond = g_cond_new ();
62   test1_mutex = g_mutex_new ();
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   g_mutex_free (test1_mutex);
70   g_cond_free (test1_cond);
71 }
72
73 static gboolean
74 idle_start_test1_thread (gpointer loop)
75 {
76   GTimeVal time;
77   GThread *thread;
78   gboolean io_completed;
79
80   g_mutex_lock (test1_mutex);
81   thread = g_thread_create (test1_thread, NULL, TRUE, NULL);
82
83   g_get_current_time (&time);
84   time.tv_sec += 2;
85   io_completed = g_cond_timed_wait (test1_cond, test1_mutex, &time);
86   g_assert (io_completed);
87   g_thread_join (thread);
88
89   g_mutex_unlock (test1_mutex);
90   g_main_loop_quit (loop);
91   return FALSE;
92 }
93
94 static gpointer
95 test1_thread (gpointer user_data)
96 {
97   GMainContext *context;
98   GMainLoop *loop;
99   GFile *file;
100
101   /* Wait for main thread to be waiting on test1_cond */
102   g_mutex_lock (test1_mutex);
103   g_mutex_unlock (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_main_loop_run (loop);
117   g_main_loop_unref (loop);
118
119   g_cond_signal (test1_cond);
120   return NULL;
121 }
122
123 /* Test 2: If we push a thread-default context in the main thread, we
124  * can run async ops in that context without running the default
125  * context.
126  */
127
128 static gboolean test2_fail (gpointer user_data);
129
130 static void
131 test_context_independence (void)
132 {
133   GMainContext *context;
134   GMainLoop *loop;
135   GFile *file;
136   guint default_timeout;
137   GSource *thread_default_timeout;
138
139   context = g_main_context_new ();
140   g_assert (g_main_context_get_thread_default () == NULL);
141   g_main_context_push_thread_default (context);
142   g_assert (g_main_context_get_thread_default () == context);
143
144   file = g_file_new_for_path (TEST_FILE);
145   g_assert (g_file_supports_thread_contexts (file));
146
147   /* Add a timeout to the main loop, to fail immediately if it gets run */
148   default_timeout = g_timeout_add_full (G_PRIORITY_HIGH, 0,
149                                         test2_fail, NULL, NULL);
150   /* Add a timeout to the alternate loop, to fail if the I/O *doesn't* run */
151   thread_default_timeout = g_timeout_source_new_seconds (2);
152   g_source_set_callback (thread_default_timeout, test2_fail, NULL, NULL);
153   g_source_attach (thread_default_timeout, context);
154
155   loop = g_main_loop_new (context, FALSE);
156   g_file_read_async (file, G_PRIORITY_DEFAULT, NULL,
157                      opened_for_read, loop);
158   g_main_loop_run (loop);
159   g_main_loop_unref (loop);
160
161   g_source_remove (default_timeout);
162   g_source_destroy (thread_default_timeout);
163   g_source_unref (thread_default_timeout);
164 }
165
166 static gboolean
167 test2_fail (gpointer user_data)
168 {
169   g_assert_not_reached ();
170   return FALSE;
171 }
172
173 int
174 main (int argc, char **argv)
175 {
176   GError *error = NULL;
177
178   g_thread_init (NULL);
179   g_type_init ();
180   g_test_init (&argc, &argv, NULL);
181
182   g_file_get_contents (TEST_FILE, &test_file_buffer,
183                        &test_file_size, &error);
184   g_assert_no_error (error);
185
186   g_test_add_func ("/gio/contexts/thread-independence", test_thread_independence);
187   g_test_add_func ("/gio/contexts/context-independence", test_context_independence);
188
189   return g_test_run();
190 }