ced14e6b5f5db528f0d4ea39963457de966c900f
[oweals/gnunet.git] / src / statistics / test_statistics_api_watch.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2011, 2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file statistics/test_statistics_api_watch.c
20  * @brief testcase for statistics_api.c watch functions
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_statistics_service.h"
26
27
28 static int ok;
29
30 static struct GNUNET_STATISTICS_Handle *h;
31
32 static struct GNUNET_STATISTICS_Handle *h2;
33
34 static struct GNUNET_SCHEDULER_Task *shutdown_task;
35
36
37 static void
38 force_shutdown (void *cls)
39 {
40   fprintf (stderr, "Timeout, failed to receive notifications: %d\n", ok);
41   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
42   GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
43   ok = 7;
44 }
45
46
47 static void
48 normal_shutdown (void *cls)
49 {
50   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
51   GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
52 }
53
54
55 static int
56 watch_1 (void *cls,
57          const char *subsystem,
58          const char *name,
59          uint64_t value,
60          int is_persistent)
61 {
62   GNUNET_assert (value == 42);
63   GNUNET_assert (0 == strcmp (name, "test-1"));
64   ok &= ~1;
65   if (0 == ok)
66   {
67     GNUNET_SCHEDULER_cancel (shutdown_task);
68     GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
69   }
70   return GNUNET_OK;
71 }
72
73
74 static int
75 watch_2 (void *cls,
76          const char *subsystem,
77          const char *name,
78          uint64_t value,
79          int is_persistent)
80 {
81   GNUNET_assert (value == 43);
82   GNUNET_assert (0 == strcmp (name, "test-2"));
83   ok &= ~2;
84   if (0 == ok)
85   {
86     GNUNET_SCHEDULER_cancel (shutdown_task);
87     GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
88   }
89   return GNUNET_OK;
90 }
91
92
93 static void
94 run (void *cls,
95      char *const *args,
96      const char *cfgfile,
97      const struct GNUNET_CONFIGURATION_Handle *cfg)
98 {
99   h = GNUNET_STATISTICS_create ("dummy", cfg);
100   GNUNET_assert (GNUNET_OK ==
101                  GNUNET_STATISTICS_watch (h, "test-statistics-api-watch",
102                                           "test-1", &watch_1, NULL));
103   GNUNET_assert (GNUNET_OK ==
104                  GNUNET_STATISTICS_watch (h, "test-statistics-api-watch",
105                                           "test-2", &watch_2, NULL));
106   h2 = GNUNET_STATISTICS_create ("test-statistics-api-watch", cfg);
107   GNUNET_STATISTICS_set (h2, "test-1", 42, GNUNET_NO);
108   GNUNET_STATISTICS_set (h2, "test-2", 43, GNUNET_NO);
109   shutdown_task =
110       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
111                                     &force_shutdown,
112                                     NULL);
113 }
114
115
116 int
117 main (int argc, char *argv_ign[])
118 {
119   char *const argv[] = { "test-statistics-api",
120     "-c",
121     "test_statistics_api_data.conf",
122     NULL
123   };
124   struct GNUNET_GETOPT_CommandLineOption options[] = {
125     GNUNET_GETOPT_OPTION_END
126   };
127   struct GNUNET_OS_Process *proc;
128   char *binary;
129
130   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics");
131   proc =
132     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
133                              NULL, NULL, NULL,
134                              binary,
135                              "gnunet-service-statistics",
136                              "-c", "test_statistics_api_data.conf", NULL);
137   GNUNET_assert (NULL != proc);
138   ok = 3;
139   GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp", options, &run,
140                       NULL);
141   if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
142   {
143     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
144     ok = 1;
145   }
146   GNUNET_OS_process_wait (proc);
147   GNUNET_OS_process_destroy (proc);
148   proc = NULL;
149   GNUNET_free (binary);
150   return ok;
151 }
152
153
154 /* end of test_statistics_api_watch.c */