Skip to content

Commit

Permalink
fix race in test
Browse files Browse the repository at this point in the history
  • Loading branch information
thorstink committed Sep 17, 2023
1 parent 8a6a076 commit fa6e010
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
5 changes: 0 additions & 5 deletions symmetri/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ FetchContent_Declare(
# https://stackoverflow.com/questions/65527126/disable-install-for-fetchcontent
FetchContent_GetProperties(concurrentqueue)

if(NOT concurrentqueue_POPULATED)
FetchContent_Populate(concurrentqueue)
add_subdirectory(${concurrentqueue_SOURCE_DIR} ${concurrentqueue_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()

include_directories(${EIGEN3_INCLUDE_DIRS} ../include ..)

include(CTest)
Expand Down
61 changes: 34 additions & 27 deletions symmetri/tests/test_external_input.cc
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);
}
}

0 comments on commit fa6e010

Please sign in to comment.