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-"));
}
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());
}
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());
}
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();
}
}
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());
}
Aggregations