1 #include <glib.h>
2 #ifdef G_OS_UNIX
3 #include <unistd.h>
4 #endif
5
6 static GMainLoop *loop;
7
8 static void
9 stop_waiting (gpointer data)
10 {
11 g_main_loop_quit (loop);
12 }
13
14 static gboolean
15 unreachable_callback (gpointer data)
16 {
17 g_assert_not_reached ();
18
19 return G_SOURCE_REMOVE;
20 }
21
22 static void
23 unreachable_void_callback (gpointer data)
24 {
25 g_assert_not_reached ();
26 }
27
28 static void
29 test_seconds (void)
30 {
31 guint id;
32
33 /* Bug 642052 mentions that g_timeout_add_seconds(21475) schedules a
34 * job that runs once per second.
35 *
36 * Test that that isn't true anymore by scheduling two jobs:
37 * - one, as above
38 * - another that runs in 2100ms
39 *
40 * If everything is working properly, the 2100ms one should run first
41 * (and exit the mainloop). If we ever see the 21475 second job run
42 * then we have trouble (since it ran in less than 2 seconds).
43 *
44 * We need a timeout of at least 2 seconds because
45 * g_timeout_add_seconds() can add as much as an additional second of
46 * latency.
47 */
48 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=642052");
49 loop = g_main_loop_new (NULL, FALSE);
50
51 g_timeout_add_once (2100, stop_waiting, NULL);
52 id = g_timeout_add_seconds (21475, unreachable_callback, NULL);
53
54 g_main_loop_run (loop);
55 g_main_loop_unref (loop);
56
57 g_source_remove (id);
58 }
59
60 static void
61 test_seconds_once (void)
62 {
63 /* Use the same principle as in test_seconds() */
64 loop = g_main_loop_new (NULL, FALSE);
65
66 g_timeout_add_once (2100, stop_waiting, NULL);
67 g_timeout_add_seconds_once (21475, unreachable_void_callback, NULL);
68
69 g_main_loop_run (loop);
70 g_main_loop_unref (loop);
71 }
72
73 static void
74 test_weeks_overflow (void)
75 {
76 guint id;
77 guint interval_seconds;
78
79 /* Internally, the guint interval (in seconds) was converted to milliseconds
80 * then stored in a guint variable. This meant that any interval larger than
81 * G_MAXUINT / 1000 would overflow.
82 *
83 * On a system with 32-bit guint, the interval (G_MAXUINT / 1000) + 1 seconds
84 * (49.7 days) would end wrapping to 704 milliseconds.
85 *
86 * Test that that isn't true anymore by scheduling two jobs:
87 * - one, as above
88 * - another that runs in 2100ms
89 *
90 * If everything is working properly, the 2100ms one should run first
91 * (and exit the mainloop). If we ever see the other job run
92 * then we have trouble (since it ran in less than 2 seconds).
93 *
94 * We need a timeout of at least 2 seconds because
95 * g_timeout_add_seconds() can add as much as an additional second of
96 * latency.
97 */
98 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/issues/1600");
99 loop = g_main_loop_new (NULL, FALSE);
100
101 g_timeout_add_once (2100, stop_waiting, NULL);
102 interval_seconds = 1 + G_MAXUINT / 1000;
103 id = g_timeout_add_seconds (interval_seconds, unreachable_callback, NULL);
104
105 g_main_loop_run (loop);
106 g_main_loop_unref (loop);
107
108 g_source_remove (id);
109 }
110
111 /* The ready_time for a GSource is stored as a gint64, as an absolute monotonic
112 * time in microseconds. To call poll(), this must be converted to a relative
113 * timeout, in milliseconds, as a gint. If the ready_time is sufficiently far
114 * in the future, the timeout will not fit. Previously, it would be narrowed in
115 * an implementation-defined way; if this gave a negative result, poll() would
116 * block forever.
117 *
118 * This test creates a GSource with the largest possible ready_time (a little
119 * over 292 millennia, assuming g_get_monotonic_time() starts from near 0 when
120 * the system boots), adds it to a GMainContext, queries it for the parameters
121 * to pass to poll() -- essentially the first half of
122 * g_main_context_iteration() -- and checks that the timeout is a large
123 * positive number.
124 */
125 static void
126 test_far_future_ready_time (void)
127 {
128 GSourceFuncs source_funcs = { 0 };
129 GMainContext *context = g_main_context_new ();
130 GSource *source = g_source_new (&source_funcs, sizeof (GSource));
131 gboolean acquired, ready;
132 gint priority, timeout_, n_fds;
133
134 g_source_set_ready_time (source, G_MAXINT64);
135 g_source_attach (source, context);
136
137 acquired = g_main_context_acquire (context);
138 g_assert_true (acquired);
139
140 ready = g_main_context_prepare (context, &priority);
141 g_assert_false (ready);
142
143 n_fds = 0;
144 n_fds = g_main_context_query (context, priority, &timeout_, NULL, n_fds);
145
146 g_assert_cmpint (n_fds, >=, 0);
147
148 /* The true timeout in milliseconds doesn't fit into a gint. We definitely
149 * don't want poll() to block forever:
150 */
151 g_assert_cmpint (timeout_, >=, 0);
152 /* Instead, we want it to block for as long as possible: */
153 g_assert_cmpint (timeout_, ==, G_MAXINT);
154
155 g_main_context_release (context);
156 g_main_context_unref (context);
157 g_source_unref (source);
158 }
159
160 static gint64 last_time;
161 static gint count;
162
163 static gboolean
164 test_func (gpointer data)
165 {
166 gint64 current_time;
167
168 current_time = g_get_monotonic_time ();
169
170 /* We accept 2 on the first iteration because _add_seconds() can
171 * have an initial latency of 1 second, see its documentation.
172 *
173 * Allow up to 500ms leeway for rounding and scheduling.
174 */
175 if (count == 0)
176 g_assert_cmpint (current_time / 1000 - last_time / 1000, <=, 2500);
177 else
178 g_assert_cmpint (current_time / 1000 - last_time / 1000, <=, 1500);
179
180 last_time = current_time;
181 count++;
182
183 /* Make the timeout take up to 0.1 seconds.
184 * We should still get scheduled for the next second.
185 */
186 g_usleep (count * 10000);
187
188 if (count < 10)
189 return TRUE;
190
191 g_main_loop_quit (loop);
192
193 return FALSE;
194 }
195
196 static void
197 test_rounding (void)
198 {
199 loop = g_main_loop_new (NULL, FALSE);
200
201 last_time = g_get_monotonic_time ();
202 g_timeout_add_seconds (1, test_func, NULL);
203
204 g_main_loop_run (loop);
205 g_main_loop_unref (loop);
206 }
207
208 int
209 main (int argc, char *argv[])
210 {
211 g_test_init (&argc, &argv, NULL);
212
213 g_test_add_func ("/timeout/seconds", test_seconds);
214 g_test_add_func ("/timeout/seconds-once", test_seconds_once);
215 g_test_add_func ("/timeout/weeks-overflow", test_weeks_overflow);
216 g_test_add_func ("/timeout/far-future-ready-time", test_far_future_ready_time);
217 g_test_add_func ("/timeout/rounding", test_rounding);
218
219 return g_test_run ();
220 }