aboutsummaryrefslogtreecommitdiff
path: root/src/datastore/test_datastore_api.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2015-08-03 09:06:44 +0000
committerChristian Grothoff <christian@grothoff.org>2015-08-03 09:06:44 +0000
commitc77ee3b95455fc2974edebc8e02db5d30be87d22 (patch)
tree29fe839ecd58d321f786c789b778911bc94bba0d /src/datastore/test_datastore_api.c
parent89c5d0047e261ed7e5fabb2e771123f1707e4670 (diff)
downloadgnunet-c77ee3b95455fc2974edebc8e02db5d30be87d22.tar.gz
gnunet-c77ee3b95455fc2974edebc8e02db5d30be87d22.zip
-cleaning up test logic, modernizing calls, improving test documentation
Diffstat (limited to 'src/datastore/test_datastore_api.c')
-rw-r--r--src/datastore/test_datastore_api.c233
1 files changed, 181 insertions, 52 deletions
diff --git a/src/datastore/test_datastore_api.c b/src/datastore/test_datastore_api.c
index a29e01a1e..36e637845 100644
--- a/src/datastore/test_datastore_api.c
+++ b/src/datastore/test_datastore_api.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2004, 2005, 2006, 2007, 2009, 2015 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -40,10 +40,16 @@
40 40
41#define ITERATIONS 256 41#define ITERATIONS 256
42 42
43/**
44 * Handle to the datastore.
45 */
43static struct GNUNET_DATASTORE_Handle *datastore; 46static struct GNUNET_DATASTORE_Handle *datastore;
44 47
45static struct GNUNET_TIME_Absolute now; 48static struct GNUNET_TIME_Absolute now;
46 49
50/**
51 * Value we return from #main().
52 */
47static int ok; 53static int ok;
48 54
49/** 55/**
@@ -51,6 +57,7 @@ static int ok;
51 */ 57 */
52static const char *plugin_name; 58static const char *plugin_name;
53 59
60
54static size_t 61static size_t
55get_size (int i) 62get_size (int i)
56{ 63{
@@ -98,9 +105,20 @@ get_expiration (int i)
98 return av; 105 return av;
99} 106}
100 107
108
109/**
110 * Which phase of the process are we in?
111 */
101enum RunPhase 112enum RunPhase
102{ 113{
114 /**
115 * We are done (shutting down normally).
116 */
103 RP_DONE = 0, 117 RP_DONE = 0,
118
119 /**
120 * We are adding new entries to the datastore.
121 */
104 RP_PUT = 1, 122 RP_PUT = 1,
105 RP_GET = 2, 123 RP_GET = 2,
106 RP_DEL = 3, 124 RP_DEL = 3,
@@ -113,29 +131,61 @@ enum RunPhase
113 RP_GET_MULTIPLE_NEXT = 10, 131 RP_GET_MULTIPLE_NEXT = 10,
114 RP_UPDATE = 11, 132 RP_UPDATE = 11,
115 RP_UPDATE_VALIDATE = 12, 133 RP_UPDATE_VALIDATE = 12,
134
135 /**
136 * Execution failed with some kind of error.
137 */
116 RP_ERROR 138 RP_ERROR
117}; 139};
118 140
119 141
142/**
143 * Closure we give to all of the functions executing the
144 * benchmark. Could right now be global, but this allows
145 * us to theoretically run multiple clients "in parallel".
146 */
120struct CpsRunContext 147struct CpsRunContext
121{ 148{
149 /**
150 * Execution phase we are in.
151 */
152 enum RunPhase phase;
153
122 struct GNUNET_HashCode key; 154 struct GNUNET_HashCode key;
123 int i; 155 int i;
124 int rid; 156 int rid;
125 const struct GNUNET_CONFIGURATION_Handle *cfg;
126 void *data; 157 void *data;
127 size_t size; 158 size_t size;
128 enum RunPhase phase; 159
129 uint64_t uid; 160 uint64_t uid;
130 uint64_t offset; 161 uint64_t offset;
131 uint64_t first_uid; 162 uint64_t first_uid;
132}; 163};
133 164
134 165
166/**
167 * Main state machine. Executes the next step of the test
168 * depending on the current state.
169 *
170 * @param cls the `struct CpsRunContext`
171 * @param tc scheduler context (unused)
172 */
135static void 173static void
136run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); 174run_continuation (void *cls,
175 const struct GNUNET_SCHEDULER_TaskContext *tc);
137 176
138 177
178/**
179 * Continuation called to notify client about result of an
180 * operation. Checks for errors, updates our iteration counters and
181 * continues execution with #run_continuation().
182 *
183 * @param cls the `struct CpsRunContext`
184 * @param success #GNUNET_SYSERR on failure
185 * @param min_expiration minimum expiration time required for content to be stored
186 * by the datacache at this time, zero for unknown
187 * @param msg NULL on success, otherwise an error message
188 */
139static void 189static void
140check_success (void *cls, 190check_success (void *cls,
141 int success, 191 int success,
@@ -147,14 +197,15 @@ check_success (void *cls,
147 if (GNUNET_OK != success) 197 if (GNUNET_OK != success)
148 { 198 {
149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
150 "Operation %d/%d not successfull: `%s'\n", crc->phase, crc->i, 200 "Operation %d/%d not successfull: `%s'\n",
201 crc->phase,
202 crc->i,
151 msg); 203 msg);
152 crc->phase = RP_ERROR; 204 crc->phase = RP_ERROR;
153 } 205 }
154 GNUNET_free_non_null (crc->data); 206 GNUNET_free_non_null (crc->data);
155 crc->data = NULL; 207 crc->data = NULL;
156 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc, 208 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
157 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
158} 209}
159 210
160 211
@@ -172,8 +223,8 @@ get_reserved (void *cls,
172 msg); 223 msg);
173 GNUNET_assert (0 < success); 224 GNUNET_assert (0 < success);
174 crc->rid = success; 225 crc->rid = success;
175 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc, 226 GNUNET_SCHEDULER_add_now (&run_continuation,
176 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 227 crc);
177} 228}
178 229
179 230
@@ -199,8 +250,8 @@ check_value (void *cls,
199 crc->phase, 250 crc->phase,
200 crc->i); 251 crc->i);
201 crc->phase = RP_ERROR; 252 crc->phase = RP_ERROR;
202 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc, 253 GNUNET_SCHEDULER_add_now (&run_continuation,
203 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 254 crc);
204 return; 255 return;
205 } 256 }
206#if 0 257#if 0
@@ -225,8 +276,8 @@ check_value (void *cls,
225 crc->phase = RP_DEL; 276 crc->phase = RP_DEL;
226 crc->i = ITERATIONS; 277 crc->i = ITERATIONS;
227 } 278 }
228 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc, 279 GNUNET_SCHEDULER_add_now (&run_continuation,
229 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 280 crc);
230} 281}
231 282
232 283
@@ -250,15 +301,20 @@ delete_value (void *cls,
250 crc->data = GNUNET_malloc (size); 301 crc->data = GNUNET_malloc (size);
251 memcpy (crc->data, data, size); 302 memcpy (crc->data, data, size);
252 crc->phase = RP_DO_DEL; 303 crc->phase = RP_DO_DEL;
253 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc, 304 GNUNET_SCHEDULER_add_now (&run_continuation,
254 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 305 crc);
255} 306}
256 307
257 308
258static void 309static void
259check_nothing (void *cls, const struct GNUNET_HashCode * key, size_t size, 310check_nothing (void *cls,
260 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, 311 const struct GNUNET_HashCode *key,
261 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration, 312 size_t size,
313 const void *data,
314 enum GNUNET_BLOCK_Type type,
315 uint32_t priority,
316 uint32_t anonymity,
317 struct GNUNET_TIME_Absolute expiration,
262 uint64_t uid) 318 uint64_t uid)
263{ 319{
264 struct CpsRunContext *crc = cls; 320 struct CpsRunContext *crc = cls;
@@ -266,8 +322,8 @@ check_nothing (void *cls, const struct GNUNET_HashCode * key, size_t size,
266 GNUNET_assert (key == NULL); 322 GNUNET_assert (key == NULL);
267 if (crc->i == 0) 323 if (crc->i == 0)
268 crc->phase = RP_RESERVE; 324 crc->phase = RP_RESERVE;
269 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc, 325 GNUNET_SCHEDULER_add_now (&run_continuation,
270 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 326 crc);
271} 327}
272 328
273 329
@@ -303,8 +359,7 @@ check_multiple (void *cls,
303 } 359 }
304 if (priority == get_priority (42)) 360 if (priority == get_priority (42))
305 crc->uid = uid; 361 crc->uid = uid;
306 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc, 362 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
307 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
308} 363}
309 364
310 365
@@ -330,11 +385,17 @@ check_update (void *cls,
330 GNUNET_assert (size == get_size (43)); 385 GNUNET_assert (size == get_size (43));
331 crc->offset++; 386 crc->offset++;
332 } 387 }
333 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc, 388 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
334 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
335} 389}
336 390
337 391
392/**
393 * Main state machine. Executes the next step of the test
394 * depending on the current state.
395 *
396 * @param cls the `struct CpsRunContext`
397 * @param tc scheduler context (unused)
398 */
338static void 399static void
339run_continuation (void *cls, 400run_continuation (void *cls,
340 const struct GNUNET_SCHEDULER_TaskContext *tc) 401 const struct GNUNET_SCHEDULER_TaskContext *tc)
@@ -430,43 +491,68 @@ run_continuation (void *cls,
430 break; 491 break;
431 case RP_PUT_MULTIPLE_NEXT: 492 case RP_PUT_MULTIPLE_NEXT:
432 crc->phase = RP_GET_MULTIPLE; 493 crc->phase = RP_GET_MULTIPLE;
433 GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (43), 494 GNUNET_DATASTORE_put (datastore, crc->rid,
434 get_data (43), get_type (42), get_priority (43), 495 &crc->key,
435 get_anonymity (43), 0, get_expiration (43), 1, 1, 496 get_size (43),
436 TIMEOUT, &check_success, crc); 497 get_data (43),
498 get_type (42),
499 get_priority (43),
500 get_anonymity (43),
501 0,
502 get_expiration (43),
503 1, 1,
504 TIMEOUT,
505 &check_success, crc);
437 break; 506 break;
438 case RP_GET_MULTIPLE: 507 case RP_GET_MULTIPLE:
439 GNUNET_assert (NULL != 508 GNUNET_assert (NULL !=
440 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key, 509 GNUNET_DATASTORE_get_key (datastore,
441 get_type (42), 1, 1, TIMEOUT, 510 crc->offset,
511 &crc->key,
512 get_type (42), 1, 1,
513 TIMEOUT,
442 &check_multiple, crc)); 514 &check_multiple, crc));
443 break; 515 break;
444 case RP_GET_MULTIPLE_NEXT: 516 case RP_GET_MULTIPLE_NEXT:
445 GNUNET_assert (NULL != 517 GNUNET_assert (NULL !=
446 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key, 518 GNUNET_DATASTORE_get_key (datastore,
447 get_type (42), 1, 1, TIMEOUT, 519 crc->offset,
520 &crc->key,
521 get_type (42),
522 1, 1,
523 TIMEOUT,
448 &check_multiple, crc)); 524 &check_multiple, crc));
449 break; 525 break;
450 case RP_UPDATE: 526 case RP_UPDATE:
451 GNUNET_assert (crc->uid > 0); 527 GNUNET_assert (crc->uid > 0);
452 crc->phase = RP_UPDATE_VALIDATE; 528 crc->phase = RP_UPDATE_VALIDATE;
453 GNUNET_DATASTORE_update (datastore, crc->uid, 100, get_expiration (42), 1, 529 GNUNET_DATASTORE_update (datastore,
454 1, TIMEOUT, &check_success, crc); 530 crc->uid, 100,
531 get_expiration (42), 1,
532 1, TIMEOUT,
533 &check_success, crc);
455 break; 534 break;
456 case RP_UPDATE_VALIDATE: 535 case RP_UPDATE_VALIDATE:
457 GNUNET_assert (NULL != 536 GNUNET_assert (NULL !=
458 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key, 537 GNUNET_DATASTORE_get_key (datastore,
459 get_type (42), 1, 1, TIMEOUT, 538 crc->offset,
539 &crc->key,
540 get_type (42),
541 1, 1,
542 TIMEOUT,
460 &check_update, crc)); 543 &check_update, crc));
461 break; 544 break;
462 case RP_DONE: 545 case RP_DONE:
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n"); 546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
464 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES); 547 "Finished, disconnecting\n");
548 GNUNET_DATASTORE_disconnect (datastore,
549 GNUNET_YES);
465 GNUNET_free (crc); 550 GNUNET_free (crc);
466 ok = 0; 551 ok = 0;
467 break; 552 break;
468 case RP_ERROR: 553 case RP_ERROR:
469 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES); 554 GNUNET_DATASTORE_disconnect (datastore,
555 GNUNET_YES);
470 GNUNET_free (crc); 556 GNUNET_free (crc);
471 ok = 43; 557 ok = 43;
472 break; 558 break;
@@ -474,6 +560,18 @@ run_continuation (void *cls,
474} 560}
475 561
476 562
563/**
564 * Function called with the result of the initial PUT operation. If
565 * the PUT succeeded, we start the actual benchmark loop, otherwise we
566 * bail out with an error.
567 *
568 *
569 * @param cls closure
570 * @param success #GNUNET_SYSERR on failure
571 * @param min_expiration minimum expiration time required for content to be stored
572 * by the datacache at this time, zero for unknown
573 * @param msg NULL on success, otherwise an error message
574 */
477static void 575static void
478run_tests (void *cls, 576run_tests (void *cls,
479 int32_t success, 577 int32_t success,
@@ -485,20 +583,22 @@ run_tests (void *cls,
485 switch (success) 583 switch (success)
486 { 584 {
487 case GNUNET_YES: 585 case GNUNET_YES:
488 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc, 586 GNUNET_SCHEDULER_add_now (&run_continuation,
489 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 587 crc);
490 return; 588 return;
491 case GNUNET_NO: 589 case GNUNET_NO:
492 FPRINTF (stderr, 590 FPRINTF (stderr,
493 "%s", "Test 'put' operation failed, key already exists (!?)\n"); 591 "%s", "Test 'put' operation failed, key already exists (!?)\n");
494 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES); 592 GNUNET_DATASTORE_disconnect (datastore,
593 GNUNET_YES);
495 GNUNET_free (crc); 594 GNUNET_free (crc);
496 return; 595 return;
497 case GNUNET_SYSERR: 596 case GNUNET_SYSERR:
498 FPRINTF (stderr, 597 FPRINTF (stderr,
499 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n", 598 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
500 msg); 599 msg);
501 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES); 600 GNUNET_DATASTORE_disconnect (datastore,
601 GNUNET_YES);
502 GNUNET_free (crc); 602 GNUNET_free (crc);
503 return; 603 return;
504 default: 604 default:
@@ -507,6 +607,15 @@ run_tests (void *cls,
507} 607}
508 608
509 609
610/**
611 * Beginning of the actual execution of the benchmark.
612 * Performs a first test operation (PUT) to verify that
613 * the plugin works at all.
614 *
615 * @param cls NULL
616 * @param cfg configuration to use
617 * @param peer peer handle (unused)
618 */
510static void 619static void
511run (void *cls, 620run (void *cls,
512 const struct GNUNET_CONFIGURATION_Handle *cfg, 621 const struct GNUNET_CONFIGURATION_Handle *cfg,
@@ -516,32 +625,52 @@ run (void *cls,
516 static struct GNUNET_HashCode zkey; 625 static struct GNUNET_HashCode zkey;
517 626
518 crc = GNUNET_new (struct CpsRunContext); 627 crc = GNUNET_new (struct CpsRunContext);
519 crc->cfg = cfg;
520 crc->phase = RP_PUT; 628 crc->phase = RP_PUT;
521 now = GNUNET_TIME_absolute_get (); 629 now = GNUNET_TIME_absolute_get ();
522 datastore = GNUNET_DATASTORE_connect (cfg); 630 datastore = GNUNET_DATASTORE_connect (cfg);
523 if (NULL == 631 if (NULL ==
524 GNUNET_DATASTORE_put (datastore, 0, &zkey, 4, "TEST", 632 GNUNET_DATASTORE_put (datastore,
525 GNUNET_BLOCK_TYPE_TEST, 0, 0, 0, 633 0,
634 &zkey,
635 4,
636 "TEST",
637 GNUNET_BLOCK_TYPE_TEST,
638 0, 0, 0,
526 GNUNET_TIME_relative_to_absolute 639 GNUNET_TIME_relative_to_absolute
527 (GNUNET_TIME_UNIT_SECONDS), 0, 1, 640 (GNUNET_TIME_UNIT_SECONDS),
528 GNUNET_TIME_UNIT_MINUTES, &run_tests, crc)) 641 0, 1,
642 GNUNET_TIME_UNIT_MINUTES,
643 &run_tests, crc))
529 { 644 {
530 FPRINTF (stderr, "%s", "Test 'put' operation failed.\n"); 645 FPRINTF (stderr,
646 "%s",
647 "Test 'put' operation failed.\n");
531 ok = 1; 648 ok = 1;
532 GNUNET_free (crc); 649 GNUNET_free (crc);
533 } 650 }
534} 651}
535 652
536 653
654/**
655 * Entry point into the test. Determines which configuration / plugin
656 * we are running with based on the name of the binary and starts
657 * the peer.
658 *
659 * @param argc should be 1
660 * @param argv used to determine plugin / configuration name.
661 * @return 0 on success
662 */
537int 663int
538main (int argc, char *argv[]) 664main (int argc,
665 char *argv[])
539{ 666{
540 char cfg_name[128]; 667 char cfg_name[128];
541 668
542 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]); 669 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
543 GNUNET_snprintf (cfg_name, sizeof (cfg_name), 670 GNUNET_snprintf (cfg_name,
544 "test_datastore_api_data_%s.conf", plugin_name); 671 sizeof (cfg_name),
672 "test_datastore_api_data_%s.conf",
673 plugin_name);
545 if (0 != 674 if (0 !=
546 GNUNET_TESTING_peer_run ("test-gnunet-datastore", 675 GNUNET_TESTING_peer_run ("test-gnunet-datastore",
547 cfg_name, 676 cfg_name,