4
Copyright 2013 Canonical Ltd.
7
Ted Gould <ted@canonical.com>
9
This program is free software: you can redistribute it and/or modify it
10
under the terms of the GNU General Public License version 3, as published
11
by the Free Software Foundation.
13
This program is distributed in the hope that it will be useful, but
14
WITHOUT ANY WARRANTY; without even the implied warranties of
15
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
16
PURPOSE. See the GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License along
19
with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include <glib-object.h>
28
test_watchdog_create (void)
30
g_unsetenv("HUD_SERVICE_TIMEOUT");
33
HudWatchdog * doggie = hud_watchdog_new(NULL);
35
g_assert(IS_HUD_WATCHDOG(doggie));
36
g_assert(hud_watchdog_get_timeout(doggie) == 600);
38
g_clear_object(&doggie);
41
GMainLoop * loop = g_main_loop_new(NULL, FALSE);
42
doggie = hud_watchdog_new(loop);
44
g_assert(IS_HUD_WATCHDOG(doggie));
45
g_assert(hud_watchdog_get_timeout(doggie) == 600);
47
g_clear_object(&doggie);
48
g_main_loop_unref(loop);
50
/* Set the environment variable */
51
g_setenv("HUD_SERVICE_TIMEOUT", "1000", TRUE);
53
doggie = hud_watchdog_new(NULL);
55
g_assert(IS_HUD_WATCHDOG(doggie));
56
g_assert(hud_watchdog_get_timeout(doggie) == 1000);
58
g_clear_object(&doggie);
64
final_fail (gpointer ploop)
66
g_error("Timeout not via the watchdog. It didn't work.");
67
g_main_loop_quit((GMainLoop *)ploop);
72
ping_watchdog (gpointer pwatchdog)
74
hud_watchdog_ping(pwatchdog);
79
hit_one_sec (gpointer pboolean)
81
gboolean * onesec = (gboolean *)pboolean;
87
test_watchdog_timing (void)
89
HudWatchdog * doggie = NULL;
90
GMainLoop * loop = NULL;
92
g_setenv("HUD_SERVICE_TIMEOUT", "1", TRUE);
94
/* Test base timeout */
95
loop = g_main_loop_new(NULL, FALSE);
96
doggie = hud_watchdog_new(loop);
98
glong final = g_timeout_add_seconds(5, final_fail, loop);
99
g_main_loop_run(loop);
100
g_source_remove(final);
102
g_clear_object(&doggie);
104
/* Test a single ping */
105
gboolean one_sec_hit = FALSE;
106
doggie = hud_watchdog_new(loop);
108
final = g_timeout_add_seconds(3, final_fail, loop);
109
g_timeout_add(500, ping_watchdog, doggie);
110
g_timeout_add(1000, hit_one_sec, &one_sec_hit);
112
g_main_loop_run(loop);
113
g_source_remove(final);
115
g_assert(one_sec_hit);
116
g_clear_object(&doggie);
119
/* Clean up the loop */
120
g_main_loop_unref(loop);
125
/* Test to ensure we can ping with a NULL watchdog so the other tests
126
suites will work fine */
128
test_watchdog_null_ping (void)
130
hud_watchdog_ping(NULL);
134
/* Build the test suite */
136
test_watchdog_suite (void)
138
g_test_add_func ("/hud/watchdog/nullping", test_watchdog_null_ping);
139
g_test_add_func ("/hud/watchdog/create", test_watchdog_create);
140
g_test_add_func ("/hud/watchdog/timing", test_watchdog_timing);
145
main (gint argc, gchar * argv[])
147
#ifndef GLIB_VERSION_2_36
151
g_test_init(&argc, &argv, NULL);
154
test_watchdog_suite();
156
return g_test_run ();