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