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