fix usage of mesh_api
[oweals/gnunet.git] / src / statistics / test_statistics_api_loop.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_loop.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 VERBOSE GNUNET_NO
33
34 #define START_SERVICE GNUNET_YES
35
36 #define ROUNDS (1024 * 1024)
37
38 static int
39 check_1 (void *cls,
40          const char *subsystem,
41          const char *name, uint64_t value, int is_persistent)
42 {
43   GNUNET_assert (0 == strcmp (name, "test-0"));
44   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api-loop"));
45   GNUNET_assert (is_persistent == GNUNET_NO);
46   return GNUNET_OK;
47 }
48
49 static struct GNUNET_STATISTICS_Handle *h;
50
51 static void
52 next (void *cls, int success)
53 {
54   int *ok = cls;
55
56   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
57   GNUNET_assert (success == GNUNET_OK);
58   *ok = 0;
59 }
60
61 static void
62 run (void *cls,
63      char *const *args,
64      const char *cfgfile,
65      const struct GNUNET_CONFIGURATION_Handle *cfg)
66 {
67   int i;
68   char name[128];
69
70   h = GNUNET_STATISTICS_create ("test-statistics-api-loop", cfg);
71   for (i=0;i<ROUNDS;i++)
72     {
73       GNUNET_snprintf (name, sizeof (name), "test-%d", i % 256);
74       GNUNET_STATISTICS_set (h, name, i, GNUNET_NO);
75       GNUNET_snprintf (name, sizeof (name), "test-%d", i % 128);
76       GNUNET_STATISTICS_update (h, name, 1, GNUNET_NO);
77     }
78   i = 0;
79   GNUNET_break (NULL != 
80                 GNUNET_STATISTICS_get (h, NULL, "test-0",
81                                        GNUNET_TIME_UNIT_MINUTES, &next, &check_1, cls));
82 }
83
84
85 static int
86 check ()
87 {
88   int ok = 1;
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 #if START_SERVICE
98   struct GNUNET_OS_Process *proc;
99   proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
100                                  "gnunet-service-statistics",
101 #if DEBUG_STATISTICS
102                                  "-L", "DEBUG",
103 #endif
104                                  "-c", "test_statistics_api_data.conf", NULL);
105 #endif
106   GNUNET_assert (NULL != proc);
107   GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp",
108                       options, &run, &ok);
109 #if START_SERVICE
110   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
111     {
112       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
113       ok = 1;
114     }
115   GNUNET_OS_process_wait (proc);
116   GNUNET_OS_process_close (proc);
117   proc = NULL;
118 #endif
119   return ok;
120 }
121
122 int
123 main (int argc, char *argv[])
124 {
125   int ret;
126
127   ret = check ();
128
129   return ret;
130 }
131
132 /* end of test_statistics_api_loop.c */