-removed operation type
[oweals/gnunet.git] / src / testbed / test_testbed_api_operations.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 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_operations.c
23  * @brief tests cases for testbed_api_operations.c
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "testbed_api_operations.h"
30
31 /**
32  * Generic logging shortcut
33  */
34 #define LOG(kind,...)                           \
35   GNUNET_log (kind, __VA_ARGS__)
36
37 /**
38  * Queue A
39  */
40 struct OperationQueue *q1;
41
42 /**
43  * Queue B
44  */
45 struct OperationQueue *q2;
46
47 /**
48  * This operation should go into both queues and block op2 until it is done
49  */
50 struct GNUNET_TESTBED_Operation *op1;
51
52 /**
53  * This operation should go into q1 and q2
54  */
55 struct GNUNET_TESTBED_Operation *op2;
56
57
58 /**
59  * Enumeration of test stages
60  */
61 enum Test
62   {
63     /**
64      * Initial stage
65      */
66     TEST_INIT,
67
68     /**
69      * op1 has been started
70      */
71     TEST_OP1_STARTED,
72
73     /**
74      * op1 has been released
75      */
76     TEST_OP1_RELEASED,
77
78     /**
79      * op2 has started
80      */
81     TEST_OP2_STARTED,
82
83     /**
84      * op2 released
85      */
86     TEST_OP2_RELEASED
87
88   };
89
90 /**
91  * The test result
92  */
93 enum Test result;
94
95
96 /**
97  * Task to simulate artificial delay and change the test stage
98  *
99  * @param cls NULL
100  * @param tc the task context
101  */
102 static void
103 step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 {
105   switch (result)
106   {
107   case TEST_OP1_STARTED:
108     GNUNET_TESTBED_operation_release_ (op1);
109     break;
110   case TEST_OP2_STARTED:
111     GNUNET_TESTBED_operation_release_ (op2);
112     break;
113   default:
114     GNUNET_assert (0);
115   }
116 }
117
118
119 /**
120  * Function to call to start an operation once all
121  * queues the operation is part of declare that the
122  * operation can be activated.
123  */
124 static void
125 start_cb (void *cls)
126 {
127   switch (result)
128   {
129   case TEST_INIT:
130     GNUNET_assert (&op1 == cls);
131     result = TEST_OP1_STARTED;
132     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &step, NULL);
133     break;
134   case TEST_OP1_RELEASED:
135     GNUNET_assert (&op2 == cls);
136     result = TEST_OP2_STARTED;
137     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &step, NULL);
138     break;
139   default:
140     GNUNET_assert (0);
141   }
142 }
143
144
145 /**
146  * Function to call to cancel an operation (release all associated
147  * resources).  This can be because of a call to
148  * "GNUNET_TESTBED_operation_cancel" (before the operation generated
149  * an event) or AFTER the operation generated an event due to a call
150  * to "GNUNET_TESTBED_operation_done".  Thus it is not guaranteed that
151  * a callback to the 'OperationStart' preceeds the call to
152  * 'OperationRelease'.  Implementations of this function are expected
153  * to clean up whatever state is in 'cls' and release all resources
154  * associated with the operation. 
155  */
156 static void
157 release_cb (void *cls)
158 {
159   switch (result)
160   {
161   case TEST_OP1_STARTED:
162     GNUNET_assert (&op1 == cls);
163     result = TEST_OP1_RELEASED;
164     break;
165   case TEST_OP2_STARTED:
166     GNUNET_assert (&op2 == cls);
167     result = TEST_OP2_RELEASED;
168     GNUNET_TESTBED_operation_queue_destroy_ (q1);
169     GNUNET_TESTBED_operation_queue_destroy_ (q2);
170     break;
171   default:
172     GNUNET_assert (0);
173   }
174 }
175
176
177 /**
178  * Main run function. 
179  *
180  * @param cls NULL
181  * @param args arguments passed to GNUNET_PROGRAM_run
182  * @param cfgfile the path to configuration file
183  * @param cfg the configuration file handle
184  */
185 static void
186 run (void *cls, char *const *args, const char *cfgfile,
187      const struct GNUNET_CONFIGURATION_Handle *config)
188 {
189   q1 = GNUNET_TESTBED_operation_queue_create_ (1);
190   GNUNET_assert (NULL != q1);
191   q2 = GNUNET_TESTBED_operation_queue_create_ (2);
192   GNUNET_assert (NULL != q2);
193   op1 = GNUNET_TESTBED_operation_create_ (&op1, start_cb, release_cb);  
194   GNUNET_assert (NULL != op1);
195   op2 = GNUNET_TESTBED_operation_create_ (&op2, start_cb, release_cb);
196   GNUNET_TESTBED_operation_queue_insert_ (q1, op1);
197   GNUNET_TESTBED_operation_queue_insert_ (q2, op1);
198   GNUNET_TESTBED_operation_queue_insert_ (q1, op2);
199   GNUNET_TESTBED_operation_queue_insert_ (q2, op2);
200   result = TEST_INIT;
201 }
202
203 /**
204  * Main function
205  */
206 int main (int argc, char **argv)
207 {
208   int ret;
209   char *const argv2[] = 
210     {"test_testbed_api_operations", "-c", "test_testbed_api.conf", NULL};
211   struct GNUNET_GETOPT_CommandLineOption options[] = 
212     {GNUNET_GETOPT_OPTION_END};
213
214   ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
215                             "test_testbed_api_operations", "nohelp", options,
216                             &run, NULL);
217   if ((GNUNET_OK != ret) || (TEST_OP2_RELEASED != result))
218     return 1;
219   op1 = NULL;
220   op2 = NULL;
221   q1 = NULL;
222   q2 = NULL;
223   return 0;
224 }