Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add activity-test-wait testing test() and wait_for() for activities.
[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 #include "simgrid/s4u.hpp"
7
8 #include <cmath>
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
11
12 std::vector<simgrid::s4u::Host*> all_hosts;
13
14 /* Helper function easing the testing of actor's ending condition */
15 static void assert_exit(bool exp_failed, double duration)
16 {
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",
21                expected_time);
22     XBT_VERB("Checks on exit successful");
23   });
24 }
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)
27 {
28   simgrid::s4u::Actor::create(test_name, all_hosts[0], test);
29   simgrid::s4u::this_actor::sleep_for(10);
30
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");
41     }
42   }
43   xbt_assert("TODO: Check that all LMM are empty");
44   XBT_INFO("SUCCESS: %s", test_name);
45   XBT_INFO("#########################################################################################################");
46 }
47
48 /**
49  ** Each tests
50  **/
51
52 // Calls exec->test() and returns its result
53 static bool tester_test(simgrid::s4u::ExecPtr& exec)
54 {
55   return exec->test();
56 }
57
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)
60 {
61   bool ret;
62   try {
63     exec->wait_for(Duration * 0.0125);
64     ret = true;
65   } catch (const simgrid::TimeoutException&) {
66     ret = false;
67   } catch (const simgrid::Exception&) {
68     ret = true;
69   }
70   return ret;
71 }
72
73 using tester_type = decltype(tester_test);
74
75 template <tester_type Test> void test_trivial()
76 {
77   XBT_INFO("%s: Launch a execute(5s), and let it proceed before test", __func__);
78
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");
84   });
85   exec5->join();
86 }
87
88 template <tester_type Test> void test_basic()
89 {
90   XBT_INFO("%s: Launch a execute(5s), and test while it proceeds", __func__);
91
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);
98     }
99     xbt_assert(Test(exec), "exec should be terminated now");
100   });
101   exec5->join();
102 }
103
104 template <tester_type Test> void test_cancel()
105 {
106   XBT_INFO("%s: Launch a execute(5s), and cancel it after 2s", __func__);
107
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);
112     exec->cancel();
113     xbt_assert(Test(exec), "exec should be terminated now");
114   });
115   exec5->join();
116 }
117
118 template <tester_type Test> void test_failure_actor_sleep()
119 {
120   XBT_INFO("%s: Launch a execute(5s), and kill running actor after 2s (sleep during exec)", __func__);
121
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!");
128   });
129   simgrid::s4u::this_actor::sleep_for(2.0);
130   xbt_assert(not Test(exec), "exec finished too soon!?");
131   exec5->kill();
132   xbt_assert(Test(exec), "exec should be terminated now");
133 }
134
135 template <tester_type Test> void test_failure_host_sleep()
136 {
137   XBT_INFO("%s: Launch a execute(5s), and shutdown host 2s (sleep during exec)", __func__);
138
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!");
145   });
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");
151 }
152
153 template <tester_type Test> void test_failure_actor_wait()
154 {
155   XBT_INFO("%s: Launch a execute(5s), and kill running actor after 2s (wait for exec)", __func__);
156
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);
161     exec->wait();
162     XBT_ERROR("should not be here!");
163   });
164   simgrid::s4u::this_actor::sleep_for(2.0);
165   xbt_assert(not Test(exec), "exec finished too soon!?");
166   exec5->kill();
167   xbt_assert(Test(exec), "exec should be terminated now");
168 }
169
170 template <tester_type Test> void test_failure_host_wait()
171 {
172   XBT_INFO("%s: Launch a execute(5s), and shutdown host 2s (wait for exec)", __func__);
173
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);
178     exec->wait();
179     XBT_ERROR("should not be here!");
180   });
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");
186 }
187
188 /* We need an extra actor here, so that it can sleep until the end of each test */
189 static void main_dispatcher()
190 {
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>);
199
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>>);
208
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>>);
217 }
218
219 int main(int argc, char* argv[])
220 {
221   simgrid::s4u::Engine e(&argc, argv);
222
223   const char* platf = argv[1];
224   if (argc <= 1) {
225     XBT_WARN("No platform file provided. Using './testing_platform.xml'");
226     platf = "./testing_platform.xml";
227   }
228   e.load_platform(platf);
229
230   all_hosts = e.get_all_hosts();
231   simgrid::s4u::Actor::create("main_dispatcher", all_hosts[0], main_dispatcher);
232
233   e.run();
234
235   XBT_INFO("Simulation done");
236
237   return 0;
238 }