Skip to content

Commit

Permalink
Warnings on unused sync hints (closing #151)
Browse files Browse the repository at this point in the history
  • Loading branch information
michbarsinai committed Feb 9, 2021
1 parent 865d834 commit cdbdd8f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ a link to this page somewhere in the documentation/system about section.

* :sparkles: :sparkles: :tada: :sparkles: :tada: :sparkles: :rainbow: :sparkles: :sparkles: :sparkles: BPjs now uses Rhino's native continuation equality and hash code ([#116](https://github.com/bThink-BGU/BPjs/issues/116). Also, :tada: :rainbow: :sparkles:.
* :arrow_up: Scope and context creations across BPjs have been consolidated to utility methods in a new class called `BPjs`.
* :arrow_up: Event selection strategies that ignore the synchronization statement data field, issue a warning when b-threads put data there ([#151](https://github.com/bThink-BGU/BPjs/issues/151)).
* :bug: Fixed an issue that caused `toString` for JavaScript objects to crash at certain cases ([#145](https://github.com/bThink-BGU/BPjs/issues/145)).


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class SyncStatement implements java.io.Serializable {
private final boolean hot;

/**
* Optional data a BThread can pass to the event selector. This may serve as
* Optional data a b-thread can pass to the event selector. This may serve as
* a "danger level", or any other hint for a specific event selection policy.
*/
private final Object data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,21 @@ protected Set<BEvent> getRequestedAndNotBlocked(SyncStatement stmt, EventSet blo
}
}

private boolean warningIssued = false;

/**
* Log a warning when b-threads send synchronization statement data. This
* is useful for strategies that ignore these data, as users might be sending
* them and not understand why they are ignored.
*
* @param bpss the state the b-program is at.
*/
protected void warnOnHints( BProgramSyncSnapshot bpss ){
if ( warningIssued ) return; // warn only once
if ( bpss.getStatements().stream().anyMatch( s->s.getData()!=null ) ) {
System.out.println("[WARNING] Sync statements contain data. Current strategy ("+getClass().getCanonicalName()+") ignores this field.");
warningIssued = true;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class OrderedEventSelectionStrategy extends AbstractEventSelectionStrateg

@Override
public Set<BEvent> selectableEvents(BProgramSyncSnapshot bpss) {
warnOnHints(bpss);
Set<SyncStatement> statements = bpss.getStatements();
List<BEvent> externalEvents = bpss.getExternalEvents();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public PrioritizedBThreadsEventSelectionStrategy() {

@Override
public Set<BEvent> selectableEvents(BProgramSyncSnapshot bpss) {
warnOnHints(bpss);
Set<SyncStatement> statements = bpss.getStatements();
List<BEvent> externalEvents = bpss.getExternalEvents();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public SimpleEventSelectionStrategy() {}

@Override
public Set<BEvent> selectableEvents(BProgramSyncSnapshot bpss) {
warnOnHints(bpss);
Set<SyncStatement> statements = bpss.getStatements();
List<BEvent> externalEvents = bpss.getExternalEvents();
if ( statements.isEmpty() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot;
import il.ac.bgu.cs.bp.bpjs.model.SyncStatement;
import il.ac.bgu.cs.bp.bpjs.model.eventsets.ExplicitEventSet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -164,4 +168,55 @@ public void testSeed() {

assertEquals( selectedBySut, selectedBySameSeed );
}

@Test
public void testWarnOnHints() throws IOException {

PrintStream origStdout = System.out;
String output;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream testStream = new PrintStream(baos)) {
System.setOut(testStream);
BProgramSyncSnapshot bpss = TestUtils.makeBPSS(
new MockBThreadSyncSnapshot(SyncStatement.make(null).waitFor(eventOne).data("I will create a warning"))
); sut.selectableEvents(bpss);
sut.selectableEvents(bpss);
sut.selectableEvents(bpss);
testStream.flush();
baos.flush();
output = baos.toString(StandardCharsets.UTF_8);
}
assertTrue( "SimpleSelectionStrategy should warn about using hints", output.contains("WARNING") );
output = output.replaceFirst("WARNING", "");
assertFalse( "SimpleSelectionStrategy should warn about using hints only once", output.contains("WARNING") );

System.setOut(origStdout);
}

@Test
public void testNoWarnOnNoHints() throws IOException {

PrintStream origStdout = System.out;
String output;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream testStream = new PrintStream(baos) ) {
System.setOut(testStream);

BProgramSyncSnapshot bpss = TestUtils.makeBPSS(
new MockBThreadSyncSnapshot(SyncStatement.make(null).waitFor(eventOne))
);

sut.selectableEvents(bpss);
sut.selectableEvents(bpss);
sut.selectableEvents(bpss);

testStream.flush();
baos.flush();
output = baos.toString(StandardCharsets.UTF_8);
}
assertFalse( "SimpleSelectionStrategy should not warn when there are no hints", output.contains("WARNING") );

System.setOut(origStdout);
}

}

0 comments on commit cdbdd8f

Please sign in to comment.