Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Prefer a reference for first parameter of {test,wait}_{all,any}.
[simgrid.git] / teshsuite / s4u / activity-lifecycle / testing_test-wait.cpp
index d88a11a..e47abd6 100644 (file)
@@ -10,7 +10,7 @@
 template <typename Activity> using creator_type = Activity (*)(double);
 
 // Create a new async execution with given duration
-static simgrid::s4u::ActivityPtr create_exec(double duration)
+static simgrid::s4u::ExecPtr create_exec(double duration)
 {
   double speed = simgrid::s4u::this_actor::get_host()->get_speed();
   return simgrid::s4u::this_actor::exec_async(speed * duration);
@@ -23,13 +23,13 @@ static simgrid::s4u::ActivityPtr create_exec(double duration)
 template <typename Activity> using tester_type = bool (*)(const Activity&);
 
 // Calls activity->test() and returns its result
-static bool tester_test(const simgrid::s4u::ActivityPtr& activity)
+template <typename Activity> bool tester_test(const Activity& activity)
 {
   return activity->test();
 }
 
 // Calls activity->wait_for(Duration / 128.0) and returns true when activity is terminated, just like test()
-template <int Duration> bool tester_wait(const simgrid::s4u::ActivityPtr& activity)
+template <int Duration, typename Activity> bool tester_wait(const Activity& activity)
 {
   constexpr double duration = Duration / 128.0;
   const double timeout      = simgrid::s4u::Engine::get_clock() + duration;
@@ -41,7 +41,7 @@ template <int Duration> bool tester_wait(const simgrid::s4u::ActivityPtr& activi
     ret = true;
   } catch (const simgrid::TimeoutException& e) {
     XBT_DEBUG("wait_for() timed out (%s)", e.what());
-    INFO("wait_for() timeout should expire at expected date: " << timeout);
+    INFO("wait_for() timeout should expire at expected date: " + std::to_string(timeout));
     REQUIRE(simgrid::s4u::Engine::get_clock() == Approx(timeout));
     ret = false;
   } catch (const simgrid::Exception& e) {
@@ -53,19 +53,48 @@ template <int Duration> bool tester_wait(const simgrid::s4u::ActivityPtr& activi
   return ret;
 }
 
+// Calls wait_any_for([activity], Duration / 128.0) and returns true when activity is terminated, just like test()
+template <int Duration, typename Activity> bool tester_wait_any(const Activity& activity)
+{
+  constexpr double duration = Duration / 128.0;
+  const double timeout      = simgrid::s4u::Engine::get_clock() + duration;
+  bool ret;
+  try {
+    std::vector<Activity> activities = {activity};
+    XBT_DEBUG("calling wait_any_for(%f)", duration);
+    int index = Activity::element_type::wait_any_for(activities, duration);
+    if (index == -1) {
+      XBT_DEBUG("wait_any_for() timed out");
+      INFO("wait_any_for() timeout should expire at expected date: " << timeout);
+      REQUIRE(simgrid::s4u::Engine::get_clock() == Approx(timeout));
+      ret = false;
+    } else {
+      XBT_DEBUG("wait_any_for() returned index %d", index);
+      REQUIRE(index == 0);
+      ret = true;
+    }
+  } catch (const simgrid::Exception& e) {
+    XBT_DEBUG("wait_any_for() threw an exception: %s", e.what());
+    ret = true;
+  }
+  INFO("wait_any_for() should return before timeout expiration at date: " << timeout);
+  REQUIRE(simgrid::s4u::Engine::get_clock() <= Approx(timeout));
+  return ret;
+}
+
 //========== Waiters: wait for the completion of an activity
 
 template <typename Activity> using waiter_type = void (*)(const Activity&);
 
 // Wait for 6s
-static void waiter_sleep6(const simgrid::s4u::ActivityPtr&)
+template <typename Activity> void waiter_sleep6(const Activity&)
 {
   simgrid::s4u::this_actor::sleep_for(6.0);
   XBT_DEBUG("wake up after 6s sleep");
 }
 
 // Wait for completion of activity
-static void waiter_wait(const simgrid::s4u::ActivityPtr& activity)
+template <typename Activity> void waiter_wait(const Activity& activity)
 {
   activity->wait();
   XBT_DEBUG("end of wait()");
@@ -168,23 +197,23 @@ void test_failure_host()
 
 //==========
 
-using simgrid::s4u::ActivityPtr;
+using simgrid::s4u::ExecPtr;
 
 TEST_CASE("Activity test/wait: using <tester_test>")
 {
   XBT_INFO("#####[ launch next test ]#####");
 
-  RUN_SECTION("exec: run and test once", test_trivial<ActivityPtr, create_exec, tester_test>);
-  RUN_SECTION("exec: run and test many", test_basic<ActivityPtr, create_exec, tester_test>);
-  RUN_SECTION("exec: cancel and test", test_cancel<ActivityPtr, create_exec, tester_test>);
+  RUN_SECTION("exec: run and test once", test_trivial<ExecPtr, create_exec, tester_test>);
+  RUN_SECTION("exec: run and test many", test_basic<ExecPtr, create_exec, tester_test>);
+  RUN_SECTION("exec: cancel and test", test_cancel<ExecPtr, create_exec, tester_test>);
   RUN_SECTION("exec: actor failure and test / sleep",
-              test_failure_actor<ActivityPtr, create_exec, tester_test, waiter_sleep6>);
+              test_failure_actor<ExecPtr, create_exec, tester_test, waiter_sleep6>);
   RUN_SECTION("exec: host failure and test / sleep",
-              test_failure_host<ActivityPtr, create_exec, tester_test, waiter_sleep6>);
+              test_failure_host<ExecPtr, create_exec, tester_test, waiter_sleep6>);
   RUN_SECTION("exec: actor failure and test / wait",
-              test_failure_actor<ActivityPtr, create_exec, tester_test, waiter_wait>);
+              test_failure_actor<ExecPtr, create_exec, tester_test, waiter_wait>);
   RUN_SECTION("exec: host failure and test / wait",
-              test_failure_host<ActivityPtr, create_exec, tester_test, waiter_wait>);
+              test_failure_host<ExecPtr, create_exec, tester_test, waiter_wait>);
 
   simgrid::s4u::this_actor::sleep_for(10);
   assert_cleanup();
@@ -194,13 +223,17 @@ TEST_CASE("Activity test/wait: using <tester_wait<0>>")
 {
   XBT_INFO("#####[ launch next test ]#####");
 
-  RUN_SECTION("exec: run and wait<0> once", test_trivial<ActivityPtr, create_exec, tester_wait<0>>);
-  // exec: run and wait<0> many
-  RUN_SECTION("exec: cancel and wait<0>", test_cancel<ActivityPtr, create_exec, tester_wait<0>>);
-  // exec: actor failure and wait<0> / sleep
-  // exec: host failure and wait<0> / sleep
-  // exec: actor failure and wait<0> / wait
-  // exec: host failure and wait<0> / wait
+  RUN_SECTION("exec: run and wait<0> once", test_trivial<ExecPtr, create_exec, tester_wait<0>>);
+  RUN_SECTION("exec: run and wait<0> many", test_basic<ExecPtr, create_exec, tester_wait<0>>);
+  RUN_SECTION("exec: cancel and wait<0>", test_cancel<ExecPtr, create_exec, tester_wait<0>>);
+  RUN_SECTION("exec: actor failure and wait<0> / sleep",
+              test_failure_actor<ExecPtr, create_exec, tester_wait<0>, waiter_sleep6>);
+  RUN_SECTION("exec: host failure and wait<0> / sleep",
+              test_failure_host<ExecPtr, create_exec, tester_wait<0>, waiter_sleep6>);
+  RUN_SECTION("exec: actor failure and wait<0> / wait",
+              test_failure_actor<ExecPtr, create_exec, tester_wait<0>, waiter_wait>);
+  RUN_SECTION("exec: host failure and wait<0> / wait",
+              test_failure_host<ExecPtr, create_exec, tester_wait<0>, waiter_wait>);
 
   simgrid::s4u::this_actor::sleep_for(10);
   assert_cleanup();
@@ -210,45 +243,57 @@ TEST_CASE("Activity test/wait: using <tester_wait<1>>")
 {
   XBT_INFO("#####[ launch next test ]#####");
 
-  RUN_SECTION("exec: run and wait<1> once", test_trivial<ActivityPtr, create_exec, tester_wait<1>>);
-  // exec: run and wait<1> many
-  RUN_SECTION("exec: cancel and wait<1>", test_cancel<ActivityPtr, create_exec, tester_wait<1>>);
-  // exec: actor failure and wait<1> / sleep
-  // exec: host failure and wait<1> / sleep
-  // exec: actor failure and wait<1> / wait
-  // exec: host failure and wait<1> / wait
+  RUN_SECTION("exec: run and wait<1> once", test_trivial<ExecPtr, create_exec, tester_wait<1>>);
+  RUN_SECTION("exec: run and wait<1> many", test_basic<ExecPtr, create_exec, tester_wait<1>>);
+  RUN_SECTION("exec: cancel and wait<1>", test_cancel<ExecPtr, create_exec, tester_wait<1>>);
+  RUN_SECTION("exec: actor failure and wait<1> / sleep",
+              test_failure_actor<ExecPtr, create_exec, tester_wait<1>, waiter_sleep6>);
+  RUN_SECTION("exec: host failure and wait<1> / sleep",
+              test_failure_host<ExecPtr, create_exec, tester_wait<1>, waiter_sleep6>);
+  RUN_SECTION("exec: actor failure and wait<1> / wait",
+              test_failure_actor<ExecPtr, create_exec, tester_wait<1>, waiter_wait>);
+  RUN_SECTION("exec: host failure and wait<1> / wait",
+              test_failure_host<ExecPtr, create_exec, tester_wait<1>, waiter_wait>);
 
   simgrid::s4u::this_actor::sleep_for(10);
   assert_cleanup();
 }
 
-// FIXME: The tests grouped here are currently failing. Once fixed, they should be put in the right section above.
-//        The tests can be activated with run-time parameter '*' or, more specifically '[failing]'
-TEST_CASE("Activity test/wait: tests currently failing", "[.][failing]")
+TEST_CASE("Activity test/wait: using <tester_wait_any<0>>")
 {
-  XBT_INFO("#####[ launch next failing test ]#####");
+  XBT_INFO("#####[ launch next test ]#####");
 
-  // with tester_wait<0>
-  RUN_SECTION("exec: run and wait<0> many", test_basic<ActivityPtr, create_exec, tester_wait<0>>);
-  RUN_SECTION("exec: actor failure and wait<0> / sleep",
-              test_failure_actor<ActivityPtr, create_exec, tester_wait<0>, waiter_sleep6>);
-  RUN_SECTION("exec: host failure and wait<0> / sleep",
-              test_failure_host<ActivityPtr, create_exec, tester_wait<0>, waiter_sleep6>);
-  RUN_SECTION("exec: actor failure and wait<0> / wait",
-              test_failure_actor<ActivityPtr, create_exec, tester_wait<0>, waiter_wait>);
-  RUN_SECTION("exec: host failure and wait<0> / wait",
-              test_failure_host<ActivityPtr, create_exec, tester_wait<0>, waiter_wait>);
+  RUN_SECTION("exec: run and wait_any<0> once", test_trivial<ExecPtr, create_exec, tester_wait_any<0>>);
+  RUN_SECTION("exec: run and wait_any<0> many", test_basic<ExecPtr, create_exec, tester_wait_any<0>>);
+  RUN_SECTION("exec: cancel and wait_any<0>", test_cancel<ExecPtr, create_exec, tester_wait_any<1>>);
+  RUN_SECTION("exec: actor failure and wait_any<0> / sleep",
+              test_failure_actor<ExecPtr, create_exec, tester_wait_any<0>, waiter_sleep6>);
+  RUN_SECTION("exec: host failure and wait_any<0> / sleep",
+              test_failure_host<ExecPtr, create_exec, tester_wait_any<0>, waiter_sleep6>);
+  RUN_SECTION("exec: actor failure and wait_any<0> / wait",
+              test_failure_actor<ExecPtr, create_exec, tester_wait_any<0>, waiter_wait>);
+  RUN_SECTION("exec: host failure and wait_any<0> / wait",
+              test_failure_host<ExecPtr, create_exec, tester_wait_any<0>, waiter_wait>);
 
-  // with tester_wait<1>
-  RUN_SECTION("exec: run and wait<1> many", test_basic<ActivityPtr, create_exec, tester_wait<1>>);
-  RUN_SECTION("exec: actor failure and wait<1> / sleep",
-              test_failure_actor<ActivityPtr, create_exec, tester_wait<1>, waiter_sleep6>);
-  RUN_SECTION("exec: host failure and wait<1> / sleep",
-              test_failure_host<ActivityPtr, create_exec, tester_wait<1>, waiter_sleep6>);
-  RUN_SECTION("exec: actor failure and wait<1> / wait",
-              test_failure_actor<ActivityPtr, create_exec, tester_wait<1>, waiter_wait>);
-  RUN_SECTION("exec: host failure and wait<1> / wait",
-              test_failure_host<ActivityPtr, create_exec, tester_wait<1>, waiter_wait>);
+  simgrid::s4u::this_actor::sleep_for(10);
+  assert_cleanup();
+}
+
+TEST_CASE("Activity test/wait: using <tester_wait_any<1>>")
+{
+  XBT_INFO("#####[ launch next test ]#####");
+
+  RUN_SECTION("exec: run and wait_any<1> once", test_trivial<ExecPtr, create_exec, tester_wait_any<1>>);
+  RUN_SECTION("exec: run and wait_any<1> many", test_basic<ExecPtr, create_exec, tester_wait_any<1>>);
+  RUN_SECTION("exec: cancel and wait_any<1>", test_cancel<ExecPtr, create_exec, tester_wait_any<1>>);
+  RUN_SECTION("exec: actor failure and wait_any<1> / sleep",
+              test_failure_actor<ExecPtr, create_exec, tester_wait_any<1>, waiter_sleep6>);
+  RUN_SECTION("exec: host failure and wait_any<1> / sleep",
+              test_failure_host<ExecPtr, create_exec, tester_wait_any<1>, waiter_sleep6>);
+  RUN_SECTION("exec: actor failure and wait_any<1> / wait",
+              test_failure_actor<ExecPtr, create_exec, tester_wait_any<1>, waiter_wait>);
+  RUN_SECTION("exec: host failure and wait_any<1> / wait",
+              test_failure_host<ExecPtr, create_exec, tester_wait_any<1>, waiter_wait>);
 
   simgrid::s4u::this_actor::sleep_for(10);
   assert_cleanup();