big scheduler refactoring, expect some issues
[oweals/gnunet.git] / src / datastore / test_datastore_api_management.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2007, 2009 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 datastore/test_datastore_api_management.c
22  * @brief Test for the space management functions of the datastore implementation.
23  * @author Christian Grothoff
24  */
25
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_datastore_service.h"
30
31 #define VERBOSE GNUNET_NO
32
33 /**
34  * How long until we give up on transmitting the message?
35  */
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
37
38 /**
39  * Number of iterations to run; must be large enough
40  * so that the quota will be exceeded!
41  */
42 #define ITERATIONS 5000
43
44 static struct GNUNET_DATASTORE_Handle *datastore;
45
46 static struct GNUNET_TIME_Absolute now;
47
48 static int ok;
49
50 static const char* plugin_name;
51
52 static size_t
53 get_size (int i)
54 {
55   return 8 + 8 * (i % 256);
56 }
57
58
59 static const void *
60 get_data (int i)
61 {
62   static char buf[60000]; 
63   memset (buf, i, 8 + 8 * (i % 256));
64   return buf;
65 }
66
67
68 static int
69 get_type(int i)
70 {
71   return 1;
72 }
73
74
75 static int 
76 get_priority (int i)
77 {
78   return i+1;
79 }
80
81
82 static int
83 get_anonymity(int i)
84 {
85   return i;
86 }
87
88
89 static struct GNUNET_TIME_Absolute 
90 get_expiration (int i)
91 {
92   struct GNUNET_TIME_Absolute av;
93
94   av.abs_value = now.abs_value + i * 1000;
95   return av;
96 }
97
98 enum RunPhase
99   {
100     RP_DONE = 0,
101     RP_PUT,
102     RP_GET,
103     RP_GET_FAIL
104   };
105
106
107 struct CpsRunContext
108 {
109   GNUNET_HashCode key;
110   int i;
111   int found;
112   const struct GNUNET_CONFIGURATION_Handle *cfg;
113   void *data;
114   enum RunPhase phase;
115 };
116
117
118 static void
119 run_continuation (void *cls,
120                   const struct GNUNET_SCHEDULER_TaskContext *tc);
121
122
123 static void
124 check_success (void *cls,
125                int success,
126                const char *msg)
127 {
128   struct CpsRunContext *crc = cls;
129   if (GNUNET_OK != success)
130     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
131                 "%s\n", msg);
132   GNUNET_assert (GNUNET_OK == success);
133   GNUNET_free_non_null (crc->data);
134   crc->data = NULL;
135   GNUNET_SCHEDULER_add_continuation (&run_continuation,
136                                      crc,
137                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
138 }
139
140
141 static void 
142 check_value (void *cls,
143              const GNUNET_HashCode * key,
144              size_t size,
145              const void *data,
146              enum GNUNET_BLOCK_Type type,
147              uint32_t priority,
148              uint32_t anonymity,
149              struct GNUNET_TIME_Absolute
150              expiration, uint64_t uid)
151 {
152   struct CpsRunContext *crc = cls;
153   int i;
154
155   if (key == NULL)
156     {
157       crc->i--;
158       if (crc->found == GNUNET_YES)
159         {
160           crc->phase = RP_GET;
161           crc->found = GNUNET_NO;
162         }
163       else
164         {
165           fprintf (stderr,
166                    "First not found was %u\n", crc->i);
167           crc->phase = RP_GET_FAIL;
168         }
169       if (0 == crc->i)
170         crc->phase = RP_DONE;
171       GNUNET_SCHEDULER_add_continuation (&run_continuation,
172                                          crc,
173                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
174       return;
175     }
176   i = crc->i;
177   crc->found = GNUNET_YES;
178   GNUNET_assert (size == get_size (i));
179   GNUNET_assert (0 == memcmp (data, get_data(i), size));
180   GNUNET_assert (type == get_type (i));
181   GNUNET_assert (priority == get_priority (i));
182   GNUNET_assert (anonymity == get_anonymity(i));
183   GNUNET_assert (expiration.abs_value == get_expiration(i).abs_value);
184   GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
185 }
186
187
188 static void 
189 check_nothing (void *cls,
190                const GNUNET_HashCode * key,
191                size_t size,
192                const void *data,
193                enum GNUNET_BLOCK_Type type,
194                uint32_t priority,
195                uint32_t anonymity,
196                struct GNUNET_TIME_Absolute
197                expiration, uint64_t uid)
198 {
199   struct CpsRunContext *crc = cls;
200
201   GNUNET_assert (key == NULL);
202   if (0 == --crc->i)
203     crc->phase = RP_DONE;
204   GNUNET_SCHEDULER_add_continuation (&run_continuation,
205                                      crc,
206                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
207 }
208
209
210 static void
211 run_continuation (void *cls,
212                   const struct GNUNET_SCHEDULER_TaskContext *tc)
213 {
214   struct CpsRunContext *crc = cls;
215   ok = (int) crc->phase;
216   switch (crc->phase)
217     {
218     case RP_PUT:
219 #if VERBOSE
220       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221                   "Executing `%s' number %u\n",
222                   "PUT",
223                   crc->i);
224 #endif
225       GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
226       GNUNET_DATASTORE_put (datastore,
227                             0,
228                             &crc->key,
229                             get_size (crc->i),
230                             get_data (crc->i),
231                             get_type (crc->i),
232                             get_priority (crc->i),
233                             get_anonymity (crc->i),
234                             get_expiration (crc->i),
235                             1, 1, TIMEOUT,
236                             &check_success,
237                             crc);
238       crc->i++;
239       if (crc->i == ITERATIONS)
240         {
241           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
242                       "Sleeping to give datastore time to clean up\n");
243           sleep (5);
244           crc->phase = RP_GET;
245           crc->i--;
246         }
247       break;
248     case RP_GET:
249 #if VERBOSE
250       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251                   "Executing `%s' number %u\n",
252                   "GET",
253                   crc->i);
254 #endif
255       GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
256       GNUNET_DATASTORE_get (datastore, 
257                             &crc->key,
258                             get_type (crc->i),
259                             1, 1, TIMEOUT,
260                             &check_value,
261                             crc);
262       break;
263     case RP_GET_FAIL:
264 #if VERBOSE
265       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266                   "Executing `%s' number %u\n",
267                   "GET",
268                   crc->i);
269 #endif
270       GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
271       GNUNET_DATASTORE_get (datastore, 
272                             &crc->key,
273                             get_type (crc->i),
274                             1, 1, TIMEOUT,
275                             &check_nothing,
276                             crc);
277       break;
278     case RP_DONE:
279       GNUNET_assert (0 == crc->i);
280 #if VERBOSE
281       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
282                   "Finished, disconnecting\n");
283 #endif
284       GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
285       GNUNET_free (crc);
286       ok = 0;
287     }
288 }
289
290
291 static void
292 run_tests (void *cls,
293            int success,
294            const char *msg)
295 {
296   struct CpsRunContext *crc = cls;
297
298   if (success != GNUNET_YES)
299     {
300       fprintf (stderr,
301                "Test 'put' operation failed with error `%s' database likely not setup, skipping test.",
302                msg);
303       GNUNET_free (crc);
304       return;
305     }
306   GNUNET_SCHEDULER_add_continuation (&run_continuation,
307                                      crc,
308                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
309 }
310
311
312 static void
313 run (void *cls,
314      char *const *args,
315      const char *cfgfile,
316      const struct GNUNET_CONFIGURATION_Handle *cfg)
317 {
318   struct CpsRunContext *crc;
319   static GNUNET_HashCode zkey;
320
321   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
322   crc->cfg = cfg;
323   crc->phase = RP_PUT;
324   now = GNUNET_TIME_absolute_get ();
325   datastore = GNUNET_DATASTORE_connect (cfg);
326   if (NULL ==
327       GNUNET_DATASTORE_put (datastore, 0,
328                             &zkey, 4, "TEST",
329                             GNUNET_BLOCK_TYPE_TEST,
330                             0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
331                             0, 1, GNUNET_TIME_UNIT_MINUTES,
332                             &run_tests, crc))
333     {
334       fprintf (stderr,
335                "Test 'put' operation failed.\n");
336       GNUNET_free (crc);
337       ok = 1;
338     }
339 }
340
341
342
343 static int
344 check ()
345 {
346   struct GNUNET_OS_Process *proc;
347   char cfg_name[128];
348   char *const argv[] = { 
349     "test-datastore-api-management",
350     "-c",
351     cfg_name,
352 #if VERBOSE
353     "-L", "DEBUG",
354 #endif
355     NULL
356   };
357   struct GNUNET_GETOPT_CommandLineOption options[] = {
358     GNUNET_GETOPT_OPTION_END
359   };
360   GNUNET_snprintf (cfg_name,
361                    sizeof (cfg_name),
362                    "test_datastore_api_data_%s.conf",
363                    plugin_name);
364   proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
365                                  "gnunet-service-arm",
366 #if VERBOSE
367                                  "-L", "DEBUG",
368 #endif
369                                  "-c", cfg_name, NULL);
370   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
371                       argv, "test-datastore-api", "nohelp",
372                       options, &run, NULL);
373   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
374     {
375       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
376       ok = 1;
377     }
378   GNUNET_OS_process_wait (proc);
379   GNUNET_OS_process_close (proc);
380   proc = NULL;
381   if (ok != 0)
382     fprintf (stderr, "Missed some testcases: %u\n", ok);
383   return ok;
384 }
385
386 int
387 main (int argc, char *argv[])
388 {
389   int ret;
390   
391   const char *pos;
392   char dir_name[128];
393
394   /* determine name of plugin to use */
395   plugin_name = argv[0];
396   while (NULL != (pos = strstr(plugin_name, "_")))
397     plugin_name = pos+1;
398
399   GNUNET_snprintf (dir_name,
400                    sizeof (dir_name),
401                    "/tmp/test-gnunet-datastore-%s",
402                    plugin_name);
403   GNUNET_DISK_directory_remove (dir_name);
404   GNUNET_log_setup ("test-datastore-api-management",
405 #if VERBOSE
406                     "DEBUG",
407 #else
408                     "WARNING",
409 #endif
410                     NULL);
411   ret = check ();
412   GNUNET_DISK_directory_remove (dir_name);
413   return ret;
414 }
415
416 /* end of test_datastore_api_management.c */