-changes required for acquire retry
[oweals/gnunet.git] / src / lockmanager / test_lockmanager_api_acquireretry.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_acquireretry.c
23  * @brief Test cases for lockmanager_api where the server crashes and comes
24  *          back; the api should try to acqurie the lock again
25  * @author Sree Harsha Totakura
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_lockmanager_service.h"
31
32 /**
33  * Generic logging shortcut
34  */
35 #define LOG(kind,...)                           \
36   GNUNET_log (kind, __VA_ARGS__)
37
38 #define TIME_REL_SECS(sec)                                      \
39   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
40
41 /**
42  * Various stages in 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 has successfully acquired the lock
58      */
59     TEST_CLIENT_LOCK_SUCESS,
60
61     /**
62      * Client has lost the lock
63      */
64     TEST_CLIENT_LOCK_RELEASE,
65
66     /**
67      * Client has again acquired the lock
68      */
69     TEST_CLIENT_LOCK_AGAIN_SUCCESS
70   };
71
72 /**
73  * The process id of the GNUNET ARM process
74  */
75 static struct GNUNET_OS_Process *arm_pid = NULL;
76
77 /**
78  * Configuration Handle
79  */
80 static struct GNUNET_CONFIGURATION_Handle *config;
81
82 /**
83  * The handle to the lockmanager service
84  */
85 static struct GNUNET_LOCKMANAGER_Handle *handle;
86
87 /**
88  * The locking request
89  */
90 static struct GNUNET_LOCKMANAGER_LockingRequest *request;
91
92 /**
93  * Abort task identifier
94  */
95 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
96
97 /**
98  * The test result
99  */
100 enum Test result;
101
102
103 /**
104  * Shutdown nicely
105  *
106  * @param cls
107  * @param tc the task context
108  */
109 static void
110 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
111 {
112   if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
113   {
114     GNUNET_SCHEDULER_cancel (abort_task_id);
115     abort_task_id = GNUNET_SCHEDULER_NO_TASK;
116   }
117   if (NULL != handle)
118     GNUNET_LOCKMANAGER_disconnect (handle);
119   if (NULL != arm_pid)
120   {
121     if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
122     {
123       LOG (GNUNET_ERROR_TYPE_DEBUG,
124            "Kill gnunet-service-arm manually\n");
125     }
126     GNUNET_OS_process_wait (arm_pid);
127     GNUNET_OS_process_destroy (arm_pid);
128   }
129   if (NULL != config)
130     GNUNET_CONFIGURATION_destroy (config);
131 }
132
133 /**
134  * Abort
135  *
136  * @param cls
137  * @param tc the task context
138  */
139 static void
140 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
141 {
142   LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n");
143   abort_task_id = GNUNET_SCHEDULER_NO_TASK;
144   result = TEST_FAIL;
145   do_shutdown (cls, tc);
146 }
147
148
149 /**
150  * Callback for lock status changes
151  *
152  * @param cls the handle
153  *
154  * @param domain_name the locking domain of the lock 
155  *
156  * @param lock the lock for which this status is relevant
157  *
158  * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
159  *          acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
160  */
161 static void 
162 status_cb (void *cls,
163            const char *domain_name,
164            uint32_t lock,
165            enum GNUNET_LOCKMANAGER_Status status)
166 {
167   LOG (GNUNET_ERROR_TYPE_DEBUG,
168        "Status change callback called on lock: %d of domain: %s\n",
169        lock, domain_name);
170   switch (result)
171   {
172   case TEST_INIT:
173     GNUNET_assert (handle == cls);
174     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
175     result = TEST_CLIENT_LOCK_SUCCESS;
176     /* We should kill the lockmanager process */
177     if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
178     {
179       LOG (GNUNET_ERROR_TYPE_DEBUG,
180            "Kill gnunet-service-arm manually\n");
181     }
182     GNUNET_OS_process_wait (arm_pid);
183     GNUNET_OS_process_destroy (arm_pid);
184     arm_pid =NULL;
185     break;
186   case TEST_CLIENT_LOCK_SUCCESS:
187     GNUNET_assert (handle == cls);
188     GNUNET_assert (GNUNET_LOCKMANAGER_RELEASE == status);
189     result = TEST_CLIENT_LOCK_RELEASE;
190     /* Now we should start again the lockmanager process */
191     arm_pid = 
192       GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
193                                "gnunet-service-arm",
194                                "-c", "test_lockmanager_api.conf", NULL);
195     GNUNET_assert (NULL != arm_pid);
196     break;
197   case TEST_CLIENT_LOCK_RELEASE:
198     GNUNET_asset (handle == cls);
199     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
200     result = TEST_CLIENT_LOCK_AGAIN_SUCCESS;
201     GNUNET_LOCKMANAGER_cancel_request (request);
202     request = NULL;
203     GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1), &do_shutdown, NULL);
204     break;
205   default:
206     GNUNET_assert (0);          /* We should never reach here */
207   }
208 }
209
210
211 /**
212  * Testing function
213  *
214  * @param cls NULL
215  * @param tc the task context
216  */
217 static void
218 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
219
220   result = TEST_INIT;
221   handle = GNUNET_LOCKMANAGER_connect (config);
222   GNUNET_assert (NULL != handle);
223   request = GNUNET_LOCKMANAGER_acquire_lock (handle,
224                                              "GNUNET_LOCKMANAGER_TESTING",
225                                              99,
226                                              &status_cb,
227                                              handle);
228   GNUNET_assert (NULL != request);
229   abort_task_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (10),
230                                                 &do_abort,
231                                                 NULL);  
232 }
233
234
235 /**
236  * Main point of test execution
237  */
238 static void
239 run (void *cls, char *const *args, const char *cfgfile,
240      const struct GNUNET_CONFIGURATION_Handle *cfg)
241 {
242   config = cfg;
243   arm_pid = 
244     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
245                              "gnunet-service-arm",
246                              "-c", "test_lockmanager_api.conf", NULL);
247   GNUNET_assert (NULL != arm_pid);
248   GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(3), &test, NULL);
249 }
250
251
252 /**
253  * Main function
254  */
255 int main (int argc, char **argv)
256 {
257   int ret;
258
259   char *const argv2[] = { "test_lockmanager_api_servercrash",
260                           "-c", "test_lockmanager_api.conf",
261                           NULL
262   };  
263   struct GNUNET_GETOPT_CommandLineOption options[] = {
264     GNUNET_GETOPT_OPTION_END
265   };
266   
267   ret =
268     GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
269                         "test_lockmanager_api_servercrash",
270                         "nohelp", options, &run, NULL);
271   if (GNUNET_OK != ret)
272   {
273     LOG (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
274          ret);
275     return 1;
276   }
277   if (TEST_CLIENT_LOCK_AGAIN_SUCCESS != result)
278   {
279     LOG (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
280     return 1;
281   }
282   LOG (GNUNET_ERROR_TYPE_INFO, "test OK\n");
283   return 0;
284 }