~ubuntu-branches/ubuntu/saucy/hud/saucy-proposed

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
/*
Test code for watchdog

Copyright 2013 Canonical Ltd.

Authors:
    Ted Gould <ted@canonical.com>

This program is free software: you can redistribute it and/or modify it 
under the terms of the GNU General Public License version 3, as published 
by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranties of 
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <glib.h>
#include <glib-object.h>

#include "watchdog.h"

static void
test_watchdog_create (void)
{
	g_unsetenv("HUD_SERVICE_TIMEOUT");

	/* Try with NULL */
	HudWatchdog * doggie = hud_watchdog_new(NULL);

	g_assert(IS_HUD_WATCHDOG(doggie));
	g_assert(hud_watchdog_get_timeout(doggie) == 600);

	g_clear_object(&doggie);

	/* Give it a loop */
	GMainLoop * loop = g_main_loop_new(NULL, FALSE);
	doggie = hud_watchdog_new(loop);

	g_assert(IS_HUD_WATCHDOG(doggie));
	g_assert(hud_watchdog_get_timeout(doggie) == 600);

	g_clear_object(&doggie);
	g_main_loop_unref(loop);

	/* Set the environment variable */
	g_setenv("HUD_SERVICE_TIMEOUT", "1000", TRUE);

	doggie = hud_watchdog_new(NULL);

	g_assert(IS_HUD_WATCHDOG(doggie));
	g_assert(hud_watchdog_get_timeout(doggie) == 1000);

	g_clear_object(&doggie);

	return;
}

static gboolean
final_fail (gpointer ploop)
{
	g_error("Timeout not via the watchdog.  It didn't work.");
	g_main_loop_quit((GMainLoop *)ploop);
	return FALSE;
}

static gboolean
ping_watchdog (gpointer pwatchdog)
{
	hud_watchdog_ping(pwatchdog);
	return FALSE;
}

static gboolean
hit_one_sec (gpointer pboolean)
{
	gboolean * onesec = (gboolean *)pboolean;
	*onesec = TRUE;
	return FALSE;
}

static void
test_watchdog_timing (void)
{
	HudWatchdog * doggie = NULL;
	GMainLoop * loop = NULL;

	g_setenv("HUD_SERVICE_TIMEOUT", "1", TRUE);

	/* Test base timeout */
	loop = g_main_loop_new(NULL, FALSE);
	doggie = hud_watchdog_new(loop);

	glong final = g_timeout_add_seconds(5, final_fail, loop);
	g_main_loop_run(loop);
	g_source_remove(final);

	g_clear_object(&doggie);

	/* Test a single ping */
	gboolean one_sec_hit = FALSE;
	doggie = hud_watchdog_new(loop);

	final = g_timeout_add_seconds(3, final_fail, loop);
	g_timeout_add(500, ping_watchdog, doggie);
	g_timeout_add(1000, hit_one_sec, &one_sec_hit);

	g_main_loop_run(loop);
	g_source_remove(final);

	g_assert(one_sec_hit);
	g_clear_object(&doggie);


	/* Clean up the loop */
	g_main_loop_unref(loop);

	return;
}

/* Test to ensure we can ping with a NULL watchdog so the other tests
   suites will work fine */
static void
test_watchdog_null_ping (void)
{
	hud_watchdog_ping(NULL);
	return;
}

/* Build the test suite */
static void
test_watchdog_suite (void)
{
	g_test_add_func ("/hud/watchdog/nullping", test_watchdog_null_ping);
	g_test_add_func ("/hud/watchdog/create",   test_watchdog_create);
	g_test_add_func ("/hud/watchdog/timing",   test_watchdog_timing);
	return;
}

gint
main (gint argc, gchar * argv[])
{
#ifndef GLIB_VERSION_2_36
	g_type_init ();
#endif

	g_test_init(&argc, &argv, NULL);

	/* Test suites */
	test_watchdog_suite();

	return g_test_run ();
}