Skip to content
Snippets Groups Projects
Commit e530e14c authored by Sandro Weber's avatar Sandro Weber Committed by Antoine Detailleur
Browse files

sim ready test

parent 5dcfc108
No related branches found
No related tags found
No related merge requests found
......@@ -4,16 +4,13 @@ import config from '../config.json';
import endpoints from '../services/proxy/data/endpoints';
import MockExperiments from './mock_experiments.json';
import MockAvailableServers from './mock_available-servers.json';
import MockSimulationResources from './mock_simulation-resources.json';
import ImageAI from '../assets/images/Artificial_Intelligence_2.jpg';
const availableServers = MockAvailableServers;
const experiments = MockExperiments;
const thumbnailURL = `${config.api.proxy.url}${endpoints.proxy.storage.url}` +
`/${experiments[0].name}/${experiments[0].configuration.thumbnail}`;
export const storageThumbnailExperiment = experiments[0];
export const handlers = [
rest.get(`${config.api.proxy.url}${endpoints.proxy.storage.experiments.url}`, (req, res, ctx) => {
return res(
......@@ -25,7 +22,18 @@ export const handlers = [
ctx.json(availableServers)
);
}),
rest.get(thumbnailURL, (req, res, ctx) => {
return res(ctx.body(ImageAI));
rest.get(`${config.api.proxy.url}${endpoints.proxy.storage.url}/:experimentName/:thumbnailFilename`,
(req, res, ctx) => {
return res(ctx.body(ImageAI));
}
),
rest.get('http://:serverIP/simulation/:simulationID/resources', (req, res, ctx) => {
const simulationID = parseInt(req.params.simulationID);
if (simulationID === 1) {
return res(ctx.json(MockSimulationResources));
}
else {
throw new Error('Simulation resource error');
}
})
];
\ No newline at end of file
......@@ -12,5 +12,19 @@
},
"serverJobLocation": "local",
"id": "localhost"
},
{
"internalIp": "http://1.2.3.4:8080",
"gzweb": {
"assets": "http://1.2.3.4:8080/assets",
"nrp-services": "http://1.2.3.4:8080",
"videoStreaming": "http://1.2.3.4:8080/webstream/",
"websocket": "ws://1.2.3.4:8080/gzbridge"
},
"rosbridge": {
"websocket": "ws://1.2.3.4:8080/rosbridge"
},
"serverJobLocation": "1.2.3.4",
"id": "1.2.3.4:8080"
}
]
\ No newline at end of file
{
"resources": [
{
"file": "/config-from-cloned-folder/nrp.3._xeg5cqw/ExDXMLExample.ini",
"type": "3d-settings",
"file_offset": 42
},
{
"file": "/config-from-cloned-folder/nrp.3._xeg5cqw/brainvisualizer.json",
"type": "brainvisualizer",
"file_offset": 42
},
{
"file": "/config-from-cloned-folder/nrp.3._xeg5cqw/ExDXMLExample.uis",
"type": "user-interaction-settings",
"file_offset": 42
}
]
}
\ No newline at end of file
[
{
"state": "created",
"simulationID": 0,
"environmentConfiguration": null,
"owner": "nrpuser",
"creationDate": "2021-01-27T15:51:12.893249+01:00",
"gzserverHost": "local",
"reservation": null,
"experimentID": "braitenberg_husky_holodeck_0",
"brainProcesses": 1,
"creationUniqueID": "1611759072866.1191",
"playbackPath": null
}
]
\ No newline at end of file
......@@ -4,9 +4,92 @@
import '@testing-library/jest-dom';
import 'jest-fetch-mock';
import MockAvailableServers from '../../../../mocks/mock_available-servers.json';
import MockSimulations from '../../../../mocks/mock_simulations.json';
import SimulationService from '../../../../services/experiments/execution/simulation-service.js';
import ErrorHandlerService from '../../../error-handler-service';
import { EXPERIMENT_STATE } from '../../../../services/experiments/experiment-constants.js';
jest.setTimeout(10000);
afterEach(() => {
jest.restoreAllMocks();
});
test('fetches the list of experiments', async () => {
test('makes sure that invoking the constructor fails with the right message', () => {
expect(() => {
new SimulationService();
}).toThrow(Error);
expect(() => {
new SimulationService();
}).toThrowError(Error('Use SimulationService.instance'));
});
test('the service instance always refers to the same object', () => {
const instance1 = SimulationService.instance;
const instance2 = SimulationService.instance;
expect(instance1).toBe(instance2);
});
test('initializes and gets the simulation resources', async () => {
let serverBaseURL = MockAvailableServers[0].gzweb['nrp-services'];
let simID = 1;
let resources = await SimulationService.instance.initConfigFiles(serverBaseURL, simID);
expect(resources).toBeDefined();
// failure case
jest.spyOn(ErrorHandlerService.instance, 'displayServerHTTPError').mockImplementation(() => { });
let simIDFailure = 0;
expect(ErrorHandlerService.instance.displayServerHTTPError).not.toHaveBeenCalled();
resources = await SimulationService.instance.initConfigFiles(serverBaseURL, simIDFailure);
expect(ErrorHandlerService.instance.displayServerHTTPError).toHaveBeenCalled();
});
test('verifies whether a simulation is ready', async () => {
let mockSimulationList = JSON.parse(JSON.stringify(MockSimulations));
jest.spyOn(SimulationService.instance, 'httpRequestGET').mockImplementation(() => {
if (SimulationService.instance.httpRequestGET.mock.calls.length === 1) {
mockSimulationList[0].state = EXPERIMENT_STATE.CREATED; // state pending
}
else if (SimulationService.instance.httpRequestGET.mock.calls.length === 2) {
mockSimulationList[0].state = EXPERIMENT_STATE.PAUSED; // state ok
}
else if (SimulationService.instance.httpRequestGET.mock.calls.length === 3) {
mockSimulationList[0].state = EXPERIMENT_STATE.HALTED; // state bad
}
else if (SimulationService.instance.httpRequestGET.mock.calls.length === 4) {
return Promise.reject('mock simulation GET error'); // general error
}
else if (SimulationService.instance.httpRequestGET.mock.calls.length === 5) {
mockSimulationList[0].state = EXPERIMENT_STATE.PAUSED; // state ok
}
return Promise.resolve({
json: () => {
return mockSimulationList;
}
});
});
// call 1 (created) => continue checking & call 2 (paused) => resolved successfully
let simReady = SimulationService.instance
.simulationReady('mock-server-url', mockSimulationList[0].creationUniqueID);
await expect(simReady).resolves.toEqual(mockSimulationList[0]);
// call 3 (halted) => rejected with state
simReady = SimulationService.instance
.simulationReady('mock-server-url', mockSimulationList[0].creationUniqueID);
await expect(simReady).rejects.toEqual(EXPERIMENT_STATE.HALTED);
// call 4 (error) => rejected with error
simReady = SimulationService.instance
.simulationReady('mock-server-url', mockSimulationList[0].creationUniqueID);
await expect(simReady).rejects.toEqual('mock simulation GET error');
// call 5 (state ok but wrong creation id) => rejected because of wrong creation ID
simReady = SimulationService.instance
.simulationReady('mock-server-url', 'wrong-creation-id');
await expect(simReady).rejects.toEqual(undefined);
});
\ No newline at end of file
......@@ -40,8 +40,8 @@ class SimulationService extends HttpService {
let cachedConfigFiles = undefined;
try {
let url = serverBaseUrl + '/simulation/' + simulationID + '/resources';
let response = await this.httpRequestGET(url);
cachedConfigFiles = await response.json().resources;
let response = await (await this.httpRequestGET(url)).json();
cachedConfigFiles = response.resources;
}
catch (error) {
ErrorHandlerService.instance.displayServerHTTPError(error);
......@@ -60,33 +60,35 @@ class SimulationService extends HttpService {
return new Promise((resolve, reject) => {
let verifySimulation = () => {
setTimeout(() => {
this.httpRequestGET(serverURL + '/simulation').then(async (reponse) => {
let continueVerify = true;
let simulations = await reponse.json();
if (simulations.length > 0) {
let last = simulations.length - 1;
let state = simulations[last].state;
if (state === EXPERIMENT_STATE.PAUSED || state === EXPERIMENT_STATE.INITIALIZED) {
if (simulations[last].creationUniqueID === creationUniqueID) {
continueVerify = false;
resolve(simulations[last]);
this.httpRequestGET(serverURL + '/simulation')
.then(async (reponse) => {
let continueVerify = true;
let simulations = await reponse.json();
if (simulations.length > 0) {
let last = simulations.length - 1;
let state = simulations[last].state;
if (state === EXPERIMENT_STATE.PAUSED || state === EXPERIMENT_STATE.INITIALIZED) {
if (simulations[last].creationUniqueID === creationUniqueID) {
continueVerify = false;
resolve(simulations[last]);
}
else {
reject();
}
}
else {
reject();
else if (state === EXPERIMENT_STATE.HALTED || state === EXPERIMENT_STATE.FAILED) {
continueVerify = false;
reject(state);
}
}
else if (state === EXPERIMENT_STATE.HALTED || state === EXPERIMENT_STATE.FAILED) {
continueVerify = false;
reject();
}
}
if (continueVerify) {
verifySimulation();
}
}).catch(reject);
if (continueVerify) {
verifySimulation();
}
})
.catch(reject);
}, INTERVAL_CHECK_SIMULATION_READY);
};
......
......@@ -8,7 +8,6 @@ import ExperimentStorageService from '../experiment-storage-service';
import endpoints from '../../../proxy/data/endpoints.json';
import config from '../../../../config.json';
import MockExperiments from '../../../../mocks/mock_experiments.json';
import { storageThumbnailExperiment } from '../../../../mocks/handlers.js';
jest.mock('../../../authentication-service');
const proxyEndpoint = endpoints.proxy;
......@@ -20,6 +19,21 @@ afterEach(() => {
jest.restoreAllMocks();
});
test('makes sure that invoking the constructor fails with the right message', () => {
expect(() => {
new ExperimentStorageService();
}).toThrow(Error);
expect(() => {
new ExperimentStorageService();
}).toThrowError(Error('Use ExperimentStorageService.instance'));
});
test('the experiments service instance always refers to the same object', () => {
const instance1 = ExperimentStorageService.instance;
const instance2 = ExperimentStorageService.instance;
expect(instance1).toBe(instance2);
});
test('fetches the list of experiments', async () => {
jest.spyOn(ExperimentStorageService.instance, 'performRequest');
......@@ -68,24 +82,8 @@ test('does automatic poll updates of experiment list which can be stopped', (don
}, ExperimentStorageService.CONSTANTS.INTERVAL_POLL_EXPERIMENTS);
});
test('makes sure that invoking the constructor fails with the right message', () => {
expect(() => {
new ExperimentStorageService();
}).toThrow(Error);
expect(() => {
new ExperimentStorageService();
}).toThrowError(Error('Use ExperimentStorageService.instance'));
});
test('the experiments service instance always refers to the same object', () => {
const instance1 = ExperimentStorageService.instance;
const instance2 = ExperimentStorageService.instance;
expect(instance1).toBe(instance2);
});
test('gets a thumbnail image for experiments', async () => {
let experiment = storageThumbnailExperiment;
let experiment = MockExperiments[0];
const imageBlob = await ExperimentStorageService.instance.getThumbnail(experiment.name,
experiment.configuration.thumbnail);
expect(imageBlob).toBeDefined();
......
......@@ -26,18 +26,10 @@ class ExperimentStorageService extends HttpService {
}
this.startUpdates();
<<<<<<< HEAD
if (typeof window !== 'undefined'){
window.onbeforeunload = () => {
this.stopUpdates();
};
}
=======
window.addEventListener('beforeunload', (event) => {
this.stopUpdates();
event.returnValue = '';
});
>>>>>>> bfdbf49... test optics
}
static get instance() {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment