Skip to content

Commit

Permalink
Initial Simulation Dispatcher Test
Browse files Browse the repository at this point in the history
  • Loading branch information
AvocadoMoon committed Aug 30, 2024
1 parent 182e835 commit 41a4194
Show file tree
Hide file tree
Showing 5 changed files with 325 additions and 112 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package cbit.vcell.message.server.dispatcher;

import cbit.vcell.geometry.Geometry;
import cbit.vcell.mapping.MathSymbolMapping;
import cbit.vcell.math.*;
import cbit.vcell.mathmodel.MathModel;
import cbit.vcell.parser.ExpressionBindingException;
import cbit.vcell.resource.PropertyLoader;
import cbit.vcell.server.HtcJobID;
import cbit.vcell.server.SimulationExecutionStatus;
import cbit.vcell.server.SimulationJobStatus;
import cbit.vcell.solver.MeshSpecification;
import cbit.vcell.solver.Simulation;
import cbit.vcell.solver.VCSimulationIdentifier;
import cbit.vcell.solver.server.SimulationMessage;
import org.vcell.util.DataAccessException;
import org.vcell.util.ISize;
import org.vcell.util.document.*;

import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.time.Instant;
import java.util.Date;

public class DispatcherTestUtils {
private static String previousServerID = "";
private static String previousHtcMax = "";
private static String previousHtcMin = "";
private static String previousHtcPowerFloor = "";
private static String previousMongoBlob = "";
private static String previousJMSIntHostProperty = "";
private static String previousJMSIntPortProperty = "";
private static String previousSimJMSIntHostProperty = "";
private static String previousSimJMSIntPortProperty = "";
private static String previousHTCHost = "";
private static String previousHTCUser = "";
private static String previousHTCUserKeyFile = "";
private static String previousMaxJobsPerScan = "";

public static final VCellServerID testVCellServerID = VCellServerID.getServerID("test");
public static final MockVCMessageSession testMessageSession = new MockVCMessageSession();
public static final int jobIndex = 0;
public static final int taskID = 0;
public static final KeyValue simKey = new KeyValue("0");
public static User alice = new User("Alice", new KeyValue("0"));
public static User bob = new User("Bob", new KeyValue("1"));
public static final VCSimulationIdentifier simID = new VCSimulationIdentifier(simKey, alice);

public static void setRequiredProperties(){
previousServerID = PropertyLoader.getProperty(PropertyLoader.vcellServerIDProperty, "");
PropertyLoader.setProperty(PropertyLoader.vcellServerIDProperty, "");

previousHtcMax = PropertyLoader.getProperty(PropertyLoader.htcMaxMemoryMB, "");
PropertyLoader.setProperty(PropertyLoader.htcMaxMemoryMB, "4096");

previousHtcMin = PropertyLoader.getProperty(PropertyLoader.htcMinMemoryMB, "");
PropertyLoader.setProperty(PropertyLoader.htcMinMemoryMB, "1024");

previousHtcPowerFloor = PropertyLoader.getProperty(PropertyLoader.htcPowerUserMemoryFloorMB, "");
PropertyLoader.setProperty(PropertyLoader.htcPowerUserMemoryFloorMB, "51200");

previousMongoBlob = PropertyLoader.getProperty(PropertyLoader.jmsBlobMessageUseMongo, "");
PropertyLoader.setProperty(PropertyLoader.jmsBlobMessageUseMongo, "");

previousJMSIntHostProperty = PropertyLoader.getProperty(PropertyLoader.jmsIntHostInternal, "");
PropertyLoader.setProperty(PropertyLoader.jmsIntHostInternal, "host");

previousJMSIntPortProperty = PropertyLoader.getProperty(PropertyLoader.jmsIntPortInternal, "");
PropertyLoader.setProperty(PropertyLoader.jmsIntPortInternal, "80");

previousSimJMSIntHostProperty = PropertyLoader.getProperty(PropertyLoader.jmsSimHostInternal, "");
PropertyLoader.setProperty(PropertyLoader.jmsSimHostInternal, "host");

previousSimJMSIntPortProperty = PropertyLoader.getProperty(PropertyLoader.jmsSimPortInternal, "");
PropertyLoader.setProperty(PropertyLoader.jmsSimPortInternal, "80");

previousHTCHost = PropertyLoader.getProperty(PropertyLoader.htcHosts, "");
PropertyLoader.setProperty(PropertyLoader.htcHosts, "host");

previousHTCUser = PropertyLoader.getProperty(PropertyLoader.htcUser, "");
PropertyLoader.setProperty(PropertyLoader.htcUser, "user");

previousHTCUserKeyFile = PropertyLoader.getProperty(PropertyLoader.htcUserKeyFile, "");
PropertyLoader.setProperty(PropertyLoader.htcUserKeyFile, "keyFile");

previousMaxJobsPerScan = PropertyLoader.getProperty(PropertyLoader.maxJobsPerScan, "");
PropertyLoader.setProperty(PropertyLoader.maxJobsPerScan, "100");
}

public static void restoreRequiredProperties(){
PropertyLoader.setProperty(PropertyLoader.vcellServerIDProperty, previousServerID);
PropertyLoader.setProperty(PropertyLoader.htcMaxMemoryMB, previousHtcMax);
PropertyLoader.setProperty(PropertyLoader.htcMinMemoryMB, previousHtcMin);
PropertyLoader.setProperty(PropertyLoader.htcPowerUserMemoryFloorMB, previousHtcPowerFloor);
PropertyLoader.setProperty(PropertyLoader.jmsBlobMessageUseMongo, previousMongoBlob);
PropertyLoader.setProperty(PropertyLoader.jmsIntPortInternal, previousJMSIntPortProperty);
PropertyLoader.setProperty(PropertyLoader.jmsIntHostInternal, previousJMSIntHostProperty);
PropertyLoader.setProperty(PropertyLoader.jmsSimPortInternal, previousSimJMSIntPortProperty);
PropertyLoader.setProperty(PropertyLoader.jmsSimHostInternal, previousSimJMSIntHostProperty);
PropertyLoader.setProperty(PropertyLoader.htcHosts, previousHTCHost);
PropertyLoader.setProperty(PropertyLoader.htcUser, previousHTCUser);
PropertyLoader.setProperty(PropertyLoader.htcUserKeyFile, previousHTCUserKeyFile);
PropertyLoader.setProperty(PropertyLoader.maxJobsPerScan, previousMaxJobsPerScan);
}

public static Simulation createMockSimulation(int iSizeX, int iSizeY, int iSizeZ) throws PropertyVetoException, MathException, ExpressionBindingException {
VolVariable volVariable = new VolVariable("t", new Variable.Domain(new CompartmentSubDomain("t", 1)));
VolVariable volVariable2 = new VolVariable("b", new Variable.Domain(new CompartmentSubDomain("b", 2)));
MathSymbolMapping mathSymbolMapping = new MathSymbolMapping();
Geometry geometry = new Geometry("T", 3);
MathModel mathModel = new MathModel(new Version("Test", alice));
MathDescription mathDescription = new MathDescription("Test", mathSymbolMapping);
mathDescription.setGeometry(new Geometry("T", 3));
Simulation simulation = new Simulation(SimulationVersion.createTempSimulationVersion(),
mathDescription, mathModel);
MeshSpecification meshSpecification = new MeshSpecification(geometry);
meshSpecification.setSamplingSize(new ISize(iSizeX, iSizeY, iSizeZ));
simulation.setMeshSpecification(meshSpecification);
mathDescription.setAllVariables(new Variable[]{volVariable, volVariable2});
return simulation;
}

public static void insertOrUpdateStatus(KeyValue simKey, int jobIndex, int taskID, User user, SimulationJobStatus.SchedulerStatus status, SimulationDatabase simulationDB) throws SQLException, DataAccessException {
SimulationJobStatus jobStatus = simulationDB.getLatestSimulationJobStatus(simKey, jobIndex);
VCSimulationIdentifier simID = new VCSimulationIdentifier(simKey, user);
SimulationJobStatus simulationJobStatus = new SimulationJobStatus(testVCellServerID, simID, jobIndex, Date.from(Instant.now()), status, taskID,
SimulationMessage.workerAccepted("accepted"), null,
new SimulationExecutionStatus(Date.from(Instant.now()), "",
Date.from(Instant.now()), Date.from(Instant.now()), false, new HtcJobID("2", HtcJobID.BatchSystemType.SLURM)));
if (jobStatus == null){
simulationDB.insertSimulationJobStatus(simulationJobStatus);
} else {
simulationDB.updateSimulationJobStatus(simulationJobStatus);
}
}

public static void insertOrUpdateStatus(KeyValue simKey, int jobIndex, int taskID, User user, SimulationDatabase simulationDB) throws SQLException, DataAccessException {
insertOrUpdateStatus(simKey, jobIndex, taskID, user, SimulationJobStatus.SchedulerStatus.RUNNING, simulationDB);
}

/**
Defaults to a running status.
*/
public static void insertOrUpdateStatus(SimulationDatabase simulationDatabase) throws SQLException, DataAccessException {
insertOrUpdateStatus(simKey, jobIndex, taskID, alice, simulationDatabase);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cbit.vcell.message.server.dispatcher;

import cbit.vcell.message.*;

import java.util.ArrayList;
import java.util.List;

public class MockMessagingService implements VCMessagingService {

public ArrayList<VCMessagingConsumer> messagingConsumers = new ArrayList<>();

@Override
public VCMessageSession createProducerSession() {
return new MockVCMessageSession();
}

@Override
public void addMessageConsumer(VCMessagingConsumer vcMessagingConsumer) {
messagingConsumers.add(vcMessagingConsumer);
}

@Override
public void removeMessageConsumer(VCMessagingConsumer vcMessagingConsumer) {

}

@Override
public List<VCMessagingConsumer> getMessageConsumers() {
return List.of();
}

@Override
public void close() throws VCMessagingException {

}

@Override
public VCMessageSelector createSelector(String clientMessageFilter) {
return null;
}

@Override
public VCMessagingDelegate getDelegate() {
return null;
}

@Override
public void setConfiguration(VCMessagingDelegate delegate, String jmshost, int jmsport) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@
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 org.vcell.util.document.*;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.time.Instant;
import java.util.*;

public class MockSimulationDB implements SimulationDatabase{

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

public static User.SpecialUser specialAdmin = new User.SpecialUser("Tom", new KeyValue("999"), new User.SPECIAL_CLAIM[User.SPECIAL_CLAIM.admins.ordinal()]);

private final HashMap<String, User> users = new HashMap<>(){
{put(specialAdmin.getName(), specialAdmin); put(DispatcherTestUtils.alice.getName(), DispatcherTestUtils.alice);}
};

@Override
public SimulationJobStatus getLatestSimulationJobStatus(KeyValue simKey, int jobIndex) throws DataAccessException, SQLException {
Expand Down Expand Up @@ -47,7 +53,15 @@ public void insertSimulationJobStatus(SimulationJobStatus simulationJobStatus) t

@Override
public SimulationJobStatus[] getActiveJobs(VCellServerID vcellServerID) throws DataAccessException, SQLException {
throw new SQLException();
ArrayList<SimulationJobStatus> allActiveJobs = new ArrayList<>();
for (ArrayList<SimulationJobStatus> jobStatuses : dbTable.values()){
for (SimulationJobStatus jobStatus: jobStatuses){
if (jobStatus.getSchedulerStatus().isActive()){
allActiveJobs.add(jobStatus);
}
}
}
return allActiveJobs.toArray(new SimulationJobStatus[]{});
}

@Override
Expand Down Expand Up @@ -101,7 +115,12 @@ public Set<KeyValue> getUnreferencedSimulations() throws SQLException {

@Override
public User.SpecialUser getUser(String username) throws DataAccessException, SQLException {
return null;
User user = users.get(username);
if (user instanceof User.SpecialUser){
return (User.SpecialUser) user;
}
User.SpecialUser specialUser = new User.SpecialUser(user.getName(), user.getID(), new User.SPECIAL_CLAIM[]{});
return specialUser;
}

@Override
Expand All @@ -111,7 +130,7 @@ public TreeMap<User.SPECIAL_CLAIM, TreeMap<User, String>> getSpecialUsers() thro

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

@Override
Expand All @@ -121,11 +140,30 @@ public SimulationStatus[] getSimulationStatus(KeyValue[] simKeys) throws ObjectN

@Override
public SimulationStatus getSimulationStatus(KeyValue simulationKey) throws ObjectNotFoundException, DataAccessException {
// dbTable.get(simulationKey.toString()).get(0);
return null;
}

@Override
public SimpleJobStatus[] getSimpleJobStatus(User user, SimpleJobStatusQuerySpec simStatusQuerySpec) throws ObjectNotFoundException, DataAccessException {
return new SimpleJobStatus[0];
}


private SimulationInfo mockSimulationInfo(User user, KeyValue simKey){
KeyValue versionKey = new KeyValue("22");
KeyValue versionBranchPoint = new KeyValue("23");
VersionFlag versionFlag = VersionFlag.fromInt(0);
KeyValue parentSimulationRef = new KeyValue("24");
SimulationVersion simulationVersion = new SimulationVersion(versionKey, "Mock Sim Info", user, null,
versionBranchPoint, new BigDecimal(22), Date.from(Instant.now()), versionFlag, "Version annot",
parentSimulationRef);
SimulationInfo simulationInfo = new SimulationInfo(simKey, simulationVersion, VCellSoftwareVersion.fromString("50"));
return simulationInfo;
}

public void resetDataBase(){
dbTable = new HashMap<>();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cbit.vcell.message.server.dispatcher;

import cbit.vcell.server.SimulationJobStatus;
import cbit.vcell.server.SimulationStatus;
import org.junit.jupiter.api.*;
import org.vcell.util.DataAccessException;
import org.vcell.util.document.User;

import java.sql.SQLException;

@Tag("Fast")
public class SimulationDispatcherTest {
private final static User testUser = DispatcherTestUtils.alice;
private MockSimulationDB mockSimulationDB = new MockSimulationDB();
private MockMessagingService mockMessagingServiceInternal = new MockMessagingService();
private MockMessagingService mockMessagingServiceSim = new MockMessagingService();

@BeforeAll
public static void setSystemProperties(){
DispatcherTestUtils.setRequiredProperties();
}

@AfterAll
public static void restoreSystemProperties(){
DispatcherTestUtils.restoreRequiredProperties();
}

@BeforeEach
public void beforeEach(){
mockSimulationDB = new MockSimulationDB();
}


@Test
public void onStartRequestTest() throws DataAccessException, SQLException {
SimulationDispatcher simulationDispatcher = SimulationDispatcher.simulationDispatcherCreator(mockSimulationDB, mockMessagingServiceInternal, mockMessagingServiceSim);
SimulationStatus simStatus = simulationDispatcher.simServiceImpl.startSimulation(testUser, DispatcherTestUtils.simID, 1);
SimulationJobStatus jobStatus = mockSimulationDB.getLatestSimulationJobStatus(DispatcherTestUtils.simKey, 0);
Assertions.assertTrue(jobStatus.getSchedulerStatus().isWaiting());
}

@Test
public void onStopRequestTest() throws DataAccessException, SQLException {
DispatcherTestUtils.insertOrUpdateStatus(mockSimulationDB);
SimulationDispatcher simulationDispatcher = SimulationDispatcher.simulationDispatcherCreator(mockSimulationDB, mockMessagingServiceInternal, mockMessagingServiceSim);
SimulationStatus simStatus = simulationDispatcher.simServiceImpl.stopSimulation(testUser, DispatcherTestUtils.simID);
SimulationJobStatus jobStatus = mockSimulationDB.getLatestSimulationJobStatus(DispatcherTestUtils.simKey, 0);
Assertions.assertTrue(jobStatus.getSchedulerStatus().isStopped());
}

}
Loading

0 comments on commit 41a4194

Please sign in to comment.