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 // FIXME: fix failing tests, and remove this macro
13 #define FAILING if (false)
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_failed, double duration)
20 double expected_time = simgrid::s4u::Engine::get_clock() + duration;
21 simgrid::s4u::this_actor::on_exit([exp_failed, expected_time](bool got_failed) {
22 xbt_assert(exp_failed == got_failed, "Exit failure status mismatch. Expected %d, got %d", exp_failed, got_failed);
23 xbt_assert(std::fabs(expected_time - simgrid::s4u::Engine::get_clock()) < 0.001, "Exit time mismatch. Expected %f",
25 XBT_VERB("Checks on exit successful");
28 /* Helper function in charge of running a test and doing some sanity checks afterward */
29 static void run_test(const char* test_name, const std::function<void()>& test)
31 simgrid::s4u::Actor::create(test_name, all_hosts[0], test);
32 simgrid::s4u::this_actor::sleep_for(10);
34 /* Check that no actor remain (but on host[0], where main_dispatcher lives */
35 for (unsigned int i = 0; i < all_hosts.size(); i++) {
36 std::vector<simgrid::s4u::ActorPtr> all_actors = all_hosts[i]->get_all_actors();
37 unsigned int expected_count = (i == 0) ? 1 : 0; // host[0] contains main_dispatcher, all other are empty
38 if (all_actors.size() != expected_count) {
39 XBT_CRITICAL("Host %s contains %zu actors but %u are expected (i=%u). Existing actors: ",
40 all_hosts[i]->get_cname(), all_actors.size(), expected_count, i);
41 for (auto act : all_actors)
42 XBT_CRITICAL(" - %s", act->get_cname());
43 xbt_die("This is wrong");
46 xbt_assert("TODO: Check that all LMM are empty");
47 XBT_INFO("SUCCESS: %s", test_name);
48 XBT_INFO("#########################################################################################################");
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("%s: Launch an activity for 5s, and let it proceed before test", __func__);
120 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], []() {
121 assert_exit(false, 6.);
122 simgrid::s4u::ActivityPtr activity = Create(5.0);
123 simgrid::s4u::this_actor::sleep_for(6.0);
124 xbt_assert(Test(activity), "activity should be terminated now");
129 template <creator_type Create, tester_type Test> void test_basic()
131 XBT_INFO("%s: Launch an activity for 5s, and test while it proceeds", __func__);
133 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], []() {
134 assert_exit(false, 6.);
135 simgrid::s4u::ActivityPtr activity = Create(5.0);
136 for (int i = 0; i < 3; i++) {
137 xbt_assert(not Test(activity), "activity finished too soon (i = %d)!?", i);
138 simgrid::s4u::this_actor::sleep_for(2.0);
140 xbt_assert(Test(activity), "activity should be terminated now");
145 template <creator_type Create, tester_type Test> void test_cancel()
147 XBT_INFO("%s: Launch an activity for 5s, and cancel it after 2s", __func__);
149 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], []() {
150 assert_exit(false, 2.);
151 simgrid::s4u::ActivityPtr activity = Create(5.0);
152 simgrid::s4u::this_actor::sleep_for(2.0);
154 xbt_assert(Test(activity), "activity should be terminated now");
159 template <creator_type Create, tester_type Test, waiter_type Wait> void test_failure_actor()
161 XBT_INFO("%s: Launch an activity for 5s, and kill running actor after 2s", __func__);
163 simgrid::s4u::ActivityPtr activity;
164 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&activity]() {
165 assert_exit(true, 2.);
166 activity = Create(5.0);
168 xbt_die("should not be here!");
170 simgrid::s4u::this_actor::sleep_for(2.0);
171 xbt_assert(not Test(activity), "activity finished too soon!?");
173 xbt_assert(Test(activity), "activity should be terminated now");
176 template <creator_type Create, tester_type Test, waiter_type Wait> void test_failure_host()
178 XBT_INFO("%s: Launch an activity for 5s, and shutdown host 2s", __func__);
180 simgrid::s4u::ActivityPtr activity;
181 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&activity]() {
182 assert_exit(true, 2.);
183 activity = Create(5.0);
185 xbt_die("should not be here!");
187 simgrid::s4u::this_actor::sleep_for(2.0);
188 xbt_assert(not Test(activity), "activity finished too soon!?");
189 exec5->get_host()->turn_off();
190 exec5->get_host()->turn_on();
191 xbt_assert(Test(activity), "activity should be terminated now");
196 /* We need an extra actor here, so that it can sleep until the end of each test */
197 static void main_dispatcher()
199 XBT_INFO("***** Using <tester_test> *****");
200 run_test("exec: run and test once", test_trivial<create_exec, tester_test>);
201 run_test("exec: run and test many", test_basic<create_exec, tester_test>);
202 run_test("exec: cancel and test", test_cancel<create_exec, tester_test>);
203 FAILING run_test("exec: actor failure and test / sleep", test_failure_actor<create_exec, tester_test, waiter_sleep6>);
204 FAILING run_test("exec: host failure and test / sleep", test_failure_host<create_exec, tester_test, waiter_sleep6>);
205 run_test("exec: actor failure and test / wait", test_failure_actor<create_exec, tester_test, waiter_wait>);
206 run_test("exec: host failure and test / wait", test_failure_host<create_exec, tester_test, waiter_wait>);
208 XBT_INFO("***** Using <tester_wait<0>> *****");
209 run_test("exec: run and wait<0> once", test_trivial<create_exec, tester_wait<0>>);
210 FAILING run_test("exec: run and wait<0> many", test_basic<create_exec, tester_wait<0>>);
211 run_test("exec: cancel and wait<0>", test_cancel<create_exec, tester_wait<0>>);
212 FAILING run_test("exec: actor failure and wait<0> / sleep", test_failure_actor<create_exec, tester_wait<0>, waiter_sleep6>);
213 FAILING run_test("exec: host failure and wait<0> / sleep", test_failure_host<create_exec, tester_wait<0>, waiter_sleep6>);
214 FAILING run_test("exec: actor failure and wait<0> / wait", test_failure_actor<create_exec, tester_wait<0>, waiter_wait>);
215 FAILING run_test("exec: host failure and wait<0> / wait", test_failure_host<create_exec, tester_wait<0>, waiter_wait>);
217 XBT_INFO("***** Using <tester_wait<1>> *****");
218 run_test("exec: run and wait<1> once", test_trivial<create_exec, tester_wait<1>>);
219 FAILING run_test("exec: run and wait<1> many", test_basic<create_exec, tester_wait<1>>);
220 run_test("exec: cancel and wait<1>", test_cancel<create_exec, tester_wait<1>>);
221 FAILING run_test("exec: actor failure and wait<1> / sleep", test_failure_actor<create_exec, tester_wait<1>, waiter_sleep6>);
222 FAILING run_test("exec: host failure and wait<1> / sleep", test_failure_host<create_exec, tester_wait<1>, waiter_sleep6>);
223 FAILING run_test("exec: actor failure and wait<1> / wait", test_failure_actor<create_exec, tester_wait<1>, waiter_wait>);
224 FAILING run_test("exec: host failure and wait<1> / wait", test_failure_host<create_exec, tester_wait<1>, waiter_wait>);
227 int main(int argc, char* argv[])
229 simgrid::s4u::Engine e(&argc, argv);
231 const char* platf = argv[1];
233 XBT_WARN("No platform file provided. Using './testing_platform.xml'");
234 platf = "./testing_platform.xml";
236 e.load_platform(platf);
238 all_hosts = e.get_all_hosts();
239 simgrid::s4u::Actor::create("main_dispatcher", all_hosts[0], main_dispatcher);
243 XBT_INFO("Simulation done");