LCOV - code coverage report
Current view: top level - glib/glib/tests - timeout.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 75 77 97.4 %
Date: 2024-03-26 05:16:46 Functions: 8 10 80.0 %
Branches: 4 4 100.0 %

           Branch data     Line data    Source code
       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                 :          3 : stop_waiting (gpointer data)
      10                 :            : {
      11                 :          3 :   g_main_loop_quit (loop);
      12                 :          3 : }
      13                 :            : 
      14                 :            : static gboolean
      15                 :          0 : unreachable_callback (gpointer data)
      16                 :            : {
      17                 :            :   g_assert_not_reached ();
      18                 :            : 
      19                 :            :   return G_SOURCE_REMOVE;
      20                 :            : }
      21                 :            : 
      22                 :            : static void
      23                 :          0 : unreachable_void_callback (gpointer data)
      24                 :            : {
      25                 :            :   g_assert_not_reached ();
      26                 :            : }
      27                 :            : 
      28                 :            : static void
      29                 :          1 : 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                 :          1 :   g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=642052");
      49                 :          1 :   loop = g_main_loop_new (NULL, FALSE);
      50                 :            : 
      51                 :          1 :   g_timeout_add_once (2100, stop_waiting, NULL);
      52                 :          1 :   id = g_timeout_add_seconds (21475, unreachable_callback, NULL);
      53                 :            : 
      54                 :          1 :   g_main_loop_run (loop);
      55                 :          1 :   g_main_loop_unref (loop);
      56                 :            : 
      57                 :          1 :   g_source_remove (id);
      58                 :          1 : }
      59                 :            : 
      60                 :            : static void
      61                 :          1 : test_seconds_once (void)
      62                 :            : {
      63                 :            :   /* Use the same principle as in test_seconds() */
      64                 :          1 :   loop = g_main_loop_new (NULL, FALSE);
      65                 :            : 
      66                 :          1 :   g_timeout_add_once (2100, stop_waiting, NULL);
      67                 :          1 :   g_timeout_add_seconds_once (21475, unreachable_void_callback, NULL);
      68                 :            : 
      69                 :          1 :   g_main_loop_run (loop);
      70                 :          1 :   g_main_loop_unref (loop);
      71                 :          1 : }
      72                 :            : 
      73                 :            : static void
      74                 :          1 : 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                 :          1 :   g_test_bug ("https://gitlab.gnome.org/GNOME/glib/issues/1600");
      99                 :          1 :   loop = g_main_loop_new (NULL, FALSE);
     100                 :            : 
     101                 :          1 :   g_timeout_add_once (2100, stop_waiting, NULL);
     102                 :          1 :   interval_seconds = 1 + G_MAXUINT / 1000;
     103                 :          1 :   id = g_timeout_add_seconds (interval_seconds, unreachable_callback, NULL);
     104                 :            : 
     105                 :          1 :   g_main_loop_run (loop);
     106                 :          1 :   g_main_loop_unref (loop);
     107                 :            : 
     108                 :          1 :   g_source_remove (id);
     109                 :          1 : }
     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                 :          1 : test_far_future_ready_time (void)
     127                 :            : {
     128                 :          1 :   GSourceFuncs source_funcs = { 0 };
     129                 :          1 :   GMainContext *context = g_main_context_new ();
     130                 :          1 :   GSource *source = g_source_new (&source_funcs, sizeof (GSource));
     131                 :            :   gboolean acquired, ready;
     132                 :            :   gint priority, timeout_, n_fds;
     133                 :            : 
     134                 :          1 :   g_source_set_ready_time (source, G_MAXINT64);
     135                 :          1 :   g_source_attach (source, context);
     136                 :            : 
     137                 :          1 :   acquired = g_main_context_acquire (context);
     138                 :          1 :   g_assert_true (acquired);
     139                 :            : 
     140                 :          1 :   ready = g_main_context_prepare (context, &priority);
     141                 :          1 :   g_assert_false (ready);
     142                 :            : 
     143                 :          1 :   n_fds = 0;
     144                 :          1 :   n_fds = g_main_context_query (context, priority, &timeout_, NULL, n_fds);
     145                 :            : 
     146                 :          1 :   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                 :          1 :   g_assert_cmpint (timeout_, >=, 0);
     152                 :            :   /* Instead, we want it to block for as long as possible: */
     153                 :          1 :   g_assert_cmpint (timeout_, ==, G_MAXINT);
     154                 :            : 
     155                 :          1 :   g_main_context_release (context);
     156                 :          1 :   g_main_context_unref (context);
     157                 :          1 :   g_source_unref (source);
     158                 :          1 : }
     159                 :            : 
     160                 :            : static gint64 last_time;
     161                 :            : static gint count;
     162                 :            : 
     163                 :            : static gboolean
     164                 :         10 : test_func (gpointer data)
     165                 :            : {
     166                 :            :   gint64 current_time;
     167                 :            : 
     168                 :         10 :   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         [ +  + ]:         10 :   if (count == 0)
     176                 :          1 :     g_assert_cmpint (current_time / 1000 - last_time / 1000, <=, 2500);
     177                 :            :   else
     178                 :          9 :     g_assert_cmpint (current_time / 1000 - last_time / 1000, <=, 1500);
     179                 :            : 
     180                 :         10 :   last_time = current_time;
     181                 :         10 :   count++;
     182                 :            : 
     183                 :            :   /* Make the timeout take up to 0.1 seconds.
     184                 :            :    * We should still get scheduled for the next second.
     185                 :            :    */
     186                 :         10 :   g_usleep (count * 10000);
     187                 :            : 
     188         [ +  + ]:         10 :   if (count < 10)
     189                 :          9 :     return TRUE;
     190                 :            : 
     191                 :          1 :   g_main_loop_quit (loop);
     192                 :            : 
     193                 :          1 :   return FALSE;
     194                 :            : }
     195                 :            : 
     196                 :            : static void
     197                 :          1 : test_rounding (void)
     198                 :            : {
     199                 :          1 :   loop = g_main_loop_new (NULL, FALSE);
     200                 :            : 
     201                 :          1 :   last_time = g_get_monotonic_time ();
     202                 :          1 :   g_timeout_add_seconds (1, test_func, NULL);
     203                 :            : 
     204                 :          1 :   g_main_loop_run (loop);
     205                 :          1 :   g_main_loop_unref (loop);
     206                 :          1 : }
     207                 :            : 
     208                 :            : int
     209                 :          1 : main (int argc, char *argv[])
     210                 :            : {
     211                 :          1 :   g_test_init (&argc, &argv, NULL);
     212                 :            : 
     213                 :          1 :   g_test_add_func ("/timeout/seconds", test_seconds);
     214                 :          1 :   g_test_add_func ("/timeout/seconds-once", test_seconds_once);
     215                 :          1 :   g_test_add_func ("/timeout/weeks-overflow", test_weeks_overflow);
     216                 :          1 :   g_test_add_func ("/timeout/far-future-ready-time", test_far_future_ready_time);
     217                 :          1 :   g_test_add_func ("/timeout/rounding", test_rounding);
     218                 :            : 
     219                 :          1 :   return g_test_run ();
     220                 :            : }

Generated by: LCOV version 1.14