2 This file is part of GNUnet.
3 (C) 2010,2011 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 ats/test_ats_api_scheduling_reset_backoff.c
22 * @brief test case for blocking suggests and blocking reset API
23 * measure duration of initial suggest, measure blocking duration,
24 * reset block, measure suggest, compare time
25 * @author Christian Grothoff
26 * @author Matthias Wachs
29 #include "gnunet_ats_service.h"
30 #include "gnunet_testing_lib-new.h"
32 #include "test_ats_api_common.h"
34 #define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 10)
36 static GNUNET_SCHEDULER_TaskIdentifier die_task;
38 static GNUNET_SCHEDULER_TaskIdentifier wait_task;
43 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
53 static struct Test_Address test_addr;
58 static struct PeerContext p;
63 struct GNUNET_HELLO_Address test_hello_address;
68 static void *test_session;
73 struct GNUNET_ATS_Information test_ats_info[2];
78 uint32_t test_ats_count;
81 struct GNUNET_TIME_Absolute initial_start;
83 struct GNUNET_TIME_Relative initial_duration;
88 struct GNUNET_TIME_Absolute block_start;
90 struct GNUNET_TIME_Relative block_duration;
92 struct GNUNET_TIME_Absolute reset_block_start;
94 struct GNUNET_TIME_Relative reset_block_duration;
97 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
99 die_task = GNUNET_SCHEDULER_NO_TASK;
100 if (wait_task != GNUNET_SCHEDULER_NO_TASK)
102 GNUNET_SCHEDULER_cancel (wait_task);
103 wait_task = GNUNET_SCHEDULER_NO_TASK;
105 if (sched_ats != NULL)
106 GNUNET_ATS_scheduling_done (sched_ats);
107 free_test_address (&test_addr);
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
116 if (die_task != GNUNET_SCHEDULER_NO_TASK)
118 GNUNET_SCHEDULER_cancel (die_task);
119 die_task = GNUNET_SCHEDULER_NO_TASK;
121 if (wait_task != GNUNET_SCHEDULER_NO_TASK)
123 GNUNET_SCHEDULER_cancel (wait_task);
124 wait_task = GNUNET_SCHEDULER_NO_TASK;
126 GNUNET_ATS_scheduling_done (sched_ats);
128 free_test_address (&test_addr);
132 request_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
134 wait_task = GNUNET_SCHEDULER_NO_TASK;
135 GNUNET_ATS_suggest_address (sched_ats, &p.id);
136 wait_task = GNUNET_SCHEDULER_add_delayed (WAIT, &request_task, NULL);
140 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
141 struct Session *session,
142 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
143 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
144 const struct GNUNET_ATS_Information *atsi,
147 static int stage = 0;
149 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %u\n", stage);
152 /* Suggestion after resetting block interval */
153 reset_block_duration = GNUNET_TIME_absolute_get_difference(reset_block_start, GNUNET_TIME_absolute_get());
154 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Address suggestion after resetting blocking took about %llu ms!\n",
155 (long long unsigned int) reset_block_duration.rel_value);
156 if ((block_duration.rel_value <= (initial_duration.rel_value * 3)) ||
157 (initial_duration.rel_value <= (block_duration.rel_value * 3)))
159 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
160 "Address suggestion after resetting blocking (%llu ms) took about the same as initial suggestion (%llu ms)\n",
161 (long long unsigned int) reset_block_duration.rel_value,
162 (long long unsigned int) initial_duration.rel_value);
167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Address suggestion after resetting blocking (%llu ms) has too big difference to initial suggestion (%llu ms)\n",
168 (long long unsigned int) reset_block_duration.rel_value,
169 (long long unsigned int) initial_duration.rel_value);
171 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
172 GNUNET_SCHEDULER_add_now (&end, NULL);
176 if (((initial_duration.rel_value * 3) <= block_duration.rel_value ) &&
177 ((reset_block_duration.rel_value * 3) <= block_duration.rel_value))
179 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Address suggestion after resetting blocking (%llu ms) and initial suggestion (%llu ms) much faster than with blocking (%llu ms)\n",
180 (long long unsigned int) reset_block_duration.rel_value,
181 (long long unsigned int) initial_duration.rel_value,
182 (long long unsigned int) block_duration.rel_value);
187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Address suggestion after resetting blocking (%llu ms) and initial suggestion (%llu ms) not faster than with blocking (%llu ms)\n",
188 (long long unsigned int) reset_block_duration.rel_value,
189 (long long unsigned int) initial_duration.rel_value,
190 (long long unsigned int) block_duration.rel_value);
195 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
196 GNUNET_SCHEDULER_add_now (&end, NULL);
201 /* Suggestion after block*/
202 block_duration = GNUNET_TIME_absolute_get_difference(block_start, GNUNET_TIME_absolute_get());
203 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Address suggestion was blocked for about %llu ms!\n",
204 (long long unsigned int) block_duration.rel_value);
206 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
209 GNUNET_i2s (&address->peer));
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with invalid address `%s'\n",
215 GNUNET_i2s (&address->peer));
216 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
217 GNUNET_SCHEDULER_add_now (&end, NULL);
221 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 0: Callback with incorrect ats info \n");
224 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
225 GNUNET_SCHEDULER_add_now (&end, NULL);
230 /* Reset block interval */
231 GNUNET_ATS_reset_backoff (sched_ats, &address->peer);
232 reset_block_start = GNUNET_TIME_absolute_get();
233 GNUNET_ATS_suggest_address (sched_ats, &p.id);
237 /* Initial suggestion */
238 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
241 GNUNET_i2s (&address->peer));
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with invalid address `%s'\n",
247 GNUNET_i2s (&address->peer));
248 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
249 GNUNET_SCHEDULER_add_now (&end, NULL);
253 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 0: Callback with incorrect ats info \n");
256 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
257 GNUNET_SCHEDULER_add_now (&end, NULL);
261 initial_duration = GNUNET_TIME_absolute_get_difference(initial_start, GNUNET_TIME_absolute_get());
262 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Initial suggestion took about %llu ms\n",
263 (long long unsigned int) block_duration.rel_value);
265 block_start = GNUNET_TIME_absolute_get();
266 wait_task = GNUNET_SCHEDULER_add_delayed (WAIT, &request_task, NULL);
273 const struct GNUNET_CONFIGURATION_Handle *cfg,
274 struct GNUNET_TESTING_Peer *peer)
277 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
279 /* Connect to ATS scheduling */
280 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
281 if (sched_ats == NULL)
283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
290 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.id.hashPubKey))
292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
297 GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p.id)));
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
300 GNUNET_i2s_full(&p.id));
302 /* Prepare ATS Information */
303 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
304 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
305 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
306 test_ats_info[1].value = htonl(1);
309 /* Adding address without session */
310 test_session = &test_addr;
311 create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
312 test_hello_address.peer = p.id;
313 test_hello_address.transport_name = test_addr.plugin;
314 test_hello_address.address = test_addr.addr;
315 test_hello_address.address_length = test_addr.addr_len;
316 GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
318 initial_start = GNUNET_TIME_absolute_get();
319 GNUNET_ATS_suggest_address (sched_ats, &p.id);
324 main (int argc, char *argv[])
326 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
332 /* end of file test_ats_api_scheduling_reset_backoff.c */