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.c
23 * @brief Test cases for lockmanager_api.c
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 shortcut
35 #define LOG(kind,...) \
36 GNUNET_log (kind, __VA_ARGS__)
38 #define TIME_REL_SECONDS(min) \
39 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, min)
43 * Enumeration of testing steps
60 static enum Test result;
63 * Configuration Handle
65 static const struct GNUNET_CONFIGURATION_Handle *config;
68 * The handle to the lockmanager service
70 static struct GNUNET_LOCKMANAGER_Handle *handle;
75 static struct GNUNET_LOCKMANAGER_LockingRequest *request;
78 * The second locking request
80 static struct GNUNET_LOCKMANAGER_LockingRequest *request2;
83 * Abort task identifier
85 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
91 * @param tc the task context
94 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
96 if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
98 GNUNET_SCHEDULER_cancel (abort_task_id);
99 abort_task_id = GNUNET_SCHEDULER_NO_TASK;
102 GNUNET_LOCKMANAGER_cancel_request (request);
103 if (NULL != request2)
104 GNUNET_LOCKMANAGER_cancel_request (request2);
105 GNUNET_LOCKMANAGER_disconnect (handle);
106 GNUNET_SCHEDULER_shutdown ();
114 * @param tc the task context
117 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
119 LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n");
120 abort_task_id = GNUNET_SCHEDULER_NO_TASK;
122 do_shutdown (cls, tc);
126 * Callback for lock status changes
128 * @param cls the closure from GNUNET_LOCKMANAGER_lock call
130 * @param domain_name the locking domain of the lock
132 * @param lock the lock for which this status is relevant
134 * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
135 * acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
138 status_cb (void *cls, const char *domain_name, uint32_t lock,
139 enum GNUNET_LOCKMANAGER_Status status)
141 LOG (GNUNET_ERROR_TYPE_DEBUG,
142 "Status change callback called on lock: %d of domain: %s\n", lock,
147 GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
148 GNUNET_assert (NULL != request);
149 //GNUNET_LOCKMANAGER_cancel_request (request);
151 result = LOCK2_ACQUIRE;
153 GNUNET_LOCKMANAGER_acquire_lock (handle, "GNUNET_LOCKMANAGER_TESTING",
154 100, &status_cb, NULL);
155 GNUNET_assert (NULL != request2);
158 GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
159 GNUNET_assert (NULL != request);
160 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1), &do_shutdown, NULL);
169 * Main point of test execution
172 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
173 struct GNUNET_TESTING_Peer *peer)
175 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting test...\n");
177 handle = GNUNET_LOCKMANAGER_connect (config);
178 GNUNET_assert (NULL != handle);
179 result = LOCK1_ACQUIRE;
181 GNUNET_LOCKMANAGER_acquire_lock (handle, "GNUNET_LOCKMANAGER_TESTING", 99,
184 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (30), &do_abort, NULL);
192 main (int argc, char **argv)
196 GNUNET_TESTING_peer_run ("test_lockmanager_api",
197 "test_lockmanager_api.conf", &run, NULL))
199 return (TEST_FAIL == result) ? 1 : 0;
202 /* end of test_lockmanager_api.c */