stagger shutdown
[oweals/gnunet.git] / src / testing / test_testing_group_remote.c
1 /*
2      This file is part of GNUnet.
3      (C) 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 testing/test_testing_group_remote.c
22  * @brief testcase for testing remote and local starting and connecting
23  *        of hosts from the testing library.  The test_testing_data_remote.conf
24  *        file should be modified if this testcase is intended to be used.
25  */
26 #include "platform.h"
27 #include "gnunet_testing_lib.h"
28
29 #define VERBOSE GNUNET_YES
30
31
32 /**
33  * How long until we give up on connecting the peers?
34  */
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
36
37 #define DEFAULT_NUM_PEERS 8;
38
39 static int ok;
40
41 static int peers_left;
42
43 static int peers_failed;
44
45 static struct GNUNET_TESTING_PeerGroup *pg;
46
47 static struct GNUNET_SCHEDULER_Handle *sched;
48
49 static unsigned long long num_peers;
50
51 static char *hostnames;
52
53 /**
54  * Check whether peers successfully shut down.
55  */
56 void shutdown_callback (void *cls,
57                         const char *emsg)
58 {
59   if (emsg != NULL)
60     {
61 #if VERBOSE
62       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
63                   "Shutdown of peers failed!\n");
64 #endif
65       if (ok == 0)
66         ok = 666;
67     }
68   else
69     {
70 #if VERBOSE
71       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
72                   "All peers successfully shut down!\n");
73 #endif
74     }
75 }
76
77
78 static void
79 my_cb (void *cls,
80        const struct GNUNET_PeerIdentity *id,
81        const struct GNUNET_CONFIGURATION_Handle *cfg,
82        struct GNUNET_TESTING_Daemon *d, const char *emsg)
83 {
84   if (emsg != NULL)
85     {
86       peers_failed++;
87     }
88
89   peers_left--;
90   if (peers_left == 0)
91     {
92       GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
93       ok = 0;
94     }
95   else if (peers_failed == peers_left)
96     {
97       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Too many peers failed, ending test!\n");
98       GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
99     }
100 }
101
102
103 static void
104 run (void *cls,
105      struct GNUNET_SCHEDULER_Handle *s,
106      char *const *args,
107      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
108 {
109   sched = s;
110   ok = 1;
111 #if VERBOSE
112   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
113 #endif
114
115   if (GNUNET_SYSERR ==
116       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
117                                              &num_peers))
118     num_peers = DEFAULT_NUM_PEERS;
119
120   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "hosts",
121                                          &hostnames))
122   {
123     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No hosts specified, running all tests on localhost\n");
124   }
125
126
127   peers_left = num_peers;
128   pg = GNUNET_TESTING_daemons_start (sched,
129                                      cfg,
130                                      peers_left,
131                                      TIMEOUT,
132                                      NULL,
133                                      NULL,
134                                      &my_cb,
135                                      NULL,
136                                      NULL,
137                                      NULL,
138                                      hostnames);
139   GNUNET_assert (pg != NULL);
140 }
141
142 static int
143 check ()
144 {
145   char *const argv[] = { "test-testing",
146     "-c",
147     "test_testing_data_remote.conf",
148 #if VERBOSE
149     "-L", "DEBUG",
150 #endif
151     NULL
152   };
153   struct GNUNET_GETOPT_CommandLineOption options[] = {
154     GNUNET_GETOPT_OPTION_END
155   };
156   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
157                       argv, "test-testing-group", "nohelp",
158                       options, &run, &ok);
159   return ok;
160 }
161
162 int
163 main (int argc, char *argv[])
164 {
165   int ret;
166
167   GNUNET_log_setup ("test-testing-group",
168 #if VERBOSE
169                     "DEBUG",
170 #else
171                     "WARNING",
172 #endif
173                     NULL);
174   ret = check ();
175   /**
176    * Still need to remove the base testing directory here,
177    * because group starts will create subdirectories under this
178    * main dir.  However, we no longer need to sleep, as the
179    * shutdown sequence won't return until everything is cleaned
180    * up.
181    */
182   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
183   return ret;
184 }
185
186 /* end of test_testing_group.c */