Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / teshsuite / s4u / activity-lifecycle / testing_comm.cpp
index 47b6657..04fb363 100644 (file)
@@ -1,9 +1,10 @@
-/* Copyright (c) 2010-2020. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2010-2023. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-#include "activity-lifecycle.hpp"
+#include "catch_simgrid.hpp"
+#include <array>
 
 static void test_link_off_helper(double delay)
 {
@@ -11,18 +12,15 @@ static void test_link_off_helper(double delay)
 
   simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], [&start]() {
     assert_exit(true, 9);
-    double milestone[5] = {0.5, 3.5, 4.5, 7.5, 9.0};
-    for (int i = 0; i < 5; i++)
-      milestone[i] += start;
+    std::array<double, 5> milestone{{0.5, 3.5, 4.5, 7.5, 9.0}};
+    for (double& m : milestone)
+      m += start;
     for (int i = 0; i < 4; i++) {
       simgrid::s4u::this_actor::sleep_until(milestone[i]);
-      try {
-        XBT_VERB("get(%c)", 'A' + i);
-        simgrid::s4u::Mailbox::by_name("mb")->get();
-        return;
-      } catch (simgrid::NetworkFailureException const&) {
-        XBT_VERB("got expected NetworkFailureException");
-      }
+      REQUIRE_NETWORK_FAILURE({
+        INFO("get(" << ('A' + i) << ")");
+        simgrid::s4u::Mailbox::by_name("mb")->get<int>();
+      });
     }
     simgrid::s4u::this_actor::sleep_until(milestone[4]);
   });
@@ -30,9 +28,9 @@ static void test_link_off_helper(double delay)
   simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], [&start]() {
     assert_exit(true, 9);
     int data            = 42;
-    double milestone[5] = {1.5, 2.5, 5.5, 6.5, 9.0};
-    for (int i = 0; i < 5; i++)
-      milestone[i] += start;
+    std::array<double, 5> milestone{{1.5, 2.5, 5.5, 6.5, 9.0}};
+    for (double& m : milestone)
+      m += start;
     for (int i = 0; i < 2; i++) {
       simgrid::s4u::this_actor::sleep_until(milestone[i]);
       XBT_VERB("dsend(%c)", 'A' + i);
@@ -40,13 +38,10 @@ static void test_link_off_helper(double delay)
     }
     for (int i = 2; i < 4; i++) {
       simgrid::s4u::this_actor::sleep_until(milestone[i]);
-      try {
-        XBT_VERB("put(%c)", 'A' + i);
+      REQUIRE_NETWORK_FAILURE({
+        INFO("put(" << ('A' + i) << ")");
         simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000);
-        return;
-      } catch (simgrid::NetworkFailureException const&) {
-        XBT_VERB("got expected NetworkFailureException");
-      }
+      });
     }
     simgrid::s4u::this_actor::sleep_until(milestone[4]);
   });
@@ -63,6 +58,55 @@ static void test_link_off_helper(double delay)
   simgrid::s4u::this_actor::sleep_for(1.5);
 };
 
+static simgrid::s4u::ActorPtr sender_basic(bool& ending_boolean, bool expected_success, double duration,
+                                           double delay = -1.0)
+{
+  return simgrid::s4u::Actor::create("sender", all_hosts[1], [&ending_boolean, expected_success, duration, delay]() {
+    assert_exit(expected_success, duration);
+    // Encapsulate the payload in a std::unique_ptr so that it is correctly free'd if/when the sender is killed during
+    // its communication (thanks to RAII).  The pointer is then released when the communication is over.
+    std::unique_ptr<char, decltype(&xbt_free_f)> payload(xbt_strdup("toto"), &xbt_free_f);
+    if (delay > 0.0) {
+      simgrid::s4u::this_actor::sleep_for(delay / 2.0);
+      auto comm = simgrid::s4u::Mailbox::by_name("mb")->put_init(payload.get(), 5000);
+      simgrid::s4u::this_actor::sleep_for(delay / 2.0);
+      comm->wait();
+    } else {
+      simgrid::s4u::Mailbox::by_name("mb")->put(payload.get(), 5000);
+    }
+    payload.release();
+    ending_boolean = true;
+  });
+}
+static simgrid::s4u::ActorPtr receiver_basic(bool& ending_boolean, bool expected_success, double duration,
+                                             double delay = -1.0)
+{
+  return simgrid::s4u::Actor::create("receiver", all_hosts[2], [&ending_boolean, expected_success, duration, delay]() {
+    assert_exit(expected_success, duration);
+    char* payload;
+    if (delay > 0.0) {
+      simgrid::s4u::this_actor::sleep_for(delay / 2.0);
+      auto comm = simgrid::s4u::Mailbox::by_name("mb")->get_init()->set_dst_data(reinterpret_cast<void**>(&payload),
+                                                                                 sizeof(void*));
+      simgrid::s4u::this_actor::sleep_for(delay / 2.0);
+      comm->wait();
+    } else {
+      payload = simgrid::s4u::Mailbox::by_name("mb")->get<char>();
+    }
+    xbt_free(payload);
+    ending_boolean = true;
+  });
+}
+static simgrid::s4u::ActorPtr sender_dtach(bool& ending_boolean, bool expected_success, double duration)
+{
+  return simgrid::s4u::Actor::create("sender", all_hosts[1], [&ending_boolean, expected_success, duration]() {
+    assert_exit(expected_success, duration);
+    char* payload = xbt_strdup("toto");
+    simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
+    ending_boolean = true;
+  });
+}
+
 TEST_CASE("Activity lifecycle: comm activities")
 {
   XBT_INFO("#####[ launch next \"comm\" test ]#####");
@@ -73,19 +117,42 @@ TEST_CASE("Activity lifecycle: comm activities")
     bool send_done = false;
     bool recv_done = false;
 
-    simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
-      assert_exit(true, 5);
-      char* payload = xbt_strdup("toto");
-      simgrid::s4u::Mailbox::by_name("mb")->put(payload, 5000);
-      send_done = true;
-    });
+    sender_basic(send_done, true, 5);
+    receiver_basic(recv_done, true, 5);
 
-    simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
-      assert_exit(true, 5);
-      void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
-      xbt_free(payload);
-      recv_done = true;
-    });
+    simgrid::s4u::this_actor::sleep_for(9);
+    INFO("Sender or receiver killed somehow. It shouldn't");
+    REQUIRE(send_done);
+    REQUIRE(recv_done);
+
+    END_SECTION;
+  }
+
+  BEGIN_SECTION("comm (delayed send)")
+  {
+    XBT_INFO("Launch a communication with a delay for the send");
+    bool send_done = false;
+    bool recv_done = false;
+
+    sender_basic(send_done, true, 6, 1); // cover Comm::send
+    receiver_basic(recv_done, true, 6);
+
+    simgrid::s4u::this_actor::sleep_for(9);
+    INFO("Sender or receiver killed somehow. It shouldn't");
+    REQUIRE(send_done);
+    REQUIRE(recv_done);
+
+    END_SECTION;
+  }
+
+  BEGIN_SECTION("comm (delayed recv)")
+  {
+    XBT_INFO("Launch a communication with a delay for the recv");
+    bool send_done = false;
+    bool recv_done = false;
+
+    sender_basic(send_done, true, 6);
+    receiver_basic(recv_done, true, 6, 1); // cover Comm::recv
 
     simgrid::s4u::this_actor::sleep_for(9);
     INFO("Sender or receiver killed somehow. It shouldn't");
@@ -101,22 +168,11 @@ TEST_CASE("Activity lifecycle: comm activities")
     bool dsend_done = false;
     bool recv_done  = false;
 
-    simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&dsend_done]() {
-      assert_exit(true, 0);
-      char* payload = xbt_strdup("toto");
-      simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
-      dsend_done = true;
-    });
-
-    simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
-      assert_exit(true, 3);
-      simgrid::s4u::this_actor::sleep_for(2);
-      void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
-      xbt_free(payload);
-      recv_done = true;
-    });
+    sender_dtach(dsend_done, true, 0);
+    simgrid::s4u::this_actor::sleep_for(2);
+    receiver_basic(recv_done, true, 1);
 
-    // Sleep long enough to let the test ends by itself. 3 + surf_precision should be enough.
+    // Sleep long enough to let the test ends by itself. 1 + surf_precision should be enough.
     simgrid::s4u::this_actor::sleep_for(4);
     INFO("Sender or receiver killed somehow. It shouldn't");
     REQUIRE(dsend_done);
@@ -131,20 +187,9 @@ TEST_CASE("Activity lifecycle: comm activities")
     bool dsend_done = false;
     bool recv_done  = false;
 
-    simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&dsend_done]() {
-      assert_exit(true, 2);
-      char* payload = xbt_strdup("toto");
-      simgrid::s4u::this_actor::sleep_for(2);
-      simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
-      dsend_done = true;
-    });
-
-    simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
-      assert_exit(true, 3);
-      void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
-      xbt_free(payload);
-      recv_done = true;
-    });
+    receiver_basic(recv_done, true, 3);
+    simgrid::s4u::this_actor::sleep_for(2);
+    sender_dtach(dsend_done, true, 0);
 
     // Sleep long enough to let the test ends by itself. 3 + surf_precision should be enough.
     simgrid::s4u::this_actor::sleep_for(4);
@@ -161,21 +206,14 @@ TEST_CASE("Activity lifecycle: comm activities")
     bool send_done = false;
     bool recv_done = false;
 
-    simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
-      assert_exit(false, 2);
-      // Encapsulate the payload in a std::unique_ptr so that it is correctly free'd when the sender is killed during
-      // its communication (thanks to RAII).  The pointer is then released when the communication is over.
-      std::unique_ptr<char, decltype(&xbt_free_f)> payload(xbt_strdup("toto"), &xbt_free_f);
-      simgrid::s4u::Mailbox::by_name("mb")->put(payload.get(), 5000);
-      payload.release();
-      send_done = true;
-    });
+    simgrid::s4u::ActorPtr sender = sender_basic(send_done, false, 2);
 
     simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
       assert_exit(true, 2);
-      void* payload = nullptr;
-      REQUIRE_THROWS_AS(payload = simgrid::s4u::Mailbox::by_name("mb")->get(), simgrid::NetworkFailureException);
-      xbt_free(payload);
+      REQUIRE_NETWORK_FAILURE({
+        char* payload = simgrid::s4u::Mailbox::by_name("mb")->get<char>();
+        xbt_free(payload);
+      });
       recv_done = true;
     });
 
@@ -205,9 +243,9 @@ TEST_CASE("Activity lifecycle: comm activities")
                                                                &in_catch_before_on_exit, &in_catch_after_on_exit]() {
           assert_exit(false, 1);
           try {
-            simgrid::s4u::Mailbox::by_name("mb")->get();
+            simgrid::s4u::Mailbox::by_name("mb")->get<int>();
           } catch (simgrid::NetworkFailureException const&) {
-            // Shouldn't get in here
+            // Shouldn't get in here after the on_exit function
             in_catch_before_on_exit = not in_on_exit;
             in_catch_after_on_exit  = in_on_exit;
           }
@@ -219,7 +257,7 @@ TEST_CASE("Activity lifecycle: comm activities")
     simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], [&send_done]() {
       assert_exit(true, 1);
       int data = 42;
-      REQUIRE_THROWS_AS(simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000), simgrid::NetworkFailureException);
+      REQUIRE_NETWORK_FAILURE(simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000));
       send_done = true;
     });
 
@@ -274,16 +312,15 @@ TEST_CASE("Activity lifecycle: comm activities")
     simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], []() {
       assert_exit(true, 2);
       int* data;
-      std::vector<simgrid::s4u::CommPtr> pending_comms;
-      simgrid::s4u::CommPtr comm = simgrid::s4u::Mailbox::by_name("mb")->get_async((void**)&data);
-      pending_comms.push_back(comm);
-      REQUIRE_THROWS_AS(simgrid::s4u::Comm::wait_any(&pending_comms), simgrid::NetworkFailureException);
+      simgrid::s4u::CommPtr comm                       = simgrid::s4u::Mailbox::by_name("mb")->get_async<int>(&data);
+      std::vector<simgrid::s4u::CommPtr> pending_comms = {comm};
+      REQUIRE_NETWORK_FAILURE(simgrid::s4u::Comm::wait_any(pending_comms));
     });
 
     simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], []() {
       assert_exit(true, 2);
       int data = 42;
-      REQUIRE_THROWS_AS(simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000), simgrid::NetworkFailureException);
+      REQUIRE_NETWORK_FAILURE(simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000));
     });
 
     simgrid::s4u::this_actor::sleep_for(2.0);