Skip to content

Commit

Permalink
chore: fix awaiting for notifications processing
Browse files Browse the repository at this point in the history
  • Loading branch information
karolsojko committed Aug 23, 2023
1 parent d1533ef commit 749cd9f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/snjs/mocha/lib/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,10 @@ export class AppContext {
return Utils.awaitPromiseOrThrow(promise, maxWait, reason)
}

awaitPromiseOrDoNothing(promise, maxWait = 2.0, reason = 'Awaiting promise timed out; No description provided') {
return Utils.awaitPromiseOrDoNothing(promise, maxWait, reason)
}

async activatePaidSubscriptionForUser(options = {}) {
const dateInAnHour = new Date()
dateInAnHour.setHours(dateInAnHour.getHours() + 1)
Expand Down Expand Up @@ -702,7 +706,7 @@ export class AppContext {
payAmount: 59.0,
})

await Utils.sleep(2, 'Waiting for premium features to be activated')
await this.sleep(2, 'Waiting for premium features to be activated')
} catch (error) {
console.warn(
`Mock events service not available. You are probably running a test suite for home server: ${error.message}`,
Expand All @@ -711,7 +715,7 @@ export class AppContext {
try {
await HomeServer.activatePremiumFeatures(this.email, options.subscriptionPlanName, options.expiresAt, uploadBytesLimit)

await Utils.sleep(1, 'Waiting for premium features to be activated')
await this.sleep(1, 'Waiting for premium features to be activated')
} catch (error) {
console.warn(
`Home server not available. You are probably running a test suite for self hosted setup: ${error.message}`,
Expand Down
20 changes: 20 additions & 0 deletions packages/snjs/mocha/lib/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,23 @@ export async function awaitPromiseOrThrow(promise, maxWait, reason) {
return result
})
}

export async function awaitPromiseOrDoNothing(promise, maxWait, reason) {
let timer = undefined

// Create a promise that resolves in <maxWait> milliseconds
const timeout = new Promise((resolve, reject) => {
timer = setTimeout(() => {
clearTimeout(timer)
const message = reason || `Promise timed out after ${maxWait} milliseconds: ${reason}`
console.warn(message)
resolve()
}, maxWait * 1000)
})

// Returns a race between our timeout and the passed in promise
return Promise.race([promise, timeout]).then((result) => {
clearTimeout(timer)
return result
})
}
10 changes: 9 additions & 1 deletion packages/snjs/mocha/lib/VaultsContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ export class VaultsContext extends AppContext {

await this.sync()

await this.awaitPromiseOrThrow(promise, undefined, 'Waiting for notifications to process timed out')
await this.awaitPromiseOrDoNothing(
promise,
0.25,
'Waiting for notifications timed out. Notifications might have been processed in previous sync.'
)

if (this.notifications['handleReceivedNotifications'].restore) {
this.notifications['handleReceivedNotifications'].restore()
}
}

async syncAndAwaitMessageProcessing() {
Expand Down
8 changes: 7 additions & 1 deletion packages/snjs/mocha/vaults/quota.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ describe('shared vault quota', function () {
const buffer = new Uint8Array(await response.arrayBuffer())
await Files.uploadFile(context.files, buffer, 'my-file', 'md', 1000, sharedVault)

await context.syncAndAwaitNotificationsProcessing()

const updatedVault = context.vaults.getVault({ keySystemIdentifier: sharedVault.systemIdentifier })
expect(updatedVault.sharing.fileBytesUsed).to.equal(1374)

Expand Down Expand Up @@ -101,7 +103,8 @@ describe('shared vault quota', function () {

await Files.uploadFile(contactContext.files, buffer, 'my-file', 'md', 1000, sharedVault)

await context.sync()
await context.syncAndAwaitNotificationsProcessing()
await contactContext.syncAndAwaitNotificationsProcessing()

const updatedVault = context.vaults.getVault({ keySystemIdentifier: sharedVault.systemIdentifier })
expect(updatedVault.sharing.fileBytesUsed).to.equal(1374)
Expand Down Expand Up @@ -164,6 +167,9 @@ describe('shared vault quota', function () {

const uploadedFile = await Files.uploadFile(contactContext.files, buffer, 'my-file', 'md', 1000, secondVault)

await context.syncAndAwaitNotificationsProcessing()
await contactContext.syncAndAwaitNotificationsProcessing()

let updatedSharedVault = context.vaults.getVault({ keySystemIdentifier: sharedVault.systemIdentifier })
expect(updatedSharedVault.sharing.fileBytesUsed).to.equal(0)

Expand Down

0 comments on commit 749cd9f

Please sign in to comment.