mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-18 07:51:55 +03:00
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:
parent
0caf8bff18
commit
e170f293e3
@ -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 {anyObjectId, anyString, anyEtag, anyNumber} = matchers;
|
||||||
|
|
||||||
const matchLink = {
|
const matchLink = {
|
||||||
@ -14,12 +14,6 @@ const matchLink = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
async function sleep(ms) {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
setTimeout(resolve, ms);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('Links API', function () {
|
describe('Links API', function () {
|
||||||
let agent;
|
let agent;
|
||||||
beforeEach(async function () {
|
beforeEach(async function () {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
const assert = require('assert');
|
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 {anyEtag, anyObjectId, anyLocationFor, anyISODateTime, anyErrorId, anyUuid, anyNumber, anyBoolean} = matchers;
|
||||||
const should = require('should');
|
const should = require('should');
|
||||||
const models = require('../../../core/server/models');
|
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) {
|
function escapeRegExp(string) {
|
||||||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||||
}
|
}
|
||||||
|
@ -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 should = require('should');
|
||||||
const settingsCache = require('../../../core/shared/settings-cache');
|
const settingsCache = require('../../../core/shared/settings-cache');
|
||||||
const {anyErrorId} = matchers;
|
const {anyErrorId} = matchers;
|
||||||
|
|
||||||
let membersAgent, membersService;
|
let membersAgent, membersService;
|
||||||
|
|
||||||
async function sleep(ms) {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
setTimeout(resolve, ms);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('sendMagicLink', function () {
|
describe('sendMagicLink', function () {
|
||||||
before(async function () {
|
before(async function () {
|
||||||
const agents = await agentProvider.getAgentsForMembers();
|
const agents = await agentProvider.getAgentsForMembers();
|
||||||
|
@ -4,7 +4,7 @@ const nock = require('nock');
|
|||||||
const should = require('should');
|
const should = require('should');
|
||||||
const stripe = require('stripe');
|
const stripe = require('stripe');
|
||||||
const {Product} = require('../../../core/server/models/product');
|
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 models = require('../../../core/server/models');
|
||||||
const urlService = require('../../../core/server/services/url');
|
const urlService = require('../../../core/server/services/url');
|
||||||
const urlUtils = require('../../../core/shared/url-utils');
|
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);
|
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 () {
|
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 ends (should create comped -> free event)
|
||||||
// @todo: Test what happens when a complimentary subscription starts a paid subscription
|
// @todo: Test what happens when a complimentary subscription starts a paid subscription
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
const sinon = require('sinon');
|
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 assert = require('assert');
|
||||||
const models = require('../../../../core/server/models');
|
const models = require('../../../../core/server/models');
|
||||||
const domainEvents = require('@tryghost/domain-events');
|
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 membersService = require('../../../../core/server/services/members');
|
||||||
const {EmailDeliveredEvent} = require('@tryghost/email-events');
|
const {EmailDeliveredEvent} = require('@tryghost/email-events');
|
||||||
|
|
||||||
async function sleep(ms) {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
setTimeout(resolve, ms);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function resetFailures(emailId) {
|
async function resetFailures(emailId) {
|
||||||
await models.EmailRecipientFailure.destroy({
|
await models.EmailRecipientFailure.destroy({
|
||||||
destroyBy: {
|
destroyBy: {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
require('should');
|
require('should');
|
||||||
const {agentProvider, fixtureManager, mockManager} = require('../../utils/e2e-framework');
|
const {agentProvider, fixtureManager, mockManager, sleep} = require('../../utils/e2e-framework');
|
||||||
const moment = require('moment');
|
const moment = require('moment');
|
||||||
const ObjectId = require('bson-objectid').default;
|
const ObjectId = require('bson-objectid').default;
|
||||||
const models = require('../../../core/server/models');
|
const models = require('../../../core/server/models');
|
||||||
@ -7,12 +7,6 @@ const sinon = require('sinon');
|
|||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
let agent;
|
let agent;
|
||||||
|
|
||||||
async function sleep(ms) {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
setTimeout(resolve, ms);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function createPublishedPostEmail() {
|
async function createPublishedPostEmail() {
|
||||||
const post = {
|
const post = {
|
||||||
title: 'A random test post',
|
title: 'A random test post',
|
||||||
|
@ -9,18 +9,12 @@ const configUtils = require('../../../utils/configUtils');
|
|||||||
const settingsCache = require('../../../../core/shared/settings-cache');
|
const settingsCache = require('../../../../core/shared/settings-cache');
|
||||||
const models = require('../../../../core/server/models');
|
const models = require('../../../../core/server/models');
|
||||||
|
|
||||||
const {mockManager} = require('../../../utils/e2e-framework');
|
const {mockManager, sleep} = require('../../../utils/e2e-framework');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const {_updateVerificationTrigger} = require('../../../../core/server/services/members');
|
const {_updateVerificationTrigger} = require('../../../../core/server/services/members');
|
||||||
|
|
||||||
let request;
|
let request;
|
||||||
|
|
||||||
async function sleep(ms) {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
setTimeout(resolve, ms);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('Members Importer API', function () {
|
describe('Members Importer API', function () {
|
||||||
before(async function () {
|
before(async function () {
|
||||||
await localUtils.startGhost();
|
await localUtils.startGhost();
|
||||||
|
@ -3,17 +3,11 @@ const sinon = require('sinon');
|
|||||||
const staffService = require('../../../../../core/server/services/staff');
|
const staffService = require('../../../../../core/server/services/staff');
|
||||||
|
|
||||||
const DomainEvents = require('@tryghost/domain-events');
|
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 models = require('../../../../../core/server/models');
|
||||||
|
|
||||||
const {SubscriptionCreatedEvent, SubscriptionCancelledEvent, MemberCreatedEvent} = require('@tryghost/member-events');
|
const {SubscriptionCreatedEvent, SubscriptionCancelledEvent, MemberCreatedEvent} = require('@tryghost/member-events');
|
||||||
|
|
||||||
async function sleep(ms) {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
setTimeout(resolve, ms);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('Staff Service:', function () {
|
describe('Staff Service:', function () {
|
||||||
before(function () {
|
before(function () {
|
||||||
models.init();
|
models.init();
|
||||||
|
@ -432,5 +432,6 @@ module.exports = {
|
|||||||
configUtils: require('./configUtils'),
|
configUtils: require('./configUtils'),
|
||||||
dbUtils: require('./db-utils'),
|
dbUtils: require('./db-utils'),
|
||||||
urlUtils: require('./urlUtils'),
|
urlUtils: require('./urlUtils'),
|
||||||
|
sleep: require('./sleep'),
|
||||||
resetRateLimits
|
resetRateLimits
|
||||||
};
|
};
|
||||||
|
15
ghost/core/test/utils/sleep.js
Normal file
15
ghost/core/test/utils/sleep.js
Normal 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);
|
||||||
|
})
|
||||||
|
);
|
Loading…
Reference in New Issue
Block a user