Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use a smart pointer immediately. Uncomment fixed test.
[simgrid.git] / teshsuite / s4u / activity-lifecycle / testing_comm_direct.cpp
1 /* Copyright (c) 2010-2021. 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 "activity-lifecycle.hpp"
7
8 // Normally, we should be able use Catch2's REQUIRE_THROWS_AS(...), but it generates errors with Address Sanitizer.
9 // They're certainly false positive. Nevermind and use this simpler replacement.
10 #define REQUIRE_NETWORK_FAILURE(...)                                                                                   \
11   do {                                                                                                                 \
12     try {                                                                                                              \
13       __VA_ARGS__;                                                                                                     \
14       FAIL("Expected exception NetworkFailureException not caught");                                                   \
15     } catch (simgrid::NetworkFailureException const&) {                                                                \
16       XBT_VERB("got expected NetworkFailureException");                                                                \
17     }                                                                                                                  \
18   } while (0)
19
20 TEST_CASE("Activity lifecycle: direct communication activities")
21 {
22   XBT_INFO("#####[ launch next \"direct-comm\" test ]#####");
23
24   BEGIN_SECTION("dcomm")
25   {
26     XBT_INFO("Launch a dcomm(5s), and let it proceed");
27     bool global = false;
28
29     simgrid::s4u::ActorPtr dcomm5 = simgrid::s4u::Actor::create("dcomm5", all_hosts[1], [&global]() {
30       assert_exit(true, 5.);
31       all_hosts[1]->sendto(all_hosts[2], 5000);
32       global = true;
33     });
34
35     simgrid::s4u::this_actor::sleep_for(9);
36     INFO("Did the forked actor modify the global after sleeping, or was it killed before?");
37     REQUIRE(global);
38
39     END_SECTION;
40   }
41
42   BEGIN_SECTION("dcomm actor killed at start")
43   {
44     XBT_INFO("Launch a dcomm(5s), and kill it right after start");
45     simgrid::s4u::ActorPtr dcomm5 = simgrid::s4u::Actor::create("dcomm5_killed", all_hosts[1], []() {
46       assert_exit(false, 0);
47       all_hosts[1]->sendto(all_hosts[2], 5000);
48       FAIL("I should be dead now");
49     });
50
51     simgrid::s4u::this_actor::yield();
52     dcomm5->kill();
53
54     END_SECTION;
55   }
56
57   BEGIN_SECTION("dcomm actor killed in middle")
58   {
59     XBT_INFO("Launch a dcomm(5s), and kill it after 2 secs");
60     simgrid::s4u::ActorPtr dcomm5 = simgrid::s4u::Actor::create("dcomm5_killed", all_hosts[1], []() {
61       assert_exit(false, 2);
62       all_hosts[1]->sendto(all_hosts[2], 5000);
63       FAIL("I should be dead now");
64     });
65
66     simgrid::s4u::this_actor::sleep_for(2);
67     dcomm5->kill();
68
69     END_SECTION;
70   }
71
72   BEGIN_SECTION("dcomm host restarted at start")
73   {
74     XBT_INFO("Launch a dcomm(5s), and restart its host right after start");
75     simgrid::s4u::ActorPtr dcomm5 = simgrid::s4u::Actor::create("dcomm5_restarted", all_hosts[1], []() {
76       assert_exit(false, 0);
77       all_hosts[1]->sendto(all_hosts[2], 5000);
78       FAIL("I should be dead now");
79     });
80
81     simgrid::s4u::this_actor::yield();
82     dcomm5->get_host()->turn_off();
83     dcomm5->get_host()->turn_on();
84
85     END_SECTION;
86   }
87
88   BEGIN_SECTION("dcomm host restarted in middle")
89   {
90     XBT_INFO("Launch a dcomm(5s), and restart its host after 2 secs");
91     simgrid::s4u::ActorPtr dcomm5 = simgrid::s4u::Actor::create("dcomm5_restarted", all_hosts[1], []() {
92       assert_exit(false, 2);
93       all_hosts[1]->sendto(all_hosts[2], 5000);
94       FAIL("I should be dead now");
95     });
96
97     simgrid::s4u::this_actor::sleep_for(2);
98     dcomm5->get_host()->turn_off();
99     dcomm5->get_host()->turn_on();
100
101     END_SECTION;
102   }
103
104   BEGIN_SECTION("dcomm host restarted at end")
105   {
106     XBT_INFO("Launch a dcomm(5s), and restart its host right when it stops");
107     bool execution_done = false;
108
109     simgrid::s4u::Actor::create("dcomm5_restarted", all_hosts[1], [&execution_done]() {
110       assert_exit(true, 5);
111       all_hosts[1]->sendto(all_hosts[2], 5000);
112       execution_done = true;
113     });
114
115     simgrid::s4u::Actor::create("killer", all_hosts[0], []() {
116       simgrid::s4u::this_actor::sleep_for(5);
117       XBT_VERB("Killer!");
118       all_hosts[1]->turn_off();
119       all_hosts[1]->turn_on();
120     });
121
122     simgrid::s4u::this_actor::sleep_for(9);
123     INFO("Was restarted actor already dead in the scheduling round during which the host_off simcall was issued?");
124     REQUIRE(execution_done);
125
126     END_SECTION;
127   }
128
129   BEGIN_SECTION("dcomm link restarted at start")
130   {
131     XBT_INFO("Launch a dcomm(5s), and restart the used link right after start");
132     simgrid::s4u::ActorPtr dcomm5 = simgrid::s4u::Actor::create("dcomm5_restarted", all_hosts[1], []() {
133       assert_exit(true, 0);
134       REQUIRE_NETWORK_FAILURE(all_hosts[1]->sendto(all_hosts[2], 5000));
135     });
136
137     simgrid::s4u::this_actor::yield();
138     auto link = simgrid::s4u::Engine::get_instance()->link_by_name("link1");
139     link->turn_off();
140     link->turn_on();
141
142     END_SECTION;
143   }
144
145   BEGIN_SECTION("dcomm link restarted in middle")
146   {
147     XBT_INFO("Launch a dcomm(5s), and restart the used link after 2 secs");
148     simgrid::s4u::ActorPtr dcomm5 = simgrid::s4u::Actor::create("dcomm5_restarted", all_hosts[1], []() {
149       assert_exit(true, 2);
150       REQUIRE_NETWORK_FAILURE(all_hosts[1]->sendto(all_hosts[2], 5000));
151     });
152
153     simgrid::s4u::this_actor::sleep_for(2);
154     auto link = simgrid::s4u::Engine::get_instance()->link_by_name("link1");
155     link->turn_off();
156     link->turn_on();
157
158     END_SECTION;
159   }
160
161   BEGIN_SECTION("dcomm link restarted at end")
162   {
163     XBT_INFO("Launch a dcomm(5s), and restart the used link right when it stops");
164     bool execution_done = false;
165
166     simgrid::s4u::Actor::create("dcomm5_restarted", all_hosts[1], [&execution_done]() {
167       assert_exit(true, 5);
168       all_hosts[1]->sendto(all_hosts[2], 5000);
169       execution_done = true;
170     });
171
172     simgrid::s4u::Actor::create("killer", all_hosts[0], []() {
173       simgrid::s4u::this_actor::sleep_for(5);
174       XBT_VERB("Killer!");
175       auto link = simgrid::s4u::Engine::get_instance()->link_by_name("link1");
176       link->turn_off();
177       link->turn_on();
178     });
179
180     simgrid::s4u::this_actor::sleep_for(9);
181     INFO("Was restarted actor already dead in the scheduling round during which the host_off simcall was issued?");
182     REQUIRE(execution_done);
183
184     END_SECTION;
185   }
186
187   simgrid::s4u::this_actor::sleep_for(10);
188   assert_cleanup();
189 }