1 /* Copyright (c) 2010-2020. 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(bool exp_failed, double duration)
17 double expected_time = simgrid::s4u::Engine::get_clock() + duration;
18 simgrid::s4u::this_actor::on_exit([exp_failed, expected_time](bool got_failed) {
19 xbt_assert(exp_failed == got_failed, "Exit failure status mismatch. Expected %d, got %d", exp_failed, got_failed);
20 xbt_assert(std::fabs(expected_time - simgrid::s4u::Engine::get_clock()) < 0.001, "Exit time mismatch. Expected %f",
22 XBT_VERB("Checks on exit successful");
25 /* Helper function in charge of running a test and doing some sanity checks afterward */
26 static void run_test(const char* test_name, const std::function<void()>& test)
28 simgrid::s4u::Actor::create(test_name, all_hosts[0], test);
29 simgrid::s4u::this_actor::sleep_for(10);
31 /* Check that no actor remain (but on host[0], where main_dispatcher lives */
32 for (unsigned int i = 0; i < all_hosts.size(); i++) {
33 std::vector<simgrid::s4u::ActorPtr> all_actors = all_hosts[i]->get_all_actors();
34 unsigned int expected_count = (i == 0) ? 1 : 0; // host[0] contains main_dispatcher, all other are empty
35 if (all_actors.size() != expected_count) {
36 XBT_CRITICAL("Host %s contains %zu actors but %u are expected (i=%u). Existing actors: ",
37 all_hosts[i]->get_cname(), all_actors.size(), expected_count, i);
38 for (auto act : all_actors)
39 XBT_CRITICAL(" - %s", act->get_cname());
40 xbt_die("This is wrong");
43 xbt_assert("TODO: Check that all LMM are empty");
44 XBT_INFO("SUCCESS: %s", test_name);
45 XBT_INFO("#########################################################################################################");
52 // Calls exec->test() and returns its result
53 static bool tester_test(simgrid::s4u::ExecPtr& exec)
58 // Calls exec->wait_for(Duration * 0.0125) and returns true when exec is terminated, just like test()
59 template <int Duration> bool tester_wait(simgrid::s4u::ExecPtr& exec)
63 exec->wait_for(Duration * 0.0125);
65 } catch (const simgrid::TimeoutException&) {
67 } catch (const simgrid::Exception&) {
73 using tester_type = decltype(tester_test);
75 template <tester_type Test> void test_trivial()
77 XBT_INFO("%s: Launch a execute(5s), and let it proceed before test", __func__);
79 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], []() {
80 assert_exit(false, 6.);
81 auto exec = simgrid::s4u::this_actor::exec_async(500000000);
82 simgrid::s4u::this_actor::sleep_for(6.0);
83 xbt_assert(Test(exec), "exec should be terminated now");
88 template <tester_type Test> void test_basic()
90 XBT_INFO("%s: Launch a execute(5s), and test while it proceeds", __func__);
92 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], []() {
93 assert_exit(false, 6.);
94 auto exec = simgrid::s4u::this_actor::exec_async(500000000);
95 for (int i = 0; i < 3; i++) {
96 xbt_assert(not Test(exec), "exec finished too soon (i = %d)!?", i);
97 simgrid::s4u::this_actor::sleep_for(2.0);
99 xbt_assert(Test(exec), "exec should be terminated now");
104 template <tester_type Test> void test_cancel()
106 XBT_INFO("%s: Launch a execute(5s), and cancel it after 2s", __func__);
108 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], []() {
109 assert_exit(false, 2.);
110 auto exec = simgrid::s4u::this_actor::exec_async(500000000);
111 simgrid::s4u::this_actor::sleep_for(2.0);
113 xbt_assert(Test(exec), "exec should be terminated now");
118 template <tester_type Test> void test_failure_actor_sleep()
120 XBT_INFO("%s: Launch a execute(5s), and kill running actor after 2s (sleep during exec)", __func__);
122 simgrid::s4u::ExecPtr exec;
123 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&exec]() {
124 assert_exit(true, 2.);
125 exec = simgrid::s4u::this_actor::exec_async(500000000);
126 simgrid::s4u::this_actor::sleep_for(6.0);
127 XBT_ERROR("should not be here!");
129 simgrid::s4u::this_actor::sleep_for(2.0);
130 xbt_assert(not Test(exec), "exec finished too soon!?");
132 xbt_assert(Test(exec), "exec should be terminated now");
135 template <tester_type Test> void test_failure_host_sleep()
137 XBT_INFO("%s: Launch a execute(5s), and shutdown host 2s (sleep during exec)", __func__);
139 simgrid::s4u::ExecPtr exec;
140 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&exec]() {
141 assert_exit(true, 2.);
142 exec = simgrid::s4u::this_actor::exec_async(500000000);
143 simgrid::s4u::this_actor::sleep_for(6.0);
144 XBT_ERROR("should not be here!");
146 simgrid::s4u::this_actor::sleep_for(2.0);
147 xbt_assert(not Test(exec), "exec finished too soon!?");
148 exec5->get_host()->turn_off();
149 exec5->get_host()->turn_on();
150 xbt_assert(Test(exec), "exec should be terminated now");
153 template <tester_type Test> void test_failure_actor_wait()
155 XBT_INFO("%s: Launch a execute(5s), and kill running actor after 2s (wait for exec)", __func__);
157 simgrid::s4u::ExecPtr exec;
158 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&exec]() {
159 assert_exit(true, 2.);
160 exec = simgrid::s4u::this_actor::exec_async(500000000);
162 XBT_ERROR("should not be here!");
164 simgrid::s4u::this_actor::sleep_for(2.0);
165 xbt_assert(not Test(exec), "exec finished too soon!?");
167 xbt_assert(Test(exec), "exec should be terminated now");
170 template <tester_type Test> void test_failure_host_wait()
172 XBT_INFO("%s: Launch a execute(5s), and shutdown host 2s (wait for exec)", __func__);
174 simgrid::s4u::ExecPtr exec;
175 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&exec]() {
176 assert_exit(true, 2.);
177 exec = simgrid::s4u::this_actor::exec_async(500000000);
179 XBT_ERROR("should not be here!");
181 simgrid::s4u::this_actor::sleep_for(2.0);
182 xbt_assert(not Test(exec), "exec finished too soon!?");
183 exec5->get_host()->turn_off();
184 exec5->get_host()->turn_on();
185 xbt_assert(Test(exec), "exec should be terminated now");
188 /* We need an extra actor here, so that it can sleep until the end of each test */
189 static void main_dispatcher()
191 XBT_INFO("***** Using <tester_test> *****");
192 run_test("exec and test once", test_trivial<tester_test>);
193 run_test("exec and test many", test_basic<tester_test>);
194 run_test("cancel and test", test_cancel<tester_test>);
195 // run_test("actor failure and test / sleep", test_failure_actor_sleep<tester_test>);
196 // run_test("host failure and test / sleep", test_failure_host_sleep<tester_test>);
197 run_test("actor failure and test / wait", test_failure_actor_wait<tester_test>);
198 run_test("host failure and test / wait", test_failure_host_wait<tester_test>);
200 XBT_INFO("***** Using <tester_wait<0>> *****");
201 run_test("exec and wait<0> once", test_trivial<tester_wait<0>>);
202 // run_test("exec and wait<0> many", test_basic<tester_wait<0>>);
203 run_test("cancel and wait<0>", test_cancel<tester_wait<0>>);
204 // run_test("actor failure and wait<0> / sleep", test_failure_actor_sleep<tester_wait<0>>);
205 // run_test("host failure and wait<0> / sleep", test_failure_host_sleep<tester_wait<0>>);
206 // run_test("actor failure and wait<0> / wait", test_failure_actor_wait<tester_wait<0>>);
207 // run_test("host failure and wait<0> / wait", test_failure_host_wait<tester_wait<0>>);
209 XBT_INFO("***** Using <tester_wait<1>> *****");
210 run_test("exec and wait<1> once", test_trivial<tester_wait<1>>);
211 // run_test("exec and wait<1> many", test_basic<tester_wait<1>>);
212 run_test("cancel and wait<1>", test_cancel<tester_wait<1>>);
213 // run_test("actor failure and wait<1> / sleep", test_failure_actor_sleep<tester_wait<1>>);
214 // run_test("host failure and wait<1> / sleep", test_failure_host_sleep<tester_wait<1>>);
215 // run_test("actor failure and wait<1> / wait", test_failure_actor_wait<tester_wait<1>>);
216 // run_test("host failure and wait<1> / wait", test_failure_host_wait<tester_wait<1>>);
219 int main(int argc, char* argv[])
221 simgrid::s4u::Engine e(&argc, argv);
223 const char* platf = argv[1];
225 XBT_WARN("No platform file provided. Using './testing_platform.xml'");
226 platf = "./testing_platform.xml";
228 e.load_platform(platf);
230 all_hosts = e.get_all_hosts();
231 simgrid::s4u::Actor::create("main_dispatcher", all_hosts[0], main_dispatcher);
235 XBT_INFO("Simulation done");