Extracted sleep method to e2e framework module

no issue

- The sleep method has been used in 8 modules reimplementing the same thing over and over again. It's usually a sign of async event processing outside of the request/response loop. It's good to have a single point of implementation for a "hack" like this, so we could track it easier and address the even processing delay in a more optimal way centrally if it ever becomes a bottleneck
This commit is contained in:
Naz 2022-12-05 16:51:09 +07:00
parent 0caf8bff18
commit e170f293e3
No known key found for this signature in database
10 changed files with 24 additions and 56 deletions

View File

@ -1,4 +1,4 @@
const {agentProvider, fixtureManager, matchers} = require('../../utils/e2e-framework');
const {agentProvider, fixtureManager, matchers, sleep} = require('../../utils/e2e-framework');
const {anyObjectId, anyString, anyEtag, anyNumber} = matchers;
const matchLink = {
@ -14,12 +14,6 @@ const matchLink = {
}
};
async function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
describe('Links API', function () {
let agent;
beforeEach(async function () {

View File

@ -1,5 +1,5 @@
const assert = require('assert');
const {agentProvider, mockManager, fixtureManager, matchers, configUtils} = require('../../utils/e2e-framework');
const {agentProvider, mockManager, fixtureManager, matchers, configUtils, sleep} = require('../../utils/e2e-framework');
const {anyEtag, anyObjectId, anyLocationFor, anyISODateTime, anyErrorId, anyUuid, anyNumber, anyBoolean} = matchers;
const should = require('should');
const models = require('../../../core/server/models');
@ -49,12 +49,6 @@ function commentMatcherWithReplies(options = {replies: 0}) {
};
}
async function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

View File

@ -1,16 +1,10 @@
const {agentProvider, mockManager, fixtureManager, matchers} = require('../../utils/e2e-framework');
const {agentProvider, mockManager, fixtureManager, matchers, sleep} = require('../../utils/e2e-framework');
const should = require('should');
const settingsCache = require('../../../core/shared/settings-cache');
const {anyErrorId} = matchers;
let membersAgent, membersService;
async function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
describe('sendMagicLink', function () {
before(async function () {
const agents = await agentProvider.getAgentsForMembers();

View File

@ -4,7 +4,7 @@ const nock = require('nock');
const should = require('should');
const stripe = require('stripe');
const {Product} = require('../../../core/server/models/product');
const {agentProvider, mockManager, fixtureManager, matchers} = require('../../utils/e2e-framework');
const {agentProvider, mockManager, fixtureManager, matchers, sleep} = require('../../utils/e2e-framework');
const models = require('../../../core/server/models');
const urlService = require('../../../core/server/services/url');
const urlUtils = require('../../../core/shared/url-utils');
@ -43,12 +43,6 @@ async function assertSubscription(subscriptionId, asserts) {
models.Base.Model.prototype.serialize.call(subscription).should.match(asserts);
}
async function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
describe('Members API', function () {
// @todo: Test what happens when a complimentary subscription ends (should create comped -> free event)
// @todo: Test what happens when a complimentary subscription starts a paid subscription

View File

@ -1,5 +1,5 @@
const sinon = require('sinon');
const {agentProvider, fixtureManager, mockManager} = require('../../../utils/e2e-framework');
const {agentProvider, fixtureManager, sleep} = require('../../../utils/e2e-framework');
const assert = require('assert');
const models = require('../../../../core/server/models');
const domainEvents = require('@tryghost/domain-events');
@ -8,12 +8,6 @@ const {run} = require('../../../../core/server/services/email-analytics/jobs/fet
const membersService = require('../../../../core/server/services/members');
const {EmailDeliveredEvent} = require('@tryghost/email-events');
async function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function resetFailures(emailId) {
await models.EmailRecipientFailure.destroy({
destroyBy: {

View File

@ -1,5 +1,5 @@
require('should');
const {agentProvider, fixtureManager, mockManager} = require('../../utils/e2e-framework');
const {agentProvider, fixtureManager, mockManager, sleep} = require('../../utils/e2e-framework');
const moment = require('moment');
const ObjectId = require('bson-objectid').default;
const models = require('../../../core/server/models');
@ -7,12 +7,6 @@ const sinon = require('sinon');
const assert = require('assert');
let agent;
async function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function createPublishedPostEmail() {
const post = {
title: 'A random test post',

View File

@ -9,18 +9,12 @@ const configUtils = require('../../../utils/configUtils');
const settingsCache = require('../../../../core/shared/settings-cache');
const models = require('../../../../core/server/models');
const {mockManager} = require('../../../utils/e2e-framework');
const {mockManager, sleep} = require('../../../utils/e2e-framework');
const assert = require('assert');
const {_updateVerificationTrigger} = require('../../../../core/server/services/members');
let request;
async function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
describe('Members Importer API', function () {
before(async function () {
await localUtils.startGhost();

View File

@ -3,17 +3,11 @@ const sinon = require('sinon');
const staffService = require('../../../../../core/server/services/staff');
const DomainEvents = require('@tryghost/domain-events');
const {mockManager} = require('../../../../utils/e2e-framework');
const {mockManager, sleep} = require('../../../../utils/e2e-framework');
const models = require('../../../../../core/server/models');
const {SubscriptionCreatedEvent, SubscriptionCancelledEvent, MemberCreatedEvent} = require('@tryghost/member-events');
async function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
describe('Staff Service:', function () {
before(function () {
models.init();

View File

@ -432,5 +432,6 @@ module.exports = {
configUtils: require('./configUtils'),
dbUtils: require('./db-utils'),
urlUtils: require('./urlUtils'),
sleep: require('./sleep'),
resetRateLimits
};

View File

@ -0,0 +1,15 @@
/**
* Adds artificial "sleep" time that can be awaited
* In most cases where this module is used we are awaiting
* for the async event processing to trigger/finish
*
* Can probably be substituted by timerPromises in the future
* ref.: https://nodejs.org/dist/latest-v18.x/docs/api/timers.html#timerspromisessettimeoutdelay-value-options
* @param {number} ms
* @returns {Promise<void>}
*/
module.exports = ms => (
new Promise((resolve) => {
setTimeout(resolve, ms);
})
);