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 #define CATCH_CONFIG_RUNNER // we supply our own main()
8 #include "../../src/include/catch.hpp"
10 #include "simgrid/s4u.hpp"
11 #include <xbt/config.hpp>
13 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
15 std::vector<simgrid::s4u::Host*> all_hosts;
17 /* Helper function easing the testing of actor's ending condition */
18 static void assert_exit(bool exp_success, double duration)
20 double expected_time = simgrid::s4u::Engine::get_clock() + duration;
21 simgrid::s4u::this_actor::on_exit([exp_success, expected_time](bool got_failed) {
22 INFO("Check exit status. Expected: " << exp_success);
23 REQUIRE(exp_success == not got_failed);
24 INFO("Check date at exit. Expected: " << expected_time);
25 REQUIRE(simgrid::s4u::Engine::get_clock() == Approx(expected_time));
26 XBT_VERB("Checks on exit successful");
30 /* Helper function in charge of doing some sanity checks after each test */
31 static void assert_cleanup()
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 INFO("Host " << all_hosts[i]->get_cname() << " contains " << all_actors.size() << " actors but " << expected_count
39 << " are expected (i=" << i << "). Existing actors: ");
40 for (auto act : all_actors)
41 UNSCOPED_INFO(" - " << act->get_cname());
42 FAIL("This is wrong");
45 // TODO: Check that all LMM are empty
52 //========== Creators: create an async activity
54 // Create a new async execution with given duration
55 static simgrid::s4u::ActivityPtr create_exec(double duration)
57 double speed = simgrid::s4u::this_actor::get_host()->get_speed();
58 return simgrid::s4u::this_actor::exec_async(speed * duration);
61 // TODO: check other kinds of activities too (Io, Comm, ...)
63 using creator_type = decltype(create_exec);
65 //========== Testers: test the completion of an activity
67 // Calls exec->test() and returns its result
68 static bool tester_test(const simgrid::s4u::ActivityPtr& exec)
73 // Calls exec->wait_for(Duration * 0.0125) and returns true when exec is terminated, just like test()
74 template <int Duration> bool tester_wait(const simgrid::s4u::ActivityPtr& exec)
78 exec->wait_for(Duration * 0.0125);
79 XBT_DEBUG("wait_for() returned normally");
81 } catch (const simgrid::TimeoutException& e) {
82 XBT_DEBUG("wait_for() timed out (%s)", e.what());
84 } catch (const simgrid::Exception& e) {
85 XBT_DEBUG("wait_for() threw an exception: %s", e.what());
91 using tester_type = decltype(tester_test);
93 //========== Waiters: wait for the completion of an activity
96 static void waiter_sleep6(const simgrid::s4u::ActivityPtr&)
98 simgrid::s4u::this_actor::sleep_for(6.0);
99 XBT_DEBUG("wake up after 6s sleep");
102 // Wait for completion of exec
103 static void waiter_wait(const simgrid::s4u::ActivityPtr& exec)
106 XBT_DEBUG("end of wait()");
109 using waiter_type = decltype(waiter_wait);
111 //========== Finally, the test templates
113 template <creator_type Create, tester_type Test> void test_trivial()
115 XBT_INFO("Launch an activity for 5s, and let it proceed before test");
117 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], []() {
118 assert_exit(true, 6.);
119 simgrid::s4u::ActivityPtr activity = Create(5.0);
120 simgrid::s4u::this_actor::sleep_for(6.0);
121 INFO("activity should be terminated now");
122 REQUIRE(Test(activity));
127 template <creator_type Create, tester_type Test> void test_basic()
129 XBT_INFO("Launch an activity for 5s, and test while it proceeds");
131 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], []() {
132 assert_exit(true, 6.);
133 simgrid::s4u::ActivityPtr activity = Create(5.0);
134 for (int i = 0; i < 3; i++) {
135 INFO("activity should be still running (i = " << i << ")");
136 REQUIRE(not Test(activity));
137 simgrid::s4u::this_actor::sleep_for(2.0);
139 INFO("activity should be terminated now");
140 REQUIRE(Test(activity));
145 template <creator_type Create, tester_type Test> void test_cancel()
147 XBT_INFO("Launch an activity for 5s, and cancel it after 2s");
149 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], []() {
150 assert_exit(true, 2.);
151 simgrid::s4u::ActivityPtr activity = Create(5.0);
152 simgrid::s4u::this_actor::sleep_for(2.0);
154 INFO("activity should be terminated now");
155 REQUIRE(Test(activity));
160 template <creator_type Create, tester_type Test, waiter_type Wait> void test_failure_actor()
162 XBT_INFO("Launch an activity for 5s, and kill running actor after 2s");
164 simgrid::s4u::ActivityPtr activity;
165 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&activity]() {
166 assert_exit(false, 2.);
167 activity = Create(5.0);
169 FAIL("should not be here!");
171 simgrid::s4u::this_actor::sleep_for(2.0);
172 INFO("activity should be still running");
173 REQUIRE(not Test(activity));
175 INFO("activity should be terminated now");
176 REQUIRE(Test(activity));
179 template <creator_type Create, tester_type Test, waiter_type Wait> void test_failure_host()
181 XBT_INFO("Launch an activity for 5s, and shutdown host 2s");
183 simgrid::s4u::ActivityPtr activity;
184 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&activity]() {
185 assert_exit(false, 2.);
186 activity = Create(5.0);
188 FAIL("should not be here!");
190 simgrid::s4u::this_actor::sleep_for(2.0);
191 INFO("activity should be still running");
192 REQUIRE(not Test(activity));
193 exec5->get_host()->turn_off();
194 exec5->get_host()->turn_on();
195 INFO("activity should be terminated now");
196 REQUIRE(Test(activity));
201 /* We need an extra actor here, so that it can sleep until the end of each test */
202 #define RUN_SECTION(descr, ...) SECTION(descr) { simgrid::s4u::Actor::create(descr, all_hosts[0], __VA_ARGS__); }
204 TEST_CASE("Activity test/wait: using <tester_test>")
206 XBT_INFO("#####[ launch next test ]#####");
208 RUN_SECTION("exec: run and test once", test_trivial<create_exec, tester_test>);
209 RUN_SECTION("exec: run and test many", test_basic<create_exec, tester_test>);
210 RUN_SECTION("exec: cancel and test", test_cancel<create_exec, tester_test>);
211 // exec: actor failure and test / sleep
212 // exec: host failure and test / sleep
213 RUN_SECTION("exec: actor failure and test / wait", test_failure_actor<create_exec, tester_test, waiter_wait>);
214 RUN_SECTION("exec: host failure and test / wait", test_failure_host<create_exec, tester_test, waiter_wait>);
216 simgrid::s4u::this_actor::sleep_for(10);
220 TEST_CASE("Activity test/wait: using <tester_wait<0>>")
222 XBT_INFO("#####[ launch next test ]#####");
224 RUN_SECTION("exec: run and wait<0> once", test_trivial<create_exec, tester_wait<0>>);
225 // exec: run and wait<0> many
226 RUN_SECTION("exec: cancel and wait<0>", test_cancel<create_exec, tester_wait<0>>);
227 // exec: actor failure and wait<0> / sleep
228 // exec: host failure and wait<0> / sleep
229 // exec: actor failure and wait<0> / wait
230 // exec: host failure and wait<0> / wait
232 simgrid::s4u::this_actor::sleep_for(10);
236 TEST_CASE("Activity test/wait: using <tester_wait<1>>")
238 XBT_INFO("#####[ launch next test ]#####");
240 RUN_SECTION("exec: run and wait<1> once", test_trivial<create_exec, tester_wait<1>>);
241 // exec: run and wait<1> many
242 RUN_SECTION("exec: cancel and wait<1>", test_cancel<create_exec, tester_wait<1>>);
243 // exec: actor failure and wait<1> / sleep
244 // exec: host failure and wait<1> / sleep
245 // exec: actor failure and wait<1> / wait
246 // exec: host failure and wait<1> / wait
248 simgrid::s4u::this_actor::sleep_for(10);
252 // FIXME: The tests grouped here are currently failing. Once fixed, they should be put in the right section above.
253 // The tests can be activated with run-time parameter '*' or, more specifically '[failing]'
254 TEST_CASE("Activity test/wait: tests currently failing", "[.][failing]")
256 XBT_INFO("#####[ launch next failing test ]#####");
259 RUN_SECTION("exec: actor failure and test / sleep", test_failure_actor<create_exec, tester_test, waiter_sleep6>);
260 RUN_SECTION("exec: host failure and test / sleep", test_failure_host<create_exec, tester_test, waiter_sleep6>);
262 // with tester_wait<0>
263 RUN_SECTION("exec: run and wait<0> many", test_basic<create_exec, tester_wait<0>>);
264 RUN_SECTION("exec: actor failure and wait<0> / sleep", test_failure_actor<create_exec, tester_wait<0>, waiter_sleep6>);
265 RUN_SECTION("exec: host failure and wait<0> / sleep", test_failure_host<create_exec, tester_wait<0>, waiter_sleep6>);
266 RUN_SECTION("exec: actor failure and wait<0> / wait", test_failure_actor<create_exec, tester_wait<0>, waiter_wait>);
267 RUN_SECTION("exec: host failure and wait<0> / wait", test_failure_host<create_exec, tester_wait<0>, waiter_wait>);
269 // with tester_test<1>
270 RUN_SECTION("exec: run and wait<1> many", test_basic<create_exec, tester_wait<1>>);
271 RUN_SECTION("exec: actor failure and wait<1> / sleep", test_failure_actor<create_exec, tester_wait<1>, waiter_sleep6>);
272 RUN_SECTION("exec: host failure and wait<1> / sleep", test_failure_host<create_exec, tester_wait<1>, waiter_sleep6>);
273 RUN_SECTION("exec: actor failure and wait<1> / wait", test_failure_actor<create_exec, tester_wait<1>, waiter_wait>);
274 RUN_SECTION("exec: host failure and wait<1> / wait", test_failure_host<create_exec, tester_wait<1>, waiter_wait>);
276 simgrid::s4u::this_actor::sleep_for(10);
280 int main(int argc, char* argv[])
282 simgrid::config::set_value("help-nostop", true);
283 simgrid::s4u::Engine e(&argc, argv);
292 XBT_WARN("No platform file provided. Using './testing_platform.xml'");
293 platf = "./testing_platform.xml";
295 e.load_platform(platf);
298 all_hosts = e.get_all_hosts();
299 simgrid::s4u::Actor::create("main_dispatcher", all_hosts[0],
300 [&argc, &argv, &status]() { status = Catch::Session().run(argc, argv); });
303 XBT_INFO("Simulation done");