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