-simplifying event loop for UDP, separting v4/v6 for better performance (in theory...
[oweals/gnunet.git] / src / statistics / test_statistics_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2012 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  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_statistics_service.h"
28
29
30 static struct GNUNET_STATISTICS_Handle *h;
31
32
33 static int
34 check_1 (void *cls, const char *subsystem, const char *name, uint64_t value,
35          int is_persistent)
36 {
37   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received value %llu for `%s:%s\n",
38               (unsigned long long) value, subsystem, name);
39   GNUNET_assert (0 == strcmp (name, "test-1"));
40   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
41   GNUNET_assert (value == 1);
42   GNUNET_assert (is_persistent == GNUNET_NO);
43   return GNUNET_OK;
44 }
45
46
47 static int
48 check_2 (void *cls, const char *subsystem, const char *name, uint64_t value,
49          int is_persistent)
50 {
51   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received value %llu for `%s:%s\n",
52               (unsigned long long) value, subsystem, name);
53   GNUNET_assert (0 == strcmp (name, "test-2"));
54   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
55   GNUNET_assert (value == 2);
56   GNUNET_assert (is_persistent == GNUNET_NO);
57   return GNUNET_OK;
58 }
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
75 static void
76 next_fin (void *cls, int success)
77 {
78   int *ok = cls;
79
80   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
81   GNUNET_assert (success == GNUNET_OK);
82   *ok = 0;
83 }
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
98 static void
99 run (void *cls, char *const *args, const char *cfgfile,
100      const struct GNUNET_CONFIGURATION_Handle *cfg)
101 {
102   h = GNUNET_STATISTICS_create ("test-statistics-api", cfg);
103   GNUNET_STATISTICS_set (h, "test-1", 1, GNUNET_NO);
104   GNUNET_STATISTICS_set (h, "test-2", 2, GNUNET_NO);
105   GNUNET_STATISTICS_set (h, "test-3", 2, GNUNET_NO);
106   GNUNET_STATISTICS_update (h, "test-3", 1, GNUNET_YES);
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Issuing GET request\n");
108   GNUNET_break (NULL !=
109                 GNUNET_STATISTICS_get (h, NULL, "test-1",
110                                        GNUNET_TIME_UNIT_SECONDS, &next,
111                                        &check_1, cls));
112 }
113
114
115 static void
116 run_more (void *cls, char *const *args, const char *cfgfile,
117           const struct GNUNET_CONFIGURATION_Handle *cfg)
118 {
119   h = GNUNET_STATISTICS_create ("test-statistics-api", cfg);
120   GNUNET_break (NULL !=
121                 GNUNET_STATISTICS_get (h, NULL, "test-3",
122                                        GNUNET_TIME_UNIT_SECONDS, &next_fin,
123                                        &check_3, cls));
124 }
125
126
127 int
128 main (int argc, char *argv_ign[])
129 {
130   int ok = 1;
131
132   char *const argv[] = { "test-statistics-api",
133     "-c",
134     "test_statistics_api_data.conf",
135     "-L", "WARNING",
136     NULL
137   };
138   struct GNUNET_GETOPT_CommandLineOption options[] = {
139     GNUNET_GETOPT_OPTION_END
140   };
141   struct GNUNET_OS_Process *proc;
142   char *binary;
143
144   GNUNET_log_setup ("test_statistics_api",
145                     "WARNING",
146                     NULL);
147   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics");
148   proc =
149       GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
150                                NULL, NULL, NULL,
151                                binary,
152                                "gnunet-service-statistics",
153                                "-c", "test_statistics_api_data.conf", NULL);
154   GNUNET_assert (NULL != proc);
155   GNUNET_PROGRAM_run (5, argv, "test-statistics-api", "nohelp", options, &run,
156                       &ok);
157   if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
158   {
159     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
160     ok = 1;
161   }
162   GNUNET_OS_process_wait (proc);
163   GNUNET_OS_process_destroy (proc);
164   proc = NULL;
165   if (ok != 0)
166   {
167     GNUNET_free (binary);
168     return ok;
169   }
170   ok = 1;
171   /* restart to check persistence! */
172   proc =
173       GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
174                                NULL, NULL, NULL,
175                                binary,
176                                "gnunet-service-statistics",
177                                "-c", "test_statistics_api_data.conf", NULL);
178   GNUNET_PROGRAM_run (5, argv, "test-statistics-api", "nohelp", options,
179                       &run_more, &ok);
180   if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
181   {
182     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
183     ok = 1;
184   }
185   GNUNET_OS_process_wait (proc);
186   GNUNET_OS_process_destroy (proc);
187   proc = NULL;
188   GNUNET_free (binary);
189   return ok;
190 }
191
192 /* end of test_statistics_api.c */