diff options
Diffstat (limited to 'src/pq/test_pq.c')
-rw-r--r-- | src/pq/test_pq.c | 121 |
1 files changed, 119 insertions, 2 deletions
diff --git a/src/pq/test_pq.c b/src/pq/test_pq.c index b3747dfc3..1df1cd126 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 | */ | ||
31 | static struct GNUNET_PQ_Context *db; | ||
32 | |||
33 | /** | ||
34 | * Global return value, 0 on success. | ||
35 | */ | ||
36 | static int ret; | ||
37 | |||
38 | /** | ||
39 | * An event handler. | ||
40 | */ | ||
41 | static struct GNUNET_PQ_EventHandler *eh; | ||
42 | |||
43 | /** | ||
44 | * Timeout task. | ||
45 | */ | ||
46 | static struct GNUNET_SCHEDULER_Task *tt; | ||
47 | |||
28 | 48 | ||
29 | /** | 49 | /** |
30 | * Setup prepared statements. | 50 | * Setup prepared statements. |
@@ -277,6 +297,91 @@ test_notify (struct GNUNET_PQ_Context *db) | |||
277 | } | 297 | } |
278 | 298 | ||
279 | 299 | ||
300 | /** | ||
301 | * Task called on shutdown. | ||
302 | * | ||
303 | * @param cls NULL | ||
304 | */ | ||
305 | static void | ||
306 | event_end (void *cls) | ||
307 | { | ||
308 | GNUNET_PQ_event_scheduler_stop (db); | ||
309 | GNUNET_PQ_event_listen_cancel (eh); | ||
310 | eh = NULL; | ||
311 | if (NULL != tt) | ||
312 | { | ||
313 | GNUNET_SCHEDULER_cancel (tt); | ||
314 | tt = NULL; | ||
315 | } | ||
316 | } | ||
317 | |||
318 | |||
319 | /** | ||
320 | * Task called on timeout. Should not happen, means | ||
321 | * we did not get the expected event. | ||
322 | * | ||
323 | * @param cls NULL | ||
324 | */ | ||
325 | static void | ||
326 | timeout_cb (void *cls) | ||
327 | { | ||
328 | ret = 2; | ||
329 | GNUNET_break (0); | ||
330 | tt = NULL; | ||
331 | GNUNET_SCHEDULER_shutdown (); | ||
332 | } | ||
333 | |||
334 | |||
335 | /** | ||
336 | * Task called on expected event | ||
337 | * | ||
338 | * @param cls NULL | ||
339 | */ | ||
340 | static void | ||
341 | event_sched_cb (void *cls, | ||
342 | const void *extra, | ||
343 | size_t extra_size) | ||
344 | { | ||
345 | GNUNET_assert (5 == extra_size); | ||
346 | GNUNET_assert (0 == | ||
347 | memcmp ("hello", | ||
348 | extra, | ||
349 | 5)); | ||
350 | GNUNET_SCHEDULER_shutdown (); | ||
351 | } | ||
352 | |||
353 | |||
354 | /** | ||
355 | * Run tests that need a scheduler. | ||
356 | * | ||
357 | * @param cls NULL | ||
358 | */ | ||
359 | static void | ||
360 | sched_tests (void *cls) | ||
361 | { | ||
362 | struct GNUNET_PQ_EventHeaderP es = { | ||
363 | .size = htons (sizeof (es)), | ||
364 | .type = htons (42) | ||
365 | }; | ||
366 | |||
367 | |||
368 | tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
369 | &timeout_cb, | ||
370 | NULL); | ||
371 | GNUNET_PQ_event_scheduler_start (db); | ||
372 | eh = GNUNET_PQ_event_listen (db, | ||
373 | &es, | ||
374 | &event_sched_cb, | ||
375 | NULL); | ||
376 | GNUNET_PQ_reconnect (db); | ||
377 | GNUNET_SCHEDULER_add_shutdown (&event_end, | ||
378 | NULL); | ||
379 | GNUNET_PQ_event_notify (db, | ||
380 | &es, | ||
381 | "hello", | ||
382 | 5); | ||
383 | } | ||
384 | |||
280 | 385 | ||
281 | int | 386 | int |
282 | main (int argc, | 387 | main (int argc, |
@@ -297,8 +402,6 @@ main (int argc, | |||
297 | ")"), | 402 | ")"), |
298 | GNUNET_PQ_EXECUTE_STATEMENT_END | 403 | GNUNET_PQ_EXECUTE_STATEMENT_END |
299 | }; | 404 | }; |
300 | struct GNUNET_PQ_Context *db; | ||
301 | int ret; | ||
302 | 405 | ||
303 | GNUNET_log_setup ("test-pq", | 406 | GNUNET_log_setup ("test-pq", |
304 | "WARNING", | 407 | "WARNING", |
@@ -332,6 +435,20 @@ main (int argc, | |||
332 | ret = run_queries (db); | 435 | ret = run_queries (db); |
333 | ret |= test_notify (db); | 436 | ret |= test_notify (db); |
334 | ret |= test_notify (db); | 437 | ret |= test_notify (db); |
438 | if (0 != ret) | ||
439 | { | ||
440 | GNUNET_break (0); | ||
441 | GNUNET_PQ_disconnect (db); | ||
442 | return ret; | ||
443 | } | ||
444 | GNUNET_SCHEDULER_run (&sched_tests, | ||
445 | NULL); | ||
446 | if (0 != ret) | ||
447 | { | ||
448 | GNUNET_break (0); | ||
449 | GNUNET_PQ_disconnect (db); | ||
450 | return ret; | ||
451 | } | ||
335 | #if TEST_RESTART | 452 | #if TEST_RESTART |
336 | fprintf (stderr, "Please restart Postgres database now!\n"); | 453 | fprintf (stderr, "Please restart Postgres database now!\n"); |
337 | sleep (60); | 454 | sleep (60); |