]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
Add a timeout in case something got stuck
authorMichal 'vorner' Vaner <michal.vaner@nic.cz>
Mon, 1 Oct 2012 13:22:39 +0000 (15:22 +0200)
committerMichal 'vorner' Vaner <michal.vaner@nic.cz>
Mon, 1 Oct 2012 13:22:39 +0000 (15:22 +0200)
src/lib/util/threads/tests/lock_unittest.cc

index 748580cffb2e8a3605d44f4ee6ca394f99b4bc68..e1edd701f21698d8c35130a5b94f1cf917d19d57 100644 (file)
@@ -18,6 +18,8 @@
 #include <gtest/gtest.h>
 
 #include <boost/bind.hpp>
+#include <unistd.h>
+#include <signal.h>
 
 using namespace isc::util::thread;
 
@@ -117,7 +119,18 @@ performStrangeOperation(std::vector<long long unsigned> array, int direction,
     }
 }
 
+void
+no_handler(int) {}
+
 TEST(MutexTest, swarm) {
+    // Create a timeout in case something got stuck here
+    struct sigaction ignored, original;
+    memset(&ignored, 0, sizeof ignored);
+    ignored.sa_handler = no_handler;
+    if (sigaction(SIGALRM, &ignored, &original)) {
+        FAIL() << "Couldn't set alarm";
+    }
+    alarm(10);
     // This type has a low chance of being atomic itself, further raising
     // the chance of problems appearing.
     std::vector<long long unsigned> array(length);
@@ -136,6 +149,11 @@ TEST(MutexTest, swarm) {
         sum += array[i];
     }
     EXPECT_EQ(length * value, sum) << "Threads are badly synchronized";
+    // Cancel the alarm and return the original handler
+    alarm(0);
+    if (sigaction(SIGALRM, &original, NULL)) {
+        FAIL() << "Couldn't restore alarm";
+    }
 }
 
 }