]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
add two tests for MTasker, including catching an exception
authorbert hubert <bert.hubert@powerdns.com>
Tue, 10 Oct 2017 12:41:44 +0000 (14:41 +0200)
committerbert hubert <bert.hubert@powerdns.com>
Tue, 10 Oct 2017 13:38:25 +0000 (15:38 +0200)
pdns/recursordist/Makefile.am
pdns/recursordist/test-mtasker.cc [new file with mode: 0644]

index 0775481e129665f77569f0def5aad3b2fc489af1..082c90c2785b2c44a98db65f1c1a07e07ed8ce96 100644 (file)
@@ -206,6 +206,7 @@ testrunner_SOURCES = \
        ixfr.cc ixfr.hh \
        logger.cc logger.hh \
        misc.cc misc.hh \
+       mtasker_fcontext.cc \
        negcache.hh negcache.cc \
        namespaces.hh \
        nsecrecords.cc \
@@ -237,6 +238,7 @@ testrunner_SOURCES = \
        test-iputils_hh.cc \
        test-ixfr_cc.cc \
        test-misc_hh.cc \
+       test-mtasker.cc \
        test-nmtree.cc \
        test-negcache_cc.cc \
        test-rcpgenerator_cc.cc \
@@ -254,10 +256,12 @@ testrunner_SOURCES = \
 
 testrunner_LDFLAGS = \
        $(AM_LDFLAGS) \
+       $(BOOST_CONTEXT_LDFLAGS) \
        $(BOOST_UNIT_TEST_FRAMEWORK_LDFLAGS) \
        $(LIBCRYPTO_LDFLAGS)
 
 testrunner_LDADD = \
+       $(BOOST_CONTEXT_LIBS) \
        $(BOOST_UNIT_TEST_FRAMEWORK_LIBS) \
        $(LIBCRYPTO_LIBS) \
        $(RT_LIBS)
diff --git a/pdns/recursordist/test-mtasker.cc b/pdns/recursordist/test-mtasker.cc
new file mode 100644 (file)
index 0000000..9da5825
--- /dev/null
@@ -0,0 +1,60 @@
+#define BOOST_TEST_DYN_LINK
+#define BOOST_TEST_NO_MAIN
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <boost/test/unit_test.hpp>
+#include <boost/test/floating_point_comparison.hpp>
+#include "mtasker.hh"
+
+BOOST_AUTO_TEST_SUITE(mtasker_cc)
+
+static int g_result;
+
+static void doSomething(void* p)
+{
+  MTasker<>* mt = reinterpret_cast<MTasker<>*>(p);
+  int i=12, o;
+  mt->waitEvent(i, &o);
+  g_result = o;
+  
+}
+
+BOOST_AUTO_TEST_CASE(test_Simple) {
+  MTasker<> mt;
+  mt.makeThread(doSomething, &mt);
+  struct timeval now;
+  gettimeofday(&now, 0);
+  bool first=true;
+  int o=24;
+  for(;;) {
+    while(mt.schedule(&now));
+    if(first) {
+      mt.sendEvent(12, &o);
+      first=false;
+    }
+    if(mt.noProcesses())
+      break;
+  }
+  BOOST_CHECK_EQUAL(g_result, o);
+}
+
+static void willThrow(void* p)
+{
+  throw std::runtime_error("Help!");
+}
+
+
+BOOST_AUTO_TEST_CASE(test_MtaskerException) {
+  BOOST_CHECK_EXCEPTION( {
+      MTasker<> mt;
+      mt.makeThread(willThrow, 0);
+      struct timeval now;
+      
+      for(;;) {
+       mt.schedule(&now);
+      }
+    }, std::exception, [](const std::exception& e) { return true; });
+}
+BOOST_AUTO_TEST_SUITE_END()