- verboser log, faster start
[oweals/gnunet.git] / src / lockmanager / test_lockmanager_api_lockrelease.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file lockmanager/test_lockmanager_api_lockrelease.c
23  * @brief Test cases for lockmanager_api where client disconnects abruptly
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_lockmanager_service.h"
31
32 /**
33  * Generic Logging shorthand
34  */
35 #define LOG(kind,...)                           \
36   GNUNET_log (kind, __VA_ARGS__)
37
38 /**
39  * Relative seconds shorthand
40  */
41 #define TIME_REL_SECONDS(min)                                   \
42   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, min)
43
44 /**
45  * Various steps of the test
46  */
47 enum Test
48 {
49     /**
50      * Signal test failure
51      */
52   TEST_FAIL,
53
54     /**
55      * Testing just began
56      */
57   TEST_INIT,
58
59     /**
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
62      */
63   TEST_CLIENT1_LOCK_SUCCESS,
64
65     /**
66      * Client 2 has got the lock; Should release it and call shutdown
67      */
68   TEST_CLIENT2_LOCK_SUCCESS,
69 };
70
71 /**
72  * The testing result
73  */
74 static enum Test result;
75
76 /**
77  * Configuration Handle
78  */
79 static const struct GNUNET_CONFIGURATION_Handle *config;
80
81 /**
82  * The handle to the lockmanager service
83  */
84 static struct GNUNET_LOCKMANAGER_Handle *handle;
85
86 /**
87  * A second client handle to the lockmanager service
88  */
89 static struct GNUNET_LOCKMANAGER_Handle *handle2;
90
91 /**
92  * The locking request
93  */
94 static struct GNUNET_LOCKMANAGER_LockingRequest *request;
95
96 /**
97  * The locking request of second client
98  */
99 static struct GNUNET_LOCKMANAGER_LockingRequest *request2;
100
101 /**
102  * Abort task identifier
103  */
104 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
105
106
107 /**
108  * Shutdown nicely
109  *
110  * @param cls
111  * @param tc the task context
112  */
113 static void
114 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 {
116   if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
117   {
118     GNUNET_SCHEDULER_cancel (abort_task_id);
119     abort_task_id = GNUNET_SCHEDULER_NO_TASK;
120   }
121   GNUNET_LOCKMANAGER_disconnect (handle);
122   GNUNET_LOCKMANAGER_disconnect (handle2);
123 }
124
125
126 /**
127  * Abort
128  *
129  * @param cls
130  * @param tc the task context
131  */
132 static void
133 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
134 {
135   LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n");
136   abort_task_id = GNUNET_SCHEDULER_NO_TASK;
137   result = TEST_FAIL;
138   do_shutdown (cls, tc);
139 }
140
141
142 /**
143  * Callback for lock status changes
144  *
145  * @param cls the handle
146  *
147  * @param domain_name the locking domain of the lock
148  *
149  * @param lock the lock for which this status is relevant
150  *
151  * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
152  *          acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
153  */
154 static void
155 status_cb (void *cls, const char *domain_name, uint32_t lock,
156            enum GNUNET_LOCKMANAGER_Status status)
157 {
158   LOG (GNUNET_ERROR_TYPE_DEBUG,
159        "Status change callback called on lock: %d of domain: %s\n", lock,
160        domain_name);
161   GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
162   switch (result)
163   {
164   case TEST_INIT:
165     GNUNET_assert (handle == cls);
166     result = TEST_CLIENT1_LOCK_SUCCESS;
167     request2 =
168         GNUNET_LOCKMANAGER_acquire_lock (handle2, "GNUNET_LOCKMANAGER_TESTING",
169                                          99, &status_cb, handle2);
170     GNUNET_assert (NULL != request2);
171     GNUNET_LOCKMANAGER_cancel_request (request);
172     request = NULL;
173     break;
174   case TEST_CLIENT1_LOCK_SUCCESS:
175     GNUNET_assert (handle2 == cls);
176     result = TEST_CLIENT2_LOCK_SUCCESS;
177     GNUNET_LOCKMANAGER_cancel_request (request2);
178     GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1), &do_shutdown, NULL);
179     break;
180   default:
181     GNUNET_assert (0);          /* We should never reach here */
182   }
183
184 }
185
186
187 /**
188  * Main point of test execution
189  */
190 static void
191 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
192      struct GNUNET_TESTING_Peer *peer)
193 {
194   LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting test...\n");
195   config = cfg;
196   result = TEST_INIT;
197   handle = GNUNET_LOCKMANAGER_connect (config);
198   GNUNET_assert (NULL != handle);
199   handle2 = GNUNET_LOCKMANAGER_connect (config);
200
201   request =
202       GNUNET_LOCKMANAGER_acquire_lock (handle, "GNUNET_LOCKMANAGER_TESTING", 99,
203                                        &status_cb, handle);
204   GNUNET_assert (NULL != request);
205   abort_task_id =
206       GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (10), &do_abort, NULL);
207 }
208
209
210 /**
211  * Main function
212  */
213 int
214 main (int argc, char **argv)
215 {
216   if (0 !=
217       GNUNET_TESTING_peer_run ("test_lockmanager_api_lockrelease",
218                                "test_lockmanager_api.conf", &run, NULL))
219     return 1;
220   return (TEST_CLIENT2_LOCK_SUCCESS != result) ? 1 : 0;
221 }
222
223 /* end of test_lockmanager_api_lockrelease.c */