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