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"
31 #include "gnunet_testing_lib.h"
34 * Generic logging shorthand
36 #define LOG(kind,...) \
37 GNUNET_log (kind, __VA_ARGS__)
40 * Relative seconds shorthand
42 #define TIME_REL_SECS(sec) \
43 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
46 * Various stages in test
61 * Client has successfully acquired the lock
63 TEST_CLIENT_LOCK_SUCCESS,
66 * Client has lost the lock
68 TEST_CLIENT_LOCK_RELEASE,
71 * Client has again acquired the lock
73 TEST_CLIENT_LOCK_AGAIN_SUCCESS
77 * Configuration Handle
79 static const struct GNUNET_CONFIGURATION_Handle *config;
82 * The handle to the lockmanager service
84 static struct GNUNET_LOCKMANAGER_Handle *handle;
89 static struct GNUNET_LOCKMANAGER_LockingRequest *request;
92 * Abort task identifier
94 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
104 static struct GNUNET_TESTING_Peer *self;
111 * @param tc the task context
114 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
118 GNUNET_SCHEDULER_cancel (abort_task_id);
119 abort_task_id = GNUNET_SCHEDULER_NO_TASK;
122 GNUNET_LOCKMANAGER_disconnect (handle);
129 * @param tc the task context
132 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
134 LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n");
135 abort_task_id = GNUNET_SCHEDULER_NO_TASK;
137 do_shutdown (cls, tc);
142 * Callback for lock status changes
144 * @param cls the handle
146 * @param domain_name the locking domain of the lock
148 * @param lock the lock for which this status is relevant
150 * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
151 * acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
154 status_cb (void *cls, const char *domain_name, uint32_t lock,
155 enum GNUNET_LOCKMANAGER_Status status)
157 LOG (GNUNET_ERROR_TYPE_DEBUG,
158 "Status change callback called on lock: %d of domain: %s\n", lock,
163 GNUNET_assert (handle == cls);
164 GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
165 result = TEST_CLIENT_LOCK_SUCCESS;
166 /* We should kill the lockmanager process */
167 GNUNET_TESTING_peer_stop (self);
169 case TEST_CLIENT_LOCK_SUCCESS:
170 GNUNET_assert (handle == cls);
171 GNUNET_assert (GNUNET_LOCKMANAGER_RELEASE == status);
172 result = TEST_CLIENT_LOCK_RELEASE;
173 /* Now we should start again the lockmanager process */
174 GNUNET_TESTING_peer_start (self);
176 case TEST_CLIENT_LOCK_RELEASE:
177 GNUNET_assert (handle == cls);
178 GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
179 result = TEST_CLIENT_LOCK_AGAIN_SUCCESS;
180 GNUNET_LOCKMANAGER_cancel_request (request);
182 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (1), &do_shutdown, NULL);
185 GNUNET_assert (0); /* We should never reach here */
191 * Main point of test execution
194 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
195 struct GNUNET_TESTING_Peer *peer)
200 handle = GNUNET_LOCKMANAGER_connect (config);
201 GNUNET_assert (NULL != handle);
203 GNUNET_LOCKMANAGER_acquire_lock (handle, "GNUNET_LOCKMANAGER_TESTING", 99,
205 GNUNET_assert (NULL != request);
207 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (30), &do_abort, NULL);
215 main (int argc, char **argv)
218 GNUNET_TESTING_peer_run ("test_lockmanager_api_acquireretry",
219 "test_lockmanager_api.conf", &run, NULL))
221 return (TEST_CLIENT_LOCK_AGAIN_SUCCESS != result) ? 1 : 0;
224 /* end of test_lockmanager_api_acquireretry.c */