-LRN: calculate file size for single files when needed and use GNUNET_DISK_file_size...
[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_NO
33
34 #define START_SERVICE GNUNET_YES
35
36 static int ok;
37
38 static struct GNUNET_STATISTICS_Handle *h;
39
40 static struct GNUNET_STATISTICS_Handle *h2;
41
42 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
43
44
45 static void
46 force_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
47 {
48   fprintf (stderr, "Timeout, failed to receive notifications: %d\n", ok);
49   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
50   GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
51   ok = 7;
52 }
53
54
55 static void
56 normal_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
57 {
58   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
59   GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
60 }
61
62
63 static int
64 watch_1 (void *cls, const char *subsystem, const char *name, uint64_t value,
65          int is_persistent)
66 {
67   GNUNET_assert (value == 42);
68   GNUNET_assert (0 == strcmp (name, "test-1"));
69   ok &= ~1;
70   if (0 == ok)
71   {
72     GNUNET_SCHEDULER_cancel (shutdown_task);
73     GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
74   }
75   return GNUNET_OK;
76 }
77
78
79 static int
80 watch_2 (void *cls, const char *subsystem, const char *name, uint64_t value,
81          int is_persistent)
82 {
83   GNUNET_assert (value == 43);
84   GNUNET_assert (0 == strcmp (name, "test-2"));
85   ok &= ~2;
86   if (0 == ok)
87   {
88     GNUNET_SCHEDULER_cancel (shutdown_task);
89     GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
90   }
91   return GNUNET_OK;
92 }
93
94
95 static void
96 run (void *cls, char *const *args, const char *cfgfile,
97      const struct GNUNET_CONFIGURATION_Handle *cfg)
98 {
99   h = GNUNET_STATISTICS_create ("dummy", cfg);
100   GNUNET_assert (GNUNET_OK ==
101                  GNUNET_STATISTICS_watch (h, "test-statistics-api-watch",
102                                           "test-1", &watch_1, NULL));
103   GNUNET_assert (GNUNET_OK ==
104                  GNUNET_STATISTICS_watch (h, "test-statistics-api-watch",
105                                           "test-2", &watch_2, NULL));
106   h2 = GNUNET_STATISTICS_create ("test-statistics-api-watch", cfg);
107   GNUNET_STATISTICS_set (h2, "test-1", 42, GNUNET_NO);
108   GNUNET_STATISTICS_set (h2, "test-2", 43, GNUNET_NO);
109   shutdown_task =
110       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &force_shutdown,
111                                     NULL);
112 }
113
114
115 static int
116 check ()
117 {
118   char *const argv[] = { "test-statistics-api",
119     "-c",
120     "test_statistics_api_data.conf",
121     NULL
122   };
123   struct GNUNET_GETOPT_CommandLineOption options[] = {
124     GNUNET_GETOPT_OPTION_END
125   };
126 #if START_SERVICE
127   struct GNUNET_OS_Process *proc;
128
129   proc =
130     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-statistics",
131                                "gnunet-service-statistics",
132 #if VERBOSE
133                                "-L", "DEBUG",
134 #endif
135                                "-c", "test_statistics_api_data.conf", NULL);
136 #endif
137   GNUNET_assert (NULL != proc);
138   ok = 3;
139   GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp", options, &run,
140                       NULL);
141 #if START_SERVICE
142   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
143   {
144     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
145     ok = 1;
146   }
147   GNUNET_OS_process_wait (proc);
148   GNUNET_OS_process_close (proc);
149   proc = NULL;
150 #endif
151   return ok;
152 }
153
154 int
155 main (int argc, char *argv[])
156 {
157   int ret;
158
159   ret = check ();
160
161   return ret;
162 }
163
164 /* end of test_statistics_api_watch.c */