From e170f293e3fbc02072ea8a6cee306e4badc5e24f Mon Sep 17 00:00:00 2001 From: Naz Date: Mon, 5 Dec 2022 16:51:09 +0700 Subject: [PATCH] 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 --- ghost/core/test/e2e-api/admin/links.test.js | 8 +------- .../e2e-api/members-comments/comments.test.js | 8 +------- .../test/e2e-api/members/send-magic-link.test.js | 8 +------- ghost/core/test/e2e-api/members/webhooks.test.js | 8 +------- .../email-service/email-event-storage.test.js | 8 +------- ghost/core/test/integration/services/mega.test.js | 8 +------- .../regression/api/admin/members-importer.test.js | 8 +------- .../test/unit/server/services/staff/index.test.js | 8 +------- ghost/core/test/utils/e2e-framework.js | 1 + ghost/core/test/utils/sleep.js | 15 +++++++++++++++ 10 files changed, 24 insertions(+), 56 deletions(-) create mode 100644 ghost/core/test/utils/sleep.js diff --git a/ghost/core/test/e2e-api/admin/links.test.js b/ghost/core/test/e2e-api/admin/links.test.js index 6f4f325c09..821531aacd 100644 --- a/ghost/core/test/e2e-api/admin/links.test.js +++ b/ghost/core/test/e2e-api/admin/links.test.js @@ -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 () { diff --git a/ghost/core/test/e2e-api/members-comments/comments.test.js b/ghost/core/test/e2e-api/members-comments/comments.test.js index 73227362c6..8a91059793 100644 --- a/ghost/core/test/e2e-api/members-comments/comments.test.js +++ b/ghost/core/test/e2e-api/members-comments/comments.test.js @@ -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, '\\$&'); } diff --git a/ghost/core/test/e2e-api/members/send-magic-link.test.js b/ghost/core/test/e2e-api/members/send-magic-link.test.js index 87655f7ed3..b49bd5be71 100644 --- a/ghost/core/test/e2e-api/members/send-magic-link.test.js +++ b/ghost/core/test/e2e-api/members/send-magic-link.test.js @@ -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(); diff --git a/ghost/core/test/e2e-api/members/webhooks.test.js b/ghost/core/test/e2e-api/members/webhooks.test.js index c7af12145b..4a6ed00386 100644 --- a/ghost/core/test/e2e-api/members/webhooks.test.js +++ b/ghost/core/test/e2e-api/members/webhooks.test.js @@ -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 diff --git a/ghost/core/test/integration/services/email-service/email-event-storage.test.js b/ghost/core/test/integration/services/email-service/email-event-storage.test.js index 5a702eeea1..daf40400a4 100644 --- a/ghost/core/test/integration/services/email-service/email-event-storage.test.js +++ b/ghost/core/test/integration/services/email-service/email-event-storage.test.js @@ -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: { diff --git a/ghost/core/test/integration/services/mega.test.js b/ghost/core/test/integration/services/mega.test.js index 79a693e21c..5d15e983e7 100644 --- a/ghost/core/test/integration/services/mega.test.js +++ b/ghost/core/test/integration/services/mega.test.js @@ -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', diff --git a/ghost/core/test/regression/api/admin/members-importer.test.js b/ghost/core/test/regression/api/admin/members-importer.test.js index 1bfc748999..382382da89 100644 --- a/ghost/core/test/regression/api/admin/members-importer.test.js +++ b/ghost/core/test/regression/api/admin/members-importer.test.js @@ -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(); diff --git a/ghost/core/test/unit/server/services/staff/index.test.js b/ghost/core/test/unit/server/services/staff/index.test.js index 3bb1178658..d26ccd34a9 100644 --- a/ghost/core/test/unit/server/services/staff/index.test.js +++ b/ghost/core/test/unit/server/services/staff/index.test.js @@ -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(); diff --git a/ghost/core/test/utils/e2e-framework.js b/ghost/core/test/utils/e2e-framework.js index a26ebcdcfe..cb6d70a6e0 100644 --- a/ghost/core/test/utils/e2e-framework.js +++ b/ghost/core/test/utils/e2e-framework.js @@ -432,5 +432,6 @@ module.exports = { configUtils: require('./configUtils'), dbUtils: require('./db-utils'), urlUtils: require('./urlUtils'), + sleep: require('./sleep'), resetRateLimits }; diff --git a/ghost/core/test/utils/sleep.js b/ghost/core/test/utils/sleep.js new file mode 100644 index 0000000000..70767e0d07 --- /dev/null +++ b/ghost/core/test/utils/sleep.js @@ -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} + */ +module.exports = ms => ( + new Promise((resolve) => { + setTimeout(resolve, ms); + }) +);