Skip to content

Commit

Permalink
Enhance setTaskLocksBeforeTheSimulationStarts method to support defau…
Browse files Browse the repository at this point in the history
…lt unit handling and improve test coverage for TaskLocks functionality
  • Loading branch information
77it committed Jan 7, 2025
1 parent dc234e7 commit f3c5e97
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/modules/default_tasklocks/_set_default_tasklocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export class Module {

/** Set TaskLocks */
setTaskLocksBeforeTheSimulationStarts () {
// set default TaskLocks
this.#simulationContext.setTaskLock({ name: TaskLocks_Names.SIMULATION__SIMULATION_SETTINGS__MISSING__SET_WITH_DEFAULT_VALUE, value: this.#taskLock_setMissingSettingsWithDefaultValue });
this.#simulationContext.setTaskLock({ name: TaskLocks_Names.SIMULATION__SIMULATION_SETTINGS__JS_ENGINE_CONFIGURATION__GLOBAL_VALUES__SET, value: this.#taskLock_setJsEngineConfigurationGlobalValuesFromSimulationSettings });
// set default TaskLocks on Simulation (when unit is empty or missing the default unit is used)
this.#simulationContext.setTaskLock({ unit: '', name: TaskLocks_Names.SIMULATION__SIMULATION_SETTINGS__MISSING__SET_WITH_DEFAULT_VALUE, value: this.#taskLock_setMissingSettingsWithDefaultValue });
this.#simulationContext.setTaskLock({ unit: '', name: TaskLocks_Names.SIMULATION__SIMULATION_SETTINGS__JS_ENGINE_CONFIGURATION__GLOBAL_VALUES__SET, value: this.#taskLock_setJsEngineConfigurationGlobalValuesFromSimulationSettings });
}

// is an arrow function because it is used as a callback
Expand Down
26 changes: 25 additions & 1 deletion test/engine_test/tasklocks/tasklocks__test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,39 @@ import assert from 'node:assert';
t('TaskLocks tests', async () => {
const taskLocks = new TaskLocks({ defaultUnit: CFG.SIMULATION_NAME });

// set
// set on default unit
assert(taskLocks.set({ name: 'testFunc', value: testFunc }));
assert(!taskLocks.set({ name: 'testFunc', value: testFunc }));
assert(taskLocks.set({ unit: '', name: 'testFunc2', value: testFunc }));

// set on named unit
assert(taskLocks.set({ unit: 'AAA', name: 'testFunc', value: testFunc }));

// get without unit
assert.deepStrictEqual(taskLocks.get({ name: 'testFunc' })(), 99);
assert.deepStrictEqual(taskLocks.get({ name: 'testFunc2' })(), 99);

// get without unit, with invalid/empty unit values
assert.deepStrictEqual(taskLocks.get({ unit: '', name: 'testFunc' })(), 99);
assert.deepStrictEqual(taskLocks.get({ unit: ' ', name: 'testFunc' })(), 99);
assert.deepStrictEqual(taskLocks.get({ unit: undefined, name: 'testFunc' })(), 99);
//@ts-ignore
assert.deepStrictEqual(taskLocks.get({ unit: null, name: 'testFunc' })(), 99);

// get with unit
assert.deepStrictEqual(taskLocks.get({ unit: CFG.SIMULATION_NAME, name: 'testFunc' })(), 99);
assert.deepStrictEqual(taskLocks.get({ unit: CFG.SIMULATION_NAME, name: 'testFunc2' })(), 99);
assert.deepStrictEqual(taskLocks.get({ unit: 'AAA', name: 'testFunc' })(), 99);

// error getting non-existing TaskLock
assert.throws(() => taskLocks.get({ name: 'nonExisting' }), { message: `TaskLock '{"unit":"$","name":"nonexisting"}' is not defined.` });

// testing defined TaskLock
assert(taskLocks.isDefined({ name: 'testFunc' }));
assert(taskLocks.isDefined({ unit: CFG.SIMULATION_NAME, name: 'testFunc' }));

// testing not defined TaskLock
assert(!taskLocks.isDefined({ name: 'nonExisting' }));
});

function testFunc() {
Expand Down

0 comments on commit f3c5e97

Please sign in to comment.