aboutsummaryrefslogtreecommitdiff
path: root/src/dht/test_dht_multipeer.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-09-29 14:21:05 +0000
committerChristian Grothoff <christian@grothoff.org>2011-09-29 14:21:05 +0000
commit46dfa72665e1fcd10c1e98957de6a732f5991c13 (patch)
treee0adcf48aedc085b052d63997710cae685b58719 /src/dht/test_dht_multipeer.c
parent74072524b5e04db6377feec4f13becc7b312722a (diff)
downloadgnunet-46dfa72665e1fcd10c1e98957de6a732f5991c13.tar.gz
gnunet-46dfa72665e1fcd10c1e98957de6a732f5991c13.zip
simplify code
Diffstat (limited to 'src/dht/test_dht_multipeer.c')
-rw-r--r--src/dht/test_dht_multipeer.c353
1 files changed, 39 insertions, 314 deletions
diff --git a/src/dht/test_dht_multipeer.c b/src/dht/test_dht_multipeer.c
index 96fc8ed86..0cae62038 100644
--- a/src/dht/test_dht_multipeer.c
+++ b/src/dht/test_dht_multipeer.c
@@ -31,10 +31,10 @@
31#define VERBOSE GNUNET_NO 31#define VERBOSE GNUNET_NO
32 32
33/* Timeout for entire testcase */ 33/* Timeout for entire testcase */
34#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5) 34#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 30)
35 35
36/* Timeout for waiting for replies to get requests */ 36/* Timeout for waiting for replies to get requests */
37#define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90) 37#define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
38 38
39/* Timeout for waiting for gets to complete */ 39/* Timeout for waiting for gets to complete */
40#define GET_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1) 40#define GET_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1)
@@ -42,8 +42,6 @@
42/* Timeout for waiting for puts to complete */ 42/* Timeout for waiting for puts to complete */
43#define PUT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1) 43#define PUT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1)
44 44
45#define SECONDS_PER_PEER_START 45
46
47/* If number of peers not in config file, use this number */ 45/* If number of peers not in config file, use this number */
48#define DEFAULT_NUM_PEERS 10 46#define DEFAULT_NUM_PEERS 10
49 47
@@ -134,26 +132,10 @@ struct TestGetContext *all_gets;
134struct TestPutContext *all_puts; 132struct TestPutContext *all_puts;
135 133
136/** 134/**
137 * Directory to store temp data in, defined in config file
138 */
139static char *test_directory;
140
141/**
142 * Variable used to store the number of connections we should wait for.
143 */
144static unsigned int expected_connections;
145
146/**
147 * Variable used to keep track of how many peers aren't yet started.
148 */
149static unsigned long long peers_left;
150
151/**
152 * Handle to the set of all peers run for this test. 135 * Handle to the set of all peers run for this test.
153 */ 136 */
154static struct GNUNET_TESTING_PeerGroup *pg; 137static struct GNUNET_TESTING_PeerGroup *pg;
155 138
156
157/** 139/**
158 * Total number of peers to run, set based on config file. 140 * Total number of peers to run, set based on config file.
159 */ 141 */
@@ -185,37 +167,20 @@ static unsigned long long gets_completed;
185static unsigned long long gets_failed; 167static unsigned long long gets_failed;
186 168
187/** 169/**
188 * Global used to count how many connections we have currently 170 * Directory to remove on shutdown.
189 * been notified about (how many times has topology_callback been called
190 * with success?)
191 */ 171 */
192static unsigned int total_connections; 172static char *test_directory;
193 173
194/** 174/**
195 * Global used to count how many failed connections we have 175 * Option to use when routing.
196 * been notified about (how many times has topology_callback
197 * been called with failure?)
198 */ 176 */
199static unsigned int failed_connections;
200
201static enum GNUNET_DHT_RouteOption route_option; 177static enum GNUNET_DHT_RouteOption route_option;
202 178
203/* Task handle to use to schedule test failure */ 179/**
180 * Task handle to use to schedule test failure / success.
181 */
204static GNUNET_SCHEDULER_TaskIdentifier die_task; 182static GNUNET_SCHEDULER_TaskIdentifier die_task;
205 183
206static char *blacklist_transports;
207
208static enum GNUNET_TESTING_Topology topology;
209
210static enum GNUNET_TESTING_Topology blacklist_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* Don't do any blacklisting */
211
212static enum GNUNET_TESTING_Topology connection_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
213
214static enum GNUNET_TESTING_TopologyOption connect_topology_option =
215 GNUNET_TESTING_TOPOLOGY_OPTION_ALL;
216
217static double connect_topology_option_modifier = 0.0;
218
219/* Global return value (0 for success, anything else for failure) */ 184/* Global return value (0 for success, anything else for failure) */
220static int ok; 185static int ok;
221 186
@@ -227,6 +192,9 @@ shutdown_callback (void *cls, const char *emsg)
227{ 192{
228 if (emsg != NULL) 193 if (emsg != NULL)
229 { 194 {
195 fprintf (stderr,
196 "Failed to shutdown testing topology: %s\n",
197 emsg);
230 if (ok == 0) 198 if (ok == 0)
231 ok = 2; 199 ok = 2;
232 } 200 }
@@ -545,22 +513,14 @@ do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
545 513
546 514
547/** 515/**
548 * This function is called whenever a connection attempt is finished between two of 516 * This function is called once testing has finished setting up the topology.
549 * the started peers (started with GNUNET_TESTING_daemons_start). The total
550 * number of times this function is called should equal the number returned
551 * from the GNUNET_TESTING_connect_topology call.
552 * 517 *
553 * The emsg variable is NULL on success (peers connected), and non-NULL on 518 * @param cls unused
519 * @param emsg variable is NULL on success (peers connected), and non-NULL on
554 * failure (peers failed to connect). 520 * failure (peers failed to connect).
555 */ 521 */
556static void 522static void
557topology_callback (void *cls, const struct GNUNET_PeerIdentity *first, 523run_dht_test (void *cls, const char *emsg)
558 const struct GNUNET_PeerIdentity *second, uint32_t distance,
559 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
560 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
561 struct GNUNET_TESTING_Daemon *first_daemon,
562 struct GNUNET_TESTING_Daemon *second_daemon,
563 const char *emsg)
564{ 524{
565 unsigned long long i; 525 unsigned long long i;
566 unsigned long long j; 526 unsigned long long j;
@@ -568,33 +528,22 @@ topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
568 struct TestPutContext *test_put; 528 struct TestPutContext *test_put;
569 struct TestGetContext *test_get; 529 struct TestGetContext *test_get;
570 530
571 if (emsg == NULL) 531 if (emsg != NULL)
572 {
573 total_connections++;
574#if VERBOSE > 1
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576 "connected peer %s to peer %s, distance %u\n",
577 first_daemon->shortname, second_daemon->shortname, distance);
578#endif
579 }
580 else
581 { 532 {
582 failed_connections++;
583 fprintf (stderr, 533 fprintf (stderr,
584 "Failed to connect peer %s to peer %s with error :\n%s\n", 534 "Failed to setup topology: %s\n",
585 first_daemon->shortname, second_daemon->shortname, emsg); 535 emsg);
586 GNUNET_SCHEDULER_cancel (die_task);
587 die_task = 536 die_task =
588 GNUNET_SCHEDULER_add_now (&end_badly, 537 GNUNET_SCHEDULER_add_now (&end_badly,
589 "from topology_callback (connections failed)"); 538 "topology setup failed");
590 return; 539 return;
591 } 540 }
592 if (total_connections < expected_connections) 541
593 return; 542#if PATH_TRACKING
594 fprintf (stderr, 543 route_option = GNUNET_DHT_RO_RECORD_ROUTE;
595 "Created topology with %u connections\n", 544#else
596 total_connections); 545 route_option = GNUNET_DHT_RO_NONE;
597 GNUNET_SCHEDULER_cancel (die_task); 546#endif
598 die_task = 547 die_task =
599 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, 548 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
600 "from setup puts/gets"); 549 "from setup puts/gets");
@@ -628,143 +577,9 @@ topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
628 577
629 578
630static void 579static void
631peers_started_callback (void *cls, const struct GNUNET_PeerIdentity *id,
632 const struct GNUNET_CONFIGURATION_Handle *cfg,
633 struct GNUNET_TESTING_Daemon *d, const char *emsg)
634{
635 if (emsg != NULL)
636 {
637 fprintf (stderr,
638 "Failed to start daemon with error: `%s'\n", emsg);
639 return;
640 }
641 GNUNET_assert (id != NULL);
642
643#if VERBOSE
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
645 (num_peers - peers_left) + 1, num_peers);
646#endif
647
648 peers_left--;
649 if (peers_left > 0)
650 return;
651
652#if VERBOSE
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "All %d daemons started, now connecting peers!\n", num_peers);
655#endif
656
657 expected_connections = -1;
658 if ((pg != NULL) && (peers_left == 0))
659 {
660 expected_connections =
661 GNUNET_TESTING_connect_topology (pg, connection_topology,
662 connect_topology_option,
663 connect_topology_option_modifier,
664 TIMEOUT, num_peers, NULL, NULL);
665#if VERBOSE
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Have %d expected connections\n",
667 expected_connections);
668#endif
669 }
670
671 GNUNET_SCHEDULER_cancel (die_task);
672 if (expected_connections == GNUNET_SYSERR)
673 {
674 die_task =
675 GNUNET_SCHEDULER_add_now (&end_badly,
676 "from connect topology (bad return)");
677 }
678 else
679 {
680 die_task =
681 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
682 "from connect topology (timeout)");
683 }
684 ok = 0;
685}
686
687
688/**
689 * Callback indicating that the hostkey was created for a peer.
690 *
691 * @param cls NULL
692 * @param id the peer identity
693 * @param d the daemon handle (pretty useless at this point, remove?)
694 * @param emsg non-null on failure
695 */
696static void
697hostkey_callback (void *cls, const struct GNUNET_PeerIdentity *id,
698 struct GNUNET_TESTING_Daemon *d, const char *emsg)
699{
700 if (emsg != NULL)
701 {
702 fprintf (stderr,
703 "Hostkey callback received error: %s\n", emsg);
704 return;
705 }
706
707#if VERBOSE > 1
708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
709 "Hostkey (%d/%d) created for peer `%s'\n", num_peers - peers_left,
710 num_peers, GNUNET_i2s (id));
711#endif
712
713
714 peers_left--;
715 if (peers_left == 0)
716 {
717#if VERBOSE
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 "All %d hostkeys created, now creating topology!\n", num_peers);
720#endif
721 GNUNET_SCHEDULER_cancel (die_task);
722 /* Set up task in case topology creation doesn't finish
723 * within a reasonable amount of time */
724 ok = 0;
725 die_task =
726 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
727 "from create_topology");
728 peers_left = num_peers; /* Reset counter */
729 GNUNET_SCHEDULER_cancel (die_task);
730 if (GNUNET_TESTING_create_topology
731 (pg, topology, blacklist_topology, blacklist_transports) != GNUNET_SYSERR)
732 {
733#if VERBOSE
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
735 "Topology set up, now starting peers!\n");
736#endif
737 GNUNET_TESTING_daemons_continue_startup (pg);
738 die_task =
739 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
740 "from continue startup (timeout)");
741 }
742 else
743 {
744 die_task =
745 GNUNET_SCHEDULER_add_now (&end_badly,
746 "from create topology (bad return)");
747 }
748 }
749}
750
751
752static void
753run (void *cls, char *const *args, const char *cfgfile, 580run (void *cls, char *const *args, const char *cfgfile,
754 const struct GNUNET_CONFIGURATION_Handle *cfg) 581 const struct GNUNET_CONFIGURATION_Handle *cfg)
755{ 582{
756 char *topology_str;
757 char *connect_topology_str;
758 char *blacklist_topology_str;
759 char *connect_topology_option_str;
760 char *connect_topology_option_modifier_string;
761
762#if PATH_TRACKING
763 route_option = GNUNET_DHT_RO_RECORD_ROUTE;
764#else
765 route_option = GNUNET_DHT_RO_NONE;
766#endif
767
768 /* Get path from configuration file */ 583 /* Get path from configuration file */
769 if (GNUNET_YES != 584 if (GNUNET_YES !=
770 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome", 585 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
@@ -774,114 +589,22 @@ run (void *cls, char *const *args, const char *cfgfile,
774 ok = 404; 589 ok = 404;
775 return; 590 return;
776 } 591 }
777
778 if ((GNUNET_YES ==
779 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "topology",
780 &topology_str)) &&
781 (GNUNET_NO == GNUNET_TESTING_topology_get (&topology, topology_str)))
782 {
783 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
784 "Invalid topology `%s' given for section %s option %s\n",
785 topology_str, "TESTING", "TOPOLOGY");
786 topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Defaults to NONE, so set better default here */
787 }
788
789 if ((GNUNET_YES ==
790 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
791 "connect_topology",
792 &connect_topology_str)) &&
793 (GNUNET_NO ==
794 GNUNET_TESTING_topology_get (&connection_topology,
795 connect_topology_str)))
796 {
797 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
798 "Invalid connect topology `%s' given for section %s option %s\n",
799 connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
800 }
801 GNUNET_free_non_null (connect_topology_str);
802 if ((GNUNET_YES ==
803 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
804 "connect_topology_option",
805 &connect_topology_option_str)) &&
806 (GNUNET_NO ==
807 GNUNET_TESTING_topology_option_get (&connect_topology_option,
808 connect_topology_option_str)))
809 {
810 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
811 "Invalid connect topology option `%s' given for section %s option %s\n",
812 connect_topology_option_str, "TESTING",
813 "CONNECT_TOPOLOGY_OPTION");
814 connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Defaults to NONE, set to ALL */
815 }
816 GNUNET_free_non_null (connect_topology_option_str);
817 if (GNUNET_YES ==
818 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
819 "connect_topology_option_modifier",
820 &connect_topology_option_modifier_string))
821 {
822 if (sscanf
823 (connect_topology_option_modifier_string, "%lf",
824 &connect_topology_option_modifier) != 1)
825 {
826 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
827 _
828 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
829 connect_topology_option_modifier_string,
830 "connect_topology_option_modifier", "TESTING");
831 }
832 GNUNET_free (connect_topology_option_modifier_string);
833 }
834
835 if (GNUNET_YES !=
836 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
837 "blacklist_transports",
838 &blacklist_transports))
839 blacklist_transports = NULL;
840
841 if ((GNUNET_YES ==
842 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
843 "blacklist_topology",
844 &blacklist_topology_str)) &&
845 (GNUNET_NO ==
846 GNUNET_TESTING_topology_get (&blacklist_topology,
847 blacklist_topology_str)))
848 {
849 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
850 "Invalid topology `%s' given for section %s option %s\n",
851 topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
852 }
853 GNUNET_free_non_null (topology_str);
854 GNUNET_free_non_null (blacklist_topology_str);
855
856 /* Get number of peers to start from configuration */
857 if (GNUNET_SYSERR == 592 if (GNUNET_SYSERR ==
858 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers", 593 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
859 &num_peers)) 594 &num_peers))
860 num_peers = DEFAULT_NUM_PEERS; 595 num_peers = DEFAULT_NUM_PEERS;
861 596 pg = GNUNET_TESTING_peergroup_start (cfg,
862 /* Set peers_left so we know when all peers started */ 597 num_peers,
863 peers_left = num_peers; 598 TIMEOUT,
864 599 NULL,
865 /* Set up a task to end testing if peer start fails */ 600 &run_dht_test,
866 die_task = 601 NULL,
867 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 602 NULL);
868 (GNUNET_TIME_UNIT_SECONDS, 603 if (NULL == pg)
869 SECONDS_PER_PEER_START * num_peers), 604 {
870 &end_badly, 605 GNUNET_break (0);
871 "didn't generate all hostkeys within a reasonable amount of time!!!"); 606 return;
872 fprintf (stderr, 607 }
873 "Starting P2P network with %llu peers\n",
874 peers_left);
875 pg = GNUNET_TESTING_daemons_start (cfg, peers_left, /* Total number of peers */
876 peers_left, /* Number of outstanding connections */
877 peers_left, /* Number of parallel ssh connections, or peers being started at once */
878 GNUNET_TIME_relative_multiply
879 (GNUNET_TIME_UNIT_SECONDS,
880 SECONDS_PER_PEER_START * num_peers),
881 &hostkey_callback, NULL,
882 &peers_started_callback, NULL,
883 &topology_callback, NULL, NULL);
884
885} 608}
886 609
887 610
@@ -914,11 +637,13 @@ check ()
914 return ok; 637 return ok;
915} 638}
916 639
640
917int 641int
918main (int argc, char *argv[]) 642main (int argc, char *argv[])
919{ 643{
920 int ret; 644 int ret;
921 645
646
922 GNUNET_log_setup ("test-dht-multipeer", 647 GNUNET_log_setup ("test-dht-multipeer",
923#if VERBOSE 648#if VERBOSE
924 "DEBUG", 649 "DEBUG",