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