glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / statistics / test_statistics_api_watch_zero_value.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2011, 2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15 /**
16  * @file statistics/test_statistics_api_watch_zero_value.c
17  * @brief testcase for statistics_api.c watch functions with initial 0 value
18  */
19 #include "platform.h"
20 #include "gnunet_util_lib.h"
21 #include "gnunet_statistics_service.h"
22
23 static int ok;
24
25 static int ok2;
26
27 static struct GNUNET_STATISTICS_Handle *h;
28
29 static struct GNUNET_STATISTICS_Handle *h2;
30
31 static struct GNUNET_SCHEDULER_Task *shutdown_task;
32
33
34 static void
35 force_shutdown (void *cls)
36 {
37   fprintf (stderr, "Timeout, failed to receive notifications: %d\n", ok);
38   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
39   GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
40   ok = 7;
41 }
42
43
44 static void
45 normal_shutdown (void *cls)
46 {
47   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
48   GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
49 }
50
51
52 static int
53 watch_1 (void *cls, const char *subsystem, const char *name, uint64_t value,
54          int is_persistent)
55 {
56   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
57               "Received value `%s' `%s' %llu\n",
58               subsystem,
59               name,
60               (unsigned long long) value);
61   GNUNET_assert (0 == strcmp (name, "test-1"));
62   if ((0 == value) && (3 == ok))
63   {
64     ok--;
65     GNUNET_STATISTICS_set (h, "test-1", 42, GNUNET_NO);
66   }
67
68   if ((42 == value) && (2 == ok))
69   {
70     ok--;
71     GNUNET_STATISTICS_set (h, "test-1", 0, GNUNET_NO);
72   }
73
74   if ((0 == value) && (1 == ok))
75   {
76     ok--;
77   }
78   if ((0 == ok) && (0 == ok2))
79   {
80     GNUNET_SCHEDULER_cancel (shutdown_task);
81     GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
82   }
83
84   return GNUNET_OK;
85 }
86
87
88 static int
89 watch_2 (void *cls,
90          const char *subsystem,
91          const char *name,
92          uint64_t value,
93          int is_persistent)
94 {
95   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96               "Received value `%s' `%s' %llu\n",
97               subsystem,
98               name,
99               (unsigned long long) value);
100
101   GNUNET_assert (0 == strcmp (name, "test-2"));
102   if ((42 == value) && (1 == ok2))
103   {
104     ok2 = 0;
105     if (0 == ok)
106     {
107       GNUNET_SCHEDULER_cancel (shutdown_task);
108       GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
109     }
110   }
111   else
112   {
113     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
114                 "Received unexpected value %llu\n",
115                 (unsigned long long) value);
116
117     GNUNET_break (0);
118     GNUNET_SCHEDULER_cancel (shutdown_task);
119     GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
120   }
121
122   return GNUNET_OK;
123 }
124
125
126 static void
127 run (void *cls, char *const *args, const char *cfgfile,
128      const struct GNUNET_CONFIGURATION_Handle *cfg)
129 {
130   h = GNUNET_STATISTICS_create ("dummy", cfg);
131   h2 = GNUNET_STATISTICS_create ("dummy-2", cfg);
132   GNUNET_assert (GNUNET_OK ==
133                  GNUNET_STATISTICS_watch (h, "dummy",
134                                           "test-1", &watch_1, NULL));
135
136   GNUNET_assert (GNUNET_OK ==
137                  GNUNET_STATISTICS_watch (h2, "dummy-2",
138                                           "test-2", &watch_2, NULL));
139
140   /* Set initial value to 0 */
141   GNUNET_STATISTICS_set (h, "test-1", 0, GNUNET_NO);
142   GNUNET_STATISTICS_set (h2, "test-2", 42, GNUNET_NO);
143
144   shutdown_task =
145       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
146                                     &force_shutdown,
147                                     NULL);
148 }
149
150
151 int
152 main (int argc, char *argv_ign[])
153 {
154   char *const argv[] = { "test-statistics-api",
155     "-c",
156     "test_statistics_api_data.conf",
157     NULL
158   };
159   struct GNUNET_GETOPT_CommandLineOption options[] = {
160     GNUNET_GETOPT_OPTION_END
161   };
162   struct GNUNET_OS_Process *proc;
163   char *binary;
164
165   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics");
166   proc =
167     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
168                              NULL, NULL, NULL,
169                              binary,
170                              "gnunet-service-statistics",
171                              "-c", "test_statistics_api_data.conf", NULL);
172   GNUNET_assert (NULL != proc);
173   ok = 3;
174   ok2 = 1;
175   GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp", options, &run,
176                       NULL);
177   if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
178   {
179     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
180     ok = 1;
181   }
182   GNUNET_OS_process_wait (proc);
183   GNUNET_OS_process_destroy (proc);
184   proc = NULL;
185   GNUNET_free (binary);
186   if ((0 == ok) && (0 == ok2))
187     return 0;
188   return 1;
189 }
190
191 /* end of test_statistics_api_watch_zero_value.c */