aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <grothoff@gnunet.org>2021-07-25 16:36:25 +0200
committerChristian Grothoff <grothoff@gnunet.org>2021-07-25 16:36:25 +0200
commit3ac1088039a3c5315c4a336775b31e08671ddd81 (patch)
tree89bc9d97f39222cb0576c46ee4013aef0b5cb847
parentb1a0dcd618befbbef049d1c2ce262c97f1c70a6f (diff)
downloadgnunet-3ac1088039a3c5315c4a336775b31e08671ddd81.tar.gz
gnunet-3ac1088039a3c5315c4a336775b31e08671ddd81.zip
-extend tests, include reconnect handling for events
-rw-r--r--src/pq/pq.h11
-rw-r--r--src/pq/pq_connect.c1
-rw-r--r--src/pq/pq_event.c36
-rw-r--r--src/pq/test_pq.c121
4 files changed, 167 insertions, 2 deletions
diff --git a/src/pq/pq.h b/src/pq/pq.h
index bad99b307..107fd116c 100644
--- a/src/pq/pq.h
+++ b/src/pq/pq.h
@@ -94,4 +94,15 @@ struct GNUNET_PQ_Context
94 bool scheduler_on; 94 bool scheduler_on;
95}; 95};
96 96
97
98/**
99 * Internal API. Reconnect should re-register notifications
100 * after a disconnect.
101 *
102 * @param db the DB handle
103 */
104void
105GNUNET_PQ_event_reconnect_ (struct GNUNET_PQ_Context *db);
106
107
97#endif 108#endif
diff --git a/src/pq/pq_connect.c b/src/pq/pq_connect.c
index 8722d2151..4e614526b 100644
--- a/src/pq/pq_connect.c
+++ b/src/pq/pq_connect.c
@@ -416,6 +416,7 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
416 db->conn = NULL; 416 db->conn = NULL;
417 return; 417 return;
418 } 418 }
419 GNUNET_PQ_event_reconnect_ (db);
419 if ( (NULL != db->sc) && 420 if ( (NULL != db->sc) &&
420 (0 != GNUNET_CONTAINER_multishortmap_size (db->channel_map)) ) 421 (0 != GNUNET_CONTAINER_multishortmap_size (db->channel_map)) )
421 db->sc (db->sc_cls, 422 db->sc (db->sc_cls,
diff --git a/src/pq/pq_event.c b/src/pq/pq_event.c
index 879caf842..74b8b3653 100644
--- a/src/pq/pq_event.c
+++ b/src/pq/pq_event.c
@@ -365,6 +365,42 @@ manage_subscribe (struct GNUNET_PQ_Context *db,
365} 365}
366 366
367 367
368/**
369 * Re-subscribe to notifications after disconnect.
370 *
371 * @param cls the DB context
372 * @param sh the short hash of the channel
373 * @param eh the event handler
374 * @return #GNUNET_OK to continue to iterate
375 */
376static int
377register_notify (void *cls,
378 const struct GNUNET_ShortHashCode *sh,
379 void *value)
380{
381 struct GNUNET_PQ_Context *db = cls;
382 struct GNUNET_PQ_EventHandler *eh = value;
383
384 manage_subscribe (db,
385 "LISTEN ",
386 eh);
387 return GNUNET_OK;
388}
389
390
391void
392GNUNET_PQ_event_reconnect_ (struct GNUNET_PQ_Context *db)
393{
394 GNUNET_assert (0 ==
395 pthread_mutex_lock (&db->notify_lock));
396 GNUNET_CONTAINER_multishortmap_iterate (db->channel_map,
397 &register_notify,
398 db);
399 GNUNET_assert (0 ==
400 pthread_mutex_unlock (&db->notify_lock));
401}
402
403
368struct GNUNET_PQ_EventHandler * 404struct GNUNET_PQ_EventHandler *
369GNUNET_PQ_event_listen (struct GNUNET_PQ_Context *db, 405GNUNET_PQ_event_listen (struct GNUNET_PQ_Context *db,
370 const struct GNUNET_PQ_EventHeaderP *es, 406 const struct GNUNET_PQ_EventHeaderP *es,
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 */
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_PQ_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.
@@ -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 */
305static void
306event_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 */
325static void
326timeout_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 */
340static void
341event_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 */
359static void
360sched_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
281int 386int
282main (int argc, 387main (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);