use of il.ac.bgu.cs.bp.bpjs.model.StringBProgram in project BPjs by bThink-BGU.
the class DfsBProgramVerifierTest method testDoublePathDiscovery.
/**
* Running this transition system. State 3 should be arrived at twice.
* +-»B1»--+
* | |
* -»1--»A»--2 3--»C»----+
* | | | |
* | +-»B2»--+ |
* +------------«------------+
*
* event trace "AB1-" is the result of execution
*
* -»1-»A»-2-»B1»-3
*
* Whose stack is:
*
* +---+----+---+
* |1,A|2,B1|3,-|
* +---+----+---+
*
* With C selected, we get to
*
* +---+----+---+
* |1,A|2,B1|3,C| + cycleTo 0
* +---+----+---+
*
* @throws Exception
*/
@Test
public void testDoublePathDiscovery() throws Exception {
BProgram bprog = new //
StringBProgram("bp.registerBThread(\"round\", function(){\n" + " while( true ) {\n" + " bp.sync({request:bp.Event(\"A\")});\n" + " bp.sync({waitFor:[bp.Event(\"B1\"), bp.Event(\"B2\")]});\n" + " bp.sync({request:bp.Event(\"C\")});\n" + " }\n" + "});\n" + "\n" + "bp.registerBThread(\"round-s1\", function(){\n" + " while( true ) {\n" + " bp.sync({waitFor:bp.Event(\"A\")});\n" + " bp.sync({request:bp.Event(\"B1\"), waitFor:bp.Event(\"B2\")});\n" + " }\n" + "});\n" + "\n" + "bp.registerBThread(\"round-s2\", function(){\n" + " while( true ) {\n" + " bp.sync({waitFor:bp.Event(\"A\")});\n" + " bp.sync({request:bp.Event(\"B2\"), waitFor:bp.Event(\"B1\")});\n" + " }\n" + "});");
DfsBProgramVerifier sut = new DfsBProgramVerifier();
final Set<String> foundTraces = new HashSet<>();
sut.addInspection(ExecutionTraceInspection.named("DFS trace captures", et -> {
String eventTrace = et.getNodes().stream().map(n -> n.getEvent()).map(o -> o.map(BEvent::getName).orElse("-")).collect(joining(""));
System.out.println("eventTrace = " + eventTrace);
foundTraces.add(eventTrace);
return Optional.empty();
}));
sut.setProgressListener(new PrintDfsVerifierListener());
VerificationResult res = sut.verify(bprog);
assertEquals(3, res.getScannedStatesCount());
assertEquals(4, res.getScannedEdgesCount());
Set<String> expected1 = new TreeSet<>(Arrays.asList("-", "A-", "AB1-", "AB1C", "AB2-"));
Set<String> expected2 = new TreeSet<>(Arrays.asList("-", "A-", "AB2-", "AB2C", "AB1-"));
String eventTraces = foundTraces.stream().sorted().collect(joining(", "));
assertTrue("Traces don't match expected: " + eventTraces, foundTraces.equals(expected1) || foundTraces.equals(expected2));
System.out.println("Event traces: " + eventTraces);
}
use of il.ac.bgu.cs.bp.bpjs.model.StringBProgram in project BPjs by bThink-BGU.
the class DfsBProgramVerifierTest method testVariablesEquailtyInBT.
@Test(timeout = 2000)
public void testVariablesEquailtyInBT() throws Exception {
BProgram bprog = new //
StringBProgram(//
"bp.registerBThread('bt1', function(){" + // 1
" bp.sync({ waitFor:[ bp.Event(\"X\") ] });\n" + // 2
" bp.sync({ waitFor:[ bp.Event(\"X\") ] });\n" + // 3
" bp.sync({ waitFor:[ bp.Event(\"X\") ] });\n" + // 4
" bp.sync({ waitFor:[ bp.Event(\"X\") ] });\n" + "});\n" + //
"bp.registerBThread('bt2', function(){" + //
" while(true){\n" + //
" bp.sync({ request:[ bp.Event(\"X\") ] });\n" + "}});\n");
DfsBProgramVerifier sut = new DfsBProgramVerifier();
sut.setIterationCountGap(1);
sut.setProgressListener(new PrintDfsVerifierListener());
sut.setDebugMode(true);
VerificationResult res = sut.verify(bprog);
assertFalse(res.isViolationFound());
// 10 syncs while bt1 is alive, 1 sync per bt2's infinite loop alone.
assertEquals(5, res.getScannedStatesCount());
// in this case only one option per state
assertEquals(5, res.getScannedEdgesCount());
}
use of il.ac.bgu.cs.bp.bpjs.model.StringBProgram in project BPjs by bThink-BGU.
the class DfsBProgramVerifierTest method testJavaScriptError.
@Test
public void testJavaScriptError() throws Exception {
BProgram bprog = new StringBProgram("bp.registerBThread( function(){\n" + " bp.sync({request:bp.Event(\"A\")});\n" + " bp.sync({request:bp.Event(\"A\")});\n" + " bp.sync({request:bp.Event(\"A\")});\n" + " var myNullVar;\n" + " myNullVar.isNullAndSoThisInvocationShouldCrash();\n" + " bp.sync({request:bp.Event(\"A\")});\n" + "});");
final AtomicBoolean errorCalled = new AtomicBoolean();
final AtomicBoolean errorMadeSense = new AtomicBoolean();
DfsBProgramVerifier sut = new DfsBProgramVerifier();
sut.setProgressListener(new DfsBProgramVerifier.ProgressListener() {
@Override
public void started(DfsBProgramVerifier vfr) {
}
@Override
public void iterationCount(long count, long statesHit, DfsBProgramVerifier vfr) {
}
@Override
public void maxTraceLengthHit(ExecutionTrace aTrace, DfsBProgramVerifier vfr) {
}
@Override
public void done(DfsBProgramVerifier vfr) {
}
@Override
public boolean violationFound(Violation aViolation, DfsBProgramVerifier vfr) {
errorCalled.set(aViolation instanceof JsErrorViolation);
JsErrorViolation jsev = (JsErrorViolation) aViolation;
errorMadeSense.set(jsev.decsribe().contains("isNullAndSoThisInvocationShouldCrash"));
System.out.println(jsev.getThrownException().getMessage());
return true;
}
});
sut.verify(bprog);
assertTrue(errorCalled.get());
assertTrue(errorMadeSense.get());
}
use of il.ac.bgu.cs.bp.bpjs.model.StringBProgram in project BPjs by bThink-BGU.
the class TestUtils method makeBPSS.
public static BProgramSyncSnapshot makeBPSS(Collection<BThreadSyncSnapshot> snapshots) {
BProgram bprog = new StringBProgram("");
Set<BThreadSyncSnapshot> bts = new HashSet<>(snapshots);
return new MockBProgramSyncSnapshot(new BProgramSyncSnapshot(bprog, bts, new HashMap<>(), Collections.emptyList(), null));
}
use of il.ac.bgu.cs.bp.bpjs.model.StringBProgram in project BPjs by bThink-BGU.
the class DfsBProgramVerifierTest method testJavaVariablesInBT.
@Test(timeout = 6000)
public void testJavaVariablesInBT() throws Exception {
BProgram bprog = new //
StringBProgram(//
"bp.registerBThread('bt1', function(){" + " var sampleArray=[1,2,3,4,5];\n" + //
" while(true) \n" + //
" for(var i=0; i<10; i++){\n" + //
" bp.sync({ request:[ bp.Event(\"X\"+i) ] });\n" + //
" if (i == 5) {bp.sync({ request:[ bp.Event(\"X\"+i) ] });}\n" + //
" }\n" + "});\n" + "var xs = bp.EventSet( \"X\", function(e){\r\n" + " return e.getName().startsWith(\"X\");\r\n" + "} );\r\n" + "" + //
"bp.registerBThread('bt2', function(){" + //
" var lastE = {name:\"what\"};" + //
" while(true) {\n" + //
" var e = bp.sync({ waitFor: xs});\n" + //
" lastE = e;" + //
" if( e.name == lastE.name) { bp.ASSERT(false,\"Poof\");} " + "}});\n");
DfsBProgramVerifier sut = new DfsBProgramVerifier();
sut.setIterationCountGap(1);
sut.setProgressListener(new PrintDfsVerifierListener());
sut.setDebugMode(true);
VerificationResult res = sut.verify(bprog);
assertTrue(res.isViolationFound());
assertTrue(res.getViolation().get() instanceof DetectedSafetyViolation);
assertEquals(2, res.getScannedStatesCount());
assertEquals(1, res.getScannedEdgesCount());
}
Aggregations