4 static GMainLoop *loop;
7 stop_waiting (gpointer data)
9 g_main_loop_quit (loop);
11 return G_SOURCE_REMOVE;
15 function (gpointer data)
17 g_assert_not_reached ();
19 return G_SOURCE_REMOVE;
25 /* Bug 642052 mentions that g_timeout_add_seconds(21475) schedules a
26 * job that runs once per second.
28 * Test that that isn't true anymore by scheduling two jobs:
30 * - another that runs in 2100ms
32 * If everything is working properly, the 2100ms one should run first
33 * (and exit the mainloop). If we ever see the 21475 second job run
34 * then we have trouble (since it ran in less than 2 seconds).
36 * We need a timeout of at least 2 seconds because
37 * g_timeout_add_second can add as much as an additional second of
40 loop = g_main_loop_new (NULL, FALSE);
42 g_timeout_add (2100, stop_waiting, NULL);
43 g_timeout_add_seconds (21475, function, NULL);
45 g_main_loop_run (loop);
46 g_main_loop_unref (loop);
49 static gint64 last_time;
53 test_func (gpointer data)
57 current_time = g_get_monotonic_time ();
59 /* We accept 2 on the first iteration because _add_seconds() can
60 * have an initial latency of 1 second, see its documentation.
63 g_assert (current_time / 1000000 - last_time / 1000000 <= 2);
65 g_assert (current_time / 1000000 - last_time / 1000000 == 1);
67 last_time = current_time;
70 /* Make the timeout take up to 0.1 seconds.
71 * We should still get scheduled for the next second.
73 g_usleep (count * 10000);
78 g_main_loop_quit (loop);
86 loop = g_main_loop_new (NULL, FALSE);
88 last_time = g_get_monotonic_time ();
89 g_timeout_add_seconds (1, test_func, NULL);
91 g_main_loop_run (loop);
92 g_main_loop_unref (loop);
96 main (int argc, char *argv[])
98 g_test_init (&argc, &argv, NULL);
100 g_test_add_func ("/timeout/seconds", test_seconds);
101 g_test_add_func ("/timeout/rounding", test_rounding);
103 return g_test_run ();