-typo
[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-new.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 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 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,
156            const char *domain_name,
157            uint32_t lock,
158            enum GNUNET_LOCKMANAGER_Status status)
159 {
160   LOG (GNUNET_ERROR_TYPE_DEBUG,
161        "Status change callback called on lock: %d of domain: %s\n",
162        lock, domain_name);
163   GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
164   switch (result)
165   {
166   case TEST_INIT:
167     GNUNET_assert (handle == cls);
168     result = TEST_CLIENT1_LOCK_SUCCESS;
169     request2 = GNUNET_LOCKMANAGER_acquire_lock (handle2,
170                                                 "GNUNET_LOCKMANAGER_TESTING",
171                                                 99,
172                                                 &status_cb,
173                                                 handle2);
174     GNUNET_assert (NULL != request2);
175     GNUNET_LOCKMANAGER_cancel_request (request);
176     request = NULL;
177     break;
178   case TEST_CLIENT1_LOCK_SUCCESS:
179     GNUNET_assert (handle2 == cls);
180     result = TEST_CLIENT2_LOCK_SUCCESS;
181     GNUNET_LOCKMANAGER_cancel_request (request2);
182     GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1),
183                                   &do_shutdown,
184                                   NULL);
185     break;
186   default:
187     GNUNET_assert (0);          /* We should never reach here */
188   }
189
190 }
191
192
193 /**
194  * Main point of test execution
195  */
196 static void
197 run (void *cls,
198      const struct GNUNET_CONFIGURATION_Handle *cfg,
199      struct GNUNET_TESTING_Peer *peer)
200 {
201   LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting test...\n");
202   config = cfg;
203   result = TEST_INIT;
204   handle = GNUNET_LOCKMANAGER_connect (config);
205   GNUNET_assert (NULL != handle);
206   handle2 = GNUNET_LOCKMANAGER_connect (config);
207   
208   request = GNUNET_LOCKMANAGER_acquire_lock (handle,
209                                              "GNUNET_LOCKMANAGER_TESTING",
210                                              99,
211                                              &status_cb,
212                                              handle);
213   GNUNET_assert (NULL != request);
214   abort_task_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (10),
215                                                 &do_abort,
216                                                 NULL);
217 }
218
219
220 /**
221  * Main function
222  */
223 int main (int argc, char **argv)
224 {
225   if (0 != GNUNET_TESTING_peer_run ("test_lockmanager_api_lockrelease",
226                                     "test_lockmanager_api.conf",
227                                     &run, NULL))
228     return 1;
229   return (TEST_CLIENT2_LOCK_SUCCESS != result) ? 1 : 0;
230 }