spacing
[oweals/gnunet.git] / src / statistics / test_statistics_api_watch.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2011 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_watch.c
22  * @brief testcase for statistics_api.c watch functions
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 ok;
39
40 static struct GNUNET_STATISTICS_Handle *h;
41
42 static struct GNUNET_STATISTICS_Handle *h2;
43
44 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
45
46
47 static void
48 force_shutdown (void *cls,
49                 const struct GNUNET_SCHEDULER_TaskContext *tc)
50 {
51   fprintf (stderr, "Timeout, failed to receive notifications: %d\n", ok);
52   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
53   GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
54   ok = 7;
55 }
56
57
58 static void
59 normal_shutdown (void *cls,
60                  const struct GNUNET_SCHEDULER_TaskContext *tc)
61 {
62   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
63   GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
64 }
65
66
67 static int
68 watch_1 (void *cls, 
69          const char *subsystem,
70          const char *name,
71          uint64_t value,
72          int is_persistent)
73 {
74   GNUNET_assert (value == 42);
75   GNUNET_assert (0 == strcmp (name, "test-1"));
76   ok &= ~1;
77   if (0 == ok)
78     {
79       GNUNET_SCHEDULER_cancel (shutdown_task);
80       GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
81     }  
82   return GNUNET_OK;
83 }
84
85
86 static int
87 watch_2 (void *cls, 
88          const char *subsystem,
89          const char *name,
90          uint64_t value,
91          int is_persistent)
92 {
93   GNUNET_assert (value == 43);
94   GNUNET_assert (0 == strcmp (name, "test-2"));
95   ok &= ~2;
96   if (0 == ok)
97     {
98       GNUNET_SCHEDULER_cancel (shutdown_task);
99       GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
100     }  
101   return GNUNET_OK;
102 }
103
104
105 static void
106 run (void *cls,
107      char *const *args,
108      const char *cfgfile,
109      const struct GNUNET_CONFIGURATION_Handle *cfg)
110 {
111   h = GNUNET_STATISTICS_create ("dummy", cfg);
112   GNUNET_assert (GNUNET_OK ==
113                  GNUNET_STATISTICS_watch (h,
114                                           "test-statistics-api-watch",
115                                           "test-1",
116                                           &watch_1,
117                                           NULL));
118   GNUNET_assert (GNUNET_OK ==
119                  GNUNET_STATISTICS_watch (h,
120                                           "test-statistics-api-watch",
121                                           "test-2",
122                                           &watch_2,
123                                           NULL));
124   h2 = GNUNET_STATISTICS_create ("test-statistics-api-watch", cfg);
125   GNUNET_STATISTICS_set (h2, "test-1", 42, GNUNET_NO);
126   GNUNET_STATISTICS_set (h2, "test-2", 43, GNUNET_NO);
127   shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
128                                                 &force_shutdown, NULL);
129 }
130
131
132 static int
133 check ()
134 {
135   char *const argv[] = { "test-statistics-api",
136     "-c",
137     "test_statistics_api_data.conf",
138     NULL
139   };
140   struct GNUNET_GETOPT_CommandLineOption options[] = {
141     GNUNET_GETOPT_OPTION_END
142   };
143 #if START_SERVICE
144   struct GNUNET_OS_Process *proc;
145   proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
146                                  "gnunet-service-statistics",
147 #if VERBOSE
148                                  "-L", "DEBUG",
149 #endif
150                                  "-c", "test_statistics_api_data.conf", NULL);
151 #endif
152   GNUNET_assert (NULL != proc);
153   ok = 3;
154   GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp",
155                       options, &run, NULL);
156 #if START_SERVICE
157   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
158     {
159       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
160       ok = 1;
161     }
162   GNUNET_OS_process_wait (proc);
163   GNUNET_OS_process_close (proc);
164   proc = NULL;
165 #endif
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_watch.c */