Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
stfsy committed Dec 8, 2024
1 parent 79fefd9 commit e69bc6e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default async (callback, { maxRetries = 10, delay = 250, backOff = 2 } = {}) => {
export default async (callback, { maxRetries = 10, delay = 250, backOff = 4 } = {}) => {
for (let i = 1; i <= maxRetries; i++) {
try {
await callback()
Expand All @@ -7,8 +7,9 @@ export default async (callback, { maxRetries = 10, delay = 250, backOff = 2 } =
throw e
}

console.log(`Caught error ${e}. Will retry ${maxRetries - i} more times.`)
await new Promise((resolve) => setTimeout(resolve, delay * i * backOff))
const time = delay * i * backOff
console.log(`Caught error ${e}. Will retry ${maxRetries - i} more times after ${time}ms.`)
await new Promise((resolve) => setTimeout(resolve, time))
}
}
}
27 changes: 8 additions & 19 deletions test-e2e/spec/hydration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { expect, test } from '@playwright/test'
import { randomUUID } from 'crypto'
import { Api, SubscriptionHooks, SubscriptionHydration, SubscriptionInfo, subscriptionStorage } from '../../lib/index.js'
import mongodbClient from '../../test/spec/mongodb-client.js'
import expectWithRetry from './expect-with-retry.js'
import catchAndRetry from './catch-and-retry.js'

const client = mongodbClient(27017)
const storage = subscriptionStorage({ client })
Expand All @@ -26,20 +26,9 @@ test.beforeAll(async () => {

test.beforeEach(async () => {
apiClientId = randomUUID()
console.log('Creating placeholder for', apiClientId)
await subscriptions.addSubscriptionPlaceholder([apiClientId])
})

test.afterAll(async () => {
const subscriptions = await api.listSubscriptions()
for (let i = 0; i < subscriptions.length; i++) {
const subscription = subscriptions[i]
await api.cancelSubscription(subscription)
}

return storage.close()
})

async function createNewSubscription(page, apiClientId) {
setTimeout(async () => {
const nextYear = new Date().getFullYear() + 1
Expand Down Expand Up @@ -110,7 +99,7 @@ test('hydrates the initial payment too', async ({ page }) => {
})

subscription = await storage.get([apiClientId])
await expectWithRetry(async () => {
await catchAndRetry(async () => {
// .. expect sub to be not active anymore after we reset all status and payments
let sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription)
expect(sub['52450']).toBeFalsy()
Expand Down Expand Up @@ -173,7 +162,7 @@ test('provides enough data for a hydrated status to look like a real one', async
}
})

await expectWithRetry(async () => {
await catchAndRetry(async () => {
subscription = await storage.get([apiClientId])
// .. expect sub to be not active anymore after we reset all status and payments
let sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription)
Expand Down Expand Up @@ -217,7 +206,7 @@ test('provides enough data for a hydrated payment to look like a real one', asyn
// .. now hydrate status again ..
await subscriptionHydration.hydrateSubscriptionCreated([apiClientId], { subscription_id: subscriptionId }, 'checkoutId')

await expectWithRetry(async () => {
await catchAndRetry(async () => {
// .. and expect subscription to be active again
const subInfo = await subscriptionInfo.getSubscriptionInfo([apiClientId])
const { payments_trail: paymentsTrail } = subInfo['52450']
Expand Down Expand Up @@ -267,7 +256,7 @@ test('throws if subscription was created for another client', async ({ page }) =
}
})

await expectWithRetry(async () => {
await catchAndRetry(async () => {
subscription = await storage.get([apiClientId])
// .. expect sub to be not active anymore after we reset all status and payments
let sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription)
Expand Down Expand Up @@ -296,7 +285,7 @@ test('does not hydrate if status created was already received', async ({ page })
let subscription = await storage.get([apiClientId])
const subscriptionId = subscription.status[0].subscription_id

await expectWithRetry(async () => {
await catchAndRetry(async () => {
// .. and check subscription is active to make sure setup was correct
let sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription)
expect(sub['52450']).toBeTruthy()
Expand All @@ -305,7 +294,7 @@ test('does not hydrate if status created was already received', async ({ page })
// .. now hydrate status again ..
await subscriptionHydration.hydrateSubscriptionCreated([apiClientId], { subscription_id: subscriptionId }, 'checkoutId')

await expectWithRetry(async () => {
await catchAndRetry(async () => {
// .. and expect subscription to be active again
subscription = await storage.get([apiClientId])
expect(subscription.status).toHaveLength(1)
Expand All @@ -332,7 +321,7 @@ test('hydrate a deleted subscription', async ({ page }) => {
await subscriptionHydration.hydrateSubscriptionCancelled([apiClientId], '52450')


await expectWithRetry(async () => {
await catchAndRetry(async () => {
subscription = await storage.get([apiClientId])
const sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription, new Date(new Date().getTime() + 1000 * 3600 * 24 * 35))
expect(sub['52450']).toBeFalsy()
Expand Down
37 changes: 13 additions & 24 deletions test-e2e/spec/subscription.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { expect as chaiExpect } from 'chai'
import { randomUUID } from 'crypto'
import { Api, SubscriptionHooks, SubscriptionInfo, subscriptionStorage } from '../../lib/index.js'
import mongodbClient from '../../test/spec/mongodb-client.js'
import retry from './expect-with-retry.js'
import catchAndRetry from './catch-and-retry.js'

const client = mongodbClient(27017)
const storage = subscriptionStorage({ client })
Expand All @@ -25,17 +25,6 @@ test.beforeEach(async () => {
await subscriptions.addSubscriptionPlaceholder([apiClientId])
})

test.afterAll(async () => {
const subscriptions = await api.listSubscriptions()
for (let i = 0; i < subscriptions.length; i++) {
const subscription = subscriptions[i]
await api.cancelSubscription(subscription)
}

return storage.close()
})


/**
*
* @param {import('@playwright/test').Page} page
Expand Down Expand Up @@ -121,7 +110,7 @@ test('test cancel via subscription info and subscription object', async ({ page
const success = await subscriptionInfo.cancelSubscription(subscription, '52450')
expect(success).toBeTruthy()

await retry(async () => {
await catchAndRetry(async () => {
// ... verify subscription still active today ...
subscription = await storage.get([apiClientId])
expect(subscription.status).toHaveLength(2)
Expand Down Expand Up @@ -154,7 +143,7 @@ test('test cancel via subscription info and client id array', async ({ page }) =
const success = await subscriptionInfo.cancelSubscription([apiClientId], '52450')
expect(success).toBeTruthy()

await retry(async () => {
await catchAndRetry(async () => {
// ... verify subscription still active today ...
subscription = await storage.get([apiClientId])
expect(subscription.status).toHaveLength(2)
Expand Down Expand Up @@ -187,7 +176,7 @@ test('test cancel via api', async ({ page }) => {
const success = await api.cancelSubscription(subscription.status[0])
expect(success).toBeTruthy()

await retry(async () => {
await catchAndRetry(async () => {
// ... verify subscription still active today ...
subscription = await storage.get([apiClientId])
expect(subscription.status).toHaveLength(2)
Expand Down Expand Up @@ -220,7 +209,7 @@ test('test update via subscription info and subscription object', async ({ page
const updated = await subscriptionInfo.updateSubscription(subscription, '52450', '52451')
expect(updated).toBeTruthy()

await retry(async () => {
await catchAndRetry(async () => {
// .. check new status and payments added ...
subscription = await storage.get([apiClientId])
expect(subscription).not.toBeFalsy()
Expand Down Expand Up @@ -250,7 +239,7 @@ test('test update via subscription info and api client id', async ({ page }) =>
const updated = await subscriptionInfo.updateSubscription([apiClientId], '52450', '52451')
expect(updated).toBeTruthy()

await retry(async () => {
await catchAndRetry(async () => {
// .. check new status and payments added ...
subscription = await storage.get([apiClientId])
expect(subscription).not.toBeFalsy()
Expand Down Expand Up @@ -279,7 +268,7 @@ test('test update via api', async ({ page }) => {
// update subscription plan via api ...
await api.updateSubscriptionPlan(subscription.status[0], '52451')

await retry(async () => {
await catchAndRetry(async () => {
// .. check new status and payments added ...
subscription = await storage.get([apiClientId])
expect(subscription).not.toBeFalsy()
Expand Down Expand Up @@ -309,7 +298,7 @@ test('test refund via api', async ({ page }) => {
const refunded = await api.refundFullPayment(subscription.payments[0])
expect(refunded).toBeTruthy()

await retry(async () => {
await catchAndRetry(async () => {
// .. check no new status and payments added ...
// .. because refunds need to be reviewed by paddle time
// .. it's their money we're playing with
Expand All @@ -324,7 +313,7 @@ test('test refund via api', async ({ page }) => {
expect(sub['52450']).toBeTruthy()
})

test('test create, update, and cancel via ui', async ({ page }) => {
test('test create, update payment, and cancel via ui', async ({ page }) => {
// create new subscription and ...
await createNewSubscription(page, apiClientId)

Expand All @@ -339,7 +328,7 @@ test('test create, update, and cancel via ui', async ({ page }) => {
// update payment method ...
await updatePaymentMethod(page, subscription)

await retry(async () => {
await catchAndRetry(async () => {
// .. check no new status or payments added ...
subscription = await storage.get([apiClientId])
expect(subscription).not.toBeFalsy()
Expand All @@ -354,7 +343,7 @@ test('test create, update, and cancel via ui', async ({ page }) => {
// cancel subscription ...
await cancelSubscription(page, subscription)

await retry(async () => {
await catchAndRetry(async () => {
// ... verify subscription still active today ...
subscription = await storage.get([apiClientId])
expect(subscription.status).toHaveLength(2)
Expand All @@ -373,11 +362,11 @@ test('test create, update, and cancel via ui', async ({ page }) => {

async function getAndValidateSubscription() {
let subscription
await retry(async () => {
await catchAndRetry(async () => {
subscription = await storage.get([apiClientId])
expect(subscription).not.toBeFalsy()
expect(subscription.status).toHaveLength(1)
expect(subscription.payments).toHaveLength(1)
})
}, { maxRetries: 15 })
return subscription
}

0 comments on commit e69bc6e

Please sign in to comment.