Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and add federated execution tests for TypeScript target #1062

Merged
merged 6 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions test/TypeScript/src/federated/DistributedCount.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Test a particularly simple form of a distributed deterministic system
* where a federation that receives timestamped messages has only those
* messages as triggers. Therefore, no additional coordination of the
* advancement of time (HLA or Ptides) is needed.
* @author Edward A. Lee
* @author Hokeun Kim
*/
target TypeScript {
timeout: 5 sec
};

// TODO(hokeun): Replace the following reactor with import Count from "../lib/Count.lf";
// once TypeScript serialization is implemented.
reactor Count(offset:time(0), period:time(1 sec)) {
output out:string;
timer t(offset, period);
state count:number(1);
reaction(t) -> out {=
out = String(count++);
=}
}

reactor Print {
// TODO(hokeun): Change type of inp to number once
// once TypeScript serialization is implemented.
input inp:string;
state c:number(1);
reaction(inp) {=
const elapsedTime = util.getElapsedLogicalTime();
console.log("At time " + elapsedTime + ", received " + inp);
if (inp !== String(c)) {
util.requestErrorStop("Expected to receive " + c + ".");
}
if (elapsedTime.isEqualTo(TimeValue.msec(200).add(TimeValue.sec(c - 1)))) {
util.requestErrorStop("Expected received time to be " + TimeValue.msec(200).add(TimeValue.sec(c - 1)) + ".");
}
c++;
=}
reaction(shutdown) {=
if (c != 6) {
util.reportError("Expected to receive 5 items.");
}
=}
}

federated reactor DistributedCount(offset:time(200 msec)) {
c = new Count();
p = new Print();
c.out -> p.inp after offset;
}
78 changes: 38 additions & 40 deletions test/TypeScript/src/federated/HelloDistributed.lf
Original file line number Diff line number Diff line change
@@ -1,52 +1,50 @@
/**
* A test-version of distributed HelloWorld in the example directory,
* example/TypeScript/src/DistributedHelloWorld/HelloWorld.lf
* This is to make sure that the federated execution for TypeScript target
* always works when there is a relevant change.
*
* @author Edward A. Lee
* @author Hokeun Kim
* Test a particularly simple form of a distributed deterministic system
* where a federation that receives timestamped messages has only those
* messages as triggers. Therefore, no additional coordination of the
* advancement of time (HLA or Ptides) is needed.
* @author Edward A. Lee
* @author Hokeun Kim
*/
// TODO(hokeun): Add a check for the number of received messages to be exactly 2.
target TypeScript {
timeout: 2 secs
};

/**
* Reactor that generates a sequence of messages, one per second.
* The message will be a string consisting of a root string followed
* by a count.
* @param root The root string.
* @output message The message.
*/
reactor MessageGenerator(root:string("")) {
// Output type char* instead of string is used for dynamically
// allocated character arrays (as opposed to static constant strings).
output message:string;
state count:number(1);
// Send first message after 1 sec so that the startup reactions
// do not factor into the transport time measurement on the first message.
timer t(1 sec, 1 sec);
reaction(t) -> message {=
message = root + " " + count++;
console.log(`At time (elapsed) logical tag ${util.getElapsedLogicalTime()}, source sends message: ${message}`);
reactor Source {
output out:string;
reaction(startup) -> out {=
console.log("Sending 'Hello World!' message from source federate.");
out = "Hello World!";
util.requestStop();
=}
}

/**
* Reactor that prints the current tag and an incoming string.
*
* @input message The message.
*/
reactor PrintMessage {
input message:string;
reaction(message) {=
console.log(`PrintMessage: At (elapsed) logical time ${util.getElapsedLogicalTime()}, receiver receives: ${message}`);
reactor Destination {
input inp:string;
state received:boolean(false);
reaction(startup) {=
console.log("Destination started.");
=}
reaction(inp) {=
console.log(`At logical time ${util.getElapsedLogicalTime()}, destination received: $inp`);
if (inp !== "Hello World!") {
util.requestErrorStop("ERROR: Expected to receive 'Hello World!'");
}
received = true;
=}
reaction(shutdown) {=
console.log("Shutdown invoked.");
if (!received) {
util.reportError("Destination did not receive the message.");
}
=}
}

federated reactor HelloDistributed {
source = new MessageGenerator(root = "Hello World");
print = new PrintMessage();
source.message -> print.message;
}
federated reactor HelloDistributed at localhost {
reaction(startup) {=
console.log("Printing something in top-level federated reactor.");
=}
s = new Source(); // Reactor s is in federate Source
d = new Destination(); // Reactor d is in federate Destination
s.out -> d.inp; // This version preserves the timestamp.
}