Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update tests.
[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 // FIXME: fix failing tests, and remove this macro
13 #define FAILING if (false)
14
15 std::vector<simgrid::s4u::Host*> all_hosts;
16
17 /* Helper function easing the testing of actor's ending condition */
18 static void assert_exit(bool exp_failed, double duration)
19 {
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",
24                expected_time);
25     XBT_VERB("Checks on exit successful");
26   });
27 }
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)
30 {
31   simgrid::s4u::Actor::create(test_name, all_hosts[0], test);
32   simgrid::s4u::this_actor::sleep_for(10);
33
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");
44     }
45   }
46   xbt_assert("TODO: Check that all LMM are empty");
47   XBT_INFO("SUCCESS: %s", test_name);
48   XBT_INFO("#########################################################################################################");
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("%s: Launch an activity for 5s, and let it proceed before test", __func__);
119
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");
125   });
126   exec5->join();
127 }
128
129 template <creator_type Create, tester_type Test> void test_basic()
130 {
131   XBT_INFO("%s: Launch an activity for 5s, and test while it proceeds", __func__);
132
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);
139     }
140     xbt_assert(Test(activity), "activity should be terminated now");
141   });
142   exec5->join();
143 }
144
145 template <creator_type Create, tester_type Test> void test_cancel()
146 {
147   XBT_INFO("%s: Launch an activity for 5s, and cancel it after 2s", __func__);
148
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);
153     activity->cancel();
154     xbt_assert(Test(activity), "activity should be terminated now");
155   });
156   exec5->join();
157 }
158
159 template <creator_type Create, tester_type Test, waiter_type Wait> void test_failure_actor()
160 {
161   XBT_INFO("%s: Launch an activity for 5s, and kill running actor after 2s", __func__);
162
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);
167     Wait(activity);
168     xbt_die("should not be here!");
169   });
170   simgrid::s4u::this_actor::sleep_for(2.0);
171   xbt_assert(not Test(activity), "activity finished too soon!?");
172   exec5->kill();
173   xbt_assert(Test(activity), "activity should be terminated now");
174 }
175
176 template <creator_type Create, tester_type Test, waiter_type Wait> void test_failure_host()
177 {
178   XBT_INFO("%s: Launch an activity for 5s, and shutdown host 2s", __func__);
179
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);
184     Wait(activity);
185     xbt_die("should not be here!");
186   });
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");
192 }
193
194 //==========
195
196 /* We need an extra actor here, so that it can sleep until the end of each test */
197 static void main_dispatcher()
198 {
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>);
207
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>);
216
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>);
225 }
226
227 int main(int argc, char* argv[])
228 {
229   simgrid::s4u::Engine e(&argc, argv);
230
231   const char* platf = argv[1];
232   if (argc <= 1) {
233     XBT_WARN("No platform file provided. Using './testing_platform.xml'");
234     platf = "./testing_platform.xml";
235   }
236   e.load_platform(platf);
237
238   all_hosts = e.get_all_hosts();
239   simgrid::s4u::Actor::create("main_dispatcher", all_hosts[0], main_dispatcher);
240
241   e.run();
242
243   XBT_INFO("Simulation done");
244
245   return 0;
246 }