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 // FIXME: fix failing tests, and remove this macro
16 #define FAILING if (false)
18 std::vector<simgrid::s4u::Host*> all_hosts;
20 /* Helper function easing the testing of actor's ending condition */
21 static void assert_exit(bool exp_success, double duration)
23 double expected_time = simgrid::s4u::Engine::get_clock() + duration;
24 simgrid::s4u::this_actor::on_exit([exp_success, expected_time](bool got_failed) {
25 INFO("Check exit status. Expected: " << exp_success);
26 REQUIRE(exp_success == not got_failed);
27 INFO("Check date at exit. Expected: " << expected_time);
28 REQUIRE(simgrid::s4u::Engine::get_clock() == Approx(expected_time));
29 XBT_VERB("Checks on exit successful");
33 /* Helper function in charge of doing some sanity checks after each test */
34 static void assert_cleanup()
36 /* Check that no actor remain (but on host[0], where main_dispatcher lives */
37 for (unsigned int i = 0; i < all_hosts.size(); i++) {
38 std::vector<simgrid::s4u::ActorPtr> all_actors = all_hosts[i]->get_all_actors();
39 unsigned int expected_count = (i == 0) ? 1 : 0; // host[0] contains main_dispatcher, all other are empty
40 if (all_actors.size() != expected_count) {
41 INFO("Host " << all_hosts[i]->get_cname() << " contains " << all_actors.size() << " actors but " << expected_count
42 << " are expected (i=" << i << "). Existing actors: ");
43 for (auto act : all_actors)
44 UNSCOPED_INFO(" - " << act->get_cname());
45 FAIL("This is wrong");
48 // TODO: Check that all LMM are empty
55 //========== Creators: create an async activity
57 // Create a new async execution with given duration
58 static simgrid::s4u::ActivityPtr create_exec(double duration)
60 double speed = simgrid::s4u::this_actor::get_host()->get_speed();
61 return simgrid::s4u::this_actor::exec_async(speed * duration);
64 // TODO: check other kinds of activities too (Io, Comm, ...)
66 using creator_type = decltype(create_exec);
68 //========== Testers: test the completion of an activity
70 // Calls exec->test() and returns its result
71 static bool tester_test(const simgrid::s4u::ActivityPtr& exec)
76 // Calls exec->wait_for(Duration * 0.0125) and returns true when exec is terminated, just like test()
77 template <int Duration> bool tester_wait(const simgrid::s4u::ActivityPtr& exec)
81 exec->wait_for(Duration * 0.0125);
82 XBT_DEBUG("wait_for() returned normally");
84 } catch (const simgrid::TimeoutException& e) {
85 XBT_DEBUG("wait_for() timed out (%s)", e.what());
87 } catch (const simgrid::Exception& e) {
88 XBT_DEBUG("wait_for() threw an exception: %s", e.what());
94 using tester_type = decltype(tester_test);
96 //========== Waiters: wait for the completion of an activity
99 static void waiter_sleep6(const simgrid::s4u::ActivityPtr&)
101 simgrid::s4u::this_actor::sleep_for(6.0);
102 XBT_DEBUG("wake up after 6s sleep");
105 // Wait for completion of exec
106 static void waiter_wait(const simgrid::s4u::ActivityPtr& exec)
109 XBT_DEBUG("end of wait()");
112 using waiter_type = decltype(waiter_wait);
114 //========== Finally, the test templates
116 template <creator_type Create, tester_type Test> void test_trivial()
118 XBT_INFO("Launch an activity for 5s, and let it proceed before test");
120 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], []() {
121 assert_exit(true, 6.);
122 simgrid::s4u::ActivityPtr activity = Create(5.0);
123 simgrid::s4u::this_actor::sleep_for(6.0);
124 INFO("activity should be terminated now");
125 REQUIRE(Test(activity));
130 template <creator_type Create, tester_type Test> void test_basic()
132 XBT_INFO("Launch an activity for 5s, and test while it proceeds");
134 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], []() {
135 assert_exit(true, 6.);
136 simgrid::s4u::ActivityPtr activity = Create(5.0);
137 for (int i = 0; i < 3; i++) {
138 INFO("activity should be still running (i = " << i << ")");
139 REQUIRE(not Test(activity));
140 simgrid::s4u::this_actor::sleep_for(2.0);
142 INFO("activity should be terminated now");
143 REQUIRE(Test(activity));
148 template <creator_type Create, tester_type Test> void test_cancel()
150 XBT_INFO("Launch an activity for 5s, and cancel it after 2s");
152 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], []() {
153 assert_exit(true, 2.);
154 simgrid::s4u::ActivityPtr activity = Create(5.0);
155 simgrid::s4u::this_actor::sleep_for(2.0);
157 INFO("activity should be terminated now");
158 REQUIRE(Test(activity));
163 template <creator_type Create, tester_type Test, waiter_type Wait> void test_failure_actor()
165 XBT_INFO("Launch an activity for 5s, and kill running actor after 2s");
167 simgrid::s4u::ActivityPtr activity;
168 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&activity]() {
169 assert_exit(false, 2.);
170 activity = Create(5.0);
172 FAIL("should not be here!");
174 simgrid::s4u::this_actor::sleep_for(2.0);
175 INFO("activity should be still running");
176 REQUIRE(not Test(activity));
178 INFO("activity should be terminated now");
179 REQUIRE(Test(activity));
182 template <creator_type Create, tester_type Test, waiter_type Wait> void test_failure_host()
184 XBT_INFO("Launch an activity for 5s, and shutdown host 2s");
186 simgrid::s4u::ActivityPtr activity;
187 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&activity]() {
188 assert_exit(false, 2.);
189 activity = Create(5.0);
191 FAIL("should not be here!");
193 simgrid::s4u::this_actor::sleep_for(2.0);
194 INFO("activity should be still running");
195 REQUIRE(not Test(activity));
196 exec5->get_host()->turn_off();
197 exec5->get_host()->turn_on();
198 INFO("activity should be terminated now");
199 REQUIRE(Test(activity));
204 /* We need an extra actor here, so that it can sleep until the end of each test */
205 #define RUN_SECTION(descr, ...) SECTION(descr) { simgrid::s4u::Actor::create(descr, all_hosts[0], __VA_ARGS__); }
207 TEST_CASE("Activity test/wait: using <tester_test>")
209 XBT_INFO("#####[ launch next test ]#####");
211 RUN_SECTION("exec: run and test once", test_trivial<create_exec, tester_test>);
212 RUN_SECTION("exec: run and test many", test_basic<create_exec, tester_test>);
213 RUN_SECTION("exec: cancel and test", test_cancel<create_exec, tester_test>);
214 FAILING{} RUN_SECTION("exec: actor failure and test / sleep", test_failure_actor<create_exec, tester_test, waiter_sleep6>);
215 FAILING RUN_SECTION("exec: host failure and test / sleep", test_failure_host<create_exec, tester_test, waiter_sleep6>);
216 RUN_SECTION("exec: actor failure and test / wait", test_failure_actor<create_exec, tester_test, waiter_wait>);
217 RUN_SECTION("exec: host failure and test / wait", test_failure_host<create_exec, tester_test, waiter_wait>);
219 simgrid::s4u::this_actor::sleep_for(10);
223 TEST_CASE("Activity test/wait: using <tester_wait<0>>")
225 XBT_INFO("#####[ launch next test ]#####");
227 RUN_SECTION("exec: run and wait<0> once", test_trivial<create_exec, tester_wait<0>>);
228 FAILING RUN_SECTION("exec: run and wait<0> many", test_basic<create_exec, tester_wait<0>>);
229 RUN_SECTION("exec: cancel and wait<0>", test_cancel<create_exec, tester_wait<0>>);
230 FAILING RUN_SECTION("exec: actor failure and wait<0> / sleep", test_failure_actor<create_exec, tester_wait<0>, waiter_sleep6>);
231 FAILING RUN_SECTION("exec: host failure and wait<0> / sleep", test_failure_host<create_exec, tester_wait<0>, waiter_sleep6>);
232 FAILING RUN_SECTION("exec: actor failure and wait<0> / wait", test_failure_actor<create_exec, tester_wait<0>, waiter_wait>);
233 FAILING RUN_SECTION("exec: host failure and wait<0> / wait", test_failure_host<create_exec, tester_wait<0>, waiter_wait>);
235 simgrid::s4u::this_actor::sleep_for(10);
239 TEST_CASE("Activity test/wait: using <tester_wait<1>>")
241 XBT_INFO("#####[ launch next test ]#####");
243 RUN_SECTION("exec: run and wait<1> once", test_trivial<create_exec, tester_wait<1>>);
244 FAILING RUN_SECTION("exec: run and wait<1> many", test_basic<create_exec, tester_wait<1>>);
245 RUN_SECTION("exec: cancel and wait<1>", test_cancel<create_exec, tester_wait<1>>);
246 FAILING RUN_SECTION("exec: actor failure and wait<1> / sleep", test_failure_actor<create_exec, tester_wait<1>, waiter_sleep6>);
247 FAILING RUN_SECTION("exec: host failure and wait<1> / sleep", test_failure_host<create_exec, tester_wait<1>, waiter_sleep6>);
248 FAILING RUN_SECTION("exec: actor failure and wait<1> / wait", test_failure_actor<create_exec, tester_wait<1>, waiter_wait>);
249 FAILING RUN_SECTION("exec: host failure and wait<1> / wait", test_failure_host<create_exec, tester_wait<1>, waiter_wait>);
251 simgrid::s4u::this_actor::sleep_for(10);
255 int main(int argc, char* argv[])
257 simgrid::config::set_value("help-nostop", true);
258 simgrid::s4u::Engine e(&argc, argv);
267 XBT_WARN("No platform file provided. Using './testing_platform.xml'");
268 platf = "./testing_platform.xml";
270 e.load_platform(platf);
273 all_hosts = e.get_all_hosts();
274 simgrid::s4u::Actor::create("main_dispatcher", all_hosts[0],
275 [&argc, &argv, &status]() { status = Catch::Session().run(argc, argv); });
278 XBT_INFO("Simulation done");