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