code clean up
[oweals/gnunet.git] / src / statistics / test_statistics_api.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 2, 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.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 static int
35 check_1 (void *cls,
36          const char *subsystem,
37          const char *name, unsigned long long value, int is_persistent)
38 {
39   GNUNET_assert (0 == strcmp (name, "test-1"));
40   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
41   GNUNET_assert (value == 1);
42   GNUNET_assert (is_persistent == GNUNET_NO);
43   return GNUNET_OK;
44 }
45
46 static int
47 check_2 (void *cls,
48          const char *subsystem,
49          const char *name, unsigned long long value, int is_persistent)
50 {
51   GNUNET_assert (0 == strcmp (name, "test-2"));
52   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
53   GNUNET_assert (value == 2);
54   GNUNET_assert (is_persistent == GNUNET_NO);
55   return GNUNET_OK;
56 }
57
58 static int
59 check_3 (void *cls,
60          const char *subsystem,
61          const char *name, unsigned long long value, int is_persistent)
62 {
63   GNUNET_assert (0 == strcmp (name, "test-3"));
64   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
65   GNUNET_assert (value == 3);
66   GNUNET_assert (is_persistent == GNUNET_YES);
67   return GNUNET_OK;
68 }
69
70 static struct GNUNET_STATISTICS_Handle *h;
71
72 static void
73 next_fin (void *cls, int success)
74 {
75   int *ok = cls;
76
77   GNUNET_STATISTICS_destroy (h);
78   GNUNET_assert (success == GNUNET_OK);
79   *ok = 0;
80 }
81
82 static void
83 next (void *cls, int success)
84 {
85   GNUNET_assert (success == GNUNET_OK);
86   GNUNET_STATISTICS_get (h, NULL, "test-2",
87                          GNUNET_TIME_UNIT_SECONDS, &next_fin, &check_2, cls);
88 }
89
90 static void
91 run (void *cls,
92      struct GNUNET_SCHEDULER_Handle *sched,
93      char *const *args,
94      const char *cfgfile,
95      const struct GNUNET_CONFIGURATION_Handle *cfg)
96 {
97
98   h = GNUNET_STATISTICS_create (sched, "test-statistics-api", cfg);
99   GNUNET_STATISTICS_set (h, "test-1", 1, GNUNET_NO);
100   GNUNET_STATISTICS_set (h, "test-2", 2, GNUNET_NO);
101   GNUNET_STATISTICS_set (h, "test-3", 2, GNUNET_NO);
102   GNUNET_STATISTICS_update (h, "test-3", 1, GNUNET_YES);
103   GNUNET_STATISTICS_get (h, NULL, "test-1",
104                          GNUNET_TIME_UNIT_SECONDS, &next, &check_1, cls);
105 }
106
107 static void
108 run_more (void *cls,
109           struct GNUNET_SCHEDULER_Handle *sched,
110           char *const *args,
111           const char *cfgfile,
112           const struct GNUNET_CONFIGURATION_Handle *cfg)
113 {
114   h = GNUNET_STATISTICS_create (sched, "test-statistics-api", cfg);
115   GNUNET_STATISTICS_get (h, NULL, "test-3",
116                          GNUNET_TIME_UNIT_SECONDS, &next_fin, &check_3, cls);
117 }
118
119 static int
120 check ()
121 {
122   int ok = 1;
123   pid_t pid;
124   char *const argv[] = { "test-statistics-api",
125     "-c",
126     "test_statistics_api_data.conf",
127     NULL
128   };
129   struct GNUNET_GETOPT_CommandLineOption options[] = {
130     GNUNET_GETOPT_OPTION_END
131   };
132   pid = GNUNET_OS_start_process ("gnunet-service-statistics",
133                                  "gnunet-service-statistics",
134 #if DEBUG_STATISTICS
135                                  "-L", "DEBUG",
136 #endif
137                                  "-c", "test_statistics_api_data.conf", NULL);
138   sleep (1);
139   GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp",
140                       options, &run, &ok);
141   if (0 != PLIBC_KILL (pid, SIGTERM))
142     {
143       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
144       ok = 1;
145     }
146   GNUNET_OS_process_wait(pid);
147   if (ok != 0)
148     return ok;
149   ok = 1;
150   /* restart to check persistence! */
151   pid = GNUNET_OS_start_process ("gnunet-service-statistics",
152                                  "gnunet-service-statistics",
153 #if DEBUG_STATISTICS
154                                  "-L", "DEBUG",
155 #endif
156                                  "-c", "test_statistics_api_data.conf", NULL);
157   sleep (1);
158   GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp",
159                       options, &run_more, &ok);
160   if (0 != PLIBC_KILL (pid, SIGTERM))
161     {
162       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
163       ok = 1;
164     }
165   GNUNET_OS_process_wait(pid);
166   return ok;
167 }
168
169 int
170 main (int argc, char *argv[])
171 {
172   int ret;
173
174   ret = check ();
175
176   return ret;
177 }
178
179 /* end of test_statistics_api.c */