Merge lp:~ted/hud/automatic-shutdown into lp:hud/phablet

Proposed by Ted Gould
Status: Merged
Approved by: Pete Woods
Approved revision: 375
Merged at revision: 373
Proposed branch: lp:~ted/hud/automatic-shutdown
Merge into: lp:hud/phablet
Prerequisite: lp:~pete-woods/hud/cmake
Diff against target: 654 lines (+414/-6)
10 files modified
src/CMakeLists.txt (+2/-0)
src/hud-service.c (+10/-1)
src/hudquery.c (+18/-2)
src/hudquery.h (+2/-0)
src/watchdog.c (+178/-0)
src/watchdog.h (+53/-0)
tests/CMakeLists.txt (+1/-0)
tests/hud-performance.c (+1/-1)
tests/test-source.c (+2/-2)
tests/test-watchdog.c (+147/-0)
To merge this branch: bzr merge lp:~ted/hud/automatic-shutdown
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Pete Woods (community) Approve
Review via email: mp+156145@code.launchpad.net

This proposal supersedes a proposal from 2013-03-26.

Commit message

Shutdown the HUD service if it's not used for 10 minutes.

Description of the change

Adds a watchdog to shutdown if it's not being used. This is to help with memory and CPU usage on mobile platforms, all services that might not be used are being asked to shutdown with this style of watchdog.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote : Posted in a previous version of this proposal
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :

FAILED: Continuous integration, rev:374
No commit message was specified in the merge proposal. Click on the following link and set the commit message (if you want a jenkins rebuild you need to trigger it yourself):
https://code.launchpad.net/~ted/hud/automatic-shutdown/+merge/156145/+edit-commit-message

http://s-jenkins:8080/job/hud-small-ci/280/
Executed test runs:
    FAILURE: http://s-jenkins:8080/job/hud-small-ci/./build=pbuilder,distribution=quantal,flavor=i386/280/console

Click here to trigger a rebuild:
http://s-jenkins:8080/job/hud-small-ci/280/rebuild

review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Pete Woods (pete-woods) wrote :

An obvious initial problem is that this is be generating artifacts into the source tree during tests. All generated artifacts must go into the binary tree.

(incomplete review)

review: Needs Fixing
lp:~ted/hud/automatic-shutdown updated
375. By Ted Gould

Removing autotools built files from the ignore file

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Pete Woods (pete-woods) :
review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/CMakeLists.txt'
2--- src/CMakeLists.txt 2013-03-27 23:46:44 +0000
3+++ src/CMakeLists.txt 2013-04-02 14:39:23 +0000
4@@ -96,6 +96,7 @@
5 pronounce-dict.h
6 query-columns.h
7 usage-tracker.h
8+watchdog.h
9 )
10
11 set(HUD_SERVICE_LIB_SOURCES
12@@ -125,6 +126,7 @@
13 load-app-info.c
14 pronounce-dict.c
15 usage-tracker.c
16+watchdog.c
17 )
18
19 add_glib_enumtypes_t(HUD_SERVICE_LIB_SOURCES
20
21=== modified file 'src/hud-service.c'
22--- src/hud-service.c 2013-03-26 17:33:39 +0000
23+++ src/hud-service.c 2013-04-02 14:39:23 +0000
24@@ -36,6 +36,7 @@
25 #include "hud-iface.h"
26 #include "shared-values.h"
27 #include "hudquery.h"
28+#include "watchdog.h"
29
30 /* Prototypes */
31 static void bus_method (GDBusConnection *connection,
32@@ -65,6 +66,7 @@
33 .set_property = NULL
34 };
35 static HudApplicationList * application_list = NULL;
36+static HudWatchdog * watchdog = NULL;
37
38 /* Get our error domain */
39 GQuark
40@@ -228,7 +230,7 @@
41 static HudQuery *
42 build_query (HudSourceList * all_sources, HudApplicationList * app_list, GDBusConnection * connection, const gchar * search_string)
43 {
44- HudQuery * query = hud_query_new (HUD_SOURCE(all_sources), application_list, search_string, 10, connection, ++query_count);
45+ HudQuery * query = hud_query_new (HUD_SOURCE(all_sources), watchdog, application_list, search_string, 10, connection, ++query_count);
46
47 g_ptr_array_add(query_list, query);
48 g_object_weak_ref(G_OBJECT(query), query_destroyed, query_list);
49@@ -263,6 +265,8 @@
50 GDBusMethodInvocation *invocation,
51 gpointer user_data)
52 {
53+ hud_watchdog_ping(watchdog);
54+
55 if (g_str_equal (method_name, "CreateQuery")) {
56 HudSourceList *all_sources = user_data;
57 GVariant * vsearch;
58@@ -385,6 +389,7 @@
59 static GVariant *
60 bus_get_prop (GDBusConnection * connection, const gchar * sender, const gchar * object_path, const gchar * interface_name, const gchar * property_name, GError ** error, gpointer user_data)
61 {
62+ hud_watchdog_ping(watchdog);
63 // HudSource *source = user_data;
64
65 if (g_str_equal(property_name, "OpenQueries")) {
66@@ -532,9 +537,13 @@
67
68 signal(SIGTERM, sigterm_graceful_exit);
69
70+ watchdog = hud_watchdog_new(mainloop);
71+
72 g_main_loop_run (mainloop);
73 g_main_loop_unref (mainloop);
74
75+ g_clear_object(&watchdog);
76+
77 g_object_unref (application_list);
78 g_object_unref (source_list);
79 g_ptr_array_free(query_list, TRUE);
80
81=== modified file 'src/hudquery.c'
82--- src/hudquery.c 2013-03-26 18:32:27 +0000
83+++ src/hudquery.c 2013-04-02 14:39:23 +0000
84@@ -30,6 +30,7 @@
85 #include "application-source.h"
86 #include "application-list.h"
87 #include "query-columns.h"
88+#include "watchdog.h"
89
90 /**
91 * SECTION:hudquery
92@@ -58,6 +59,7 @@
93 {
94 GObject parent_instance;
95
96+ HudWatchdog * watchdog;
97 HudSource *all_sources;
98 HudApplicationList *app_list;
99 HudSource *current_source;
100@@ -285,6 +287,7 @@
101 {
102 guint64 start_time;
103
104+ hud_watchdog_ping(query->watchdog);
105 start_time = g_get_monotonic_time ();
106
107 dee_model_clear(query->results_model);
108@@ -416,6 +419,7 @@
109 g_clear_pointer(&query->appstack_name, g_free);
110
111 g_clear_object(&query->voice);
112+ g_clear_object(&query->watchdog);
113
114 G_OBJECT_CLASS (hud_query_parent_class)
115 ->finalize (object);
116@@ -427,6 +431,7 @@
117 {
118 g_return_val_if_fail(HUD_IS_QUERY(user_data), FALSE);
119 HudQuery * query = HUD_QUERY(user_data);
120+ hud_watchdog_ping(query->watchdog);
121
122 g_debug("Voice query is loading");
123 hud_query_iface_com_canonical_hud_query_emit_voice_query_loading (
124@@ -484,6 +489,7 @@
125 {
126 g_return_val_if_fail(HUD_IS_QUERY(user_data), FALSE);
127 HudQuery * query = HUD_QUERY(user_data);
128+ hud_watchdog_ping(query->watchdog);
129
130 g_debug("Updating Query to: '%s'", search_string);
131
132@@ -517,6 +523,7 @@
133 {
134 g_return_val_if_fail(HUD_IS_QUERY(user_data), FALSE);
135 HudQuery * query = HUD_QUERY(user_data);
136+ hud_watchdog_ping(query->watchdog);
137
138 g_debug("Updating App to: '%s'", app_id);
139
140@@ -540,7 +547,8 @@
141 handle_execute (HudQueryIfaceComCanonicalHudQuery * skel, GDBusMethodInvocation * invocation, GVariant * command_id, guint timestamp, gpointer user_data)
142 {
143 g_return_val_if_fail(HUD_IS_QUERY(user_data), FALSE);
144- //HudQuery * query = HUD_QUERY(user_data);
145+ HudQuery * query = HUD_QUERY(user_data);
146+ hud_watchdog_ping(query->watchdog);
147
148 /* Do good */
149 GVariant * inner = g_variant_get_variant(command_id);
150@@ -578,7 +586,8 @@
151 handle_parameterized (HudQueryIfaceComCanonicalHudQuery * skel, GDBusMethodInvocation * invocation, GVariant * command_id, guint timestamp, gpointer user_data)
152 {
153 g_return_val_if_fail(HUD_IS_QUERY(user_data), FALSE);
154- //HudQuery * query = HUD_QUERY(user_data);
155+ HudQuery * query = HUD_QUERY(user_data);
156+ hud_watchdog_ping(query->watchdog);
157
158 GVariant * inner = g_variant_get_variant(command_id);
159 guint64 id = g_variant_get_uint64(inner);
160@@ -630,6 +639,7 @@
161 {
162 g_return_val_if_fail(HUD_IS_QUERY(user_data), FALSE);
163 HudQuery * query = HUD_QUERY(user_data);
164+ hud_watchdog_ping(query->watchdog);
165
166 HudClientQueryToolbarItems item = hud_client_query_toolbar_items_get_value_from_nick(arg_item);
167
168@@ -681,6 +691,7 @@
169 {
170 g_return_val_if_fail(HUD_IS_QUERY(user_data), FALSE);
171 HudQuery * query = HUD_QUERY(user_data);
172+ hud_watchdog_ping(query->watchdog);
173
174 /* Close the query */
175 hud_query_close(query);
176@@ -791,6 +802,7 @@
177 **/
178 HudQuery *
179 hud_query_new (HudSource *all_sources,
180+ HudWatchdog *watchdog,
181 HudApplicationList *application_list,
182 const gchar *search_string,
183 gint num_results,
184@@ -807,6 +819,10 @@
185 query->app_list = g_object_ref (application_list);
186 query->search_string = g_strdup (search_string);
187 query->token_list = NULL;
188+
189+ if (watchdog != NULL) {
190+ query->watchdog = g_object_ref(watchdog);
191+ }
192
193 if (query->search_string[0] != '\0') {
194 query->token_list = hud_token_list_new_from_string (query->search_string);
195
196=== modified file 'src/hudquery.h'
197--- src/hudquery.h 2013-02-27 17:35:32 +0000
198+++ src/hudquery.h 2013-04-02 14:39:23 +0000
199@@ -22,6 +22,7 @@
200 #include "hudsource.h"
201 #include "hudresult.h"
202 #include "application-list.h"
203+#include "watchdog.h"
204
205 #define HUD_TYPE_QUERY (hud_query_get_type ())
206 #define HUD_QUERY(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
207@@ -36,6 +37,7 @@
208 GType hud_query_get_type (void);
209
210 HudQuery * hud_query_new (HudSource *all_sources,
211+ HudWatchdog * watchdog,
212 HudApplicationList * application_list,
213 const gchar *search_string,
214 gint num_results,
215
216=== added file 'src/watchdog.c'
217--- src/watchdog.c 1970-01-01 00:00:00 +0000
218+++ src/watchdog.c 2013-04-02 14:39:23 +0000
219@@ -0,0 +1,178 @@
220+/*
221+ * Copyright © 2013 Canonical Ltd.
222+ *
223+ * This program is free software: you can redistribute it and/or modify it
224+ * under the terms of the GNU General Public License version 3, as
225+ * published by the Free Software Foundation.
226+ *
227+ * This program is distributed in the hope that it will be useful, but
228+ * WITHOUT ANY WARRANTY; without even the implied warranties of
229+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
230+ * PURPOSE. See the GNU General Public License for more details.
231+ *
232+ * You should have received a copy of the GNU General Public License along
233+ * with this program. If not, see <http://www.gnu.org/licenses/>.
234+ *
235+ */
236+
237+#ifdef HAVE_CONFIG_H
238+#include "config.h"
239+#endif
240+
241+#include <stdlib.h>
242+#include "watchdog.h"
243+
244+#define DEFAULT_TIMEOUT 600 /* seconds, or 10 minutes */
245+
246+typedef struct _HudWatchdogPrivate HudWatchdogPrivate;
247+struct _HudWatchdogPrivate {
248+ guint timeout;
249+ gulong timer;
250+ GMainLoop * loop;
251+};
252+
253+#define HUD_WATCHDOG_GET_PRIVATE(o) \
254+(G_TYPE_INSTANCE_GET_PRIVATE ((o), HUD_WATCHDOG_TYPE, HudWatchdogPrivate))
255+
256+static void hud_watchdog_class_init (HudWatchdogClass *klass);
257+static void hud_watchdog_init (HudWatchdog *self);
258+static void hud_watchdog_dispose (GObject *object);
259+static void hud_watchdog_finalize (GObject *object);
260+static gboolean fire_watchdog (gpointer user_data);
261+
262+G_DEFINE_TYPE (HudWatchdog, hud_watchdog, G_TYPE_OBJECT);
263+
264+static void
265+hud_watchdog_class_init (HudWatchdogClass *klass)
266+{
267+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
268+
269+ g_type_class_add_private (klass, sizeof (HudWatchdogPrivate));
270+
271+ object_class->dispose = hud_watchdog_dispose;
272+ object_class->finalize = hud_watchdog_finalize;
273+
274+ return;
275+}
276+
277+static void
278+hud_watchdog_init (HudWatchdog *self)
279+{
280+ self->priv = HUD_WATCHDOG_GET_PRIVATE(self);
281+
282+ const gchar * envvar = g_getenv("HUD_SERVICE_TIMEOUT");
283+ if (envvar == NULL) {
284+ self->priv->timeout = DEFAULT_TIMEOUT;
285+ } else {
286+ self->priv->timeout = atoi(envvar);
287+ }
288+
289+ if (self->priv->timeout != 0) {
290+ self->priv->timer = g_timeout_add_seconds(self->priv->timeout, fire_watchdog, self);
291+ }
292+
293+ return;
294+}
295+
296+static void
297+hud_watchdog_dispose (GObject *object)
298+{
299+ HudWatchdog * self = HUD_WATCHDOG(object);
300+
301+ if (self->priv->timer != 0) {
302+ g_source_remove(self->priv->timer);
303+ self->priv->timer = 0;
304+ }
305+
306+ G_OBJECT_CLASS (hud_watchdog_parent_class)->dispose (object);
307+ return;
308+}
309+
310+static void
311+hud_watchdog_finalize (GObject *object)
312+{
313+
314+ G_OBJECT_CLASS (hud_watchdog_parent_class)->finalize (object);
315+ return;
316+}
317+
318+/* Oh noes! It's our time to go! Do this! */
319+static gboolean
320+fire_watchdog (gpointer user_data)
321+{
322+ g_return_val_if_fail(IS_HUD_WATCHDOG(user_data), TRUE);
323+ HudWatchdog * self = HUD_WATCHDOG(user_data);
324+
325+ g_debug("Firing Watchdog");
326+
327+ if (self->priv->loop != NULL) {
328+ g_main_loop_quit(self->priv->loop);
329+ }
330+
331+ return FALSE;
332+}
333+
334+/**
335+ * hud_watchdog_new:
336+ * @loop: Mainloop to quit if we timeout
337+ *
338+ * Sets up a watchdog that will quit on the main loop if it
339+ * doesn't get enough attention. Reminds of an girlfriend
340+ * I had once...
341+ *
342+ * Return value: (transfer full): A new #HudWatchdog object
343+ */
344+HudWatchdog *
345+hud_watchdog_new (GMainLoop * loop)
346+{
347+ HudWatchdog * watchdog = g_object_new(HUD_WATCHDOG_TYPE, NULL);
348+
349+ watchdog->priv->loop = loop;
350+
351+ return watchdog;
352+}
353+
354+/**
355+ * hud_watchdog_ping:
356+ * @watchdog: Watchdog to give attention to
357+ *
358+ * Makes sure to startover and not timeout.
359+ */
360+void
361+hud_watchdog_ping (HudWatchdog * watchdog)
362+{
363+ /* Doing a silent fail on NULL so that our tests can not worry about
364+ setting up dummy watchdogs when just testing the query. They have
365+ their own timeouts */
366+ if (watchdog == NULL) {
367+ return;
368+ }
369+
370+ g_return_if_fail(IS_HUD_WATCHDOG(watchdog));
371+
372+ if (watchdog->priv->timer != 0) {
373+ g_source_remove(watchdog->priv->timer);
374+ watchdog->priv->timer = 0;
375+ }
376+
377+ if (watchdog->priv->timeout != 0) {
378+ watchdog->priv->timer = g_timeout_add_seconds(watchdog->priv->timeout, fire_watchdog, watchdog);
379+ }
380+ return;
381+}
382+
383+/**
384+ * hud_watchdog_get_timeout:
385+ * @watchdog: Watchdog to interegate
386+ *
387+ * Get the timeout of this watchdog.
388+ *
389+ * Return value: The number of seconds before it goes off
390+ */
391+guint
392+hud_watchdog_get_timeout (HudWatchdog * watchdog)
393+{
394+ g_return_val_if_fail(IS_HUD_WATCHDOG(watchdog), 0);
395+
396+ return watchdog->priv->timeout;
397+}
398
399=== added file 'src/watchdog.h'
400--- src/watchdog.h 1970-01-01 00:00:00 +0000
401+++ src/watchdog.h 2013-04-02 14:39:23 +0000
402@@ -0,0 +1,53 @@
403+/*
404+ * Copyright © 2013 Canonical Ltd.
405+ *
406+ * This program is free software: you can redistribute it and/or modify it
407+ * under the terms of the GNU General Public License version 3, as
408+ * published by the Free Software Foundation.
409+ *
410+ * This program is distributed in the hope that it will be useful, but
411+ * WITHOUT ANY WARRANTY; without even the implied warranties of
412+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
413+ * PURPOSE. See the GNU General Public License for more details.
414+ *
415+ * You should have received a copy of the GNU General Public License along
416+ * with this program. If not, see <http://www.gnu.org/licenses/>.
417+ *
418+ */
419+
420+#ifndef __HUD_WATCHDOG_H__
421+#define __HUD_WATCHDOG_H__
422+
423+#include <glib-object.h>
424+
425+G_BEGIN_DECLS
426+
427+#define HUD_WATCHDOG_TYPE (hud_watchdog_get_type ())
428+#define HUD_WATCHDOG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), HUD_WATCHDOG_TYPE, HudWatchdog))
429+#define HUD_WATCHDOG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), HUD_WATCHDOG_TYPE, HudWatchdogClass))
430+#define IS_HUD_WATCHDOG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), HUD_WATCHDOG_TYPE))
431+#define IS_HUD_WATCHDOG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), HUD_WATCHDOG_TYPE))
432+#define HUD_WATCHDOG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), HUD_WATCHDOG_TYPE, HudWatchdogClass))
433+
434+typedef struct _HudWatchdog HudWatchdog;
435+typedef struct _HudWatchdogClass HudWatchdogClass;
436+typedef struct _HudWatchdogPrivate HudWatchdogPrivate;
437+
438+struct _HudWatchdogClass {
439+ GObjectClass parent_class;
440+};
441+
442+struct _HudWatchdog {
443+ GObject parent;
444+ HudWatchdogPrivate * priv;
445+};
446+
447+GType hud_watchdog_get_type (void);
448+
449+HudWatchdog * hud_watchdog_new (GMainLoop * loop);
450+void hud_watchdog_ping (HudWatchdog * watchdog);
451+guint hud_watchdog_get_timeout (HudWatchdog * watchdog);
452+
453+G_END_DECLS
454+
455+#endif
456
457=== modified file 'tests/CMakeLists.txt'
458--- tests/CMakeLists.txt 2013-04-02 08:34:53 +0000
459+++ tests/CMakeLists.txt 2013-04-02 14:39:23 +0000
460@@ -129,6 +129,7 @@
461 # HUD Test Suite
462 #######################
463
464+hudtest(test-watchdog test-watchdog.c test-watchdog.xml)
465 hudtest(test-distance test-distance.c test-distance.xml)
466 hudtest(test-hud-item test-huditem.c test-hud-item.xml)
467 hudtest(test-result-highlighting test-result-highlighting.c test-result-highlighting.xml)
468
469=== modified file 'tests/hud-performance.c'
470--- tests/hud-performance.c 2013-03-19 01:19:32 +0000
471+++ tests/hud-performance.c 2013-04-02 14:39:23 +0000
472@@ -63,7 +63,7 @@
473 gchar *part_search = g_strndup (search, j);
474
475 start_time = g_get_monotonic_time ();
476- query = hud_query_new (source, NULL, part_search, 1u<<30, g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL), 0);
477+ query = hud_query_new (source, NULL, NULL, part_search, 1u<<30, g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL), 0);
478 g_print ("%-60s: %dus\n", part_search,
479 (int) (g_get_monotonic_time () - start_time));
480 g_object_unref (query);
481
482=== modified file 'tests/test-source.c'
483--- tests/test-source.c 2013-03-26 17:33:39 +0000
484+++ tests/test-source.c 2013-04-02 14:39:23 +0000
485@@ -193,7 +193,7 @@
486 {
487 g_debug ("query: [%s], on [%s]", search, g_dbus_connection_get_unique_name(session));
488
489- HudQuery * query = hud_query_new (source, list, search, 1u << 30, session, query_count);
490+ HudQuery * query = hud_query_new (source, NULL, list, search, 1u << 30, session, query_count);
491
492 return query;
493 }
494@@ -355,7 +355,7 @@
495 g_assert(G_N_ELEMENTS(expected) == G_N_ELEMENTS(expected_distances));
496
497 AppListDummy * dummy = app_list_dummy_new(HUD_SOURCE(collector));
498- HudQuery *query = hud_query_new (HUD_SOURCE(source_list), HUD_APPLICATION_LIST(dummy), search, 1u << 30, session, 6);
499+ HudQuery *query = hud_query_new (HUD_SOURCE(source_list), NULL, HUD_APPLICATION_LIST(dummy), search, 1u << 30, session, 6);
500 test_source_make_assertions_ext (query, appstack, appstack_expected_ids, appstack_expected_icons, G_N_ELEMENTS(appstack_expected_ids), path, name, expected, expected_distances, G_N_ELEMENTS(expected));
501
502 // Change the app to the manual_source
503
504=== added file 'tests/test-watchdog.c'
505--- tests/test-watchdog.c 1970-01-01 00:00:00 +0000
506+++ tests/test-watchdog.c 2013-04-02 14:39:23 +0000
507@@ -0,0 +1,147 @@
508+/*
509+Test code for watchdog
510+
511+Copyright 2013 Canonical Ltd.
512+
513+Authors:
514+ Ted Gould <ted@canonical.com>
515+
516+This program is free software: you can redistribute it and/or modify it
517+under the terms of the GNU General Public License version 3, as published
518+by the Free Software Foundation.
519+
520+This program is distributed in the hope that it will be useful, but
521+WITHOUT ANY WARRANTY; without even the implied warranties of
522+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
523+PURPOSE. See the GNU General Public License for more details.
524+
525+You should have received a copy of the GNU General Public License along
526+with this program. If not, see <http://www.gnu.org/licenses/>.
527+*/
528+
529+#include <glib.h>
530+#include <glib-object.h>
531+
532+#include "watchdog.h"
533+
534+static void
535+test_watchdog_create (void)
536+{
537+ g_unsetenv("HUD_SERVICE_TIMEOUT");
538+
539+ /* Try with NULL */
540+ HudWatchdog * doggie = hud_watchdog_new(NULL);
541+
542+ g_assert(IS_HUD_WATCHDOG(doggie));
543+ g_assert(hud_watchdog_get_timeout(doggie) == 600);
544+
545+ g_clear_object(&doggie);
546+
547+ /* Give it a loop */
548+ GMainLoop * loop = g_main_loop_new(NULL, FALSE);
549+ doggie = hud_watchdog_new(loop);
550+
551+ g_assert(IS_HUD_WATCHDOG(doggie));
552+ g_assert(hud_watchdog_get_timeout(doggie) == 600);
553+
554+ g_clear_object(&doggie);
555+ g_main_loop_unref(loop);
556+
557+ /* Set the environment variable */
558+ g_setenv("HUD_SERVICE_TIMEOUT", "1000", TRUE);
559+
560+ doggie = hud_watchdog_new(NULL);
561+
562+ g_assert(IS_HUD_WATCHDOG(doggie));
563+ g_assert(hud_watchdog_get_timeout(doggie) == 1000);
564+
565+ g_clear_object(&doggie);
566+
567+ return;
568+}
569+
570+static gboolean
571+final_fail (gpointer ploop)
572+{
573+ g_error("Timeout not via the watchdog. It didn't work.");
574+ g_main_loop_quit((GMainLoop *)ploop);
575+ return FALSE;
576+}
577+
578+static gboolean
579+ping_watchdog (gpointer pwatchdog)
580+{
581+ hud_watchdog_ping(pwatchdog);
582+ return FALSE;
583+}
584+
585+static gboolean
586+hit_one_sec (gpointer pboolean)
587+{
588+ gboolean * onesec = (gboolean *)pboolean;
589+ *onesec = TRUE;
590+ return FALSE;
591+}
592+
593+static void
594+test_watchdog_timing (void)
595+{
596+ HudWatchdog * doggie = NULL;
597+ GMainLoop * loop = NULL;
598+
599+ g_setenv("HUD_SERVICE_TIMEOUT", "1", TRUE);
600+
601+ /* Test base timeout */
602+ loop = g_main_loop_new(NULL, FALSE);
603+ doggie = hud_watchdog_new(loop);
604+
605+ glong final = g_timeout_add_seconds(5, final_fail, loop);
606+ g_main_loop_run(loop);
607+ g_source_remove(final);
608+
609+ g_clear_object(&doggie);
610+
611+ /* Test a single ping */
612+ gboolean one_sec_hit = FALSE;
613+ doggie = hud_watchdog_new(loop);
614+
615+ final = g_timeout_add_seconds(3, final_fail, loop);
616+ g_timeout_add(500, ping_watchdog, doggie);
617+ g_timeout_add(1000, hit_one_sec, &one_sec_hit);
618+
619+ g_main_loop_run(loop);
620+ g_source_remove(final);
621+
622+ g_assert(one_sec_hit);
623+ g_clear_object(&doggie);
624+
625+
626+ /* Clean up the loop */
627+ g_main_loop_unref(loop);
628+
629+ return;
630+}
631+
632+/* Build the test suite */
633+static void
634+test_watchdog_suite (void)
635+{
636+ g_test_add_func ("/hud/watchdog/create", test_watchdog_create);
637+ g_test_add_func ("/hud/watchdog/timing", test_watchdog_timing);
638+ return;
639+}
640+
641+gint
642+main (gint argc, gchar * argv[])
643+{
644+#ifndef GLIB_VERSION_2_36
645+ g_type_init ();
646+#endif
647+
648+ g_test_init(&argc, &argv, NULL);
649+
650+ /* Test suites */
651+ test_watchdog_suite();
652+
653+ return g_test_run ();
654+}

Subscribers

People subscribed via source and target branches