If I hashed the whole block I could not retrieve it without knowing it...
[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      struct GNUNET_SCHEDULER_Handle *sched,
64      char *const *args,
65      const char *cfgfile,
66      const struct GNUNET_CONFIGURATION_Handle *cfg)
67 {
68   int i;
69   char name[128];
70
71   h = GNUNET_STATISTICS_create (sched, "test-statistics-api-loop", cfg);
72   for (i=0;i<ROUNDS;i++)
73     {
74       GNUNET_snprintf (name, sizeof (name), "test-%d", i % 256);
75       GNUNET_STATISTICS_set (h, name, i, GNUNET_NO);
76       GNUNET_snprintf (name, sizeof (name), "test-%d", i % 128);
77       GNUNET_STATISTICS_update (h, name, 1, GNUNET_NO);
78     }
79   i = 0;
80   GNUNET_break (NULL != 
81                 GNUNET_STATISTICS_get (h, NULL, "test-0",
82                                        GNUNET_TIME_UNIT_MINUTES, &next, &check_1, cls));
83 }
84
85
86 static int
87 check ()
88 {
89   int ok = 1;
90   char *const argv[] = { "test-statistics-api",
91     "-c",
92     "test_statistics_api_data.conf",
93     NULL
94   };
95   struct GNUNET_GETOPT_CommandLineOption options[] = {
96     GNUNET_GETOPT_OPTION_END
97   };
98 #if START_SERVICE
99   pid_t pid;
100   pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
101                                  "gnunet-service-statistics",
102 #if DEBUG_STATISTICS
103                                  "-L", "DEBUG",
104 #endif
105                                  "-c", "test_statistics_api_data.conf", NULL);
106 #endif
107   GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp",
108                       options, &run, &ok);
109 #if START_SERVICE
110   if (0 != PLIBC_KILL (pid, SIGTERM))
111     {
112       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
113       ok = 1;
114     }
115   GNUNET_OS_process_wait(pid);
116 #endif
117   return ok;
118 }
119
120 int
121 main (int argc, char *argv[])
122 {
123   int ret;
124
125   ret = check ();
126
127   return ret;
128 }
129
130 /* end of test_statistics_api_loop.c */