2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
29 #include "gnunet_util_lib.h"
30 #include "gnunet_lockmanager_service.h"
33 * Generic logging shortcut
35 #define LOG(kind,...) \
36 GNUNET_log (kind, __VA_ARGS__)
38 #define TIME_REL_SECS(sec) \
39 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
42 * Various stages in test
57 * Client has successfully acquired the lock
59 TEST_CLIENT_LOCK_SUCESS,
62 * Client has lost the lock
64 TEST_CLIENT_LOCK_RELEASE,
67 * Client has again acquired the lock
69 TEST_CLIENT_LOCK_AGAIN_SUCCESS
73 * The process id of the GNUNET ARM process
75 static struct GNUNET_OS_Process *arm_pid = NULL;
78 * Configuration Handle
80 static struct GNUNET_CONFIGURATION_Handle *config;
83 * The handle to the lockmanager service
85 static struct GNUNET_LOCKMANAGER_Handle *handle;
90 static struct GNUNET_LOCKMANAGER_LockingRequest *request;
93 * Abort task identifier
95 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
107 * @param tc the task context
110 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
112 if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
114 GNUNET_SCHEDULER_cancel (abort_task_id);
115 abort_task_id = GNUNET_SCHEDULER_NO_TASK;
118 GNUNET_LOCKMANAGER_disconnect (handle);
121 if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
123 LOG (GNUNET_ERROR_TYPE_DEBUG,
124 "Kill gnunet-service-arm manually\n");
126 GNUNET_OS_process_wait (arm_pid);
127 GNUNET_OS_process_destroy (arm_pid);
130 GNUNET_CONFIGURATION_destroy (config);
137 * @param tc the task context
140 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
142 LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n");
143 abort_task_id = GNUNET_SCHEDULER_NO_TASK;
145 do_shutdown (cls, tc);
150 * Callback for lock status changes
152 * @param cls the handle
154 * @param domain_name the locking domain of the lock
156 * @param lock the lock for which this status is relevant
158 * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
159 * acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
162 status_cb (void *cls,
163 const char *domain_name,
165 enum GNUNET_LOCKMANAGER_Status status)
167 LOG (GNUNET_ERROR_TYPE_DEBUG,
168 "Status change callback called on lock: %d of domain: %s\n",
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))
179 LOG (GNUNET_ERROR_TYPE_DEBUG,
180 "Kill gnunet-service-arm manually\n");
182 GNUNET_OS_process_wait (arm_pid);
183 GNUNET_OS_process_destroy (arm_pid);
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 */
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);
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);
203 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1), &do_shutdown, NULL);
206 GNUNET_assert (0); /* We should never reach here */
215 * @param tc the task context
218 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
221 handle = GNUNET_LOCKMANAGER_connect (config);
222 GNUNET_assert (NULL != handle);
223 request = GNUNET_LOCKMANAGER_acquire_lock (handle,
224 "GNUNET_LOCKMANAGER_TESTING",
228 GNUNET_assert (NULL != request);
229 abort_task_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (10),
236 * Main point of test execution
239 run (void *cls, char *const *args, const char *cfgfile,
240 const struct GNUNET_CONFIGURATION_Handle *cfg)
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);
255 int main (int argc, char **argv)
259 char *const argv2[] = { "test_lockmanager_api_servercrash",
260 "-c", "test_lockmanager_api.conf",
263 struct GNUNET_GETOPT_CommandLineOption options[] = {
264 GNUNET_GETOPT_OPTION_END
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)
273 LOG (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
277 if (TEST_CLIENT_LOCK_AGAIN_SUCCESS != result)
279 LOG (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
282 LOG (GNUNET_ERROR_TYPE_INFO, "test OK\n");