-lockmanager acquire retry
[oweals/gnunet.git] / src / lockmanager / test_lockmanager_api_servercrash.c
1 /*
2      This file is part of GNUnet.
3      (C) 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 2, 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 lockmanager/test_lockmanager_api_servercrash.c
23  * @brief Test cases for lockmanager_api where the server crashes
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_lockmanager_service.h"
30
31 #define VERBOSE GNUNET_YES
32
33 #define VERBOSE_ARM 1
34
35 #define LOG(kind,...)                           \
36   GNUNET_log (kind, __VA_ARGS__)
37
38 #define TIME_REL_SECONDS(min)                                   \
39   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, min)
40
41 /**
42  * Various steps of the test
43  */
44 enum Test
45   {
46     /**
47      * Signal test failure
48      */
49     TEST_FAIL,
50
51     /**
52      * Testing just began
53      */
54     TEST_INIT,
55
56     /**
57      * Client 1 has got the lock successfully; Client 2 should try to acquire
58      * the lock now; after some time client 1 has to release the lock
59      */
60     TEST_CLIENT1_LOCK_SUCCESS,
61
62     /**
63      * Client 2 has got the lock; Server should crash now;
64      */
65     TEST_CLIENT2_LOCK_SUCCESS,
66
67     /**
68      * Client 2 should get lock release due to server crash; Should call
69      * shutdown now
70      */
71     TEST_CLIENT2_SERVER_CRASH_SUCCESS
72   };
73
74 /**
75  * The testing result
76  */
77 static enum Test result;
78
79 /**
80  * The process id of the GNUNET ARM process
81  */
82 static struct GNUNET_OS_Process *arm_pid = NULL;
83
84 /**
85  * Configuration Handle
86  */
87 static struct GNUNET_CONFIGURATION_Handle *config;
88
89 /**
90  * The handle to the lockmanager service
91  */
92 static struct GNUNET_LOCKMANAGER_Handle *handle;
93
94 /**
95  * A second client handle to the lockmanager service
96  */
97 static struct GNUNET_LOCKMANAGER_Handle *handle2;
98
99 /**
100  * The locking request
101  */
102 static struct GNUNET_LOCKMANAGER_LockingRequest *request;
103
104 /**
105  * The locking request of second client
106  */
107 static struct GNUNET_LOCKMANAGER_LockingRequest *request2;
108
109 /**
110  * Abort task identifier
111  */
112 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
113
114
115 /**
116  * Shutdown nicely
117  *
118  * @param cls
119  * @param tc the task context
120  */
121 static void
122 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
123 {
124   if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
125   {
126     GNUNET_SCHEDULER_cancel (abort_task_id);
127     abort_task_id = GNUNET_SCHEDULER_NO_TASK;
128   }
129   if (NULL != handle)
130     GNUNET_LOCKMANAGER_disconnect (handle);
131   if (NULL != handle2)
132     GNUNET_LOCKMANAGER_disconnect (handle2);
133   if (NULL != arm_pid)
134   {
135     if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
136     {
137       LOG (GNUNET_ERROR_TYPE_DEBUG,
138            "Kill gnunet-service-arm manually\n");
139     }
140     GNUNET_OS_process_wait (arm_pid);
141     GNUNET_OS_process_destroy (arm_pid);
142   }
143   if (NULL != config)
144     GNUNET_CONFIGURATION_destroy (config);
145 }
146
147
148 /**
149  * Abort
150  *
151  * @param cls
152  * @param tc the task context
153  */
154 static void
155 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
156 {
157   LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n");
158   abort_task_id = GNUNET_SCHEDULER_NO_TASK;
159   result = TEST_FAIL;
160   do_shutdown (cls, tc);
161 }
162
163
164 /**
165  * Callback for lock status changes
166  *
167  * @param cls the handle
168  *
169  * @param domain_name the locking domain of the lock 
170  *
171  * @param lock the lock for which this status is relevant
172  *
173  * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
174  *          acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
175  */
176 static void 
177 status_cb (void *cls,
178            const char *domain_name,
179            uint32_t lock,
180            enum GNUNET_LOCKMANAGER_Status status)
181 {
182   LOG (GNUNET_ERROR_TYPE_DEBUG,
183        "Status change callback called on lock: %d of domain: %s\n",
184        lock, domain_name);
185   switch (result)
186   {
187   case TEST_INIT:
188     GNUNET_assert (handle == cls);
189     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
190     result = TEST_CLIENT1_LOCK_SUCCESS;
191     request2 = GNUNET_LOCKMANAGER_acquire_lock (handle2,
192                                                 "GNUNET_LOCKMANAGER_TESTING",
193                                                 99,
194                                                 &status_cb,
195                                                 handle2);
196     GNUNET_assert (NULL != request2);
197     GNUNET_LOCKMANAGER_cancel_request (request);
198     request = NULL;
199     break;
200   case TEST_CLIENT1_LOCK_SUCCESS:
201     GNUNET_assert (handle2 == cls);
202     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
203     result = TEST_CLIENT2_LOCK_SUCCESS;
204     /* We should kill the lockmanager process */
205     if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
206     {
207       LOG (GNUNET_ERROR_TYPE_DEBUG,
208            "Kill gnunet-service-arm manually\n");
209     }
210     GNUNET_OS_process_wait (arm_pid);
211     GNUNET_OS_process_destroy (arm_pid);
212     arm_pid =NULL;
213     break;
214   case TEST_CLIENT2_LOCK_SUCCESS:
215     GNUNET_assert (handle2 == cls);
216     GNUNET_assert (GNUNET_LOCKMANAGER_RELEASE == status);
217     GNUNET_assert (99 == lock);
218     GNUNET_assert (0 == strcmp (domain_name, "GNUNET_LOCKMANAGER_TESTING"));
219     result = TEST_CLIENT2_SERVER_CRASH_SUCCESS;
220     GNUNET_LOCKMANAGER_cancel_request (request2);
221     request2 = NULL;
222     GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1),
223                                   &do_shutdown,
224                                   NULL);
225     break;
226   default:
227     GNUNET_assert (0);          /* We should never reach here */
228   }
229
230 }
231
232
233 /**
234  * Testing function
235  *
236  * @param cls NULL
237  * @param tc the task context
238  */
239 static void
240 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
241
242   result = TEST_INIT;
243   handle = GNUNET_LOCKMANAGER_connect (config);
244   GNUNET_assert (NULL != handle);
245   handle2 = GNUNET_LOCKMANAGER_connect (config);
246   
247   request = GNUNET_LOCKMANAGER_acquire_lock (handle,
248                                              "GNUNET_LOCKMANAGER_TESTING",
249                                              99,
250                                              &status_cb,
251                                              handle);
252   GNUNET_assert (NULL != request);
253   abort_task_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (10),
254                                                 &do_abort,
255                                                 NULL);
256 }
257
258
259 /**
260  * Main point of test execution
261  */
262 static void
263 run (void *cls, char *const *args, const char *cfgfile,
264      const struct GNUNET_CONFIGURATION_Handle *cfg)
265 {
266   LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting test...\n");
267   config = GNUNET_CONFIGURATION_dup (cfg);
268   arm_pid = 
269     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
270                              "gnunet-service-arm",
271 #if VERBOSE_ARM
272                              "-L", "DEBUG",
273 #endif
274                              "-c", "test_lockmanager_api.conf", NULL);
275
276   GNUNET_assert (NULL != arm_pid);
277   GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (3),
278                                 &test,
279                                 NULL);
280 }
281
282
283 /**
284  * Main function
285  */
286 int main (int argc, char **argv)
287 {
288   int ret;
289
290   char *const argv2[] = { "test_lockmanager_api_servercrash",
291                           "-c", "test_lockmanager_api.conf",
292 #if VERBOSE
293                           "-L", "DEBUG",
294 #endif
295                           NULL
296   };
297   
298   struct GNUNET_GETOPT_CommandLineOption options[] = {
299     GNUNET_GETOPT_OPTION_END
300   };
301   
302   GNUNET_log_setup ("test_lockmanager_api_servercrash",
303 #if VERBOSE
304                     "DEBUG",
305 #else
306                     "WARNING",
307 #endif
308                     NULL);
309
310   ret =
311     GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
312                         "test_lockmanager_api_servercrash",
313                         "nohelp", options, &run, NULL);
314
315   if (GNUNET_OK != ret)
316   {
317     LOG (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
318          ret);
319     return 1;
320   }
321   if (TEST_CLIENT2_SERVER_CRASH_SUCCESS != result)
322   {
323     LOG (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
324     return 1;
325   }
326   LOG (GNUNET_ERROR_TYPE_INFO, "test OK\n");
327   return 0;
328 }