2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file test_plugin_datastore.c
22 * @brief Profile database plugin directly, focusing on iterators.
23 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_datastore_plugin.h"
31 #define VERBOSE GNUNET_NO
34 * Target datastore size (in bytes). Realistic sizes are
35 * more like 16 GB (not the default of 16 MB); however,
36 * those take too long to run them in the usual "make check"
37 * sequence. Hence the value used for shipping is tiny.
39 #define MAX_SIZE 1024LL * 1024 * 128
44 * Number of put operations to perform.
48 static unsigned long long stored_bytes;
50 static unsigned long long stored_entries;
52 static unsigned long long stored_ops;
54 static const char *plugin_name;
73 const struct GNUNET_CONFIGURATION_Handle *cfg;
74 struct GNUNET_DATASTORE_PluginFunctions * api;
82 * Function called by plugins to notify us about a
83 * change in their disk utilization.
85 * @param cls closure (NULL)
86 * @param delta change in disk utilization,
87 * 0 for "reset to empty"
90 disk_utilization_change_cb (void *cls,
101 memset (key, 0, sizeof (GNUNET_HashCode));
102 key->bits[0] = (unsigned int) i;
103 GNUNET_CRYPTO_hash (key, sizeof (GNUNET_HashCode), key);
108 put_value (struct GNUNET_DATASTORE_PluginFunctions * api,
118 /* most content is 32k */
121 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
122 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
123 size = size - (size & 7); /* always multiple of 8 */
125 /* generate random key */
127 memset (value, i, size);
129 memset (value, i - 255, size / 2);
132 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
133 if (GNUNET_OK != api->put (api->cls,
141 GNUNET_TIME_relative_to_absolute
142 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
143 60 * 60 * 60 * 1000 +
144 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
147 fprintf (stderr, "ERROR: `%s'\n", msg);
148 GNUNET_free_non_null (msg);
151 stored_bytes += size;
159 const struct GNUNET_SCHEDULER_TaskContext *tc);
163 iterate_one_shot (void *cls,
165 const GNUNET_HashCode * key,
168 enum GNUNET_BLOCK_Type type,
171 struct GNUNET_TIME_Absolute
175 struct CpsRunContext *crc = cls;
177 GNUNET_assert (NULL == next_cls);
178 GNUNET_assert (key != NULL);
182 "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
183 type, priority, size,
184 (unsigned long long) expiration.abs_value);
186 GNUNET_SCHEDULER_add_now (&test, crc);
191 static uint64_t guid;
194 iterate_with_next (void *cls,
196 const GNUNET_HashCode * key,
199 enum GNUNET_BLOCK_Type type,
202 struct GNUNET_TIME_Absolute
206 struct CpsRunContext *crc = cls;
211 GNUNET_SCHEDULER_add_now (&test, crc);
217 "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
218 type, priority, size,
219 (unsigned long long) expiration.abs_value);
222 crc->api->next_request (next_cls,
229 * Function called when the service shuts
230 * down. Unloads our datastore plugin.
232 * @param api api to unload
233 * @param cfg configuration to use
236 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
237 const struct GNUNET_CONFIGURATION_Handle *cfg)
243 GNUNET_CONFIGURATION_get_value_string (cfg,
244 "DATASTORE", "DATABASE", &name))
246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
247 _("No `%s' specified for `%s' in configuration!\n"),
252 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
253 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
254 GNUNET_free (libname);
261 * Last task run during shutdown. Disconnects us from
262 * the transport and core.
265 cleaning_task (void *cls,
266 const struct GNUNET_SCHEDULER_TaskContext *tc)
268 struct CpsRunContext *crc = cls;
270 unload_plugin (crc->api, crc->cfg);
277 const struct GNUNET_SCHEDULER_TaskContext *tc)
279 struct CpsRunContext *crc = cls;
281 unsigned long long os;
282 unsigned long long cs;
285 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
287 crc->phase = RP_ERROR;
294 crc->api->drop (crc->api->cls);
295 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
299 for (j=0;j<PUT_10;j++)
301 put_value (crc->api, j, crc->i);
302 cs = crc->api->get_size (crc->api->cls);
303 GNUNET_assert (os < cs);
307 GNUNET_SCHEDULER_add_now (&test, crc);
314 GNUNET_SCHEDULER_add_now (&test, crc);
318 crc->api->get (crc->api->cls,
320 GNUNET_BLOCK_TYPE_ANY,
325 GNUNET_assert (GNUNET_OK ==
326 crc->api->update (crc->api->cls,
328 GNUNET_TIME_UNIT_ZERO_ABS,
331 GNUNET_SCHEDULER_add_now (&test, crc);
339 GNUNET_SCHEDULER_add_now (&test, crc);
342 crc->api->iter_zero_anonymity (crc->api->cls,
348 crc->api->replication_get (crc->api->cls,
353 crc->api->expiration_get (crc->api->cls,
358 crc->api->drop (crc->api->cls);
359 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
366 * Load the datastore plugin.
368 static struct GNUNET_DATASTORE_PluginFunctions *
369 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
371 static struct GNUNET_DATASTORE_PluginEnvironment env;
372 struct GNUNET_DATASTORE_PluginFunctions * ret;
377 GNUNET_CONFIGURATION_get_value_string (cfg,
378 "DATASTORE", "DATABASE", &name))
380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
381 _("No `%s' specified for `%s' in configuration!\n"),
387 env.duc = &disk_utilization_change_cb;
389 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
390 _("Loading `%s' datastore plugin\n"), name);
391 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
392 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
395 "Failed to load plugin `%s'!\n",
399 GNUNET_free (libname);
409 const struct GNUNET_CONFIGURATION_Handle *c)
411 struct GNUNET_DATASTORE_PluginFunctions *api;
412 struct CpsRunContext *crc;
414 api = load_plugin (c);
418 "Could not initialize plugin, assuming database not configured. Test not run!\n");
421 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
425 GNUNET_SCHEDULER_add_now (&test, crc);
433 char *const argv[] = {
434 "test-plugin-datastore",
442 struct GNUNET_GETOPT_CommandLineOption options[] = {
443 GNUNET_GETOPT_OPTION_END
446 GNUNET_snprintf (cfg_name,
448 "test_plugin_datastore_data_%s.conf",
450 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
451 argv, "test-plugin-datastore", "nohelp",
452 options, &run, NULL);
454 fprintf (stderr, "Missed some testcases: %u\n", ok);
460 main (int argc, char *argv[])
466 /* determine name of plugin to use */
467 plugin_name = argv[0];
468 while (NULL != (pos = strstr(plugin_name, "_")))
470 if (NULL != (pos = strstr(plugin_name, ".")))
473 pos = (char *) plugin_name;
475 GNUNET_snprintf (dir_name,
477 "/tmp/test-gnunet-datastore-%s",
479 GNUNET_DISK_directory_remove (dir_name);
480 GNUNET_log_setup ("test-plugin-datastore",
488 if (pos != plugin_name)
490 GNUNET_DISK_directory_remove (dir_name);
495 /* end of test_plugin_datastore.c */