Search in sources :

Example 11 with StringBProgram

use of il.ac.bgu.cs.bp.bpjs.model.StringBProgram in project BPjs by bThink-BGU.

the class BProgramRunnerTest method testExecutorName.

@Test
public void testExecutorName() {
    BProgram bprog = new StringBProgram("var exName = 'initial value'\n" + "bp.registerBThread( function(){\n" + "  exName = bp.getJavaThreadName();\n" + "});");
    new BProgramRunner(bprog).run();
    String exName = bprog.getFromGlobalScope("exName", String.class).get();
    assertTrue("Java executor name is wrong (got:'" + exName + "')", exName.startsWith("BProgramRunner-"));
}
Also used : BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) ResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.ResourceBProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) Test(org.junit.Test)

Example 12 with StringBProgram

use of il.ac.bgu.cs.bp.bpjs.model.StringBProgram in project BPjs by bThink-BGU.

the class BProgramRunnerTest method testHalt.

@Test(timeout = 3000)
public void testHalt() {
    BProgram bprog = new StringBProgram("bp.registerBThread(function(){\n" + "while (true){\n" + "bp.sync({request:bp.Event(\"evt\")});" + "}" + "});");
    BProgramRunner rnr = new BProgramRunner(bprog);
    AtomicBoolean haltedCalled = new AtomicBoolean();
    AtomicBoolean endedCalled = new AtomicBoolean();
    rnr.addListener(new BProgramRunnerListenerAdapter() {

        @Override
        public void halted(BProgram bp) {
            haltedCalled.set(true);
        }

        @Override
        public void ended(BProgram bp) {
            endedCalled.set(true);
        }
    });
    new Thread(() -> {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
        }
        rnr.halt();
    }).start();
    rnr.run();
    assertTrue(haltedCalled.get());
    assertFalse(endedCalled.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BProgramRunnerListenerAdapter(il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListenerAdapter) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) ResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.ResourceBProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) Test(org.junit.Test)

Example 13 with StringBProgram

use of il.ac.bgu.cs.bp.bpjs.model.StringBProgram in project BPjs by bThink-BGU.

the class EngineExceptionTest method testInvalidBSyncCall.

@Test
public void testInvalidBSyncCall() throws InterruptedException {
    BProgram sut = new StringBProgram("bad", "var i=0;\n var j=42;\n var k=5; bp.sync({request:bp.Event(\"A\")});");
    final AtomicBoolean errorCalled = new AtomicBoolean(false);
    final BProgramRunner bProgramRunner = new BProgramRunner(sut);
    bProgramRunner.addListener(new BProgramRunnerListenerAdapter() {

        @Override
        public void error(BProgram bp, Exception ex) {
            errorCalled.set(true);
        }
    });
    bProgramRunner.run();
    assertTrue("The runner listener should have had its 'error' method called ", errorCalled.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BProgramRunnerListenerAdapter(il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListenerAdapter) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) BPjsCodeEvaluationException(il.ac.bgu.cs.bp.bpjs.exceptions.BPjsCodeEvaluationException) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) Test(org.junit.Test)

Example 14 with StringBProgram

use of il.ac.bgu.cs.bp.bpjs.model.StringBProgram in project BPjs by bThink-BGU.

the class JsEventSetTest method testJsSetData.

@Test
public void testJsSetData() throws InterruptedException, URISyntaxException {
    try {
        BPjs.enterRhinoContext();
        BProgram bpr = new StringBProgram("Eventset", "var es=bp.EventSet('a',function(e){return e.name=='a';});\n");
        new BProgramRunner(bpr).run();
        NativeJavaObject sut = (NativeJavaObject) bpr.getGlobalScope().get("es", bpr.getGlobalScope());
        JsEventSet jsSut = (JsEventSet) Context.jsToJava(sut, JsEventSet.class);
        assertEquals("a", jsSut.getName());
        assertTrue(jsSut.toString().contains("a"));
        assertTrue(jsSut.toString().contains("JsEventSet"));
        assertTrue(jsSut.contains(BEvent.named("a")));
        assertFalse(jsSut.contains(BEvent.named("b")));
        assertNotEquals(jsSut, "NOT AN EVENT SET");
    } finally {
        Context.exit();
    }
}
Also used : BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) ResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.ResourceBProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) JsEventSet(il.ac.bgu.cs.bp.bpjs.model.eventsets.JsEventSet) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) Test(org.junit.Test)

Example 15 with StringBProgram

use of il.ac.bgu.cs.bp.bpjs.model.StringBProgram in project BPjs by bThink-BGU.

the class JsEventSetTest method testPredicateCrash.

@Test
public void testPredicateCrash() throws InterruptedException, URISyntaxException {
    BProgramRunner sut = new BProgramRunner(new StringBProgram("var es=bp.EventSet('bad',function(e){return a.b.c();});\n" + "bp.registerBThread('a',function(){\n" + "  bp.sync({request:bp.Event('X')});" + "});\n" + "bp.registerBThread('b',function(){\n" + "  bp.sync({waitFor:es});\n" + "});"));
    final AtomicBoolean errorCalled = new AtomicBoolean();
    final AtomicBoolean errorMadeSense = new AtomicBoolean();
    sut.addListener(new BProgramRunnerListenerAdapter() {

        @Override
        public void error(BProgram bp, Exception ex) {
            errorCalled.set(true);
            System.out.println("Bad predicate:" + ex.getMessage());
            errorMadeSense.set(ex.getMessage().toLowerCase().contains("evaluating js predicate"));
        }
    });
    sut.run();
    assertTrue(errorCalled.get());
    assertTrue(errorMadeSense.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BProgramRunnerListenerAdapter(il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListenerAdapter) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) ResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.ResourceBProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) URISyntaxException(java.net.URISyntaxException) 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