5 #define TEST_FILE (SRCDIR "/Makefile.am")
6 char *test_file_buffer;
8 static char async_read_buffer[8192];
11 read_data (GObject *source, GAsyncResult *result, gpointer loop)
13 GInputStream *in = G_INPUT_STREAM (source);
17 nread = g_input_stream_read_finish (in, result, &error);
18 g_assert_no_error (error);
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);
24 g_main_loop_quit (loop);
28 opened_for_read (GObject *source, GAsyncResult *result, gpointer loop)
30 GFile *file = G_FILE (source);
34 in = g_file_read_finish (file, result, &error);
35 g_assert_no_error (error);
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,
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!)
52 static gboolean idle_start_test1_thread (gpointer loop);
53 static gpointer test1_thread (gpointer user_data);
55 static GCond *test1_cond;
56 static GMutex *test1_mutex;
59 test_thread_independence (void)
63 test1_cond = g_cond_new ();
64 test1_mutex = g_mutex_new ();
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);
71 g_mutex_free (test1_mutex);
72 g_cond_free (test1_cond);
76 idle_start_test1_thread (gpointer loop)
80 gboolean io_completed;
82 g_mutex_lock (test1_mutex);
83 thread = g_thread_create (test1_thread, NULL, TRUE, NULL);
85 g_get_current_time (&time);
87 io_completed = g_cond_timed_wait (test1_cond, test1_mutex, &time);
88 g_assert (io_completed);
89 g_thread_join (thread);
91 g_mutex_unlock (test1_mutex);
92 g_main_loop_quit (loop);
97 test1_thread (gpointer user_data)
99 GMainContext *context;
103 /* Wait for main thread to be waiting on test1_cond */
104 g_mutex_lock (test1_mutex);
105 g_mutex_unlock (test1_mutex);
107 context = g_main_context_new ();
108 g_assert (g_main_context_get_thread_default () == NULL);
109 g_main_context_push_thread_default (context);
110 g_assert (g_main_context_get_thread_default () == context);
112 file = g_file_new_for_path (TEST_FILE);
113 g_assert (g_file_supports_thread_contexts (file));
115 loop = g_main_loop_new (context, FALSE);
116 g_file_read_async (file, G_PRIORITY_DEFAULT, NULL,
117 opened_for_read, loop);
118 g_object_unref (file);
119 g_main_loop_run (loop);
120 g_main_loop_unref (loop);
122 g_cond_signal (test1_cond);
126 /* Test 2: If we push a thread-default context in the main thread, we
127 * can run async ops in that context without running the default
131 static gboolean test2_fail (gpointer user_data);
134 test_context_independence (void)
136 GMainContext *context;
139 guint default_timeout;
140 GSource *thread_default_timeout;
142 context = g_main_context_new ();
143 g_assert (g_main_context_get_thread_default () == NULL);
144 g_main_context_push_thread_default (context);
145 g_assert (g_main_context_get_thread_default () == context);
147 file = g_file_new_for_path (TEST_FILE);
148 g_assert (g_file_supports_thread_contexts (file));
150 /* Add a timeout to the main loop, to fail immediately if it gets run */
151 default_timeout = g_timeout_add_full (G_PRIORITY_HIGH, 0,
152 test2_fail, NULL, NULL);
153 /* Add a timeout to the alternate loop, to fail if the I/O *doesn't* run */
154 thread_default_timeout = g_timeout_source_new_seconds (2);
155 g_source_set_callback (thread_default_timeout, test2_fail, NULL, NULL);
156 g_source_attach (thread_default_timeout, context);
158 loop = g_main_loop_new (context, FALSE);
159 g_file_read_async (file, G_PRIORITY_DEFAULT, NULL,
160 opened_for_read, loop);
161 g_object_unref (file);
162 g_main_loop_run (loop);
163 g_main_loop_unref (loop);
165 g_source_remove (default_timeout);
166 g_source_destroy (thread_default_timeout);
167 g_source_unref (thread_default_timeout);
171 test2_fail (gpointer user_data)
173 g_assert_not_reached ();
178 main (int argc, char **argv)
180 GError *error = NULL;
182 g_thread_init (NULL);
184 g_test_init (&argc, &argv, NULL);
186 g_file_get_contents (TEST_FILE, &test_file_buffer,
187 &test_file_size, &error);
188 g_assert_no_error (error);
190 g_test_add_func ("/gio/contexts/thread-independence", test_thread_independence);
191 g_test_add_func ("/gio/contexts/context-independence", test_context_independence);