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_destroy_session.c
22 * @brief test destroying sessions: first add an address with a session,
23 * request the address and compare, delete the session, request and
24 * compare again, delete whole address, request and wait for timeout,
26 * @author Christian Grothoff
27 * @author Matthias Wachs
31 #include "gnunet_ats_service.h"
32 #include "gnunet_testing_lib.h"
34 #include "test_ats_api_common.h"
39 static GNUNET_SCHEDULER_TaskIdentifier die_task;
44 struct GNUNET_STATISTICS_Handle *stats;
49 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
59 static struct Test_Address test_addr;
64 static struct PeerContext p;
69 struct GNUNET_HELLO_Address test_hello_address;
74 static void *test_session;
79 struct GNUNET_ATS_Information test_ats_info[2];
84 uint32_t test_ats_count;
87 static void end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
88 static void end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
91 stat_cb(void *cls, const char *subsystem,
92 const char *name, uint64_t value,
95 static int first_stat_cb = GNUNET_YES;
97 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
98 subsystem,name, value);
99 if ((GNUNET_YES == first_stat_cb) && (1 == value))
101 GNUNET_ATS_address_add (sched_ats, &test_hello_address, (struct Session *) &test_session, test_ats_info, test_ats_count);
102 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end, NULL);
104 if ((GNUNET_NO == first_stat_cb) && (1 == value))
106 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS updated existing address\n");
110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS did not update existing address, but added 2nd address!\n");
111 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
119 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
123 if (die_task != GNUNET_SCHEDULER_NO_TASK)
125 GNUNET_SCHEDULER_cancel (die_task);
126 die_task = GNUNET_SCHEDULER_NO_TASK;
129 if (NULL != sched_ats)
131 GNUNET_ATS_scheduling_done (sched_ats);
135 GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
138 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
142 free_test_address (&test_addr);
148 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
150 die_task = GNUNET_SCHEDULER_NO_TASK;
156 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
157 struct Session *session,
158 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
159 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
160 const struct GNUNET_ATS_Information *atsi,
163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not expect suggestion callback!\n");
164 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
171 const struct GNUNET_CONFIGURATION_Handle *cfg,
172 struct GNUNET_TESTING_Peer *peer)
174 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
175 stats = GNUNET_STATISTICS_create ("ats", cfg);
176 GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
179 /* Connect to ATS scheduling */
180 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
181 if (sched_ats == NULL)
183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
184 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
189 memset (&p.id, '1', sizeof (p.id));
190 /* Prepare ATS Information */
191 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
192 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
193 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
194 test_ats_info[1].value = htonl(1);
197 /* Adding address without session */
198 create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
199 test_hello_address.peer = p.id;
200 test_hello_address.transport_name = test_addr.plugin;
201 test_hello_address.address = test_addr.addr;
202 test_hello_address.address_length = test_addr.addr_len;
205 GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL, test_ats_info, test_ats_count);
210 main (int argc, char *argv[])
213 if (0 != GNUNET_TESTING_peer_run ("test-ats-api",
219 /* end of file test_ats_api_scheduling_destroy_session.c */