1717
1818#include < any>
1919#include < catch2/catch_test_macros.hpp>
20- #include < initializer_list >
20+ #include < iostream >
2121#include < memory>
2222
2323#include " scl/coro.h"
2424#include " scl/protocol.h"
25+ #include " scl/simulation/cancellation.h"
2526#include " scl/simulation/event.h"
2627#include " scl/simulation/params.h"
2728#include " scl/simulation/simulator.h"
@@ -103,7 +104,7 @@ TEST_CASE("Simulator test", "[sim]") {
103104
104105namespace {
105106
106- struct OutputProtocol final : public Protocol {
107+ struct SimpleProtocol final : public Protocol {
107108 Task<Result> run (Env& /* ignored */ ) const override {
108109 co_return Result::done (123 );
109110 }
@@ -125,10 +126,89 @@ TEST_CASE("Simulator handle output", "[sim]") {
125126 auto res = sim.run (
126127 []() {
127128 std::vector<std::unique_ptr<Protocol>> p;
128- p.emplace_back (std::make_unique<OutputProtocol >());
129+ p.emplace_back (std::make_unique<SimpleProtocol >());
129130 return p;
130131 },
131132 nd);
132133
133134 REQUIRE (called);
134135}
136+
137+ TEST_CASE (" Simulator hooks no trigger" , " [sim]" ) {
138+ auto nd = NetworkParams::create (1 );
139+
140+ std::size_t num_called = 0 ;
141+
142+ Simulator sim;
143+ sim.addHook ([&num_called](std::size_t /* ignored*/ , Event* /* ignored*/ ) {
144+ num_called++;
145+ });
146+
147+ auto res = sim.run (
148+ []() {
149+ std::vector<std::unique_ptr<Protocol>> p;
150+ p.emplace_back (std::make_unique<SimpleProtocol>());
151+ return p;
152+ },
153+ nd);
154+
155+ REQUIRE (num_called == res[0 ].size ());
156+ }
157+
158+ TEST_CASE (" Simulator hooks with trigger" , " [sim]" ) {
159+ auto nd = NetworkParams::create (1 );
160+
161+ bool start_hook_called = false ;
162+ bool send_hook_called = false ;
163+
164+ Simulator sim;
165+
166+ // should be called
167+ sim.addHook (EventType::START,
168+ [&start_hook_called](std::size_t pid, Event* evt) {
169+ start_hook_called =
170+ (pid == 0 ) && (evt->type () == EventType::START);
171+ });
172+
173+ // should not be called
174+ sim.addHook (
175+ EventType::CHANNEL_SEND,
176+ [&send_hook_called](std::size_t /* ignored*/ , Event* /* ignored*/ ) {
177+ send_hook_called = true ;
178+ });
179+
180+ auto res = sim.run (
181+ []() {
182+ std::vector<std::unique_ptr<Protocol>> p;
183+ p.emplace_back (std::make_unique<SimpleProtocol>());
184+ return p;
185+ },
186+ nd);
187+
188+ REQUIRE (start_hook_called);
189+ REQUIRE_FALSE (send_hook_called);
190+ }
191+
192+ TEST_CASE (" Simulator cancellation" , " [sim]" ) {
193+ auto nd = NetworkParams::create (2 );
194+
195+ Simulator sim;
196+
197+ sim.addHook (EventType::START,
198+ [](std::size_t /* ignored */ , Event* /* ignored */ ) {
199+ forceStopProtocol ();
200+ });
201+
202+ auto res = sim.run (
203+ []() {
204+ std::vector<std::unique_ptr<Protocol>> p;
205+ p.emplace_back (std::make_unique<SimpleProtocol>());
206+ p.emplace_back (std::make_unique<SimpleProtocol>());
207+ return p;
208+ },
209+ nd);
210+
211+ REQUIRE (res.numberOfParties () == 2 );
212+
213+ assertEvents (res[0 ], {EventType::START, EventType::CANCELLED});
214+ }
0 commit comments