Skip to content

Commit

Permalink
Add global ExecutorServiceMaker that can be replaced by user
Browse files Browse the repository at this point in the history
  • Loading branch information
tallitman committed May 21, 2021
1 parent 1045869 commit 27b3f8f
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 28 deletions.
13 changes: 12 additions & 1 deletion src/main/java/il/ac/bgu/cs/bp/bpjs/BPjs.java
Expand Up @@ -23,6 +23,7 @@
*/
package il.ac.bgu.cs.bp.bpjs;

import il.ac.bgu.cs.bp.bpjs.internal.ExecutorServiceMaker;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ImporterTopLevel;
import org.mozilla.javascript.Scriptable;
Expand All @@ -39,7 +40,9 @@ public class BPjs {
* Top-level scope for all BPjs code.
*/
private static ScriptableObject BPJS_SCOPE;


private static ExecutorServiceMaker executorServiceMaker = new ExecutorServiceMaker();

static {
makeBPjsScope();
}
Expand Down Expand Up @@ -98,4 +101,12 @@ private static void makeBPjsScope() {
Context.exit();
}
}

public static ExecutorServiceMaker getExecutorServiceMaker() {
return executorServiceMaker;
}

public static void setExecutorServiceMaker(ExecutorServiceMaker executorServiceMaker) {
BPjs.executorServiceMaker = executorServiceMaker;
}
}
Expand Up @@ -23,6 +23,7 @@
*/
package il.ac.bgu.cs.bp.bpjs.analysis;

import il.ac.bgu.cs.bp.bpjs.BPjs;
import il.ac.bgu.cs.bp.bpjs.analysis.violations.JsErrorViolation;
import il.ac.bgu.cs.bp.bpjs.analysis.violations.Violation;
import il.ac.bgu.cs.bp.bpjs.exceptions.BPjsRuntimeException;
Expand Down Expand Up @@ -169,7 +170,7 @@ public VerificationResult verify(BProgram aBp) throws Exception {
inspections.addAll( ExecutionTraceInspections.DEFAULT_SET );
}

ExecutorService execSvc = ExecutorServiceMaker.makeWithName("DfsBProgramRunner-" + INSTANCE_COUNTER.incrementAndGet());
ExecutorService execSvc = BPjs.getExecutorServiceMaker().makeWithName("DfsBProgramRunner-" + INSTANCE_COUNTER.incrementAndGet());
long start = System.currentTimeMillis();
listener.started(this);
Violation vio = dfsUsingStack(new DfsTraversalNode(currentBProgram, currentBProgram.setup().start(execSvc, currentBProgram.getStorageModificationStrategy()), null), execSvc);
Expand Down
Expand Up @@ -23,6 +23,7 @@
*/
package il.ac.bgu.cs.bp.bpjs.execution;

import il.ac.bgu.cs.bp.bpjs.BPjs;
import il.ac.bgu.cs.bp.bpjs.exceptions.BPjsRuntimeException;
import il.ac.bgu.cs.bp.bpjs.model.BProgram;
import il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot;
Expand Down Expand Up @@ -73,7 +74,7 @@ public BProgramRunner(BProgram aBProgram) {
public void run() {
try {
// setup bprogram and runtime parts.
execSvc = ExecutorServiceMaker.makeWithName("BProgramRunner-" + instanceNum );
execSvc = BPjs.getExecutorServiceMaker().makeWithName("BProgramRunner-" + instanceNum );
failedAssertion = null;
listeners.forEach(l -> l.starting(bprog));
BProgramSyncSnapshot cur = bprog.setup();
Expand Down
Expand Up @@ -34,9 +34,9 @@
*
* @author michael
*/
public abstract class ExecutorServiceMaker {
public class ExecutorServiceMaker {

public static ExecutorService makeWithName( String threadNameTemplate ) {
public ExecutorService makeWithName( String threadNameTemplate ) {
final ThreadFactory dtf = Executors.defaultThreadFactory();
final AtomicInteger threadCoutner = new AtomicInteger(0);
ThreadFactory tf = (Runnable r) -> {
Expand All @@ -46,9 +46,5 @@ public static ExecutorService makeWithName( String threadNameTemplate ) {
};
return Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(), tf);
}

private ExecutorServiceMaker(){
// prevent instantiation
}


}
@@ -1,5 +1,6 @@
package il.ac.bgu.cs.bp.bpjs.analysis;

import il.ac.bgu.cs.bp.bpjs.BPjs;
import il.ac.bgu.cs.bp.bpjs.internal.ExecutorServiceMaker;
import static org.junit.Assert.*;

Expand Down Expand Up @@ -93,7 +94,7 @@ public void basicsTest() throws Exception {

@Before
public void setup() {
exSvc = ExecutorServiceMaker.makeWithName("Test");
exSvc = BPjs.getExecutorServiceMaker().makeWithName("Test");
}

@After
Expand Down
13 changes: 7 additions & 6 deletions src/test/java/il/ac/bgu/cs/bp/bpjs/analysis/StateStoreTests.java
@@ -1,5 +1,6 @@
package il.ac.bgu.cs.bp.bpjs.analysis;

import il.ac.bgu.cs.bp.bpjs.BPjs;
import il.ac.bgu.cs.bp.bpjs.bprogramio.BProgramSyncSnapshotCloner;
import il.ac.bgu.cs.bp.bpjs.internal.ExecutorServiceMaker;
import il.ac.bgu.cs.bp.bpjs.model.*;
Expand All @@ -16,7 +17,7 @@ public class StateStoreTests {
@Test
public void ForgetfulStore() throws Exception {
BProgram program = new ResourceBProgram("SnapshotTests/ABCDTrace.js");
ExecutorService execSvc = ExecutorServiceMaker.makeWithName("StoreSvc");
ExecutorService execSvc = BPjs.getExecutorServiceMaker().makeWithName("StoreSvc");
BProgramSyncSnapshot bpss = program.setup().start(execSvc, program.getStorageModificationStrategy());
DfsBProgramVerifier sut = new DfsBProgramVerifier(program);
VisitedStateStore forgetful = new ForgetfulVisitedStateStore();
Expand Down Expand Up @@ -44,7 +45,7 @@ public void VisitedStateStoreHashBasic() throws Exception {

private void TestAAABTraceStore(VisitedStateStore storeToUse) throws Exception {
BProgram program = new ResourceBProgram("SnapshotTests/ABCDTrace.js");
ExecutorService execSvc = ExecutorServiceMaker.makeWithName("StoreSvc");
ExecutorService execSvc = BPjs.getExecutorServiceMaker().makeWithName("StoreSvc");
BProgramSyncSnapshot bpss = program.setup().start(execSvc, program.getStorageModificationStrategy());
DfsBProgramVerifier sut = new DfsBProgramVerifier(program);
DfsTraversalNode initial = DfsTraversalNode.getInitialNode(program, bpss);
Expand Down Expand Up @@ -83,7 +84,7 @@ private void TestDiffJSVar(VisitedStateStore storeToUse) throws Exception {
" bp.sync({request:bp.Event(\"D\")});\n" +
" }\n" +
"});");
ExecutorService execSvc = ExecutorServiceMaker.makeWithName("StoreSvcDiiffJSVar");
ExecutorService execSvc = BPjs.getExecutorServiceMaker().makeWithName("StoreSvcDiiffJSVar");
BProgramSyncSnapshot bpss = program.setup().start(execSvc, program.getStorageModificationStrategy());

DfsBProgramVerifier sut = new DfsBProgramVerifier(program);
Expand Down Expand Up @@ -135,7 +136,7 @@ private void TestEqualJSVar(VisitedStateStore storeToUse) throws Exception {
" bp.sync({request:bp.Event(\"D\")});\n" +
" }\n" +
"});");
ExecutorService execSvc = ExecutorServiceMaker.makeWithName("StoreSvcEqualJSVar");
ExecutorService execSvc = BPjs.getExecutorServiceMaker().makeWithName("StoreSvcEqualJSVar");
BProgramSyncSnapshot bpss = program.setup().start(execSvc, program.getStorageModificationStrategy());
DfsBProgramVerifier sut = new DfsBProgramVerifier(program);
List<DfsTraversalNode> snapshots = new ArrayList<>();
Expand Down Expand Up @@ -189,7 +190,7 @@ these two objects (by debugging) share program counter and frame index and shoul
*/
private void testEqualRuns(VisitedStateStore storeToUse) throws Exception {
BProgram bprog = new ResourceBProgram("SnapshotTests/ABCDTrace.js");
ExecutorService execSvc = ExecutorServiceMaker.makeWithName("StoreSvcEqualJSVar");
ExecutorService execSvc = BPjs.getExecutorServiceMaker().makeWithName("StoreSvcEqualJSVar");
BProgramSyncSnapshot bpss1 = bprog.setup().start(execSvc, bprog.getStorageModificationStrategy());
BProgramSyncSnapshot bpss2 = BProgramSyncSnapshotCloner.clone(bpss1);

Expand Down Expand Up @@ -234,7 +235,7 @@ private void testStateStoreJavaVars(VisitedStateStore storeToUse) throws Excepti
" a = java.lang.Integer.reverse(a);\n" +
" }\n" +
"});");
ExecutorService execSvc = ExecutorServiceMaker.makeWithName("StoreSvcEqualJSVar");
ExecutorService execSvc = BPjs.getExecutorServiceMaker().makeWithName("StoreSvcEqualJSVar");
BProgramSyncSnapshot bpss = bprog.setup().start(execSvc, bprog.getStorageModificationStrategy());
DfsBProgramVerifier sut = new DfsBProgramVerifier(bprog);

Expand Down
@@ -1,5 +1,6 @@
package il.ac.bgu.cs.bp.bpjs.analysis.bprogramio;

import il.ac.bgu.cs.bp.bpjs.BPjs;
import il.ac.bgu.cs.bp.bpjs.bprogramio.BProgramSyncSnapshotIO;
import il.ac.bgu.cs.bp.bpjs.model.BProgram;
import il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner;
Expand Down Expand Up @@ -64,7 +65,7 @@ public void testProgramIsOk() throws InterruptedException {
public void testSerialization() throws Exception {
BProgram bprog = new ResourceBProgram("SnapshotTests/BProgramSyncSnapshotClonerTest.js");
BProgramSyncSnapshot cur = bprog.setup();
ExecutorService exSvc = ExecutorServiceMaker.makeWithName("test");
ExecutorService exSvc = BPjs.getExecutorServiceMaker().makeWithName("test");
cur = cur.start(exSvc, PASSTHROUGH);
cur.triggerEvent(
cur.getStatements().stream().flatMap(s->s.getRequest().stream()).findFirst().get(),
Expand All @@ -80,7 +81,7 @@ public void testSerialization() throws Exception {
public void testSerializatioWithExternals() throws Exception {
BProgram bprog = new ResourceBProgram("SnapshotTests/BProgramSyncSnapshotClonerTest.js");
BProgramSyncSnapshot cur = bprog.setup();
ExecutorService exSvc = ExecutorServiceMaker.makeWithName("test");
ExecutorService exSvc = BPjs.getExecutorServiceMaker().makeWithName("test");
bprog.enqueueExternalEvent(new BEvent("External1"));
bprog.enqueueExternalEvent(new BEvent("External2"));
cur = cur.start(exSvc, PASSTHROUGH);
Expand Down
Expand Up @@ -73,7 +73,7 @@ public static void main(String[] args) throws Exception {

// Run the top-level code (b-threads are registered but not yet run)
BProgramSyncSnapshot cur = bprog.setup();
ExecutorService execSvc = ExecutorServiceMaker.makeWithName("TEST");
ExecutorService execSvc = BPjs.getExecutorServiceMaker().makeWithName("TEST");

// Run to first bp.sync
cur = cur.start(execSvc, StorageModificationStrategy.PASSTHROUGH);
Expand Down
Expand Up @@ -27,6 +27,7 @@
import static java.util.Collections.emptySet;
import static org.junit.Assert.*;

import il.ac.bgu.cs.bp.bpjs.BPjs;
import il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListener;
import il.ac.bgu.cs.bp.bpjs.internal.ExecutorServiceMaker;
import static il.ac.bgu.cs.bp.bpjs.model.StorageModificationStrategy.PASSTHROUGH;
Expand Down Expand Up @@ -75,7 +76,7 @@ public void testDoubleTriggerEvent() throws InterruptedException {
" bp.ASSERT(false,\"Failed Assert\");\n" +
"});");
BProgramSyncSnapshot setup = bprog.setup();
ExecutorService execSvcA = ExecutorServiceMaker.makeWithName("BProgramSnapshotTriggerTest");
ExecutorService execSvcA = BPjs.getExecutorServiceMaker().makeWithName("BProgramSnapshotTriggerTest");
BProgramSyncSnapshot stepa = setup.start(execSvcA, PASSTHROUGH);
Set<BEvent> possibleEvents_a = bprog.getEventSelectionStrategy().selectableEvents(stepa);
EventSelectionResult event_a = bprog.getEventSelectionStrategy().select(stepa, possibleEvents_a).get();
Expand All @@ -92,7 +93,7 @@ public void testHotnessDetection() throws InterruptedException {
" bp.hot(true).sync({request:bp.Event(\"B\")});\n" +
"});");
BProgramSyncSnapshot setup = bprog.setup();
ExecutorService execSvcA = ExecutorServiceMaker.makeWithName("BProgramSnapshotTriggerTest");
ExecutorService execSvcA = BPjs.getExecutorServiceMaker().makeWithName("BProgramSnapshotTriggerTest");
BProgramSyncSnapshot bpss = setup.start(execSvcA, PASSTHROUGH);
assertFalse(bpss.isHot());
Set<BEvent> possibleEvents_a = bprog.getEventSelectionStrategy().selectableEvents(bpss);
Expand Down Expand Up @@ -126,8 +127,8 @@ public void testEqualsSingleStep() throws InterruptedException {


// Run first step
ExecutorService execSvcA = ExecutorServiceMaker.makeWithName("BProgramSnapshotEqualityTest");
ExecutorService execSvcB = ExecutorServiceMaker.makeWithName("BProgramSnapshotEqualityTest");
ExecutorService execSvcA = BPjs.getExecutorServiceMaker().makeWithName("BProgramSnapshotEqualityTest");
ExecutorService execSvcB = BPjs.getExecutorServiceMaker().makeWithName("BProgramSnapshotEqualityTest");
BProgramSyncSnapshot step1A = setupA.start(execSvcA, PASSTHROUGH);
BProgramSyncSnapshot step1B = setupB.start(execSvcB, PASSTHROUGH);
assertEquals(step1A, step1B);
Expand Down Expand Up @@ -183,8 +184,8 @@ public void testEqualsSingleStepAssert() throws InterruptedException {
BProgramSyncSnapshot setup2 = bprog2.setup();

// Run first step
ExecutorService execSvcA = ExecutorServiceMaker.makeWithName("BProgramSnapshotEqualityTest");
ExecutorService execSvcB = ExecutorServiceMaker.makeWithName("BProgramSnapshotEqualityTest");
ExecutorService execSvcA = BPjs.getExecutorServiceMaker().makeWithName("BProgramSnapshotEqualityTest");
ExecutorService execSvcB = BPjs.getExecutorServiceMaker().makeWithName("BProgramSnapshotEqualityTest");
BProgramSyncSnapshot postStart1 = setup1.start(execSvcA, PASSTHROUGH);
BProgramSyncSnapshot postStart2 = setup2.start(execSvcB, PASSTHROUGH);
assertNotEquals("The source code of the two bthreads is different, thus they should not eb equal.", postStart1, postStart2);
Expand Down
Expand Up @@ -23,6 +23,7 @@
*/
package il.ac.bgu.cs.bp.bpjs.model;

import il.ac.bgu.cs.bp.bpjs.BPjs;
import il.ac.bgu.cs.bp.bpjs.bprogramio.BProgramSyncSnapshotCloner;
import il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListener;
import il.ac.bgu.cs.bp.bpjs.internal.ExecutorServiceMaker;
Expand Down Expand Up @@ -53,7 +54,7 @@ public void testJSVarState() throws InterruptedException {
BProgram bprog = new ResourceBProgram("SnapshotTests/ABCDTrace.js");
BProgramSyncSnapshot setup = bprog.setup();

ExecutorService execSvc = ExecutorServiceMaker.makeWithName("BProgramSnapshotEqualityTest");
ExecutorService execSvc = BPjs.getExecutorServiceMaker().makeWithName("BProgramSnapshotEqualityTest");
List<BProgramSyncSnapshot> snapshots = new ArrayList<>();
BProgramSyncSnapshot step = setup.start(execSvc, PASSTHROUGH);
snapshots.add(BProgramSyncSnapshotCloner.clone(step));
Expand Down Expand Up @@ -87,7 +88,7 @@ public void testJavaVarState() throws InterruptedException {
" }\n" +
"});");
BProgramSyncSnapshot postSetup = bprog.setup();
ExecutorService execSvcA = ExecutorServiceMaker.makeWithName("BProgramSnapshotTriggerTest");
ExecutorService execSvcA = BPjs.getExecutorServiceMaker().makeWithName("BProgramSnapshotTriggerTest");
BProgramSyncSnapshot postSync1 = postSetup.start(execSvcA, PASSTHROUGH);
Set<BEvent> possibleEvents = bprog.getEventSelectionStrategy().selectableEvents(postSync1);
EventSelectionResult esr = bprog.getEventSelectionStrategy().select(postSync1, possibleEvents).get();
Expand Down

0 comments on commit 27b3f8f

Please sign in to comment.