[IMP] mail: correctly patch mail status manager timers

We want to avoid that a `setTimeout` started in one test has an impact on other
tests. This is especially true with the status update loop. Since mail_service
is used in multiple tests, the safest solution is to patch timeouts everytime
we make a call to getMailServices.
There is no need for an unpatch, the patch become the default behaviour until
the page is refresh at the end of tests.

It is possible to manually add the patch after calling `getMailServices`
```
this.services = mailTestUtils.getMailServices(this);
this.timeoutMock = mailTestUtils.patchMailTimeouts();
```
This can be usefull if we want to get the timeoutMock to simulate time changes.

Task: 1856205
This commit is contained in:
XavierDo
2019-02-07 12:53:14 +00:00
parent 2d45df1081
commit 5c3aaacd2e
4 changed files with 137 additions and 74 deletions
@@ -3,6 +3,7 @@ odoo.define('mail.Manager.Status', function (require) {
var core = require('web.core');
var MailManager = require('mail.Manager');
var mailUtils = require('mail.utils');
var QWeb = core.qweb;
/**
@@ -33,8 +34,8 @@ MailManager.include({
// Add to list to call it in next bus update or _fetchMissingImStatus
this._imStatus[partnerID] = undefined;
// fetch after some time if no other getImStatus occurs
this._clearStatusServiceTimeout(this._fetchStatusTimeout);
this._fetchStatusTimeout = this._setStatusServiceTimeout(function () {
mailUtils.clearTimeout(this._fetchStatusTimeout);
this._fetchStatusTimeout = mailUtils.setTimeout(function () {
self._fetchMissingImStatus();
}, 500);
}
@@ -67,15 +68,6 @@ MailManager.include({
// Private
//--------------------------------------------------------------------------
/**
* A simple clearTimeout, useful for test
*
* @private
* @param {integer} ids
*/
_clearStatusServiceTimeout: function (id) {
clearTimeout(id);
},
/**
* Fetch the list of im_status for partner with id in ids list and triggers
* an update.
@@ -168,17 +160,6 @@ MailManager.include({
}
});
},
/**
* A simple setTimeout, useful for test
*
* @private
* @param {function} func
* @param {integer} duration
* @return {integer}
*/
_setStatusServiceTimeout: function (func, duration) {
return setTimeout(func, duration);
},
/**
* Once initialised, this loop will update the im_status of registered
* users.
@@ -191,7 +172,7 @@ MailManager.include({
if (!_.isNumber(counter)) {
counter = 0;
}
this._setStatusServiceTimeout(function () {
mailUtils.setTimeout(function () {
if (counter >= self._UPDATE_INTERVAL && self._isTabFocused) {
self._fetchImStatus({ partnerIDs: self._getImStatusToUpdate() });
counter = 0;
+10
View File
@@ -91,6 +91,14 @@ function timeFromNow(date) {
return date.fromNow();
}
function o_clearTimeout(id) {
return clearTimeout(id);
}
function o_setTimeout(func, delay) {
return setTimeout(func, delay);
}
return {
addLink: addLink,
getTextToHTML: getTextToHTML,
@@ -100,6 +108,8 @@ return {
parseEmail: parseEmail,
stripHTML: stripHTML,
timeFromNow: timeFromNow,
clearTimeout: o_clearTimeout,
setTimeout: o_setTimeout,
};
});