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_SUCCESS,
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 const 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);
135 * @param tc the task context
138 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
140 LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n");
141 abort_task_id = GNUNET_SCHEDULER_NO_TASK;
143 do_shutdown (cls, tc);
148 * Callback for lock status changes
150 * @param cls the handle
152 * @param domain_name the locking domain of the lock
154 * @param lock the lock for which this status is relevant
156 * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
157 * acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
160 status_cb (void *cls,
161 const char *domain_name,
163 enum GNUNET_LOCKMANAGER_Status status)
165 LOG (GNUNET_ERROR_TYPE_DEBUG,
166 "Status change callback called on lock: %d of domain: %s\n",
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))
177 LOG (GNUNET_ERROR_TYPE_DEBUG,
178 "Kill gnunet-service-arm manually\n");
180 GNUNET_OS_process_wait (arm_pid);
181 GNUNET_OS_process_destroy (arm_pid);
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 */
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);
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);
201 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1), &do_shutdown, NULL);
204 GNUNET_assert (0); /* We should never reach here */
213 * @param tc the task context
216 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
219 handle = GNUNET_LOCKMANAGER_connect (config);
220 GNUNET_assert (NULL != handle);
221 request = GNUNET_LOCKMANAGER_acquire_lock (handle,
222 "GNUNET_LOCKMANAGER_TESTING",
226 GNUNET_assert (NULL != request);
227 abort_task_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (30),
234 * Main point of test execution
237 run (void *cls, char *const *args, const char *cfgfile,
238 const struct GNUNET_CONFIGURATION_Handle *cfg)
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);
253 int main (int argc, char **argv)
257 char *const argv2[] = { "test_lockmanager_api_servercrash",
258 "-c", "test_lockmanager_api.conf",
261 struct GNUNET_GETOPT_CommandLineOption options[] = {
262 GNUNET_GETOPT_OPTION_END
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)
271 LOG (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
275 if (TEST_CLIENT_LOCK_AGAIN_SUCCESS != result)
277 LOG (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
280 LOG (GNUNET_ERROR_TYPE_INFO, "test OK\n");