Skip to content

Commit

Permalink
fix: add retry logic for egress integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
fforbeck committed Nov 12, 2024
1 parent 94f3a4e commit eb657c6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
4 changes: 2 additions & 2 deletions billing/test/helpers/egress.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const randomEgressEvent = async (customer) => ({
customer: customer.customer,
resource: randomLink(),
bytes: Math.floor(Math.random() * 1000000),
// Random timestamp within the last 1 hour
servedAt: new Date(Date.now() - Math.floor(Math.random() * 60 * 60 * 1000)),
// Random timestamp within the last 3 minutes
servedAt: new Date(Date.now() - Math.floor(Math.random() * 3 * 60 * 1000)),
cause: randomLink()
})
51 changes: 36 additions & 15 deletions billing/test/lib/egress-traffic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { encodeStr } from '../../data/egress.js'
import { randomCustomer } from '../helpers/customer.js'
import { randomDIDMailto } from '../helpers/did.js'
import { randomEgressEvent } from '../helpers/egress.js'
import * as DidMailto from '@web3-storage/did-mailto'

Expand All @@ -14,7 +13,7 @@ export const test = {
let stripeCustomerId;
try {
// 0. Create a test customer email, add it to stripe and to the customer store
const didMailto = randomDIDMailto()
const didMailto = `did:mailto:storacha.network:egress-billing-test`
const email = DidMailto.toEmail(/** @type {`did:mailto:${string}:${string}`} */(didMailto))
const stripeCustomer = await ctx.stripe.customers.create({ email })
assert.ok(stripeCustomer.id, 'Error adding customer to stripe')
Expand All @@ -29,7 +28,7 @@ export const test = {
assert.ok(!error, 'Error adding customer')

// 1. Add egress events to the queue to simulate egress traffic from the Freeway worker
const maxEvents = 10
const maxEvents = 5
/** @type {import('../../lib/api').EgressTrafficData[]} */
const events = await Promise.all(
Array.from(
Expand Down Expand Up @@ -85,25 +84,47 @@ export const test = {
})

// 4. Check if the aggregated meter event exists and has a value greater than 0
const aggregatedMeterEvent = await ctx.stripe.billing.meters.listEventSummaries(
ctx.billingMeterId,
{
customer: stripeCustomerId,
start_time: Math.floor(events[0].servedAt.getTime() / 1000),
end_time: Math.floor(Date.now() / 1000),
const maxRetries = 5
const delay = 10000 // 10 seconds

// Convert to the start of the hour
const startTime = Math.floor(events[0].servedAt.getTime() / 3600000) * 3600
// Convert to the start of the next hour
const endTime = Math.floor((Date.now() + 3600000) / 3600000) * 3600
console.log(`Checking for aggregated meter event for customer ${stripeCustomerId}, startTime: ${startTime}, endTime: ${endTime} ...`)
let aggregatedMeterEvent
for (let attempt = 0; attempt < maxRetries; attempt++) {
console.log(`Attempt #${attempt+1}`)
aggregatedMeterEvent = await ctx.stripe.billing.meters.listEventSummaries(
ctx.billingMeterId,
{
customer: stripeCustomerId,
start_time: startTime,
end_time: endTime,
value_grouping_window: 'hour',
}
)

if (aggregatedMeterEvent.data && aggregatedMeterEvent.data.length > 0) {
break
}
)

if (attempt < maxRetries - 1) {
await new Promise(resolve => setTimeout(resolve, delay))
}
}
assert.ok(aggregatedMeterEvent, 'No aggregated meter event found')
assert.ok(aggregatedMeterEvent.data, 'No aggregated meter event found')
assert.equal(aggregatedMeterEvent.data.length, 1, 'Expected 1 aggregated meter event')
// We can't verify the total bytes served because the meter events are not immediately available in stripe
// and the test would fail intermittently
assert.ok(aggregatedMeterEvent.data[0].aggregated_value > 0, 'Aggregated value is 0')
} finally {
if (stripeCustomerId) {
// 5. Delete the test customer from stripe
const deletedCustomer = await ctx.stripe.customers.del(stripeCustomerId);
assert.ok(deletedCustomer.deleted, 'Error deleting customer from stripe')
}
// if (stripeCustomerId) {
// // 5. Delete the test customer from stripe
// const deletedCustomer = await ctx.stripe.customers.del(stripeCustomerId);
// assert.ok(deletedCustomer.deleted, 'Error deleting customer from stripe')
// }
}
}
}
1 change: 1 addition & 0 deletions billing/utils/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export async function recordBillingMeterEvent(stripe, billingMeterEventName, egr

// Identifier is only set if the event was successfully created
if (meterEvent.identifier) {
console.log(`Meter event created: ${meterEvent.identifier}`)
return { ok: { meterEvent } }
}
return {
Expand Down

0 comments on commit eb657c6

Please sign in to comment.