1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
From 31c178910f9419a16a463451249a8d85cfb1ef38 Mon Sep 17 00:00:00 2001
From: Brett A C Sheffield <bacs@librecast.net>
Date: Fri, 22 Jul 2022 20:09:55 +0200
Subject: [PATCH 3/3] test 0012: replace signal handler with sem_timedwait()
more robust timeout
---
test/0000-0012.c | 28 +++++++++++++---------------
1 file changed, 13 insertions(+), 15 deletions(-)
diff --git a/test/0000-0012.c b/test/0000-0012.c
index 499d696..395d9c1 100644
--- a/test/0000-0012.c
+++ b/test/0000-0012.c
@@ -1,23 +1,18 @@
#include "test.h"
#include <librecast/net.h>
#include "../src/librecast_pvt.h"
-#include <signal.h>
+#include <semaphore.h>
#include <time.h>
#include <unistd.h>
-static int gotmsg;
-
-void sighandler(int sig)
-{
- test_log("caught signal %i", sig);
-}
+#define WAITS 1
+static sem_t timeout;
void msg_received(lc_message_t *msg)
{
(void)msg;
test_log("message received");
- gotmsg = 1;
- kill(getpid(), SIGINT);
+ sem_post(&timeout);
}
int main()
@@ -26,6 +21,9 @@ int main()
lc_socket_t *sock;
lc_channel_t *chan;
lc_message_t msg;
+ ssize_t byt;
+ struct timespec ts;
+ int op = LC_OP_PING;
int opt = 1;
test_name("multicast ping (loopback)");
@@ -47,20 +45,20 @@ int main()
test_assert(!lc_socket_listen(sock, &msg_received, NULL), "lc_socket_listen()");
/* send packet and receive on loopback */
- int op = LC_OP_PING;
lc_msg_init(&msg);
lc_msg_set(&msg, LC_ATTR_OPCODE, &op);
- signal(SIGINT, sighandler);
- ssize_t byt;
+
byt = lc_msg_send(chan, &msg);
test_assert((size_t)byt == msg.len + sizeof(lc_message_head_t), "%zi bytes sent", byt);
if (byt == -1) {
perror("lc_msg_send");
}
- struct timespec t = { .tv_nsec = 99999999 };
- nanosleep(&t, &t);
- test_assert(gotmsg, "timeout waiting for loopback message");
+ sem_init(&timeout, 0, 0);
+ clock_gettime(CLOCK_REALTIME, &ts);
+ ts.tv_sec += WAITS;
+ test_assert(!sem_timedwait(&timeout, &ts), "timeout");
+ sem_destroy(&timeout);
test_assert(!lc_socket_listen_cancel(sock), "lc_socket_listen_cancel()");
lc_channel_free(chan);
--
2.35.1
|