making GNUNET_SCHEDULER_cancel() perform in O(1) instead of O(n) to help or even...
[oweals/gnunet.git] / src / testbed / test_testbed_api_peers_manage_services.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2013 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 /**
22  * @file testbed/test_testbed_api_peers_manage_services.c
23  * @brief testcase for testing GNUNET_TESTBED_peer_manage_service()
24  *          implementation
25  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31
32 /**
33  * Number of peers we want to start
34  */
35 #define NUM_PEERS 2
36
37 /**
38  * The array of peers; we get them from the testbed
39  */
40 static struct GNUNET_TESTBED_Peer **peers;
41
42 /**
43  * Operation handle
44  */
45 static struct GNUNET_TESTBED_Operation *op;
46
47 /**
48  * dummy pointer
49  */
50 static void *dummy_cls = (void *) 0xDEAD0001;
51
52 /**
53  * Abort task identifier
54  */
55 static struct GNUNET_SCHEDULER_Task * abort_task;
56
57 /**
58  * States in this test
59  */
60 enum {
61
62   /**
63    * Test has just been initialized
64    */
65   STATE_INIT,
66
67   /**
68    * Peers have been started
69    */
70   STATE_PEERS_STARTED,
71
72   /**
73    * statistics service went down
74    */
75   STATE_SERVICE_DOWN,
76
77   /**
78    * statistics service went up
79    */
80   STATE_SERVICE_UP,
81
82   /**
83    * Testing completed successfully
84    */
85   STATE_OK
86 } state;
87
88 /**
89  * Fail testcase
90  */
91 #define FAIL_TEST(cond, ret) do {                               \
92     if (!(cond)) {                                              \
93       GNUNET_break(0);                                          \
94       if (NULL != abort_task)               \
95         GNUNET_SCHEDULER_cancel (abort_task);                   \
96       abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);  \
97       ret;                                                      \
98     }                                                           \
99   } while (0)
100
101
102 /**
103  * Abort task
104  *
105  * @param cls NULL
106  * @param tc scheduler task context
107  */
108 static void
109 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
110 {
111   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Aborting\n");
112   abort_task = NULL;
113   if (NULL != op)
114   {
115     GNUNET_TESTBED_operation_done (op);
116     op = NULL;
117   }
118   GNUNET_SCHEDULER_shutdown();
119 }
120
121
122 /**
123  * Callback to be called when an operation is completed
124  *
125  * @param cls the callback closure from functions generating an operation
126  * @param op the operation that has been finished
127  * @param emsg error message in case the operation has failed; will be NULL if
128  *          operation has executed successfully.
129  */
130 static void
131 op_comp_cb (void *cls,
132             struct GNUNET_TESTBED_Operation *op,
133             const char *emsg)
134 {
135   FAIL_TEST (cls == dummy_cls, return);
136   FAIL_TEST (NULL == emsg, return);
137   GNUNET_TESTBED_operation_done (op);
138   op = NULL;
139   switch (state)
140   {
141   case STATE_PEERS_STARTED:
142     state = STATE_SERVICE_DOWN;
143     op = GNUNET_TESTBED_peer_manage_service (dummy_cls,
144                                              peers[1],
145                                              "topology",
146                                              op_comp_cb,
147                                              dummy_cls,
148                                              0);
149     GNUNET_assert (NULL != op);
150     break;
151   case STATE_SERVICE_DOWN:
152     state = STATE_SERVICE_UP;
153     GNUNET_SCHEDULER_cancel (abort_task);
154     abort_task = NULL;
155     state = STATE_OK;
156     GNUNET_SCHEDULER_shutdown ();
157     break;
158   default:
159     FAIL_TEST (0, return);
160   }
161 }
162
163
164 /**
165  * Signature of a main function for a testcase.
166  *
167  * @param cls closure
168  * @param h the run handle
169  * @param num_peers number of peers in 'peers'
170  * @param peers_ handle to peers run in the testbed
171  * @param links_succeeded the number of overlay link connection attempts that
172  *          succeeded
173  * @param links_failed the number of overlay link connection attempts that
174  *          failed
175  */
176 static void
177 test_master (void *cls,
178              struct GNUNET_TESTBED_RunHandle *h,
179              unsigned int num_peers,
180              struct GNUNET_TESTBED_Peer **peers_,
181              unsigned int links_succeeded,
182              unsigned int links_failed)
183 {
184   FAIL_TEST (NUM_PEERS == num_peers, return);
185   state = STATE_PEERS_STARTED;
186   peers = peers_;
187   op = GNUNET_TESTBED_peer_manage_service (dummy_cls,
188                                            peers[1],
189                                            "topology",
190                                            op_comp_cb,
191                                            dummy_cls,
192                                            1);
193   FAIL_TEST (NULL != op, return);
194   abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
195                                              (GNUNET_TIME_UNIT_MINUTES, 1),
196                                              &do_abort, NULL);
197 }
198
199
200 /**
201  * Main function
202  */
203 int
204 main (int argc, char **argv)
205 {
206   state = STATE_INIT;
207   (void) GNUNET_TESTBED_test_run ("test_testbed_api_peers_manage_services",
208                                   "test_testbed_api.conf",
209                                   NUM_PEERS,
210                                   1LL, NULL, NULL,
211                                   &test_master, NULL);
212   if (STATE_OK != state)
213     return 1;
214   return 0;
215 }