Search in sources :

Example 6 with StringBProgram

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);
}
Also used : Arrays(java.util.Arrays) PrintDfsVerifierListener(il.ac.bgu.cs.bp.bpjs.analysis.listeners.PrintDfsVerifierListener) TestUtils(il.ac.bgu.cs.bp.bpjs.TestUtils) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InMemoryEventLoggingListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeadlockViolation(il.ac.bgu.cs.bp.bpjs.analysis.violations.DeadlockViolation) ResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.ResourceBProgram) TestUtils.traceEventNamesString(il.ac.bgu.cs.bp.bpjs.TestUtils.traceEventNamesString) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) JsErrorViolation(il.ac.bgu.cs.bp.bpjs.analysis.violations.JsErrorViolation) BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) DetectedSafetyViolation(il.ac.bgu.cs.bp.bpjs.analysis.violations.DetectedSafetyViolation) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) Set(java.util.Set) Test(org.junit.Test) Collectors.joining(java.util.stream.Collectors.joining) Violation(il.ac.bgu.cs.bp.bpjs.analysis.violations.Violation) TestUtils.eventNamesString(il.ac.bgu.cs.bp.bpjs.TestUtils.eventNamesString) PrintBProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener) Optional(java.util.Optional) Assert(org.junit.Assert) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) PrintDfsVerifierListener(il.ac.bgu.cs.bp.bpjs.analysis.listeners.PrintDfsVerifierListener) TreeSet(java.util.TreeSet) ResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.ResourceBProgram) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) TestUtils.traceEventNamesString(il.ac.bgu.cs.bp.bpjs.TestUtils.traceEventNamesString) TestUtils.eventNamesString(il.ac.bgu.cs.bp.bpjs.TestUtils.eventNamesString) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with StringBProgram

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());
}
Also used : PrintDfsVerifierListener(il.ac.bgu.cs.bp.bpjs.analysis.listeners.PrintDfsVerifierListener) ResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.ResourceBProgram) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) Test(org.junit.Test)

Example 8 with StringBProgram

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());
}
Also used : DeadlockViolation(il.ac.bgu.cs.bp.bpjs.analysis.violations.DeadlockViolation) JsErrorViolation(il.ac.bgu.cs.bp.bpjs.analysis.violations.JsErrorViolation) DetectedSafetyViolation(il.ac.bgu.cs.bp.bpjs.analysis.violations.DetectedSafetyViolation) Violation(il.ac.bgu.cs.bp.bpjs.analysis.violations.Violation) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.ResourceBProgram) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) JsErrorViolation(il.ac.bgu.cs.bp.bpjs.analysis.violations.JsErrorViolation) Test(org.junit.Test)

Example 9 with StringBProgram

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));
}
Also used : MockBProgramSyncSnapshot(il.ac.bgu.cs.bp.bpjs.mocks.MockBProgramSyncSnapshot) HashMap(java.util.HashMap) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) MockBProgramSyncSnapshot(il.ac.bgu.cs.bp.bpjs.mocks.MockBProgramSyncSnapshot) BProgramSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot) BThreadSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot) MockBThreadSyncSnapshot(il.ac.bgu.cs.bp.bpjs.mocks.MockBThreadSyncSnapshot) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) HashSet(java.util.HashSet)

Example 10 with StringBProgram

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());
}
Also used : PrintDfsVerifierListener(il.ac.bgu.cs.bp.bpjs.analysis.listeners.PrintDfsVerifierListener) ResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.ResourceBProgram) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) DetectedSafetyViolation(il.ac.bgu.cs.bp.bpjs.analysis.violations.DetectedSafetyViolation) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) Test(org.junit.Test)

Aggregations

StringBProgram (il.ac.bgu.cs.bp.bpjs.model.StringBProgram)43 Test (org.junit.Test)40 BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)36 ResourceBProgram (il.ac.bgu.cs.bp.bpjs.model.ResourceBProgram)22 BThreadSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 BEvent (il.ac.bgu.cs.bp.bpjs.model.BEvent)10 BProgramSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot)10 PrintDfsVerifierListener (il.ac.bgu.cs.bp.bpjs.analysis.listeners.PrintDfsVerifierListener)9 BProgramRunnerListenerAdapter (il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListenerAdapter)9 BProgramRunner (il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner)8 NativeContinuation (org.mozilla.javascript.NativeContinuation)7 TestUtils.traceEventNamesString (il.ac.bgu.cs.bp.bpjs.TestUtils.traceEventNamesString)5 DeadlockViolation (il.ac.bgu.cs.bp.bpjs.analysis.violations.DeadlockViolation)5 DetectedSafetyViolation (il.ac.bgu.cs.bp.bpjs.analysis.violations.DetectedSafetyViolation)5 Arrays (java.util.Arrays)5 Collectors.joining (java.util.stream.Collectors.joining)5 TestUtils.eventNamesString (il.ac.bgu.cs.bp.bpjs.TestUtils.eventNamesString)4 JsErrorViolation (il.ac.bgu.cs.bp.bpjs.analysis.violations.JsErrorViolation)4 Violation (il.ac.bgu.cs.bp.bpjs.analysis.violations.Violation)4