lockmanager testcase new testing library
[oweals/gnunet.git] / src / lockmanager / test_lockmanager_api_acquireretry.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_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
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_lockmanager_service.h"
31 #include "gnunet_testing_lib-new.h"
32
33 /**
34  * Generic logging shorthand
35  */
36 #define LOG(kind,...)                           \
37   GNUNET_log (kind, __VA_ARGS__)
38
39 /**
40  * Relative seconds shorthand
41  */
42 #define TIME_REL_SECS(sec)                                   \
43   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
44
45 /**
46  * Various stages in test
47  */
48 enum Test
49   {
50     /**
51      * Signal test failure
52      */
53     TEST_FAIL,
54     
55     /**
56      * Testing just began
57      */
58     TEST_INIT,
59     
60     /**
61      * Client has successfully acquired the lock
62      */
63     TEST_CLIENT_LOCK_SUCCESS,
64
65     /**
66      * Client has lost the lock
67      */
68     TEST_CLIENT_LOCK_RELEASE,
69
70     /**
71      * Client has again acquired the lock
72      */
73     TEST_CLIENT_LOCK_AGAIN_SUCCESS
74   };
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  * The locking request
88  */
89 static struct GNUNET_LOCKMANAGER_LockingRequest *request;
90
91 /**
92  * Abort task identifier
93  */
94 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
95
96 /**
97  * The test result
98  */
99 enum Test result;
100
101 /**
102  * Our peer
103  */
104 static struct GNUNET_TESTING_Peer *self;
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   if (NULL != handle)
122     GNUNET_LOCKMANAGER_disconnect (handle);
123 }
124
125 /**
126  * Abort
127  *
128  * @param cls
129  * @param tc the task context
130  */
131 static void
132 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
133 {
134   LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n");
135   abort_task_id = GNUNET_SCHEDULER_NO_TASK;
136   result = TEST_FAIL;
137   do_shutdown (cls, tc);
138 }
139
140
141 /**
142  * Callback for lock status changes
143  *
144  * @param cls the handle
145  *
146  * @param domain_name the locking domain of the lock 
147  *
148  * @param lock the lock for which this status is relevant
149  *
150  * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
151  *          acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
152  */
153 static void 
154 status_cb (void *cls,
155            const char *domain_name,
156            uint32_t lock,
157            enum GNUNET_LOCKMANAGER_Status status)
158 {
159   LOG (GNUNET_ERROR_TYPE_DEBUG,
160        "Status change callback called on lock: %d of domain: %s\n",
161        lock, domain_name);
162   switch (result)
163   {
164   case TEST_INIT:
165     GNUNET_assert (handle == cls);
166     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
167     result = TEST_CLIENT_LOCK_SUCCESS;
168     /* We should kill the lockmanager process */
169     GNUNET_TESTING_peer_stop (self);
170     break;
171   case TEST_CLIENT_LOCK_SUCCESS:
172     GNUNET_assert (handle == cls);
173     GNUNET_assert (GNUNET_LOCKMANAGER_RELEASE == status);
174     result = TEST_CLIENT_LOCK_RELEASE;
175     /* Now we should start again the lockmanager process */
176     GNUNET_TESTING_peer_start (self);
177     break;
178   case TEST_CLIENT_LOCK_RELEASE:
179     GNUNET_assert (handle == cls);
180     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
181     result = TEST_CLIENT_LOCK_AGAIN_SUCCESS;
182     GNUNET_LOCKMANAGER_cancel_request (request);
183     request = NULL;
184     GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1), &do_shutdown, NULL);
185     break;
186   default:
187     GNUNET_assert (0);          /* We should never reach here */
188   }
189 }
190
191
192 /**
193  * Main point of test execution
194  */
195 static void
196 run (void *cls,
197      const struct GNUNET_CONFIGURATION_Handle *cfg,
198      struct GNUNET_TESTING_Peer *peer)
199 {
200   config = cfg;
201   self = peer;
202   result = TEST_INIT;
203   handle = GNUNET_LOCKMANAGER_connect (config);
204   GNUNET_assert (NULL != handle);
205   request = GNUNET_LOCKMANAGER_acquire_lock (handle,
206                                              "GNUNET_LOCKMANAGER_TESTING",
207                                              99,
208                                              &status_cb,
209                                              handle);
210   GNUNET_assert (NULL != request);
211   abort_task_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (30),
212                                                 &do_abort,
213                                                 NULL);
214 }
215
216
217 /**
218  * Main function
219  */
220 int main (int argc, char **argv)
221 {
222   if (0 != GNUNET_TESTING_peer_run ("test_lockmanager_api_servercrash",
223                                     "test_lockmanager_api.conf",
224                                     &run, NULL))
225     return 1;
226   return (TEST_CLIENT_LOCK_AGAIN_SUCCESS != result) ? 1 : 0;
227 }