first batch of license fixes (boring)
[oweals/gnunet.git] / src / statistics / test_statistics_api_watch.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 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.c
17  * @brief testcase for statistics_api.c watch functions
18  * @author Christian Grothoff
19  */
20 #include "platform.h"
21 #include "gnunet_util_lib.h"
22 #include "gnunet_statistics_service.h"
23
24
25 static int ok;
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,
54          const char *subsystem,
55          const char *name,
56          uint64_t value,
57          int is_persistent)
58 {
59   GNUNET_assert (value == 42);
60   GNUNET_assert (0 == strcmp (name, "test-1"));
61   ok &= ~1;
62   if (0 == ok)
63   {
64     GNUNET_SCHEDULER_cancel (shutdown_task);
65     GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
66   }
67   return GNUNET_OK;
68 }
69
70
71 static int
72 watch_2 (void *cls,
73          const char *subsystem,
74          const char *name,
75          uint64_t value,
76          int is_persistent)
77 {
78   GNUNET_assert (value == 43);
79   GNUNET_assert (0 == strcmp (name, "test-2"));
80   ok &= ~2;
81   if (0 == ok)
82   {
83     GNUNET_SCHEDULER_cancel (shutdown_task);
84     GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
85   }
86   return GNUNET_OK;
87 }
88
89
90 static void
91 run (void *cls,
92      char *const *args,
93      const char *cfgfile,
94      const struct GNUNET_CONFIGURATION_Handle *cfg)
95 {
96   h = GNUNET_STATISTICS_create ("dummy", cfg);
97   GNUNET_assert (GNUNET_OK ==
98                  GNUNET_STATISTICS_watch (h, "test-statistics-api-watch",
99                                           "test-1", &watch_1, NULL));
100   GNUNET_assert (GNUNET_OK ==
101                  GNUNET_STATISTICS_watch (h, "test-statistics-api-watch",
102                                           "test-2", &watch_2, NULL));
103   h2 = GNUNET_STATISTICS_create ("test-statistics-api-watch", cfg);
104   GNUNET_STATISTICS_set (h2, "test-1", 42, GNUNET_NO);
105   GNUNET_STATISTICS_set (h2, "test-2", 43, GNUNET_NO);
106   shutdown_task =
107       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
108                                     &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,
130                              NULL, NULL, NULL,
131                              binary,
132                              "gnunet-service-statistics",
133                              "-c", "test_statistics_api_data.conf", NULL);
134   GNUNET_assert (NULL != proc);
135   ok = 3;
136   GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp", options, &run,
137                       NULL);
138   if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
139   {
140     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
141     ok = 1;
142   }
143   GNUNET_OS_process_wait (proc);
144   GNUNET_OS_process_destroy (proc);
145   proc = NULL;
146   GNUNET_free (binary);
147   return ok;
148 }
149
150
151 /* end of test_statistics_api_watch.c */