2 This file is part of GNUnet.
3 Copyright (C) 2009, 2011, 2012 GNUnet e.V.
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.
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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file statistics/test_statistics_api_watch_zero_value.c
22 * @brief testcase for statistics_api.c watch functions with initial 0 value
25 #include "gnunet_util_lib.h"
26 #include "gnunet_statistics_service.h"
32 static struct GNUNET_STATISTICS_Handle *h;
34 static struct GNUNET_STATISTICS_Handle *h2;
36 static struct GNUNET_SCHEDULER_Task *shutdown_task;
40 force_shutdown (void *cls)
42 fprintf (stderr, "Timeout, failed to receive notifications: %d\n", ok);
43 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
44 GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
50 normal_shutdown (void *cls)
52 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
53 GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
58 watch_1 (void *cls, const char *subsystem, const char *name, uint64_t value,
61 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
62 "Received value `%s' `%s' %llu\n",
65 (unsigned long long) value);
66 GNUNET_assert (0 == strcmp (name, "test-1"));
67 if ((0 == value) && (3 == ok))
70 GNUNET_STATISTICS_set (h, "test-1", 42, GNUNET_NO);
73 if ((42 == value) && (2 == ok))
76 GNUNET_STATISTICS_set (h, "test-1", 0, GNUNET_NO);
79 if ((0 == value) && (1 == ok))
83 if ((0 == ok) && (0 == ok2))
85 GNUNET_SCHEDULER_cancel (shutdown_task);
86 GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
95 const char *subsystem,
100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
101 "Received value `%s' `%s' %llu\n",
104 (unsigned long long) value);
106 GNUNET_assert (0 == strcmp (name, "test-2"));
107 if ((42 == value) && (1 == ok2))
112 GNUNET_SCHEDULER_cancel (shutdown_task);
113 GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
119 "Received unexpected value %llu\n",
120 (unsigned long long) value);
123 GNUNET_SCHEDULER_cancel (shutdown_task);
124 GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
132 run (void *cls, char *const *args, const char *cfgfile,
133 const struct GNUNET_CONFIGURATION_Handle *cfg)
135 h = GNUNET_STATISTICS_create ("dummy", cfg);
136 h2 = GNUNET_STATISTICS_create ("dummy-2", cfg);
137 GNUNET_assert (GNUNET_OK ==
138 GNUNET_STATISTICS_watch (h, "dummy",
139 "test-1", &watch_1, NULL));
141 GNUNET_assert (GNUNET_OK ==
142 GNUNET_STATISTICS_watch (h2, "dummy-2",
143 "test-2", &watch_2, NULL));
145 /* Set initial value to 0 */
146 GNUNET_STATISTICS_set (h, "test-1", 0, GNUNET_NO);
147 GNUNET_STATISTICS_set (h2, "test-2", 42, GNUNET_NO);
150 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
157 main (int argc, char *argv_ign[])
159 char *const argv[] = { "test-statistics-api",
161 "test_statistics_api_data.conf",
164 struct GNUNET_GETOPT_CommandLineOption options[] = {
165 GNUNET_GETOPT_OPTION_END
167 struct GNUNET_OS_Process *proc;
170 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics");
172 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
175 "gnunet-service-statistics",
176 "-c", "test_statistics_api_data.conf", NULL);
177 GNUNET_assert (NULL != proc);
180 GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp", options, &run,
182 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
184 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
187 GNUNET_OS_process_wait (proc);
188 GNUNET_OS_process_destroy (proc);
190 GNUNET_free (binary);
191 if ((0 == ok) && (0 == ok2))
196 /* end of test_statistics_api_watch_zero_value.c */