not needed
[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,
44               subsystem,
45               name);
46   GNUNET_assert (0 == strcmp (name, "test-1"));
47   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
48   GNUNET_assert (value == 1);
49   GNUNET_assert (is_persistent == GNUNET_NO);
50   return GNUNET_OK;
51 }
52
53 static int
54 check_2 (void *cls,
55          const char *subsystem,
56          const char *name, uint64_t value, int is_persistent)
57 {
58   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
59               "Received value %llu for `%s:%s\n",
60               (unsigned long long) value,
61               subsystem,
62               name);
63   GNUNET_assert (0 == strcmp (name, "test-2"));
64   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
65   GNUNET_assert (value == 2);
66   GNUNET_assert (is_persistent == GNUNET_NO);
67   return GNUNET_OK;
68 }
69
70 static int
71 check_3 (void *cls,
72          const char *subsystem,
73          const char *name, uint64_t value, int is_persistent)
74 {
75   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
76               "Received value %llu for `%s:%s\n",
77               (unsigned long long) value,
78               subsystem,
79               name);
80   GNUNET_assert (0 == strcmp (name, "test-3"));
81   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
82   GNUNET_assert (value == 3);
83   GNUNET_assert (is_persistent == GNUNET_YES);
84   return GNUNET_OK;
85 }
86
87 static struct GNUNET_STATISTICS_Handle *h;
88
89 static void
90 next_fin (void *cls, int success)
91 {
92   int *ok = cls;
93
94   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
95   GNUNET_assert (success == GNUNET_OK);
96   *ok = 0;
97 }
98
99 static void
100 next (void *cls, int success)
101 {
102   GNUNET_assert (success == GNUNET_OK);
103   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
104               "Issuing GET request\n");       
105   GNUNET_break (NULL !=
106                 GNUNET_STATISTICS_get (h, NULL, "test-2",
107                                        GNUNET_TIME_UNIT_SECONDS, &next_fin, &check_2, cls));
108 }
109
110 static void
111 run (void *cls,
112      char *const *args,
113      const char *cfgfile,
114      const struct GNUNET_CONFIGURATION_Handle *cfg)
115 {
116   h = GNUNET_STATISTICS_create ("test-statistics-api", cfg);
117   GNUNET_STATISTICS_set (h, "test-1", 1, GNUNET_NO);
118   GNUNET_STATISTICS_set (h, "test-2", 2, GNUNET_NO);
119   GNUNET_STATISTICS_set (h, "test-3", 2, GNUNET_NO);
120   GNUNET_STATISTICS_update (h, "test-3", 1, GNUNET_YES);
121   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
122               "Issuing GET request\n");       
123   GNUNET_break (NULL != 
124                 GNUNET_STATISTICS_get (h, NULL, "test-1",
125                                        GNUNET_TIME_UNIT_SECONDS, &next, &check_1, cls));
126 }
127
128 static void
129 run_more (void *cls,
130           char *const *args,
131           const char *cfgfile,
132           const struct GNUNET_CONFIGURATION_Handle *cfg)
133 {
134   h = GNUNET_STATISTICS_create ("test-statistics-api", cfg);
135   GNUNET_break (NULL !=
136                 GNUNET_STATISTICS_get (h, NULL, "test-3",
137                                        GNUNET_TIME_UNIT_SECONDS, &next_fin, &check_3, cls));
138 }
139
140 static int
141 check ()
142 {
143   int ok = 1;
144   char *const argv[] = { "test-statistics-api",
145     "-c",
146     "test_statistics_api_data.conf",
147 #if DEBUG_STATISTICS
148                          "-L", "DEBUG",
149 #else
150                          "-L", "WARNING",
151 #endif
152     NULL
153   };
154   struct GNUNET_GETOPT_CommandLineOption options[] = {
155     GNUNET_GETOPT_OPTION_END
156   };
157 #if START_SERVICE
158   struct GNUNET_OS_Process *proc;
159   proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
160                                  "gnunet-service-statistics",
161 #if DEBUG_STATISTICS
162                                  "-L", "DEBUG",
163 #endif
164                                  "-c", "test_statistics_api_data.conf", NULL);
165 #endif
166   GNUNET_assert (NULL != proc);
167   GNUNET_PROGRAM_run (5, argv, "test-statistics-api", "nohelp",
168                       options, &run, &ok);
169 #if START_SERVICE
170   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
171     {
172       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
173       ok = 1;
174     }
175   GNUNET_OS_process_wait (proc);
176   GNUNET_OS_process_close (proc);
177   proc = NULL;
178 #endif
179   if (ok != 0)
180     return ok;
181   ok = 1;
182 #if START_SERVICE
183   /* restart to check persistence! */
184   proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
185                                  "gnunet-service-statistics",
186 #if DEBUG_STATISTICS
187                                  "-L", "DEBUG",
188 #endif
189                                  "-c", "test_statistics_api_data.conf", NULL);
190 #endif
191   GNUNET_PROGRAM_run (5, argv, "test-statistics-api", "nohelp",
192                       options, &run_more, &ok);
193 #if START_SERVICE
194   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
195     {
196       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
197       ok = 1;
198     }
199   GNUNET_OS_process_wait (proc);
200   GNUNET_OS_process_close (proc);
201   proc = NULL;
202 #endif
203   return ok;
204 }
205
206 int
207 main (int argc, char *argv[])
208 {
209   int ret;
210
211   GNUNET_log_setup ("test_statistics_api", 
212 #if DEBUG_STATISTICS
213                     "DEBUG",
214 #else
215                     "WARNING",
216 #endif
217                     NULL);
218   ret = check ();
219
220   return ret;
221 }
222
223 /* end of test_statistics_api.c */