Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/TR-5854/Detect delivery concurrency #522

Merged
merged 6 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/plugins/controls/session/preventConcurrency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2023 (original work) Open Assessment Technologies SA ;
*/
import context from 'context';
import loggerFactory from 'core/logger';
import __ from 'i18n';
import states from 'taoQtiTest/runner/config/states';
import { getSequenceNumber, getSequenceStore } from 'taoQtiTest/runner/services/sequenceStore';
import pluginFactory from 'taoTests/runner/plugin';

const logger = loggerFactory('taoQtiTest/runner/plugins/controls/session/preventConcurrency');

const FEATURE_FLAG = 'FEATURE_FLAG_PAUSE_CONCURRENT_SESSIONS';

/**
* Test Runner Control Plugin : detect concurrent deliveries launched from the same user session.
*/
export default pluginFactory({
name: 'preventConcurrency',

/**
* Initializes the plugin (called during runner's init)
*/
init() {
const testRunner = this.getTestRunner();
const options = testRunner.getOptions();
const skipPausedAssessmentDialog = !!options.skipPausedAssessmentDialog;

return Promise.all([getSequenceNumber(testRunner), getSequenceStore()]).then(
([sequenceNumber, sequenceStore]) =>
sequenceStore.setSequenceNumber(sequenceNumber).then(() => {
testRunner
.on('tick', () => {
if (context.featureFlags[FEATURE_FLAG]) {
return sequenceStore.getSequenceNumber().then(lastSequenceNumber => {
if (lastSequenceNumber !== sequenceNumber) {
testRunner.off('tick');
testRunner.trigger('disabletools');
testRunner.trigger('disablenav');
testRunner.trigger('disableitem');
testRunner.trigger('concurrency');
return Promise.reject();
}
});
}
})
.on('concurrency', () => {
logger.warn(
`The sequence number has changed. Was another delivery opened in the same browser?`
);
testRunner.trigger('leave', {
code: states.testSession.suspended,
message: __(
Copy link

@gabrielfs7 gabrielfs7 Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • @jsconan Can you confirm how this string will be linked to PO files? I think Infosign will that there. Thanks! :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gabrielfs7 the translation scanner will take care of it, from the consumer extension, as usual (say taoQtiTest)

'A concurrent delivery has been detected. Please use the last open session. The present window can be closed.'
),
skipExitMessage: skipPausedAssessmentDialog
});
});
})
);
}
});
59 changes: 59 additions & 0 deletions src/services/sequenceStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2023 (original work) Open Assessment Technologies SA ;
*/
import store from 'core/store';

const STORE_ID = 'current';
const SEQUENCE_NUMBER = 'sequence';

export default {
/**
* Creates a store for the sequence number for the session.
* @returns {Promise} - Resolved with the store API.
*/
getSequenceStore() {
return store(STORE_ID).then(sequenceStore => ({
/**
* Stores the sequence number for the session.
* @param {string} sequenceNumber - The new sequence number to set for the session.
* @returns {Promise} - Resolved once the sequence number has been stored.
*/
setSequenceNumber(sequenceNumber) {
return sequenceStore.setItem(SEQUENCE_NUMBER, sequenceNumber);
},

/**
* Reads the sequence number for the session from the storage.
* @returns {Promise<string>} - Resolved with the sequence number for the session.
*/
getSequenceNumber() {
return sequenceStore.getItem(SEQUENCE_NUMBER);
}
}));
},

/**
* Creates a sequence number for the test runner.
* @returns {Promise<string>} - Resolved with sequence number for the session.
*/
getSequenceNumber(testRunner) {
return testRunner
.getTestStore()
.getStorageIdentifier()
.then(storeId => `${storeId}-${Date.now()}`);
}
};
28 changes: 28 additions & 0 deletions test/plugins/controls/preventConcurrency/mocks/sequenceStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
define(function () {
('use strict');

let sequenceNumber = '';

return {
getSequenceStore() {
return Promise.resolve({
setSequenceNumber(seq) {
sequenceNumber = seq;
return Promise.resolve();
},

getSequenceNumber() {
return Promise.resolve(sequenceNumber);
}
});
},

getSequenceNumber(testRunner) {
return Promise.resolve(testRunner.sequenceNumber);
},

setSequenceNumber(seq) {
sequenceNumber = seq;
}
};
});
27 changes: 27 additions & 0 deletions test/plugins/controls/preventConcurrency/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test Runner Plugins - preventConcurrency</title>
<script type="text/javascript" src="/environment/require.js"></script>
<script type="text/javascript">
require(['/environment/config.js'], function() {
require(['qunitEnv'], function() {
requirejs.config({
paths: {
'taoQtiTest/runner/services/sequenceStore': '/test/plugins/controls/preventConcurrency/mocks/sequenceStore'
}
});
require(['taoQtiTest/test/runner/plugins/controls/preventConcurrency/test'], function() {
QUnit.start();
});
});
});
</script>
</head>

<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
</body>
</html>
Loading
Loading