LCOV - code coverage report
Current view: top level - pkcs11/gkm - test-timer.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 75 75
Test Date: 2024-04-08 13:24:42 Functions: 100.0 % 11 11

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /* test-timer.c: Test thread timer functionality
       3              : 
       4              :    Copyright (C) 2009 Stefan Walter
       5              : 
       6              :    The Gnome Keyring Library is free software; you can redistribute it and/or
       7              :    modify it under the terms of the GNU Library General Public License as
       8              :    published by the Free Software Foundation; either version 2 of the
       9              :    License, or (at your option) any later version.
      10              : 
      11              :    The Gnome Keyring Library is distributed in the hope that it will be useful,
      12              :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13              :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14              :    Library General Public License for more details.
      15              : 
      16              :    You should have received a copy of the GNU Library General Public
      17              :    License along with the Gnome Library; see the file COPYING.LIB.  If not,
      18              :    <http://www.gnu.org/licenses/>.
      19              : 
      20              :    Author: Stef Walter <stef@memberwebs.com>
      21              : */
      22              : 
      23              : #include "config.h"
      24              : 
      25              : #include "mock-module.h"
      26              : 
      27              : #include "gkm/gkm-timer.h"
      28              : 
      29              : #include "egg/egg-testing.h"
      30              : 
      31              : #include <glib-object.h>
      32              : 
      33              : typedef struct {
      34              :         GkmModule *module;
      35              : } Test;
      36              : 
      37              : static void
      38            6 : setup (Test* test, gconstpointer unused)
      39              : {
      40            6 :         test->module = mock_module_initialize_and_enter ();
      41            6 : }
      42              : 
      43              : static void
      44            6 : teardown (Test* test, gconstpointer unused)
      45              : {
      46            6 :         mock_module_leave_and_finalize ();
      47            6 : }
      48              : 
      49              : static void
      50            1 : test_extra_initialize (Test* test, gconstpointer unused)
      51              : {
      52            1 :         gkm_timer_initialize ();
      53            1 :         gkm_timer_shutdown ();
      54            1 : }
      55              : 
      56              : static void
      57            2 : timer_callback (GkmTimer *timer, gpointer user_data)
      58              : {
      59            2 :         GkmTimer **value = user_data;
      60            2 :         g_assert (timer);
      61            2 :         g_assert (timer == *value);
      62            2 :         *value = NULL;
      63            2 : }
      64              : 
      65              : static void
      66            1 : test_simple (Test* test, gconstpointer unused)
      67              : {
      68              :         GkmTimer *timer;
      69              : 
      70            1 :         timer = gkm_timer_start (test->module, 2, timer_callback, &timer);
      71              : 
      72            1 :         mock_module_leave ();
      73            1 :         egg_test_wait_until (2200);
      74            1 :         mock_module_enter ();
      75              : 
      76            1 :         g_assert (timer == NULL);
      77            1 : }
      78              : 
      79              : static void
      80            1 : test_cancel (Test* test, gconstpointer unused)
      81              : {
      82              :         GkmTimer *timer;
      83              : 
      84            1 :         timer = gkm_timer_start (test->module, 2, timer_callback, &timer);
      85              : 
      86            1 :         mock_module_leave ();
      87            1 :         egg_test_wait_until (50);
      88            1 :         mock_module_enter ();
      89              : 
      90            1 :         gkm_timer_cancel (timer);
      91              : 
      92            1 :         mock_module_leave ();
      93            1 :         egg_test_wait_until (3000);
      94            1 :         mock_module_enter ();
      95              : 
      96              :         /* The callback should not have been called */
      97            1 :         g_assert (timer != NULL);
      98            1 : }
      99              : 
     100              : static void
     101            1 : test_immediate (Test* test, gconstpointer unused)
     102              : {
     103              :         GkmTimer *timer;
     104              : 
     105              :         /* Setup timer in the past, should execute as soon as possible */
     106            1 :         timer = gkm_timer_start (test->module, -5, timer_callback, &timer);
     107              : 
     108              :         /* Should not be called immediately */
     109            1 :         g_assert (timer != NULL);
     110              : 
     111            1 :         mock_module_leave ();
     112            1 :         egg_test_wait_until (50);
     113            1 :         mock_module_enter ();
     114              : 
     115              :         /* Should have been called now */
     116            1 :         g_assert (timer == NULL);
     117            1 : }
     118              : 
     119              : static GkmTimer *timer_last = NULL;
     120              : static gint timer_check = 0;
     121              : 
     122              : static void
     123            4 : multiple_callback (GkmTimer *timer, gpointer user_data)
     124              : {
     125            4 :         gint value = GPOINTER_TO_INT (user_data);
     126            4 :         g_assert (timer);
     127            4 :         g_assert (timer != timer_last);
     128            4 :         g_assert (value == timer_check);
     129            4 :         timer_last = timer;
     130            4 :         timer_check += 1;
     131            4 : }
     132              : 
     133              : static void
     134            1 : test_multiple (Test* test, gconstpointer unused)
     135              : {
     136            1 :         timer_check = 0;
     137              : 
     138              :         /* Multiple timers, add out of order, should be called in order */
     139            1 :         gkm_timer_start (test->module, 1, multiple_callback, GINT_TO_POINTER (1));
     140            1 :         gkm_timer_start (test->module, 3, multiple_callback, GINT_TO_POINTER (3));
     141            1 :         gkm_timer_start (test->module, 2, multiple_callback, GINT_TO_POINTER (2));
     142            1 :         gkm_timer_start (test->module, 0, multiple_callback, GINT_TO_POINTER (0));
     143              : 
     144            1 :         mock_module_leave ();
     145            1 :         egg_test_wait_until (3500);
     146            1 :         mock_module_enter ();
     147              : 
     148            1 :         g_assert (timer_check == 4);
     149            1 : }
     150              : 
     151              : static void
     152            1 : test_outstanding (Test* test, gconstpointer unused)
     153              : {
     154              :         /* A timer that can't be called */
     155            1 :         gkm_timer_start (test->module, 5, timer_callback, NULL);
     156            1 :         gkm_timer_start (test->module, 10, timer_callback, NULL);
     157            1 :         gkm_timer_start (test->module, 1, timer_callback, NULL);
     158            1 : }
     159              : 
     160              : int
     161            1 : main (int argc, char **argv)
     162              : {
     163              : #if !GLIB_CHECK_VERSION(2,35,0)
     164              :         g_type_init ();
     165              : #endif
     166            1 :         g_test_init (&argc, &argv, NULL);
     167              : 
     168            1 :         g_test_add ("/gkm/timer/extra_initialize", Test, NULL, setup, test_extra_initialize, teardown);
     169            1 :         g_test_add ("/gkm/timer/simple", Test, NULL, setup, test_simple, teardown);
     170            1 :         g_test_add ("/gkm/timer/cancel", Test, NULL, setup, test_cancel, teardown);
     171            1 :         g_test_add ("/gkm/timer/immediate", Test, NULL, setup, test_immediate, teardown);
     172            1 :         g_test_add ("/gkm/timer/multiple", Test, NULL, setup, test_multiple, teardown);
     173            1 :         g_test_add ("/gkm/timer/outstanding", Test, NULL, setup, test_outstanding, teardown);
     174              : 
     175            1 :         return egg_tests_run_in_thread_with_loop ();
     176              : }
        

Generated by: LCOV version 2.0-1