6 static GMainLoop *loop;
9 stop_waiting (gpointer data)
11 g_main_loop_quit (loop);
13 return G_SOURCE_REMOVE;
17 unreachable_callback (gpointer data)
19 g_assert_not_reached ();
21 return G_SOURCE_REMOVE;
29 /* Bug 642052 mentions that g_timeout_add_seconds(21475) schedules a
30 * job that runs once per second.
32 * Test that that isn't true anymore by scheduling two jobs:
34 * - another that runs in 2100ms
36 * If everything is working properly, the 2100ms one should run first
37 * (and exit the mainloop). If we ever see the 21475 second job run
38 * then we have trouble (since it ran in less than 2 seconds).
40 * We need a timeout of at least 2 seconds because
41 * g_timeout_add_seconds() can add as much as an additional second of
44 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=642052");
45 loop = g_main_loop_new (NULL, FALSE);
47 g_timeout_add (2100, stop_waiting, NULL);
48 id = g_timeout_add_seconds (21475, unreachable_callback, NULL);
50 g_main_loop_run (loop);
51 g_main_loop_unref (loop);
57 test_weeks_overflow (void)
60 guint interval_seconds;
62 /* Internally, the guint interval (in seconds) was converted to milliseconds
63 * then stored in a guint variable. This meant that any interval larger than
64 * G_MAXUINT / 1000 would overflow.
66 * On a system with 32-bit guint, the interval (G_MAXUINT / 1000) + 1 seconds
67 * (49.7 days) would end wrapping to 704 milliseconds.
69 * Test that that isn't true anymore by scheduling two jobs:
71 * - another that runs in 2100ms
73 * If everything is working properly, the 2100ms one should run first
74 * (and exit the mainloop). If we ever see the other job run
75 * then we have trouble (since it ran in less than 2 seconds).
77 * We need a timeout of at least 2 seconds because
78 * g_timeout_add_seconds() can add as much as an additional second of
81 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/issues/1600");
82 loop = g_main_loop_new (NULL, FALSE);
84 g_timeout_add (2100, stop_waiting, NULL);
85 interval_seconds = 1 + G_MAXUINT / 1000;
86 id = g_timeout_add_seconds (interval_seconds, unreachable_callback, NULL);
88 g_main_loop_run (loop);
89 g_main_loop_unref (loop);
94 /* The ready_time for a GSource is stored as a gint64, as an absolute monotonic
95 * time in microseconds. To call poll(), this must be converted to a relative
96 * timeout, in milliseconds, as a gint. If the ready_time is sufficiently far
97 * in the future, the timeout will not fit. Previously, it would be narrowed in
98 * an implementation-defined way; if this gave a negative result, poll() would
101 * This test creates a GSource with the largest possible ready_time (a little
102 * over 292 millennia, assuming g_get_monotonic_time() starts from near 0 when
103 * the system boots), adds it to a GMainContext, queries it for the parameters
104 * to pass to poll() -- essentially the first half of
105 * g_main_context_iteration() -- and checks that the timeout is a large
109 test_far_future_ready_time (void)
111 GSourceFuncs source_funcs = { 0 };
112 GMainContext *context = g_main_context_new ();
113 GSource *source = g_source_new (&source_funcs, sizeof (GSource));
114 gboolean acquired, ready;
115 gint priority, timeout_, n_fds;
117 g_source_set_ready_time (source, G_MAXINT64);
118 g_source_attach (source, context);
120 acquired = g_main_context_acquire (context);
121 g_assert_true (acquired);
123 ready = g_main_context_prepare (context, &priority);
124 g_assert_false (ready);
127 n_fds = g_main_context_query (context, priority, &timeout_, NULL, n_fds);
129 /* The true timeout in milliseconds doesn't fit into a gint. We definitely
130 * don't want poll() to block forever:
132 g_assert_cmpint (timeout_, >=, 0);
133 /* Instead, we want it to block for as long as possible: */
134 g_assert_cmpint (timeout_, ==, G_MAXINT);
136 g_main_context_release (context);
137 g_main_context_unref (context);
138 g_source_unref (source);
141 static gint64 last_time;
145 test_func (gpointer data)
149 current_time = g_get_monotonic_time ();
151 /* We accept 2 on the first iteration because _add_seconds() can
152 * have an initial latency of 1 second, see its documentation.
155 g_assert (current_time / 1000000 - last_time / 1000000 <= 2);
157 g_assert (current_time / 1000000 - last_time / 1000000 == 1);
159 last_time = current_time;
162 /* Make the timeout take up to 0.1 seconds.
163 * We should still get scheduled for the next second.
165 g_usleep (count * 10000);
170 g_main_loop_quit (loop);
178 loop = g_main_loop_new (NULL, FALSE);
180 last_time = g_get_monotonic_time ();
181 g_timeout_add_seconds (1, test_func, NULL);
183 g_main_loop_run (loop);
184 g_main_loop_unref (loop);
188 main (int argc, char *argv[])
190 g_test_init (&argc, &argv, NULL);
191 g_test_bug_base ("");
193 g_test_add_func ("/timeout/seconds", test_seconds);
194 g_test_add_func ("/timeout/weeks-overflow", test_weeks_overflow);
195 g_test_add_func ("/timeout/far-future-ready-time", test_far_future_ready_time);
196 g_test_add_func ("/timeout/rounding", test_rounding);
198 return g_test_run ();