Skip to content

Commit

Permalink
test(unit): egress handler
Browse files Browse the repository at this point in the history
  • Loading branch information
fforbeck committed Oct 29, 2024
1 parent 96551c1 commit f21680e
Show file tree
Hide file tree
Showing 3 changed files with 407 additions and 72 deletions.
9 changes: 6 additions & 3 deletions src/handlers/egress-tracker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CID } from 'multiformats'
import { Accounting } from '../services/accounting.js'

/**
Expand Down Expand Up @@ -63,7 +62,7 @@ export function withEgressHandler (handler) {
*
* @param {import('@web3-storage/gateway-lib/middleware').Context} ctx - The context object.
* @param {AccountingService} accounting - The accounting service instance to record egress.
* @param {CID} dataCid - The CID of the served content.
* @param {import('@web3-storage/gateway-lib/handlers').CID} dataCid - The CID of the served content.
* @returns {TransformStream} - The created TransformStream.
*/
function createEgressPassThroughStream (ctx, accounting, dataCid) {
Expand All @@ -90,6 +89,7 @@ function createEgressPassThroughStream (ctx, accounting, dataCid) {
controller.enqueue(chunk)
totalBytesServed += chunk.byteLength
} catch (error) {
console.error('Error while counting egress bytes:', error)
controller.error(error)
}
},
Expand All @@ -105,8 +105,11 @@ function createEgressPassThroughStream (ctx, accounting, dataCid) {
async flush (controller) {
try {
// Non-blocking call to the accounting service to record egress
ctx.waitUntil(accounting.record(dataCid, totalBytesServed, new Date().toISOString()))
if (totalBytesServed > 0) {
ctx.waitUntil(accounting.record(dataCid, totalBytesServed, new Date().toISOString()))
}
} catch (error) {
console.error('Error while recording egress:', error)
controller.error(error)
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/integration/egress-tracker.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/* eslint-disable no-unused-expressions
---
`no-unused-expressions` doesn't understand that several of Chai's assertions
are implemented as getters rather than explicit function calls; it thinks
the assertions are unused expressions. */
import { expect } from 'chai'
import { describe, it } from 'mocha'
import fetch from 'node-fetch'
Expand Down
Loading

0 comments on commit f21680e

Please sign in to comment.