2 This file is part of GNUnet.
3 (C) 2008--2013 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_servercrash.c
23 * @brief Test cases for lockmanager_api where the server crashes
24 * @author Sree Harsha Totakura
28 #include "gnunet_util_lib.h"
29 #include "gnunet_lockmanager_service.h"
30 #include "gnunet_testing_lib.h"
33 * Generic logging shorthand
35 #define LOG(kind,...) \
36 GNUNET_log (kind, __VA_ARGS__)
39 * Relative seconds shorthand
41 #define TIME_REL_SECONDS(min) \
42 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, min)
45 * Various steps of the test
60 * Client 1 has got the lock successfully; Client 2 should try to acquire
61 * the lock now; after some time client 1 has to release the lock
63 TEST_CLIENT1_LOCK_SUCCESS,
66 * Client 2 has got the lock; Server should crash now;
68 TEST_CLIENT2_LOCK_SUCCESS,
71 * Client 2 should get lock release due to server crash; Should call
74 TEST_CLIENT2_SERVER_CRASH_SUCCESS
80 static enum Test result;
83 * Configuration Handle
85 static const struct GNUNET_CONFIGURATION_Handle *config;
88 * The handle to the lockmanager service
90 static struct GNUNET_LOCKMANAGER_Handle *handle;
93 * A second client handle to the lockmanager service
95 static struct GNUNET_LOCKMANAGER_Handle *handle2;
100 static struct GNUNET_LOCKMANAGER_LockingRequest *request;
103 * The locking request of second client
105 static struct GNUNET_LOCKMANAGER_LockingRequest *request2;
108 * Abort task identifier
110 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
115 static struct GNUNET_TESTING_Peer *self;
122 * @param tc the task context
125 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
127 if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
129 GNUNET_SCHEDULER_cancel (abort_task_id);
130 abort_task_id = GNUNET_SCHEDULER_NO_TASK;
133 GNUNET_LOCKMANAGER_disconnect (handle);
135 GNUNET_LOCKMANAGER_disconnect (handle2);
143 * @param tc the task context
146 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
148 LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n");
149 abort_task_id = GNUNET_SCHEDULER_NO_TASK;
151 do_shutdown (cls, tc);
156 * Callback for lock status changes
158 * @param cls the handle
160 * @param domain_name the locking domain of the lock
162 * @param lock the lock for which this status is relevant
164 * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
165 * acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
168 status_cb (void *cls, const char *domain_name, uint32_t lock,
169 enum GNUNET_LOCKMANAGER_Status status)
171 LOG (GNUNET_ERROR_TYPE_DEBUG,
172 "Status change callback called on lock: %d of domain: %s\n", lock,
177 GNUNET_assert (handle == cls);
178 GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
179 result = TEST_CLIENT1_LOCK_SUCCESS;
181 GNUNET_LOCKMANAGER_acquire_lock (handle2, "GNUNET_LOCKMANAGER_TESTING",
182 99, &status_cb, handle2);
183 GNUNET_assert (NULL != request2);
184 GNUNET_LOCKMANAGER_cancel_request (request);
187 case TEST_CLIENT1_LOCK_SUCCESS:
188 GNUNET_assert (handle2 == cls);
189 GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
190 result = TEST_CLIENT2_LOCK_SUCCESS;
191 /* We should stop our peer to simulate crash in lockmanager service */
192 GNUNET_TESTING_peer_stop (self);
194 case TEST_CLIENT2_LOCK_SUCCESS:
195 GNUNET_assert (handle2 == cls);
196 GNUNET_assert (GNUNET_LOCKMANAGER_RELEASE == status);
197 GNUNET_assert (99 == lock);
198 GNUNET_assert (0 == strcmp (domain_name, "GNUNET_LOCKMANAGER_TESTING"));
199 result = TEST_CLIENT2_SERVER_CRASH_SUCCESS;
200 GNUNET_LOCKMANAGER_cancel_request (request2);
202 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1), &do_shutdown, NULL);
205 GNUNET_assert (0); /* We should never reach here */
211 * Main point of test execution
214 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
215 struct GNUNET_TESTING_Peer *peer)
220 handle = GNUNET_LOCKMANAGER_connect (config);
221 GNUNET_assert (NULL != handle);
222 handle2 = GNUNET_LOCKMANAGER_connect (config);
225 GNUNET_LOCKMANAGER_acquire_lock (handle, "GNUNET_LOCKMANAGER_TESTING", 99,
227 GNUNET_assert (NULL != request);
229 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (10), &do_abort, NULL);
237 main (int argc, char **argv)
240 GNUNET_TESTING_peer_run ("test_lockmanager_api_servercrash",
241 "test_lockmanager_api.conf", &run, NULL))
243 return (TEST_CLIENT2_SERVER_CRASH_SUCCESS != result) ? 1 : 0;
246 /* end of test_lockmanager_api_servercrash.c */