From f7386ed8148102d8b85a4ae5c2d5d73b96d00aa2 Mon Sep 17 00:00:00 2001 From: Christian Menard Date: Wed, 20 Sep 2023 16:41:54 +0200 Subject: [PATCH] update reactor-cpp, improve and add tests --- core/src/main/resources/lib/cpp/reactor-cpp | 2 +- test/Cpp/src/ShutdownAsync.lf | 28 +++++++++ test/Cpp/src/ShutdownSync.lf | 25 ++++++++ test/Cpp/src/Starve.lf | 2 +- test/Cpp/src/StarveZero.lf | 19 ++++++ test/Cpp/src/Timeout_Test.lf | 52 ---------------- test/Cpp/src/properties/Timeout.lf | 66 ++++++++++++++++----- test/Cpp/src/properties/TimeoutZero.lf | 28 +++------ 8 files changed, 132 insertions(+), 90 deletions(-) create mode 100644 test/Cpp/src/ShutdownAsync.lf create mode 100644 test/Cpp/src/ShutdownSync.lf create mode 100644 test/Cpp/src/StarveZero.lf delete mode 100644 test/Cpp/src/Timeout_Test.lf diff --git a/core/src/main/resources/lib/cpp/reactor-cpp b/core/src/main/resources/lib/cpp/reactor-cpp index 997421c0d3..fa685f1db9 160000 --- a/core/src/main/resources/lib/cpp/reactor-cpp +++ b/core/src/main/resources/lib/cpp/reactor-cpp @@ -1 +1 @@ -Subproject commit 997421c0d35ef609b7ae202b4cbf2db38b884d12 +Subproject commit fa685f1db99b1652e39a5cf3c6356a8db26b52bb diff --git a/test/Cpp/src/ShutdownAsync.lf b/test/Cpp/src/ShutdownAsync.lf new file mode 100644 index 0000000000..c1ec5ed8e5 --- /dev/null +++ b/test/Cpp/src/ShutdownAsync.lf @@ -0,0 +1,28 @@ +target Cpp + +main reactor { + logical action check_shutdown + timer request_shutdown(1 ms) + timer after_shutdown(2 ms) + + reaction(request_shutdown) -> check_shutdown {= + // async_shutdown can be called from external threads to shutdown + // at the next possible tag. If it is called from a reaction, the + // next possible tag should always be the next microstep. + environment()->async_shutdown(); + check_shutdown.schedule(); + =} + + reaction(shutdown, check_shutdown) {= + if (!(shutdown.is_present() && check_shutdown.is_present())) { + reactor::log::Error() << "shutdown was not triggered at the expcetd tag"; + exit(1); + } + reactor::log::Info() << "Success!"; + =} + + reaction(after_shutdown) {= + reactor::log::Error() << "triggered a reaction after shutdown"; + exit(2); + =} +} diff --git a/test/Cpp/src/ShutdownSync.lf b/test/Cpp/src/ShutdownSync.lf new file mode 100644 index 0000000000..a07e4f2a59 --- /dev/null +++ b/test/Cpp/src/ShutdownSync.lf @@ -0,0 +1,25 @@ +target Cpp + +main reactor { + logical action check_shutdown + timer request_shutdown(1 ms) + timer after_shutdown(2 ms) + + reaction(request_shutdown) -> check_shutdown {= + environment()->sync_shutdown(); + check_shutdown.schedule(); + =} + + reaction(shutdown, check_shutdown) {= + if (!(shutdown.is_present() && check_shutdown.is_present())) { + reactor::log::Error() << "shutdown was not triggered at the expcetd tag"; + exit(1); + } + reactor::log::Info() << "Success!"; + =} + + reaction(after_shutdown) {= + reactor::log::Error() << "triggered a reaction after shutdown"; + exit(2); + =} +} diff --git a/test/Cpp/src/Starve.lf b/test/Cpp/src/Starve.lf index ba9c45d998..73f4e4ecfc 100644 --- a/test/Cpp/src/Starve.lf +++ b/test/Cpp/src/Starve.lf @@ -19,6 +19,6 @@ main reactor { reaction(after_shutdown) {= reactor::log::Error() << "Executed a reaction after shutdown"; - exit(1); + exit(1); =} } diff --git a/test/Cpp/src/StarveZero.lf b/test/Cpp/src/StarveZero.lf new file mode 100644 index 0000000000..e7463f6424 --- /dev/null +++ b/test/Cpp/src/StarveZero.lf @@ -0,0 +1,19 @@ +target Cpp + +main reactor { + logical action after_shutdown: void + + reaction(shutdown) -> after_shutdown {= + reactor::log::Info() << "Shutdown triggered at " << get_tag(); + if(get_elapsed_logical_time() != 0s || get_microstep() != 1) { + reactor::log::Error() << "Shutdown invoked at wrong tag"; + exit(2); + } + after_shutdown.schedule(); + =} + + reaction(after_shutdown) {= + reactor::log::Error() << "Executed a reaction after shutdown"; + exit(1); + =} +} diff --git a/test/Cpp/src/Timeout_Test.lf b/test/Cpp/src/Timeout_Test.lf deleted file mode 100644 index 6c0224941c..0000000000 --- a/test/Cpp/src/Timeout_Test.lf +++ /dev/null @@ -1,52 +0,0 @@ -/** - * A test for the timeout functionality in Lingua Franca. - * - * @author Maiko Brants TU Dresden - * - * Modeled after the C version of this test. - */ -target Cpp { - timeout: 11 msec -} - -import Sender from "lib/LoopedActionSender.lf" - -reactor Consumer { - input in: int - state success: bool = false - - logical action after_shutdown: void - - reaction(in) {= - auto current{get_elapsed_logical_time()}; - if(current > 11ms ){ - reactor::log::Error() << "Received at: " << current.count() << ". Failed to enforce timeout."; - exit(1); - } else if(current == 11ms) { - success=true; - } - =} - - reaction(shutdown) -> after_shutdown {= - reactor::log::Info() << "Shutdown invoked at tag " << get_tag(); - if((get_elapsed_logical_time() == 11ms ) && get_microstep() == 0 && (success == true)){ - reactor::log::Info() << "SUCCESS: successfully enforced timeout."; - after_shutdown.schedule(); - } else { - reactor::log::Error() << "Failed to enforce timeout at the correct tag."; - exit(1); - } - =} - - reaction(after_shutdown) {= - reactor::log::Error() << "Executed a reaction after timeout."; - exit(2); - =} -} - -main reactor Timeout_Test { - consumer = new Consumer() - producer = new Sender(take_a_break_after=10, break_interval = 1 msec) - - producer.out -> consumer.in -} diff --git a/test/Cpp/src/properties/Timeout.lf b/test/Cpp/src/properties/Timeout.lf index a278d6d712..95d60963f5 100644 --- a/test/Cpp/src/properties/Timeout.lf +++ b/test/Cpp/src/properties/Timeout.lf @@ -1,31 +1,65 @@ +/** + * A test for the timeout functionality in Lingua Franca. + * + * @author Maiko Brants TU Dresden + * + * Modeled after the C version of this test. + */ target Cpp { - timeout: 1 sec + timeout: 11 msec } -main reactor { - timer t(1 sec, 1 sec) +import Sender from "../lib/LoopedActionSender.lf" + +reactor Consumer { + input in: int + state success: bool = false - state triggered: bool = false + logical action after_shutdown: void - reaction(t) {= - triggered = true; - if (get_elapsed_logical_time() != 1s) { - std::cout << "ERROR: triggered reaction at an unexpected tag"; + timer check_shutdown(11 ms) + + reaction(in) {= + auto current{get_elapsed_logical_time()}; + if(current > 11ms ){ + reactor::log::Error() << "Received at: " << current.count() << ". Failed to enforce timeout."; exit(1); + } else if(current == 11ms) { + success=true; } =} - reaction(shutdown) {= - if (get_elapsed_logical_time() != 1s || get_microstep() != 0) { - std::cout << "ERROR: shutdown invoked at an unexpected tag"; - exit(2); + reaction(shutdown) -> after_shutdown {= + reactor::log::Info() << "Shutdown invoked at tag " << get_tag(); + if((get_elapsed_logical_time() == 11ms ) && get_microstep() == 0 && (success == true)){ + reactor::log::Info() << "SUCCESS: successfully enforced timeout."; + after_shutdown.schedule(); + } else { + reactor::log::Error() << "Failed to enforce timeout at the correct tag."; + exit(1); } + =} - if (triggered) { - std::cout << "SUCCESS!\n"; - } else { - std::cout << "ERROR: reaction was not invoked!\n"; + reaction(check_shutdown, shutdown) {= + if (check_shutdown.is_present() && !shutdown.is_present()) { + reactor::log::Error() << "Shutdown was not triggered at the expected tag"; exit(2); } + if (!check_shutdown.is_present() && shutdown.is_present()) { + reactor::log::Error() << "Shutdown was triggered at an unxpected tag: " << get_tag(); + exit(2); + } + =} + + reaction(after_shutdown) {= + reactor::log::Error() << "Executed a reaction after timeout."; + exit(2); =} } + +main reactor { + consumer = new Consumer() + producer = new Sender(take_a_break_after=10, break_interval = 1 msec) + + producer.out -> consumer.in +} diff --git a/test/Cpp/src/properties/TimeoutZero.lf b/test/Cpp/src/properties/TimeoutZero.lf index 3aa45c7203..7f32565788 100644 --- a/test/Cpp/src/properties/TimeoutZero.lf +++ b/test/Cpp/src/properties/TimeoutZero.lf @@ -1,31 +1,19 @@ target Cpp { - timeout: 0 sec + timeout: 0 s } main reactor { - timer t(0, 1 sec) + timer should_never_trigger(1 s) - state triggered: bool = false - - reaction(t) {= - triggered = true; - if (get_elapsed_logical_time() != 0s) { - std::cout << "ERROR: triggered reaction at an unexpected tag"; + reaction(startup, shutdown) {= + if (!(startup.is_present() && shutdown.is_present())) { + reactor::log::Error() << "Shutdown was not triggered at startup"; exit(1); } =} - reaction(shutdown) {= - if (get_elapsed_logical_time() != 0s || get_microstep() != 0) { - std::cout << "ERROR: shutdown invoked at an unexpected tag"; - exit(2); - } - - if (triggered) { - std::cout << "SUCCESS!\n"; - } else { - std::cout << "ERROR: reaction was not invoked!\n"; - exit(2); - } + reaction(should_never_trigger) {= + reactor::log::Error() << "Executed a reaction after timeout."; + exit(2); =} }