Skip to content

Commit

Permalink
update reactor-cpp, improve and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cmnrd committed Sep 20, 2023
1 parent 57f92aa commit f7386ed
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 90 deletions.
28 changes: 28 additions & 0 deletions test/Cpp/src/ShutdownAsync.lf
Original file line number Diff line number Diff line change
@@ -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);
=}
}
25 changes: 25 additions & 0 deletions test/Cpp/src/ShutdownSync.lf
Original file line number Diff line number Diff line change
@@ -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);
=}
}
2 changes: 1 addition & 1 deletion test/Cpp/src/Starve.lf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ main reactor {

reaction(after_shutdown) {=
reactor::log::Error() << "Executed a reaction after shutdown";
exit(1);
exit(1);
=}
}
19 changes: 19 additions & 0 deletions test/Cpp/src/StarveZero.lf
Original file line number Diff line number Diff line change
@@ -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);
=}
}
52 changes: 0 additions & 52 deletions test/Cpp/src/Timeout_Test.lf

This file was deleted.

66 changes: 50 additions & 16 deletions test/Cpp/src/properties/Timeout.lf
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 8 additions & 20 deletions test/Cpp/src/properties/TimeoutZero.lf
Original file line number Diff line number Diff line change
@@ -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);
=}
}

0 comments on commit f7386ed

Please sign in to comment.