-LRN: calculate file size for single files when needed and use GNUNET_DISK_file_size...
[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, const char *subsystem, const char *name, uint64_t value,
40          int is_persistent)
41 {
42   GNUNET_assert (0 == strcmp (name, "test-0"));
43   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api-loop"));
44   GNUNET_assert (is_persistent == GNUNET_NO);
45   return GNUNET_OK;
46 }
47
48 static struct GNUNET_STATISTICS_Handle *h;
49
50 static void
51 next (void *cls, int success)
52 {
53   int *ok = cls;
54
55   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
56   GNUNET_assert (success == GNUNET_OK);
57   *ok = 0;
58 }
59
60 static void
61 run (void *cls, char *const *args, const char *cfgfile,
62      const struct GNUNET_CONFIGURATION_Handle *cfg)
63 {
64   int i;
65   char name[128];
66
67   h = GNUNET_STATISTICS_create ("test-statistics-api-loop", cfg);
68   for (i = 0; i < ROUNDS; i++)
69   {
70     GNUNET_snprintf (name, sizeof (name), "test-%d", i % 256);
71     GNUNET_STATISTICS_set (h, name, i, GNUNET_NO);
72     GNUNET_snprintf (name, sizeof (name), "test-%d", i % 128);
73     GNUNET_STATISTICS_update (h, name, 1, GNUNET_NO);
74   }
75   i = 0;
76   GNUNET_break (NULL !=
77                 GNUNET_STATISTICS_get (h, NULL, "test-0",
78                                        GNUNET_TIME_UNIT_MINUTES, &next,
79                                        &check_1, cls));
80 }
81
82
83 static int
84 check ()
85 {
86   int ok = 1;
87
88   char *const argv[] = { "test-statistics-api",
89     "-c",
90     "test_statistics_api_data.conf",
91     NULL
92   };
93   struct GNUNET_GETOPT_CommandLineOption options[] = {
94     GNUNET_GETOPT_OPTION_END
95   };
96 #if START_SERVICE
97   struct GNUNET_OS_Process *proc;
98
99   proc =
100     GNUNET_OS_start_process (GNUNET_YES, 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_assert (NULL != proc);
108   GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp", options, &run,
109                       &ok);
110 #if START_SERVICE
111   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
112   {
113     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
114     ok = 1;
115   }
116   GNUNET_OS_process_wait (proc);
117   GNUNET_OS_process_close (proc);
118   proc = NULL;
119 #endif
120   return ok;
121 }
122
123 int
124 main (int argc, char *argv[])
125 {
126   int ret;
127
128   ret = check ();
129
130   return ret;
131 }
132
133 /* end of test_statistics_api_loop.c */