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