-add adv port
[oweals/gnunet.git] / src / statistics / test_statistics_api_watch.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2011, 2012 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  * @author Christian Grothoff 
24  */
25 #include "platform.h"
26 #include "gnunet_common.h"
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_os_lib.h"
29 #include "gnunet_program_lib.h"
30 #include "gnunet_scheduler_lib.h"
31 #include "gnunet_statistics_service.h"
32
33
34 static int ok;
35
36 static struct GNUNET_STATISTICS_Handle *h;
37
38 static struct GNUNET_STATISTICS_Handle *h2;
39
40 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
41
42
43 static void
44 force_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
45 {
46   fprintf (stderr, "Timeout, failed to receive notifications: %d\n", ok);
47   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
48   GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
49   ok = 7;
50 }
51
52
53 static void
54 normal_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
55 {
56   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
57   GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
58 }
59
60
61 static int
62 watch_1 (void *cls, const char *subsystem, const char *name, uint64_t value,
63          int is_persistent)
64 {
65   GNUNET_assert (value == 42);
66   GNUNET_assert (0 == strcmp (name, "test-1"));
67   ok &= ~1;
68   if (0 == ok)
69   {
70     GNUNET_SCHEDULER_cancel (shutdown_task);
71     GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
72   }
73   return GNUNET_OK;
74 }
75
76
77 static int
78 watch_2 (void *cls, const char *subsystem, const char *name, uint64_t value,
79          int is_persistent)
80 {
81   GNUNET_assert (value == 43);
82   GNUNET_assert (0 == strcmp (name, "test-2"));
83   ok &= ~2;
84   if (0 == ok)
85   {
86     GNUNET_SCHEDULER_cancel (shutdown_task);
87     GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
88   }
89   return GNUNET_OK;
90 }
91
92
93 static void
94 run (void *cls, char *const *args, const char *cfgfile,
95      const struct GNUNET_CONFIGURATION_Handle *cfg)
96 {
97   h = GNUNET_STATISTICS_create ("dummy", cfg);
98   GNUNET_assert (GNUNET_OK ==
99                  GNUNET_STATISTICS_watch (h, "test-statistics-api-watch",
100                                           "test-1", &watch_1, NULL));
101   GNUNET_assert (GNUNET_OK ==
102                  GNUNET_STATISTICS_watch (h, "test-statistics-api-watch",
103                                           "test-2", &watch_2, NULL));
104   h2 = GNUNET_STATISTICS_create ("test-statistics-api-watch", cfg);
105   GNUNET_STATISTICS_set (h2, "test-1", 42, GNUNET_NO);
106   GNUNET_STATISTICS_set (h2, "test-2", 43, GNUNET_NO);
107   shutdown_task =
108       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &force_shutdown,
109                                     NULL);
110 }
111
112
113 int
114 main (int argc, char *argv_ign[])
115 {
116   char *const argv[] = { "test-statistics-api",
117     "-c",
118     "test_statistics_api_data.conf",
119     NULL
120   };
121   struct GNUNET_GETOPT_CommandLineOption options[] = {
122     GNUNET_GETOPT_OPTION_END
123   };
124   struct GNUNET_OS_Process *proc;
125   char *binary;
126   
127   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics");
128   proc =
129     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL,
130                              binary,
131                              "gnunet-service-statistics",
132                              "-c", "test_statistics_api_data.conf", NULL);
133   GNUNET_assert (NULL != proc);
134   ok = 3;
135   GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp", options, &run,
136                       NULL);
137   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
138   {
139     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
140     ok = 1;
141   }
142   GNUNET_OS_process_wait (proc);
143   GNUNET_OS_process_destroy (proc);
144   proc = NULL;
145   GNUNET_free (binary);
146   return ok;
147 }
148
149
150 /* end of test_statistics_api_watch.c */