From 57a66c2f1b5cb4bcc329c657e66d1e5eb08bd5d5 Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Mon, 15 Jan 2018 10:06:32 +0100 Subject: [PATCH] tests: SocketsTest: Avoid hang forever if test fails Change-Id: Ia95e216a2ab6d397ab02c828b70f2b95d1671257 --- tests/CommonLibs/SocketsTest.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/CommonLibs/SocketsTest.cpp b/tests/CommonLibs/SocketsTest.cpp index 9a7d6f09..43c06167 100644 --- a/tests/CommonLibs/SocketsTest.cpp +++ b/tests/CommonLibs/SocketsTest.cpp @@ -30,10 +30,16 @@ #include "Threads.h" #include #include - +#include +#include static const int gNumToSend = 10; +static void sigalarm_handler(int foo) +{ + printf("FAIL: test did not run successfully\n"); + exit(EXIT_FAILURE); +} void *testReaderIP(void *param) { @@ -55,6 +61,15 @@ void *testReaderIP(void *param) int main(int argc, char * argv[] ) { + int count; + + if (signal(SIGALRM, sigalarm_handler) == SIG_ERR) { + perror("signal"); + exit(EXIT_FAILURE); + } + + /* If the test takes longer than 2*gNumToSend seconds, abort it */ + alarm(2* gNumToSend); UDPSocket readSocket("127.0.0.1", 0); UDPSocket socket1("127.0.0.1", 0, "localhost", readSocket.port());