summaryrefslogtreecommitdiff
path: root/pkcs11/gkm/test-timer.c
blob: e0e7eeefdff6742818bcd041092b7da4cdff1075 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* test-timer.c: Test thread timer functionality

   Copyright (C) 2009 Stefan Walter

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   <http://www.gnu.org/licenses/>.

   Author: Stef Walter <stef@memberwebs.com>
*/

#include "config.h"

#include "mock-module.h"

#include "gkm/gkm-timer.h"

#include "egg/egg-testing.h"

#include <glib-object.h>

typedef struct {
	GkmModule *module;
} Test;

static void
setup (Test* test, gconstpointer unused)
{
	test->module = mock_module_initialize_and_enter ();
}

static void
teardown (Test* test, gconstpointer unused)
{
	mock_module_leave_and_finalize ();
}

static void
test_extra_initialize (Test* test, gconstpointer unused)
{
	gkm_timer_initialize ();
	gkm_timer_shutdown ();
}

static void
timer_callback (GkmTimer *timer, gpointer user_data)
{
	GkmTimer **value = user_data;
	g_assert (timer);
	g_assert (timer == *value);
	*value = NULL;
}

static void
test_simple (Test* test, gconstpointer unused)
{
	GkmTimer *timer;

	timer = gkm_timer_start (test->module, 2, timer_callback, &timer);

	mock_module_leave ();
	egg_test_wait_until (2200);
	mock_module_enter ();

	g_assert (timer == NULL);
}

static void
test_cancel (Test* test, gconstpointer unused)
{
	GkmTimer *timer;

	timer = gkm_timer_start (test->module, 2, timer_callback, &timer);

	mock_module_leave ();
	egg_test_wait_until (50);
	mock_module_enter ();

	gkm_timer_cancel (timer);

	mock_module_leave ();
	egg_test_wait_until (3000);
	mock_module_enter ();

	/* The callback should not have been called */
	g_assert (timer != NULL);
}

static void
test_immediate (Test* test, gconstpointer unused)
{
	GkmTimer *timer;

	/* Setup timer in the past, should execute as soon as possible */
	timer = gkm_timer_start (test->module, -5, timer_callback, &timer);

	/* Should not be called immediately */
	g_assert (timer != NULL);

	mock_module_leave ();
	egg_test_wait_until (50);
	mock_module_enter ();

	/* Should have been called now */
	g_assert (timer == NULL);
}

static GkmTimer *timer_last = NULL;
static gint timer_check = 0;

static void
multiple_callback (GkmTimer *timer, gpointer user_data)
{
	gint value = GPOINTER_TO_INT (user_data);
	g_assert (timer);
	g_assert (timer != timer_last);
	g_assert (value == timer_check);
	timer_last = timer;
	timer_check += 1;
}

static void
test_multiple (Test* test, gconstpointer unused)
{
	timer_check = 0;

	/* Multiple timers, add out of order, should be called in order */
	gkm_timer_start (test->module, 1, multiple_callback, GINT_TO_POINTER (1));
	gkm_timer_start (test->module, 3, multiple_callback, GINT_TO_POINTER (3));
	gkm_timer_start (test->module, 2, multiple_callback, GINT_TO_POINTER (2));
	gkm_timer_start (test->module, 0, multiple_callback, GINT_TO_POINTER (0));

	mock_module_leave ();
	egg_test_wait_until (3500);
	mock_module_enter ();

	g_assert (timer_check == 4);
}

static void
test_outstanding (Test* test, gconstpointer unused)
{
	/* A timer that can't be called */
	gkm_timer_start (test->module, 5, timer_callback, NULL);
	gkm_timer_start (test->module, 10, timer_callback, NULL);
	gkm_timer_start (test->module, 1, timer_callback, NULL);
}

int
main (int argc, char **argv)
{
#if !GLIB_CHECK_VERSION(2,35,0)
	g_type_init ();
#endif
	g_test_init (&argc, &argv, NULL);

	g_test_add ("/gkm/timer/extra_initialize", Test, NULL, setup, test_extra_initialize, teardown);
	g_test_add ("/gkm/timer/simple", Test, NULL, setup, test_simple, teardown);
	g_test_add ("/gkm/timer/cancel", Test, NULL, setup, test_cancel, teardown);
	g_test_add ("/gkm/timer/immediate", Test, NULL, setup, test_immediate, teardown);
	g_test_add ("/gkm/timer/multiple", Test, NULL, setup, test_multiple, teardown);
	g_test_add ("/gkm/timer/outstanding", Test, NULL, setup, test_outstanding, teardown);

	return egg_tests_run_in_thread_with_loop ();
}