-lockmanager 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_SUCCESS,
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 const 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 }
130
131 /**
132  * Abort
133  *
134  * @param cls
135  * @param tc the task context
136  */
137 static void
138 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
139 {
140   LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n");
141   abort_task_id = GNUNET_SCHEDULER_NO_TASK;
142   result = TEST_FAIL;
143   do_shutdown (cls, tc);
144 }
145
146
147 /**
148  * Callback for lock status changes
149  *
150  * @param cls the handle
151  *
152  * @param domain_name the locking domain of the lock 
153  *
154  * @param lock the lock for which this status is relevant
155  *
156  * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
157  *          acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
158  */
159 static void 
160 status_cb (void *cls,
161            const char *domain_name,
162            uint32_t lock,
163            enum GNUNET_LOCKMANAGER_Status status)
164 {
165   LOG (GNUNET_ERROR_TYPE_DEBUG,
166        "Status change callback called on lock: %d of domain: %s\n",
167        lock, domain_name);
168   switch (result)
169   {
170   case TEST_INIT:
171     GNUNET_assert (handle == cls);
172     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
173     result = TEST_CLIENT_LOCK_SUCCESS;
174     /* We should kill the lockmanager process */
175     if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
176     {
177       LOG (GNUNET_ERROR_TYPE_DEBUG,
178            "Kill gnunet-service-arm manually\n");
179     }
180     GNUNET_OS_process_wait (arm_pid);
181     GNUNET_OS_process_destroy (arm_pid);
182     arm_pid =NULL;
183     break;
184   case TEST_CLIENT_LOCK_SUCCESS:
185     GNUNET_assert (handle == cls);
186     GNUNET_assert (GNUNET_LOCKMANAGER_RELEASE == status);
187     result = TEST_CLIENT_LOCK_RELEASE;
188     /* Now we should start again the lockmanager process */
189     arm_pid = 
190       GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
191                                "gnunet-service-arm",
192                                "-c", "test_lockmanager_api.conf", NULL);
193     GNUNET_assert (NULL != arm_pid);
194     break;
195   case TEST_CLIENT_LOCK_RELEASE:
196     GNUNET_assert (handle == cls);
197     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
198     result = TEST_CLIENT_LOCK_AGAIN_SUCCESS;
199     GNUNET_LOCKMANAGER_cancel_request (request);
200     request = NULL;
201     GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1), &do_shutdown, NULL);
202     break;
203   default:
204     GNUNET_assert (0);          /* We should never reach here */
205   }
206 }
207
208
209 /**
210  * Testing function
211  *
212  * @param cls NULL
213  * @param tc the task context
214  */
215 static void
216 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
217
218   result = TEST_INIT;
219   handle = GNUNET_LOCKMANAGER_connect (config);
220   GNUNET_assert (NULL != handle);
221   request = GNUNET_LOCKMANAGER_acquire_lock (handle,
222                                              "GNUNET_LOCKMANAGER_TESTING",
223                                              99,
224                                              &status_cb,
225                                              handle);
226   GNUNET_assert (NULL != request);
227   abort_task_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (30),
228                                                 &do_abort,
229                                                 NULL);  
230 }
231
232
233 /**
234  * Main point of test execution
235  */
236 static void
237 run (void *cls, char *const *args, const char *cfgfile,
238      const struct GNUNET_CONFIGURATION_Handle *cfg)
239 {
240   config = cfg;
241   arm_pid = 
242     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
243                              "gnunet-service-arm",
244                              "-c", "test_lockmanager_api.conf", NULL);
245   GNUNET_assert (NULL != arm_pid);
246   GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(3), &test, NULL);
247 }
248
249
250 /**
251  * Main function
252  */
253 int main (int argc, char **argv)
254 {
255   int ret;
256
257   char *const argv2[] = { "test_lockmanager_api_servercrash",
258                           "-c", "test_lockmanager_api.conf",
259                           NULL
260   };  
261   struct GNUNET_GETOPT_CommandLineOption options[] = {
262     GNUNET_GETOPT_OPTION_END
263   };
264   
265   ret =
266     GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
267                         "test_lockmanager_api_servercrash",
268                         "nohelp", options, &run, NULL);
269   if (GNUNET_OK != ret)
270   {
271     LOG (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
272          ret);
273     return 1;
274   }
275   if (TEST_CLIENT_LOCK_AGAIN_SUCCESS != result)
276   {
277     LOG (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
278     return 1;
279   }
280   LOG (GNUNET_ERROR_TYPE_INFO, "test OK\n");
281   return 0;
282 }