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