regex profiler fixes
[oweals/gnunet.git] / src / lockmanager / test_lockmanager_api_servercrash.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_servercrash.c
23  * @brief Test cases for lockmanager_api where the server crashes
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_lockmanager_service.h"
30 #include "gnunet_testing_lib-new.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; Server should crash now;
67      */
68   TEST_CLIENT2_LOCK_SUCCESS,
69
70     /**
71      * Client 2 should get lock release due to server crash; Should call
72      * shutdown now
73      */
74   TEST_CLIENT2_SERVER_CRASH_SUCCESS
75 };
76
77 /**
78  * The testing result
79  */
80 static enum Test result;
81
82 /**
83  * Configuration Handle
84  */
85 static const struct GNUNET_CONFIGURATION_Handle *config;
86
87 /**
88  * The handle to the lockmanager service
89  */
90 static struct GNUNET_LOCKMANAGER_Handle *handle;
91
92 /**
93  * A second client handle to the lockmanager service
94  */
95 static struct GNUNET_LOCKMANAGER_Handle *handle2;
96
97 /**
98  * The locking request
99  */
100 static struct GNUNET_LOCKMANAGER_LockingRequest *request;
101
102 /**
103  * The locking request of second client
104  */
105 static struct GNUNET_LOCKMANAGER_LockingRequest *request2;
106
107 /**
108  * Abort task identifier
109  */
110 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
111
112 /**
113  * Our peer
114  */
115 static struct GNUNET_TESTING_Peer *self;
116
117
118 /**
119  * Shutdown nicely
120  *
121  * @param cls
122  * @param tc the task context
123  */
124 static void
125 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
126 {
127   if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
128   {
129     GNUNET_SCHEDULER_cancel (abort_task_id);
130     abort_task_id = GNUNET_SCHEDULER_NO_TASK;
131   }
132   if (NULL != handle)
133     GNUNET_LOCKMANAGER_disconnect (handle);
134   if (NULL != handle2)
135     GNUNET_LOCKMANAGER_disconnect (handle2);
136 }
137
138
139 /**
140  * Abort
141  *
142  * @param cls
143  * @param tc the task context
144  */
145 static void
146 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
147 {
148   LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n");
149   abort_task_id = GNUNET_SCHEDULER_NO_TASK;
150   result = TEST_FAIL;
151   do_shutdown (cls, tc);
152 }
153
154
155 /**
156  * Callback for lock status changes
157  *
158  * @param cls the handle
159  *
160  * @param domain_name the locking domain of the lock
161  *
162  * @param lock the lock for which this status is relevant
163  *
164  * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully
165  *          acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost
166  */
167 static void
168 status_cb (void *cls, const char *domain_name, uint32_t lock,
169            enum GNUNET_LOCKMANAGER_Status status)
170 {
171   LOG (GNUNET_ERROR_TYPE_DEBUG,
172        "Status change callback called on lock: %d of domain: %s\n", lock,
173        domain_name);
174   switch (result)
175   {
176   case TEST_INIT:
177     GNUNET_assert (handle == cls);
178     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
179     result = TEST_CLIENT1_LOCK_SUCCESS;
180     request2 =
181         GNUNET_LOCKMANAGER_acquire_lock (handle2, "GNUNET_LOCKMANAGER_TESTING",
182                                          99, &status_cb, handle2);
183     GNUNET_assert (NULL != request2);
184     GNUNET_LOCKMANAGER_cancel_request (request);
185     request = NULL;
186     break;
187   case TEST_CLIENT1_LOCK_SUCCESS:
188     GNUNET_assert (handle2 == cls);
189     GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status);
190     result = TEST_CLIENT2_LOCK_SUCCESS;
191     /* We should stop our peer to simulate crash in lockmanager service */
192     GNUNET_TESTING_peer_stop (self);
193     break;
194   case TEST_CLIENT2_LOCK_SUCCESS:
195     GNUNET_assert (handle2 == cls);
196     GNUNET_assert (GNUNET_LOCKMANAGER_RELEASE == status);
197     GNUNET_assert (99 == lock);
198     GNUNET_assert (0 == strcmp (domain_name, "GNUNET_LOCKMANAGER_TESTING"));
199     result = TEST_CLIENT2_SERVER_CRASH_SUCCESS;
200     GNUNET_LOCKMANAGER_cancel_request (request2);
201     request2 = NULL;
202     GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1), &do_shutdown, NULL);
203     break;
204   default:
205     GNUNET_assert (0);          /* We should never reach here */
206   }
207 }
208
209
210 /**
211  * Main point of test execution
212  */
213 static void
214 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
215      struct GNUNET_TESTING_Peer *peer)
216 {
217   config = cfg;
218   self = peer;
219   result = TEST_INIT;
220   handle = GNUNET_LOCKMANAGER_connect (config);
221   GNUNET_assert (NULL != handle);
222   handle2 = GNUNET_LOCKMANAGER_connect (config);
223
224   request =
225       GNUNET_LOCKMANAGER_acquire_lock (handle, "GNUNET_LOCKMANAGER_TESTING", 99,
226                                        &status_cb, handle);
227   GNUNET_assert (NULL != request);
228   abort_task_id =
229       GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (10), &do_abort, NULL);
230 }
231
232
233 /**
234  * Main function
235  */
236 int
237 main (int argc, char **argv)
238 {
239   if (0 !=
240       GNUNET_TESTING_peer_run ("test_lockmanager_api_servercrash",
241                                "test_lockmanager_api.conf", &run, NULL))
242     return 1;
243   return (TEST_CLIENT2_SERVER_CRASH_SUCCESS != result) ? 1 : 0;
244 }
245
246 /* end of test_lockmanager_api_servercrash.c */