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

« back to all changes in this revision

Viewing changes to tests/test-watchdog.c

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2013-06-05 12:33:44 UTC
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: package-import@ubuntu.com-20130605123344-cpp4to647tyfv7kr
Tags: upstream-13.10.1daily13.06.05.1
ImportĀ upstreamĀ versionĀ 13.10.1daily13.06.05.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Test code for watchdog
 
3
 
 
4
Copyright 2013 Canonical Ltd.
 
5
 
 
6
Authors:
 
7
    Ted Gould <ted@canonical.com>
 
8
 
 
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.
 
12
 
 
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.
 
17
 
 
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/>.
 
20
*/
 
21
 
 
22
#include <glib.h>
 
23
#include <glib-object.h>
 
24
 
 
25
#include "watchdog.h"
 
26
 
 
27
static void
 
28
test_watchdog_create (void)
 
29
{
 
30
        g_unsetenv("HUD_SERVICE_TIMEOUT");
 
31
 
 
32
        /* Try with NULL */
 
33
        HudWatchdog * doggie = hud_watchdog_new(NULL);
 
34
 
 
35
        g_assert(IS_HUD_WATCHDOG(doggie));
 
36
        g_assert(hud_watchdog_get_timeout(doggie) == 600);
 
37
 
 
38
        g_clear_object(&doggie);
 
39
 
 
40
        /* Give it a loop */
 
41
        GMainLoop * loop = g_main_loop_new(NULL, FALSE);
 
42
        doggie = hud_watchdog_new(loop);
 
43
 
 
44
        g_assert(IS_HUD_WATCHDOG(doggie));
 
45
        g_assert(hud_watchdog_get_timeout(doggie) == 600);
 
46
 
 
47
        g_clear_object(&doggie);
 
48
        g_main_loop_unref(loop);
 
49
 
 
50
        /* Set the environment variable */
 
51
        g_setenv("HUD_SERVICE_TIMEOUT", "1000", TRUE);
 
52
 
 
53
        doggie = hud_watchdog_new(NULL);
 
54
 
 
55
        g_assert(IS_HUD_WATCHDOG(doggie));
 
56
        g_assert(hud_watchdog_get_timeout(doggie) == 1000);
 
57
 
 
58
        g_clear_object(&doggie);
 
59
 
 
60
        return;
 
61
}
 
62
 
 
63
static gboolean
 
64
final_fail (gpointer ploop)
 
65
{
 
66
        g_error("Timeout not via the watchdog.  It didn't work.");
 
67
        g_main_loop_quit((GMainLoop *)ploop);
 
68
        return FALSE;
 
69
}
 
70
 
 
71
static gboolean
 
72
ping_watchdog (gpointer pwatchdog)
 
73
{
 
74
        hud_watchdog_ping(pwatchdog);
 
75
        return FALSE;
 
76
}
 
77
 
 
78
static gboolean
 
79
hit_one_sec (gpointer pboolean)
 
80
{
 
81
        gboolean * onesec = (gboolean *)pboolean;
 
82
        *onesec = TRUE;
 
83
        return FALSE;
 
84
}
 
85
 
 
86
static void
 
87
test_watchdog_timing (void)
 
88
{
 
89
        HudWatchdog * doggie = NULL;
 
90
        GMainLoop * loop = NULL;
 
91
 
 
92
        g_setenv("HUD_SERVICE_TIMEOUT", "1", TRUE);
 
93
 
 
94
        /* Test base timeout */
 
95
        loop = g_main_loop_new(NULL, FALSE);
 
96
        doggie = hud_watchdog_new(loop);
 
97
 
 
98
        glong final = g_timeout_add_seconds(5, final_fail, loop);
 
99
        g_main_loop_run(loop);
 
100
        g_source_remove(final);
 
101
 
 
102
        g_clear_object(&doggie);
 
103
 
 
104
        /* Test a single ping */
 
105
        gboolean one_sec_hit = FALSE;
 
106
        doggie = hud_watchdog_new(loop);
 
107
 
 
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);
 
111
 
 
112
        g_main_loop_run(loop);
 
113
        g_source_remove(final);
 
114
 
 
115
        g_assert(one_sec_hit);
 
116
        g_clear_object(&doggie);
 
117
 
 
118
 
 
119
        /* Clean up the loop */
 
120
        g_main_loop_unref(loop);
 
121
 
 
122
        return;
 
123
}
 
124
 
 
125
/* Test to ensure we can ping with a NULL watchdog so the other tests
 
126
   suites will work fine */
 
127
static void
 
128
test_watchdog_null_ping (void)
 
129
{
 
130
        hud_watchdog_ping(NULL);
 
131
        return;
 
132
}
 
133
 
 
134
/* Build the test suite */
 
135
static void
 
136
test_watchdog_suite (void)
 
137
{
 
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);
 
141
        return;
 
142
}
 
143
 
 
144
gint
 
145
main (gint argc, gchar * argv[])
 
146
{
 
147
#ifndef GLIB_VERSION_2_36
 
148
        g_type_init ();
 
149
#endif
 
150
 
 
151
        g_test_init(&argc, &argv, NULL);
 
152
 
 
153
        /* Test suites */
 
154
        test_watchdog_suite();
 
155
 
 
156
        return g_test_run ();
 
157
}