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