Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Convert activity-test-wait to catch2.
[simgrid.git] / teshsuite / s4u / activity-test-wait / activity-test-wait.cpp
1 /* Copyright (c) 2010-2020. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #define CATCH_CONFIG_RUNNER // we supply our own main()
7
8 #include "../../src/include/catch.hpp"
9
10 #include "simgrid/s4u.hpp"
11 #include <xbt/config.hpp>
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
14
15 // FIXME: fix failing tests, and remove this macro
16 #define FAILING if (false)
17
18 std::vector<simgrid::s4u::Host*> all_hosts;
19
20 /* Helper function easing the testing of actor's ending condition */
21 static void assert_exit(bool exp_success, double duration)
22 {
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");
30   });
31 }
32
33 /* Helper function in charge of doing some sanity checks after each test */
34 static void assert_cleanup()
35 {
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");
46     }
47   }
48   // TODO: Check that all LMM are empty
49 }
50
51 /**
52  ** Each tests
53  **/
54
55 //========== Creators: create an async activity
56
57 // Create a new async execution with given duration
58 static simgrid::s4u::ActivityPtr create_exec(double duration)
59 {
60   double speed = simgrid::s4u::this_actor::get_host()->get_speed();
61   return simgrid::s4u::this_actor::exec_async(speed * duration);
62 }
63
64 // TODO: check other kinds of activities too (Io, Comm, ...)
65
66 using creator_type = decltype(create_exec);
67
68 //========== Testers: test the completion of an activity
69
70 // Calls exec->test() and returns its result
71 static bool tester_test(const simgrid::s4u::ActivityPtr& exec)
72 {
73   return exec->test();
74 }
75
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)
78 {
79   bool ret;
80   try {
81     exec->wait_for(Duration * 0.0125);
82     XBT_DEBUG("wait_for() returned normally");
83     ret = true;
84   } catch (const simgrid::TimeoutException& e) {
85     XBT_DEBUG("wait_for() timed out (%s)", e.what());
86     ret = false;
87   } catch (const simgrid::Exception& e) {
88     XBT_DEBUG("wait_for() threw an exception: %s", e.what());
89     ret = true;
90   }
91   return ret;
92 }
93
94 using tester_type = decltype(tester_test);
95
96 //========== Waiters: wait for the completion of an activity
97
98 // Wait for 6s
99 static void waiter_sleep6(const simgrid::s4u::ActivityPtr&)
100 {
101   simgrid::s4u::this_actor::sleep_for(6.0);
102   XBT_DEBUG("wake up after 6s sleep");
103 }
104
105 // Wait for completion of exec
106 static void waiter_wait(const simgrid::s4u::ActivityPtr& exec)
107 {
108   exec->wait();
109   XBT_DEBUG("end of wait()");
110 }
111
112 using waiter_type = decltype(waiter_wait);
113
114 //========== Finally, the test templates
115
116 template <creator_type Create, tester_type Test> void test_trivial()
117 {
118   XBT_INFO("Launch an activity for 5s, and let it proceed before test");
119
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));
126   });
127   exec5->join();
128 }
129
130 template <creator_type Create, tester_type Test> void test_basic()
131 {
132   XBT_INFO("Launch an activity for 5s, and test while it proceeds");
133
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);
141     }
142     INFO("activity should be terminated now");
143     REQUIRE(Test(activity));
144   });
145   exec5->join();
146 }
147
148 template <creator_type Create, tester_type Test> void test_cancel()
149 {
150   XBT_INFO("Launch an activity for 5s, and cancel it after 2s");
151
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);
156     activity->cancel();
157     INFO("activity should be terminated now");
158     REQUIRE(Test(activity));
159   });
160   exec5->join();
161 }
162
163 template <creator_type Create, tester_type Test, waiter_type Wait> void test_failure_actor()
164 {
165   XBT_INFO("Launch an activity for 5s, and kill running actor after 2s");
166
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);
171     Wait(activity);
172     FAIL("should not be here!");
173   });
174   simgrid::s4u::this_actor::sleep_for(2.0);
175   INFO("activity should be still running");
176   REQUIRE(not Test(activity));
177   exec5->kill();
178   INFO("activity should be terminated now");
179   REQUIRE(Test(activity));
180 }
181
182 template <creator_type Create, tester_type Test, waiter_type Wait> void test_failure_host()
183 {
184   XBT_INFO("Launch an activity for 5s, and shutdown host 2s");
185
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);
190     Wait(activity);
191     FAIL("should not be here!");
192   });
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));
200 }
201
202 //==========
203
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__); }
206
207 TEST_CASE("Activity test/wait: using <tester_test>")
208 {
209   XBT_INFO("#####[ launch next test ]#####");
210
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>);
218
219   simgrid::s4u::this_actor::sleep_for(10);
220   assert_cleanup();
221 }
222
223 TEST_CASE("Activity test/wait: using <tester_wait<0>>")
224 {
225   XBT_INFO("#####[ launch next test ]#####");
226
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>);
234
235   simgrid::s4u::this_actor::sleep_for(10);
236   assert_cleanup();
237 }
238
239 TEST_CASE("Activity test/wait: using <tester_wait<1>>")
240 {
241   XBT_INFO("#####[ launch next test ]#####");
242
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>);
250
251   simgrid::s4u::this_actor::sleep_for(10);
252   assert_cleanup();
253 }
254
255 int main(int argc, char* argv[])
256 {
257   simgrid::config::set_value("help-nostop", true);
258   simgrid::s4u::Engine e(&argc, argv);
259
260   std::string platf;
261   if (argc > 1) {
262     platf   = argv[1];
263     argv[1] = argv[0];
264     argv++;
265     argc--;
266   } else {
267     XBT_WARN("No platform file provided. Using './testing_platform.xml'");
268     platf = "./testing_platform.xml";
269   }
270   e.load_platform(platf);
271
272   int status = 42;
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); });
276
277   e.run();
278   XBT_INFO("Simulation done");
279   return status;
280 }