Fix: Cast closure to proper type
[oweals/gnunet.git] / src / statistics / test_statistics_api_loop.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009 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_loop.c
20  * @brief testcase for statistics_api.c
21  */
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_statistics_service.h"
25
26 #define ROUNDS (1024 * 1024)
27
28 static struct GNUNET_STATISTICS_Handle *h;
29
30
31 static int
32 check_1 (void *cls,
33          const char *subsystem,
34          const char *name,
35          uint64_t value,
36          int is_persistent)
37 {
38   GNUNET_assert (0 == strcmp (name, "test-0"));
39   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api-loop"));
40   GNUNET_assert (is_persistent == GNUNET_NO);
41   return GNUNET_OK;
42 }
43
44
45 static void
46 next (void *cls,
47       int success)
48 {
49   int *ok = cls;
50
51   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
52   GNUNET_assert (success == GNUNET_OK);
53   *ok = 0;
54 }
55
56
57 static void
58 run (void *cls,
59      char *const *args,
60      const char *cfgfile,
61      const struct GNUNET_CONFIGURATION_Handle *cfg)
62 {
63   unsigned int i;
64   char name[128];
65
66   h = GNUNET_STATISTICS_create ("test-statistics-api-loop", cfg);
67   for (i = 0; i < ROUNDS; i++)
68   {
69     GNUNET_snprintf (name, sizeof (name), "test-%d", i % 32);
70     GNUNET_STATISTICS_set (h, name, i, GNUNET_NO);
71     GNUNET_snprintf (name, sizeof (name), "test-%d", i % 16);
72     GNUNET_STATISTICS_update (h, name, 1, GNUNET_NO);
73   }
74   i = 0;
75   GNUNET_break (NULL !=
76                 GNUNET_STATISTICS_get (h, NULL, "test-0",
77                                        &next,
78                                        &check_1, cls));
79 }
80
81
82 int
83 main (int argc, char *argv_ign[])
84 {
85   int ok = 1;
86
87   char *const argv[] = { "test-statistics-api",
88     "-c",
89     "test_statistics_api_data.conf",
90     NULL
91   };
92   struct GNUNET_GETOPT_CommandLineOption options[] = {
93     GNUNET_GETOPT_OPTION_END
94   };
95   struct GNUNET_OS_Process *proc;
96   char *binary;
97
98   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics");
99   proc =
100     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
101                              NULL, NULL, NULL,
102                              binary,
103                              "gnunet-service-statistics",
104                              "-c", "test_statistics_api_data.conf", NULL);
105   GNUNET_assert (NULL != proc);
106   GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp", options, &run,
107                       &ok);
108   if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
109   {
110     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
111     ok = 1;
112   }
113   GNUNET_OS_process_wait (proc);
114   GNUNET_OS_process_destroy (proc);
115   proc = NULL;
116   GNUNET_free (binary);
117   return ok;
118 }
119
120 /* end of test_statistics_api_loop.c */