Skip to content

Commit

Permalink
Mock Simulation DB and Initial State Machine Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AvocadoMoon committed Aug 22, 2024
1 parent 228ced8 commit a36537b
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package cbit.vcell.message.server.dispatcher;

import cbit.vcell.field.FieldDataIdentifierSpec;
import cbit.vcell.messaging.db.SimulationRequirements;
import cbit.vcell.server.*;
import cbit.vcell.solver.Simulation;
import cbit.vcell.solver.SimulationInfo;
import org.vcell.util.DataAccessException;
import org.vcell.util.ObjectNotFoundException;
import org.vcell.util.document.KeyValue;
import org.vcell.util.document.User;
import org.vcell.util.document.VCellServerID;

import java.sql.SQLException;
import java.util.*;

public class MockSimulationDB implements SimulationDatabase{

private final HashMap<String, ArrayList<SimulationJobStatus>> dbTable = new HashMap<>();

@Override
public SimulationJobStatus getLatestSimulationJobStatus(KeyValue simKey, int jobIndex) throws DataAccessException, SQLException {
ArrayList<SimulationJobStatus> simList = dbTable.get(simKey.toString());
if (simList == null){
return null;
}
SimulationJobStatus latestSim = simList.get(0);
for (SimulationJobStatus jobStatus : simList){
if (jobStatus.getJobIndex() == jobIndex && latestSim.getSubmitDate().after(jobStatus.getSubmitDate())){
latestSim = jobStatus;
}
}
return latestSim;
}

@Override
public void insertSimulationJobStatus(SimulationJobStatus simulationJobStatus) throws DataAccessException, SQLException {
String simKey = simulationJobStatus.getVCSimulationIdentifier().getSimulationKey().toString();
if (dbTable.containsKey(simKey)){
dbTable.get(simKey).add(simulationJobStatus);
} else {
dbTable.put(simKey, new ArrayList<>(){{add(simulationJobStatus);}});
}
}

@Override
public SimulationJobStatus[] getActiveJobs(VCellServerID vcellServerID) throws DataAccessException, SQLException {
throw new SQLException();
}

@Override
public SimulationJobStatus[] queryJobs(SimpleJobStatusQuerySpec simStatusQuerySpec) throws ObjectNotFoundException, DataAccessException {
return new SimulationJobStatus[0];
}

@Override
public Map<KeyValue, SimulationRequirements> getSimulationRequirements(Collection<KeyValue> simKeys) throws SQLException {
return Map.of();
}

@Override
public void updateSimulationJobStatus(SimulationJobStatus newSimulationJobStatus) throws DataAccessException, UpdateSynchronizationException, SQLException {
String simKey = newSimulationJobStatus.getVCSimulationIdentifier().getSimulationKey().toString();
ArrayList<SimulationJobStatus> jobStatuses = dbTable.get(simKey);
for (int i = 0; i < jobStatuses.size(); i++){
SimulationJobStatus jobStatus = jobStatuses.get(i);
boolean sameJob = jobStatus.getJobIndex() == newSimulationJobStatus.getJobIndex();
if (sameJob){
jobStatuses.set(i,newSimulationJobStatus);
break;
}
}
}

@Override
public void updateSimulationJobStatus(SimulationJobStatus newSimulationJobStatus, StateInfo runningStateInfo) throws DataAccessException, UpdateSynchronizationException, SQLException {

}

@Override
public KeyValue[] getSimulationKeysFromBiomodel(KeyValue biomodelKey) throws SQLException, DataAccessException {
throw new SQLException();
}

@Override
public Simulation getSimulation(User user, KeyValue simKey) throws DataAccessException {
throw new DataAccessException();
}

@Override
public FieldDataIdentifierSpec[] getFieldDataIdentifierSpecs(Simulation sim) throws DataAccessException {
throw new DataAccessException();
}

@Override
public Set<KeyValue> getUnreferencedSimulations() throws SQLException {
return Set.of();
}

@Override
public User.SpecialUser getUser(String username) throws DataAccessException, SQLException {
return null;
}

@Override
public TreeMap<User.SPECIAL_CLAIM, TreeMap<User, String>> getSpecialUsers() throws DataAccessException, SQLException {
return null;
}

@Override
public SimulationInfo getSimulationInfo(User user, KeyValue simKey) throws ObjectNotFoundException, DataAccessException {
return null;
}

@Override
public SimulationStatus[] getSimulationStatus(KeyValue[] simKeys) throws ObjectNotFoundException, DataAccessException {
return new SimulationStatus[0];
}

@Override
public SimulationStatus getSimulationStatus(KeyValue simulationKey) throws ObjectNotFoundException, DataAccessException {
return null;
}

@Override
public SimpleJobStatus[] getSimpleJobStatus(User user, SimpleJobStatusQuerySpec simStatusQuerySpec) throws ObjectNotFoundException, DataAccessException {
return new SimpleJobStatus[0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package cbit.vcell.message.server.dispatcher;

import cbit.rmi.event.WorkerEvent;
import cbit.vcell.server.SimulationJobStatus;
import cbit.vcell.solver.VCSimulationIdentifier;
import cbit.vcell.solver.server.SimulationMessage;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.vcell.util.DataAccessException;
import org.vcell.util.document.KeyValue;
import org.vcell.util.document.User;
import org.vcell.util.document.VCellServerID;

import java.sql.SQLException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;

@Tag("Fast")
public class SimulationStateMachineTest {

private static final VCellServerID testVCellServerID = VCellServerID.getServerID("test");
private static final User testUser = new User("Alice", new KeyValue("0"));


private MockSimulationDB simulationDB;

@BeforeEach
public void setUp(){
simulationDB = new MockSimulationDB();
}

private record ChangedStateValues(
VCSimulationIdentifier simID,
SimulationJobStatus.SchedulerStatus schedulerStatus,
int taskID,
String changesResult
){ }

private interface WorkerEventFactory {
WorkerEvent createChangedWorker(ChangedStateValues c);
}

@Test
public void workerEventRejectionsTest() throws SQLException, DataAccessException {
KeyValue simKey = new KeyValue("1");
VCSimulationIdentifier simID = new VCSimulationIdentifier(simKey, testUser);
int jobIndex = 0;
int taskID = 16;
SimulationStateMachineCopy stateMachine = new SimulationStateMachineCopy(simKey, jobIndex);
SimulationMessage acceptedSimulationMessage = SimulationMessage.workerAccepted("accepted");


WorkerEventFactory workerEventFactory = (w) -> {return new WorkerEvent(WorkerEvent.JOB_ACCEPTED, simKey, w.simID,
jobIndex, "",
w.taskID, null, null,
acceptedSimulationMessage);};


ArrayList<ChangedStateValues> changedValues = new ArrayList<>(){{
add(new ChangedStateValues(simID, SimulationJobStatus.SchedulerStatus.RUNNING, taskID, "No old status.")); // no old status failure
add(new ChangedStateValues(simID, SimulationJobStatus.SchedulerStatus.COMPLETED, taskID, "Work is already done.")); // work is done failure
add(new ChangedStateValues(simID, SimulationJobStatus.SchedulerStatus.RUNNING, 0, "Task ID is lower")); // old status has higher number taskID failure
}};

for (int i = 0; i < changedValues.size(); i++){
ChangedStateValues workerEventChangedValues = changedValues.get(i);
if (i == 1){
simulationDB.insertSimulationJobStatus(new SimulationJobStatus(testVCellServerID, simID, jobIndex, Date.from(Instant.now()), workerEventChangedValues.schedulerStatus, taskID,
acceptedSimulationMessage, null, null));
} else if (i > 1) {
SimulationJobStatus failingStatus = new SimulationJobStatus(testVCellServerID, simID, 0, Date.from(Instant.now()), workerEventChangedValues.schedulerStatus, taskID,
acceptedSimulationMessage, null, null);
simulationDB.updateSimulationJobStatus(failingStatus);
}
WorkerEvent workerEvent = workerEventFactory.createChangedWorker(workerEventChangedValues);
Assertions.assertFalse(stateMachine.isWorkerEventOkay(workerEvent, simulationDB), workerEventChangedValues.changesResult);
}

ChangedStateValues passingWorkerValues= new ChangedStateValues(simID, null, taskID, "");
WorkerEvent passingWorkerEvent = workerEventFactory.createChangedWorker(passingWorkerValues);

for (SimulationJobStatus.SchedulerStatus passingStatus: SimulationJobStatus.SchedulerStatus.values()){
if (!passingStatus.isDone()){
SimulationJobStatus simulationJobStatus = new SimulationJobStatus(testVCellServerID, simID, jobIndex, Date.from(Instant.now()), passingStatus, taskID,
acceptedSimulationMessage, null, null);
simulationDB.updateSimulationJobStatus(simulationJobStatus);
Assertions.assertTrue(stateMachine.isWorkerEventOkay(passingWorkerEvent, simulationDB));
}
}

}

@Test
public void stateShouldTransitionToFailure(){
KeyValue simKey = new KeyValue("1");
VCSimulationIdentifier simID = new VCSimulationIdentifier(simKey, testUser);
int jobIndex = 0;
int taskID = 16;
SimulationStateMachineCopy stateMachine = new SimulationStateMachineCopy(simKey, jobIndex);
}

public void stateShouldTransitionToWaiting(){

}

public void stateShouldTransitionToDispatched(){

}

public void stateShouldTransitionToRunning(){

}

public void stateShouldTransitionToCompleted(){

}


}

0 comments on commit a36537b

Please sign in to comment.