nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | #include <glib.h> |
2 | #ifdef G_OS_UNIX |
||
3 | #include <unistd.h> |
||
4 | #endif |
||
5 | |||
6 | static GMainLoop *loop; |
||
7 | |||
8 | static gboolean |
||
9 | stop_waiting (gpointer data) |
||
10 | { |
||
11 | g_main_loop_quit (loop); |
||
12 | |||
13 | return G_SOURCE_REMOVE; |
||
14 | } |
||
15 | |||
16 | static gboolean |
||
17 | function (gpointer data) |
||
18 | { |
||
19 | g_assert_not_reached (); |
||
20 | |||
21 | return G_SOURCE_REMOVE; |
||
22 | } |
||
23 | |||
24 | static void |
||
25 | test_seconds (void) |
||
26 | { |
||
27 | guint id; |
||
28 | |||
29 | /* Bug 642052 mentions that g_timeout_add_seconds(21475) schedules a |
||
30 | * job that runs once per second. |
||
31 | * |
||
32 | * Test that that isn't true anymore by scheduling two jobs: |
||
33 | * - one, as above |
||
34 | * - another that runs in 2100ms |
||
35 | * |
||
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). |
||
39 | * |
||
40 | * We need a timeout of at least 2 seconds because |
||
41 | * g_timeout_add_second can add as much as an additional second of |
||
42 | * latency. |
||
43 | */ |
||
44 | loop = g_main_loop_new (NULL, FALSE); |
||
45 | |||
46 | g_timeout_add (2100, stop_waiting, NULL); |
||
47 | id = g_timeout_add_seconds (21475, function, NULL); |
||
48 | |||
49 | g_main_loop_run (loop); |
||
50 | g_main_loop_unref (loop); |
||
51 | |||
52 | g_source_remove (id); |
||
53 | } |
||
54 | |||
55 | static gint64 last_time; |
||
56 | static gint count; |
||
57 | |||
58 | static gboolean |
||
59 | test_func (gpointer data) |
||
60 | { |
||
61 | gint64 current_time; |
||
62 | |||
63 | current_time = g_get_monotonic_time (); |
||
64 | |||
65 | /* We accept 2 on the first iteration because _add_seconds() can |
||
66 | * have an initial latency of 1 second, see its documentation. |
||
67 | */ |
||
68 | if (count == 0) |
||
69 | g_assert (current_time / 1000000 - last_time / 1000000 <= 2); |
||
70 | else |
||
71 | g_assert (current_time / 1000000 - last_time / 1000000 == 1); |
||
72 | |||
73 | last_time = current_time; |
||
74 | count++; |
||
75 | |||
76 | /* Make the timeout take up to 0.1 seconds. |
||
77 | * We should still get scheduled for the next second. |
||
78 | */ |
||
79 | g_usleep (count * 10000); |
||
80 | |||
81 | if (count < 10) |
||
82 | return TRUE; |
||
83 | |||
84 | g_main_loop_quit (loop); |
||
85 | |||
86 | return FALSE; |
||
87 | } |
||
88 | |||
89 | static void |
||
90 | test_rounding (void) |
||
91 | { |
||
92 | loop = g_main_loop_new (NULL, FALSE); |
||
93 | |||
94 | last_time = g_get_monotonic_time (); |
||
95 | g_timeout_add_seconds (1, test_func, NULL); |
||
96 | |||
97 | g_main_loop_run (loop); |
||
98 | g_main_loop_unref (loop); |
||
99 | } |
||
100 | |||
101 | int |
||
102 | main (int argc, char *argv[]) |
||
103 | { |
||
104 | g_test_init (&argc, &argv, NULL); |
||
105 | |||
106 | g_test_add_func ("/timeout/seconds", test_seconds); |
||
107 | g_test_add_func ("/timeout/rounding", test_rounding); |
||
108 | |||
109 | return g_test_run (); |
||
110 | } |