aboutsummaryrefslogtreecommitdiff
path: root/src/pq/test_pq.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pq/test_pq.c')
-rw-r--r--src/pq/test_pq.c129
1 files changed, 123 insertions, 6 deletions
diff --git a/src/pq/test_pq.c b/src/pq/test_pq.c
index e588da45d..90b5c6489 100644
--- a/src/pq/test_pq.c
+++ b/src/pq/test_pq.c
@@ -25,6 +25,26 @@
25#include "platform.h" 25#include "platform.h"
26#include "pq.h" 26#include "pq.h"
27 27
28/**
29 * Database handle.
30 */
31static struct GNUNET_PQ_Context *db;
32
33/**
34 * Global return value, 0 on success.
35 */
36static int ret;
37
38/**
39 * An event handler.
40 */
41static struct GNUNET_DB_EventHandler *eh;
42
43/**
44 * Timeout task.
45 */
46static struct GNUNET_SCHEDULER_Task *tt;
47
28 48
29/** 49/**
30 * Setup prepared statements. 50 * Setup prepared statements.
@@ -109,7 +129,7 @@ run_queries (struct GNUNET_PQ_Context *db)
109 uint64_t u64; 129 uint64_t u64;
110 uint64_t u642; 130 uint64_t u642;
111 uint64_t uzzz = 42; 131 uint64_t uzzz = 42;
112 132
113 priv = GNUNET_CRYPTO_rsa_private_key_create (1024); 133 priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
114 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); 134 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
115 memset (&hmsg, 42, sizeof(hmsg)); 135 memset (&hmsg, 42, sizeof(hmsg));
@@ -220,6 +240,91 @@ run_queries (struct GNUNET_PQ_Context *db)
220} 240}
221 241
222 242
243/**
244 * Task called on shutdown.
245 *
246 * @param cls NULL
247 */
248static void
249event_end (void *cls)
250{
251 GNUNET_PQ_event_listen_cancel (eh);
252 eh = NULL;
253 if (NULL != tt)
254 {
255 GNUNET_SCHEDULER_cancel (tt);
256 tt = NULL;
257 }
258}
259
260
261/**
262 * Task called on timeout. Should not happen, means
263 * we did not get the expected event.
264 *
265 * @param cls NULL
266 */
267static void
268timeout_cb (void *cls)
269{
270 ret = 2;
271 GNUNET_break (0);
272 tt = NULL;
273 GNUNET_SCHEDULER_shutdown ();
274}
275
276
277/**
278 * Task called on expected event
279 *
280 * @param cls NULL
281 */
282static void
283event_sched_cb (void *cls,
284 const void *extra,
285 size_t extra_size)
286{
287 GNUNET_assert (5 == extra_size);
288 GNUNET_assert (0 ==
289 memcmp ("hello",
290 extra,
291 5));
292 GNUNET_SCHEDULER_shutdown ();
293}
294
295
296/**
297 * Run tests that need a scheduler.
298 *
299 * @param cls NULL
300 */
301static void
302sched_tests (void *cls)
303{
304 struct GNUNET_DB_EventHeaderP es = {
305 .size = htons (sizeof (es)),
306 .type = htons (42)
307 };
308
309
310 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
311 &timeout_cb,
312 NULL);
313 eh = GNUNET_PQ_event_listen (db,
314 &es,
315 GNUNET_TIME_UNIT_FOREVER_REL,
316 &event_sched_cb,
317 NULL);
318 GNUNET_PQ_reconnect (db);
319 GNUNET_SCHEDULER_add_shutdown (&event_end,
320 NULL);
321 GNUNET_PQ_event_notify (db,
322 &es,
323 "hello",
324 5);
325}
326
327
223int 328int
224main (int argc, 329main (int argc,
225 const char *const argv[]) 330 const char *const argv[])
@@ -239,11 +344,9 @@ main (int argc,
239 ")"), 344 ")"),
240 GNUNET_PQ_EXECUTE_STATEMENT_END 345 GNUNET_PQ_EXECUTE_STATEMENT_END
241 }; 346 };
242 struct GNUNET_PQ_Context *db;
243 int ret;
244 347
245 GNUNET_log_setup ("test-pq", 348 GNUNET_log_setup ("test-pq",
246 "WARNING", 349 "INFO",
247 NULL); 350 NULL);
248 db = GNUNET_PQ_connect ("postgres:///gnunetcheck", 351 db = GNUNET_PQ_connect ("postgres:///gnunetcheck",
249 NULL, 352 NULL,
@@ -272,12 +375,26 @@ main (int argc,
272 return 1; 375 return 1;
273 } 376 }
274 ret = run_queries (db); 377 ret = run_queries (db);
378 if (0 != ret)
379 {
380 GNUNET_break (0);
381 GNUNET_PQ_disconnect (db);
382 return ret;
383 }
384 GNUNET_SCHEDULER_run (&sched_tests,
385 NULL);
386 if (0 != ret)
387 {
388 GNUNET_break (0);
389 GNUNET_PQ_disconnect (db);
390 return ret;
391 }
275#if TEST_RESTART 392#if TEST_RESTART
276 fprintf (stderr, "Please restart Postgres database now!\n"); 393 fprintf (stderr, "Please restart Postgres database now!\n");
277 sleep (60); 394 sleep (60);
278 ret = run_queries (db); 395 ret |= run_queries (db);
279 fprintf (stderr, "Result: %d (expect: 1 -- if you restarted the DB)\n", ret); 396 fprintf (stderr, "Result: %d (expect: 1 -- if you restarted the DB)\n", ret);
280 ret = run_queries (db); 397 ret |= run_queries (db);
281 fprintf (stderr, "Result: %d (expect: 0)\n", ret); 398 fprintf (stderr, "Result: %d (expect: 0)\n", ret);
282#endif 399#endif
283 { 400 {