Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / teshsuite / s4u / host-on-off-actors / host-on-off-actors.cpp
index a94afdc..fcc42cb 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2010-2021. 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. */
@@ -8,9 +8,7 @@
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
 
-int tasks_done = 0;
-
-XBT_ATTRIB_NORETURN static void actor_daemon()
+XBT_ATTRIB_NORETURN static void actor_daemon(int& tasks_done)
 {
   const simgrid::s4u::Host* host = simgrid::s4u::Host::current();
   XBT_INFO("  Start daemon on %s (%f)", host->get_cname(), host->get_speed());
@@ -25,15 +23,14 @@ XBT_ATTRIB_NORETURN static void actor_daemon()
 static void commTX()
 {
   XBT_INFO("  Start TX");
-  auto* payload = new std::string("COMM");
-  simgrid::s4u::Mailbox::by_name("comm")->put_init(payload, 100000000)->detach();
+  static std::string payload = "COMM";
+  simgrid::s4u::Mailbox::by_name("comm")->put_init(&payload, 100000000)->detach();
   // We should wait a bit (if not the process will end before the communication, hence an exception on the other side).
   try {
     simgrid::s4u::this_actor::sleep_for(30);
   } catch (const simgrid::HostFailureException&) {
     XBT_INFO("The host has died ... as expected.");
   }
-  delete payload;
 
   XBT_INFO("  TX done");
 }
@@ -42,7 +39,7 @@ static void commRX()
 {
   XBT_INFO("  Start RX");
   try {
-    auto payload = simgrid::s4u::Mailbox::by_name("comm")->get_unique<std::string>();
+    const auto* payload = simgrid::s4u::Mailbox::by_name("comm")->get<std::string>();
     XBT_INFO("  Receive message: %s", payload->c_str());
   } catch (const simgrid::HostFailureException&) {
     XBT_INFO("  Receive message: HOST_FAILURE");
@@ -58,13 +55,14 @@ static void test_launcher(int test_number)
   simgrid::s4u::Host* jupiter = simgrid::s4u::Host::by_name("Jupiter");
   simgrid::s4u::ActorPtr daemon;
   simgrid::s4u::VirtualMachine* vm0 = nullptr;
+  int tasks_done                    = 0;
 
   switch (test_number) {
     case 1:
       // Create a process running a simple task on a host and turn the host off during the execution of the actor.
       XBT_INFO("Test 1:");
       XBT_INFO("  Create an actor on Jupiter");
-      simgrid::s4u::Actor::create("actor_daemon", jupiter, actor_daemon);
+      simgrid::s4u::Actor::create("actor_daemon", jupiter, actor_daemon, std::ref(tasks_done));
       simgrid::s4u::this_actor::sleep_for(3);
       XBT_INFO("  Turn off Jupiter");
       jupiter->turn_off();
@@ -73,14 +71,14 @@ static void test_launcher(int test_number)
                simgrid::s4u::Engine::get_instance()->get_actor_count(), tasks_done);
       break;
     case 2:
-      // Create an actorthat on a host that is turned off (this is not allowed)
+      // Create an actor on an host that is turned off (this is not allowed)
       XBT_INFO("Test 2:");
       XBT_INFO("  Turn off Jupiter");
       // adsein: Jupiter is already off, hence nothing should happen
       // adsein: This can be one additional test, to check that you cannot shutdown twice a host
       jupiter->turn_off();
       try {
-        simgrid::s4u::Actor::create("actor_daemon", jupiter, actor_daemon);
+        simgrid::s4u::Actor::create("actor_daemon", jupiter, actor_daemon, std::ref(tasks_done));
         simgrid::s4u::this_actor::sleep_for(10);
         XBT_INFO("  Test 2 does crash as it should. This message will not be displayed.");
       } catch (const simgrid::HostFailureException&) {
@@ -102,6 +100,7 @@ static void test_launcher(int test_number)
       simgrid::s4u::this_actor::sleep_for(10);
       XBT_INFO("  Turn Jupiter off");
       jupiter->turn_off();
+      simgrid::s4u::this_actor::sleep_for(1); // Allow some time to the other actors to die
       XBT_INFO("Test 4 is ok.  (number of actors : %zu, it should be 1 or 2 if RX has not been satisfied)."
                " An exception is raised when we turn off a node that has an actor sleeping",
                simgrid::s4u::Engine::get_instance()->get_actor_count());
@@ -117,6 +116,7 @@ static void test_launcher(int test_number)
       simgrid::s4u::this_actor::sleep_for(10);
       XBT_INFO("  Turn Jupiter off");
       jupiter->turn_off();
+      simgrid::s4u::this_actor::sleep_for(1); // Allow some time to the other actors to die
       XBT_INFO("Test 5 seems ok (number of actors: %zu, it should be 2)",
                simgrid::s4u::Engine::get_instance()->get_actor_count());
       break;
@@ -124,11 +124,11 @@ static void test_launcher(int test_number)
       XBT_INFO("Test 6: Turn on Jupiter, assign a VM on Jupiter, launch an actor inside the VM, and turn off the node");
 
       // Create VM0
-      vm0 = new simgrid::s4u::VirtualMachine("vm0", jupiter, 1);
+      vm0 = jupiter->create_vm("vm0", 1);
       vm0->start();
 
-      daemon = simgrid::s4u::Actor::create("actor_daemon", vm0, actor_daemon);
-      simgrid::s4u::Actor::create("actor_daemonJUPI", jupiter, actor_daemon);
+      daemon = simgrid::s4u::Actor::create("actor_daemon", vm0, actor_daemon, std::ref(tasks_done));
+      simgrid::s4u::Actor::create("actor_daemonJUPI", jupiter, actor_daemon, std::ref(tasks_done));
 
       daemon->suspend();
       vm0->set_bound(90);
@@ -155,12 +155,11 @@ static void test_launcher(int test_number)
 int main(int argc, char* argv[])
 {
   simgrid::s4u::Engine e(&argc, argv);
-  xbt_assert(argc == 3, "Usage: %s platform_file test_number\n\tExample: %s msg_platform.xml 1\n", argv[0], argv[0]);
+  xbt_assert(argc == 3, "Usage: %s platform_file test_number\n\tExample: %s platform.xml 1\n", argv[0], argv[0]);
 
   e.load_platform(argv[1]);
 
-  simgrid::s4u::Actor::create("test_launcher", simgrid::s4u::Host::by_name("Tremblay"), test_launcher,
-                              std::stoi(argv[2]));
+  simgrid::s4u::Actor::create("test_launcher", e.host_by_name("Tremblay"), test_launcher, std::stoi(argv[2]));
 
   e.run();