init test: ok
[oweals/gnunet.git] / src / ats / test_ats_api_scheduling_init.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 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 ats/test_ats_api_scheduling_init.c
22  * @brief test automatic transport selection scheduling API init/shutdown
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  *
26  */
27 #include "platform.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet_testing_lib.h"
30 #include "ats.h"
31
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
33 #define DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
34
35 static GNUNET_SCHEDULER_TaskIdentifier die_task;
36
37 static GNUNET_SCHEDULER_TaskIdentifier wait_task;
38
39 static struct GNUNET_ATS_SchedulingHandle *ats;
40
41 static int ret;
42
43 static void
44 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
45 {
46   die_task = GNUNET_SCHEDULER_NO_TASK;
47   if (GNUNET_SCHEDULER_NO_TASK != wait_task)
48   {
49     GNUNET_SCHEDULER_cancel (wait_task);
50     wait_task = GNUNET_SCHEDULER_NO_TASK;
51   }
52   if (ats != NULL)
53   {
54     GNUNET_ATS_scheduling_done (ats);
55     ats = NULL;
56   }
57   ret = 1;
58 }
59
60 static void
61 end_badly_now ()
62 {
63   if (die_task != GNUNET_SCHEDULER_NO_TASK)
64   {
65     GNUNET_SCHEDULER_cancel (die_task);
66     die_task = GNUNET_SCHEDULER_NO_TASK;
67   }
68   die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
69 }
70
71 static void
72 delay (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
73 {
74   static int v_delay = 5;
75   static int v_cur = 0;
76
77   if (v_cur < v_delay)
78   {
79     wait_task = GNUNET_SCHEDULER_NO_TASK;
80     wait_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &delay, NULL);
81     fprintf (stderr,".");
82     v_cur ++;
83     return;
84   }
85
86   fprintf (stderr,"\n");
87   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown ATS\n");
88   GNUNET_ATS_scheduling_done (ats);
89   ats = NULL;
90   if (die_task != GNUNET_SCHEDULER_NO_TASK)
91   {
92     GNUNET_SCHEDULER_cancel (die_task);
93     die_task = GNUNET_SCHEDULER_NO_TASK;
94   }
95   ret = 0;
96 }
97
98 static void
99 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
100                     struct Session *session,
101                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
102                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
103                     const struct GNUNET_ATS_Information *ats,
104                     uint32_t ats_count)
105 {
106   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received address without asking for it!\n");
107   end_badly_now ();
108 }
109
110
111 static void
112 run (void *cls, 
113      const struct GNUNET_CONFIGURATION_Handle *cfg,
114      struct GNUNET_TESTING_Peer *peer)
115 {
116   ret = 1;
117   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
118
119   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing ATS\n");
120   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
121   if (ats == NULL)
122   {
123     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to initialize ATS\n");
124     end_badly_now ();
125     return;
126   }
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting for %s\n", 
128               GNUNET_STRINGS_relative_time_to_string (DELAY, GNUNET_YES));
129   wait_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &delay, NULL);
130 }
131
132
133 int
134 main (int argc, char *argv[])
135 {
136   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_init",
137                                     "test_ats_api.conf",
138                                     &run, NULL))
139     return 1;
140   return ret;
141 }
142
143 /* end of file test_ats_api_scheduling_init.c */