1 /* Copyright (c) 2010-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/s4u.hpp"
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
12 std::vector<simgrid::s4u::Host*> all_hosts;
14 /* Helper function easing the testing of actor's ending condition */
15 static void assert_exit(int status, double duration)
17 double expected_time = simgrid::s4u::Engine::get_clock() + duration;
18 simgrid::s4u::this_actor::on_exit(
19 [status, expected_time](int got_status, void* /*ignored*/) {
20 xbt_assert(status == got_status, "Exit status mismatch. Expected %d, got %d", status, got_status);
21 xbt_assert(std::fabs(expected_time - simgrid::s4u::Engine::get_clock()) < 0.001,
22 "Exit time mismatch. Expected %f", expected_time);
23 XBT_VERB("Checks on exit successful");
27 /* Helper function in charge of running a test and doing some sanity checks afterward */
28 static void run_test(const char* test_name, std::function<void()> test)
30 simgrid::s4u::Actor::create(test_name, all_hosts[0], test);
31 simgrid::s4u::this_actor::sleep_for(10);
33 /* Check that no actor remain (but on host[0], where main_dispatcher lives */
34 for (unsigned int i = 0; i < all_hosts.size(); i++) {
35 std::vector<simgrid::s4u::ActorPtr> all_actors = all_hosts[i]->get_all_actors();
36 unsigned int expected_count = (i == 0) ? 1 : 0; // host[0] contains main_dispatcher, all other are empty
37 if (all_actors.size() != expected_count) {
38 XBT_CRITICAL("Host %s contains %zu actors but %u are expected (i=%u). Existing actors: ",
39 all_hosts[i]->get_cname(), all_actors.size(), expected_count, i);
40 for (auto act : all_actors)
41 XBT_CRITICAL(" - %s", act->get_cname());
42 xbt_die("This is wrong");
45 xbt_assert("TODO: Check that all LMM are empty");
46 XBT_INFO("SUCCESS: %s", test_name);
47 XBT_INFO("#########################################################################################################");
54 static void test_sleep()
56 XBT_INFO("%s: Launch a sleep(5), and let it proceed", __func__);
59 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5", all_hosts[1], [&global]() {
61 simgrid::s4u::this_actor::sleep_for(5);
64 simgrid::s4u::this_actor::sleep_for(10);
65 xbt_assert(global, "The forked actor did not modify the global after sleeping. Was it killed before?");
68 static void test_sleep_kill_middle()
70 XBT_INFO("%s: Launch a sleep(5), and kill it after 2 secs", __func__);
72 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_killed", all_hosts[1], []() {
74 simgrid::s4u::this_actor::sleep_for(5);
75 xbt_die("I should be dead now");
78 simgrid::s4u::this_actor::sleep_for(2);
82 static void test_sleep_kill_begin()
84 XBT_INFO("%s: Launch a sleep(5), and kill it right after start", __func__);
86 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_killed", all_hosts[1], []() {
88 simgrid::s4u::this_actor::sleep_for(5);
89 xbt_die("I should be dead now");
92 simgrid::s4u::this_actor::yield();
96 static void test_sleep_restart_begin()
98 XBT_INFO("%s: Launch a sleep(5), and restart its host right after start", __func__);
100 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], []() {
102 simgrid::s4u::this_actor::sleep_for(5);
103 xbt_die("I should be dead now");
106 simgrid::s4u::this_actor::yield();
107 sleeper5->get_host()->turn_off();
108 sleeper5->get_host()->turn_on();
109 XBT_INFO("Test %s is ending", __func__);
112 static void test_sleep_restart_middle()
114 XBT_INFO("%s: Launch a sleep(5), and restart its host after 2 secs", __func__);
116 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], []() {
118 simgrid::s4u::this_actor::sleep_for(5);
119 xbt_die("I should be dead now");
122 simgrid::s4u::this_actor::sleep_for(2);
123 sleeper5->get_host()->turn_off();
124 sleeper5->get_host()->turn_on();
125 XBT_INFO("Test %s is ending", __func__);
127 static void test_sleep_restart_end()
129 XBT_INFO("%s: Launch a sleep(5), and restart its host right when it stops", __func__);
130 bool sleeper_done = false;
132 simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], [&sleeper_done]() {
134 simgrid::s4u::this_actor::sleep_for(5);
137 simgrid::s4u::Actor::create("killer", all_hosts[0], []() {
138 simgrid::s4u::this_actor::sleep_for(5);
140 all_hosts[1]->turn_off();
141 all_hosts[1]->turn_on();
143 simgrid::s4u::this_actor::sleep_for(10);
144 xbt_assert(sleeper_done,
145 "Restarted actor was already dead in the scheduling round during which the host_off simcall was issued");
148 static void test_comm()
150 XBT_INFO("%s: Launch a communication", __func__);
151 bool send_done = false;
152 bool recv_done = false;
154 simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
156 char* payload = xbt_strdup("toto");
157 simgrid::s4u::Mailbox::by_name("mb")->put(payload, 5000);
160 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
162 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
167 simgrid::s4u::this_actor::sleep_for(10);
168 xbt_assert(send_done, "Sender killed somehow. It shouldn't");
169 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
172 static void test_comm_killsend()
174 XBT_INFO("%s: Launch a communication and kill the sender", __func__);
175 bool send_done = false;
176 bool recv_done = false;
178 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
180 char* payload = xbt_strdup("toto");
181 simgrid::s4u::Mailbox::by_name("mb")->put(payload, 5000);
184 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
186 bool got_exception = false;
188 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
190 } catch (simgrid::NetworkFailureException const&) {
191 got_exception = true;
193 xbt_assert(got_exception);
197 simgrid::s4u::this_actor::sleep_for(2);
200 xbt_assert(not send_done, "Sender was not killed properly");
201 // xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
204 /* We need an extra actor here, so that it can sleep until the end of each test */
205 static void main_dispatcher()
207 run_test("sleep", test_sleep);
208 run_test("sleep killed at start", test_sleep_kill_begin);
209 run_test("sleep killed in middle", test_sleep_kill_middle);
210 /* We cannot kill right at the end of the action because killer actors are always rescheduled to the end of the round
211 * to avoid that they exit before their victim dereferences their name */
212 run_test("sleep restarted at start", test_sleep_restart_begin);
213 run_test("sleep restarted at middle", test_sleep_restart_middle);
214 run_test("sleep restarted at end", test_sleep_restart_end);
216 run_test("comm", test_comm);
217 // run_test("comm kill sender", test_comm_killsend);
220 int main(int argc, char* argv[])
222 simgrid::s4u::Engine e(&argc, argv);
224 const char* platf = argv[1];
226 XBT_WARN("No platform file provided. Using './testing_platform.xml'");
227 platf = "./testing_platform.xml";
229 e.load_platform(platf);
231 all_hosts = e.get_all_hosts();
232 simgrid::s4u::Actor::create("main_dispatcher", all_hosts[0], main_dispatcher);
236 XBT_INFO("Simulation done");