-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
187 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { randomDIDMailto } from './did.js' | ||
import { randomLink } from './dag.js' | ||
|
||
/** | ||
* @param {Partial<import('../../lib/api').EgressTrafficData>} [base] | ||
* @returns {Promise<import('../../lib/api').EgressTrafficData>} | ||
*/ | ||
export const randomEgressEvent = async (base = {}) => ({ | ||
customer: await randomDIDMailto(), | ||
resource: randomLink(), | ||
bytes: BigInt(Math.floor(Math.random() * 1000000)), | ||
servedAt: new Date(), | ||
...base | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import * as EgressTrafficSuite from './lib/egress-traffic.js' | ||
import { bindTestContext, createEgressTrafficTestContext } from './helpers/context.js' | ||
|
||
export const test = bindTestContext(EgressTrafficSuite.test, createEgressTrafficTestContext) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { randomEgressEvent } from '../helpers/egress.js' | ||
|
||
/** @type {import('./api').TestSuite<import('./api').EgressTrafficTestContext>} */ | ||
export const test = { | ||
'should process egress events': async (/** @type {import('entail').assert} */ assert, ctx) => { | ||
const maxEvents = 100 | ||
const events = await Promise.all( | ||
Array.from({ length: maxEvents }, () => randomEgressEvent()) | ||
) | ||
|
||
// 1. Add egress events to the queue to simulate events from the Freeway worker | ||
for (const e of events) { | ||
console.log(`Adding egress event to the queue: CustomerId: ${e.customer}, ResourceId: ${e.resource}, ServedAt: ${e.servedAt.toISOString()}`) | ||
await ctx.egressTrafficQueue.add(e) | ||
} | ||
|
||
|
||
// 2. Create a SQS event batch | ||
// @type {import('aws-lambda').SQSEvent} | ||
const sqsEventBatch = { | ||
Records: events.map(e => ({ | ||
// @type {import('aws-lambda').SQSRecord} | ||
body: JSON.stringify(e), | ||
messageId: Math.random().toString(), | ||
receiptHandle: Math.random().toString(), | ||
awsRegion: ctx.region, | ||
eventSource: 'aws:sqs', | ||
eventSourceARN: `arn:aws:sqs:${ctx.region}:${ctx.accountId}:${ctx.egressTrafficQueueUrl}`, | ||
awsAccountId: ctx.accountId, | ||
md5OfBody: '', | ||
md5OfMessageAttributes: '', | ||
attributes: { | ||
ApproximateReceiveCount: '1', | ||
SentTimestamp: e.servedAt.getTime().toString(), | ||
SenderId: ctx.accountId, | ||
ApproximateFirstReceiveTimestamp: e.servedAt.getTime().toString(), | ||
}, | ||
messageAttributes: {}, | ||
})) | ||
} | ||
|
||
// 3. Process the SQS event to trigger the handler | ||
await ctx.egressTrafficHandler(sqsEventBatch, ctx, (err, res) => { | ||
if (err) { | ||
assert.fail(err) | ||
} | ||
assert.ok(res) | ||
assert.equal(res.statusCode, 200) | ||
assert.equal(res.body, 'Egress events processed successfully') | ||
}) | ||
|
||
// 4. Ensure we got a billing meter event or each egress event in the queue | ||
// query stripe for the billing meter events | ||
// const billingMeterEvents = await ctx.stripe.billing.meterEvents.list({ | ||
// limit: maxEvents, | ||
// }) | ||
// assert.equal(billingMeterEvents.data.length, events.length) | ||
// FIXME (fforbeck): how to check we send the events to stripe? | ||
// we need to mock the stripe client | ||
// and check that the correct events are sent to stripe | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters