de05d6c69623c0048862635a849bd338168518fa
[oweals/gnunet.git] / src / statistics / test_statistics_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file statistics/test_statistics_api.c
22  * @brief testcase for statistics_api.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_getopt_lib.h"
27 #include "gnunet_os_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_scheduler_lib.h"
30 #include "gnunet_statistics_service.h"
31
32 #define DEBUG_STATISTICS GNUNET_NO
33
34 #define START_SERVICE GNUNET_YES
35
36 static int
37 check_1 (void *cls,
38          const char *subsystem,
39          const char *name, uint64_t value, int is_persistent)
40 {
41   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
42               "Received value %llu for `%s:%s\n",
43               (unsigned long long) value, subsystem, name);
44   GNUNET_assert (0 == strcmp (name, "test-1"));
45   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
46   GNUNET_assert (value == 1);
47   GNUNET_assert (is_persistent == GNUNET_NO);
48   return GNUNET_OK;
49 }
50
51 static int
52 check_2 (void *cls,
53          const char *subsystem,
54          const char *name, uint64_t value, int is_persistent)
55 {
56   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
57               "Received value %llu for `%s:%s\n",
58               (unsigned long long) value, subsystem, name);
59   GNUNET_assert (0 == strcmp (name, "test-2"));
60   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
61   GNUNET_assert (value == 2);
62   GNUNET_assert (is_persistent == GNUNET_NO);
63   return GNUNET_OK;
64 }
65
66 static int
67 check_3 (void *cls,
68          const char *subsystem,
69          const char *name, uint64_t value, int is_persistent)
70 {
71   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
72               "Received value %llu for `%s:%s\n",
73               (unsigned long long) value, subsystem, name);
74   GNUNET_assert (0 == strcmp (name, "test-3"));
75   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
76   GNUNET_assert (value == 3);
77   GNUNET_assert (is_persistent == GNUNET_YES);
78   return GNUNET_OK;
79 }
80
81 static struct GNUNET_STATISTICS_Handle *h;
82
83 static void
84 next_fin (void *cls, int success)
85 {
86   int *ok = cls;
87
88   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
89   GNUNET_assert (success == GNUNET_OK);
90   *ok = 0;
91 }
92
93 static void
94 next (void *cls, int success)
95 {
96   GNUNET_assert (success == GNUNET_OK);
97   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Issuing GET request\n");
98   GNUNET_break (NULL !=
99                 GNUNET_STATISTICS_get (h, NULL, "test-2",
100                                        GNUNET_TIME_UNIT_SECONDS, &next_fin,
101                                        &check_2, cls));
102 }
103
104 static void
105 run (void *cls,
106      char *const *args,
107      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
108 {
109   h = GNUNET_STATISTICS_create ("test-statistics-api", cfg);
110   GNUNET_STATISTICS_set (h, "test-1", 1, GNUNET_NO);
111   GNUNET_STATISTICS_set (h, "test-2", 2, GNUNET_NO);
112   GNUNET_STATISTICS_set (h, "test-3", 2, GNUNET_NO);
113   GNUNET_STATISTICS_update (h, "test-3", 1, GNUNET_YES);
114   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Issuing GET request\n");
115   GNUNET_break (NULL !=
116                 GNUNET_STATISTICS_get (h, NULL, "test-1",
117                                        GNUNET_TIME_UNIT_SECONDS, &next,
118                                        &check_1, cls));
119 }
120
121 static void
122 run_more (void *cls,
123           char *const *args,
124           const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
125 {
126   h = GNUNET_STATISTICS_create ("test-statistics-api", cfg);
127   GNUNET_break (NULL !=
128                 GNUNET_STATISTICS_get (h, NULL, "test-3",
129                                        GNUNET_TIME_UNIT_SECONDS, &next_fin,
130                                        &check_3, cls));
131 }
132
133 static int
134 check ()
135 {
136   int ok = 1;
137
138   char *const argv[] = { "test-statistics-api",
139     "-c",
140     "test_statistics_api_data.conf",
141 #if DEBUG_STATISTICS
142     "-L", "DEBUG",
143 #else
144     "-L", "WARNING",
145 #endif
146     NULL
147   };
148   struct GNUNET_GETOPT_CommandLineOption options[] = {
149     GNUNET_GETOPT_OPTION_END
150   };
151 #if START_SERVICE
152   struct GNUNET_OS_Process *proc;
153
154   proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
155                                   "gnunet-service-statistics",
156 #if DEBUG_STATISTICS
157                                   "-L", "DEBUG",
158 #endif
159                                   "-c", "test_statistics_api_data.conf", NULL);
160 #endif
161   GNUNET_assert (NULL != proc);
162   GNUNET_PROGRAM_run (5, argv, "test-statistics-api", "nohelp",
163                       options, &run, &ok);
164 #if START_SERVICE
165   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
166   {
167     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
168     ok = 1;
169   }
170   GNUNET_OS_process_wait (proc);
171   GNUNET_OS_process_close (proc);
172   proc = NULL;
173 #endif
174   if (ok != 0)
175     return ok;
176   ok = 1;
177 #if START_SERVICE
178   /* restart to check persistence! */
179   proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
180                                   "gnunet-service-statistics",
181 #if DEBUG_STATISTICS
182                                   "-L", "DEBUG",
183 #endif
184                                   "-c", "test_statistics_api_data.conf", NULL);
185 #endif
186   GNUNET_PROGRAM_run (5, argv, "test-statistics-api", "nohelp",
187                       options, &run_more, &ok);
188 #if START_SERVICE
189   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
190   {
191     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
192     ok = 1;
193   }
194   GNUNET_OS_process_wait (proc);
195   GNUNET_OS_process_close (proc);
196   proc = NULL;
197 #endif
198   return ok;
199 }
200
201 int
202 main (int argc, char *argv[])
203 {
204   int ret;
205
206   GNUNET_log_setup ("test_statistics_api",
207 #if DEBUG_STATISTICS
208                     "DEBUG",
209 #else
210                     "WARNING",
211 #endif
212                     NULL);
213   ret = check ();
214
215   return ret;
216 }
217
218 /* end of test_statistics_api.c */