-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,44 @@ | ||
#include <catch2/catch_test_macros.hpp> | ||
#include <condition_variable> | ||
#include <mutex> | ||
|
||
#include "symmetri/symmetri.h" | ||
|
||
using namespace symmetri; | ||
|
||
std::atomic<bool> i_ran(false); | ||
void tExtInput() { i_ran.store(true); } | ||
std::atomic<bool> can_continue(false), done(false); | ||
void tAllowExitInput() { | ||
can_continue.store(true); | ||
while (!done.load()) { | ||
} // wait till trigger is done, otherwise we would deadlock | ||
} | ||
|
||
TEST_CASE("Test external input.") { | ||
Net net = { | ||
{"t0", {{}, {"Pa"}}}, {"t1", {{"Pa"}, {"Pb"}}}, {"t2", {{"Pc"}, {"Pb"}}}}; | ||
// we can omit t0, it will be auto-filled as {"t0", DirectMutation{}} | ||
Store store = {{"t1", &tExtInput}, | ||
{"t2", []() { | ||
std::this_thread::sleep_for(std::chrono::milliseconds(15)); | ||
}}}; | ||
Marking m0 = {{"Pa", 0}, {"Pb", 0}, {"Pc", 1}}; | ||
Marking final_m = {{"Pb", 2}}; | ||
auto stp = std::make_shared<TaskSystem>(3); | ||
|
||
PetriNet app(net, m0, final_m, store, {}, "test_net_ext_input", stp); | ||
|
||
// enqueue a trigger; | ||
stp->push([trigger = app.registerTransitionCallback("t0")]() { | ||
// sleep a bit so it gets triggered _after_ the net started. | ||
std::this_thread::sleep_for(std::chrono::milliseconds(10)); | ||
trigger(); | ||
}); | ||
|
||
// run the net | ||
auto res = fire(app); | ||
|
||
REQUIRE(res == state::Completed); | ||
REQUIRE(i_ran); | ||
{ | ||
Net net = {{"t0", {{}, {"Pb"}}}, | ||
{"t1", {{"Pa"}, {"Pb"}}}, | ||
{"t2", {{"Pb", "Pb"}, {"Pc"}}}}; | ||
// we can omit t0, it will be auto-filled as {"t0", DirectMutation{}} | ||
Store store = {{"t1", &tAllowExitInput}, {"t2", DirectMutation{}}}; | ||
Marking m0 = {{"Pa", 2}, {"Pb", 0}, {"Pc", 0}}; | ||
Marking final_m = {{"Pc", 1}}; | ||
auto stp = std::make_shared<TaskSystem>(3); | ||
|
||
PetriNet app(net, m0, final_m, store, {}, "test_net_ext_input", stp); | ||
|
||
// enqueue a trigger; | ||
stp->push([trigger = app.registerTransitionCallback("t0")]() { | ||
// sleep a bit so it gets triggered _after_ the net started. Otherwise the | ||
// net would deadlock | ||
while (!can_continue.load()) { | ||
} | ||
trigger(); | ||
done.store(true); | ||
}); | ||
|
||
// run the net | ||
auto res = fire(app); | ||
CHECK(can_continue); | ||
CHECK(res == state::Completed); | ||
} | ||
} |