Skip to content

Commit

Permalink
test: added first round of amount and packet count increments
Browse files Browse the repository at this point in the history
  • Loading branch information
JoblersTune committed Jul 25, 2024
1 parent 63f5462 commit d4bb021
Showing 1 changed file with 138 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import assert from 'assert'
import assert from 'assert'
import { Readable } from 'stream'
import {
serializeIlpPrepare,
Expand All @@ -11,22 +11,20 @@ import { createContext, MockIncomingMessageOptions } from '../../utils'
import {
createIlpPacketMiddleware,
IlpResponse,
ZeroCopyIlpPrepare
// OutgoingAccount
ZeroCopyIlpPrepare,
OutgoingAccount
} from '../../'
import {
IlpPrepareFactory,
IlpFulfillFactory,
IlpRejectFactory,
// IncomingAccountFactory,
IncomingAccountFactory,
RafikiServicesFactory
} from '../../factories'
import { HttpContext } from '../../rafiki'

describe('ILP Packet Middleware', () => {
test('sets up request and response', async () => {
// const incomingAccount = IncomingAccountFactory.build({ id: 'alice' })
// assert.ok(incomingAccount.id)
const services = RafikiServicesFactory.build({})

const options: MockIncomingMessageOptions = {
Expand All @@ -36,20 +34,6 @@ describe('ILP Packet Middleware', () => {
const ctx = createContext<unknown, HttpContext>({
req: options,
services: { ...services, telemetry: undefined }
// accounts: {
// incoming: incomingAccount,
// outgoing: { asset: { code: 'USD', scale: 2 } } as OutgoingAccount
// },
// state: {
// unfulfillable: false,
// incomingAccount: {
// quote: 'exists',
// asset: {
// code: 'USD',
// scale: 2
// }
// }
// }
})

const prepare = IlpPrepareFactory.build()
Expand Down Expand Up @@ -202,3 +186,137 @@ describe('IlpResponse', () => {
expect(response.rawFulfill).toBeUndefined()
})
})

describe('Telemetry Middleware', function () {
let ctx: HttpContext
let services: ReturnType<typeof RafikiServicesFactory.build>
let incomingAccount: ReturnType<typeof IncomingAccountFactory.build>

beforeEach(() => {
services = RafikiServicesFactory.build({})
incomingAccount = IncomingAccountFactory.build({ id: 'alice' })
assert.ok(incomingAccount.id)
const options: MockIncomingMessageOptions = {
headers: { 'content-type': 'application/octet-stream' }
}
ctx = createContext<unknown, HttpContext>({
req: options,
services,
accounts: {
incoming: incomingAccount,
outgoing: { asset: { code: 'USD', scale: 2 } } as OutgoingAccount
},
state: {
unfulfillable: false,
incomingAccount: {
asset: {
code: 'USD',
scale: 2
}
}
}
})
})

afterEach(() => {
jest.resetAllMocks()
})

it('should not gather telemetry if telemetry is not enabled (service is undefined)', async () => {
ctx.services= { ...services, telemetry: undefined }
const incrementCounterWithTransactionAmountSpy = jest
.spyOn(services.telemetry, 'incrementCounterWithTransactionAmount')
.mockImplementation(() => Promise.resolve())

const incrementCounterSpy = jest
.spyOn(services.telemetry, 'incrementCounter')
.mockImplementation(() => Promise.resolve())

const prepare = IlpPrepareFactory.build()
const getRawBody = async (_req: Readable) => serializeIlpPrepare(prepare)
const rawReply = serializeIlpFulfill(IlpFulfillFactory.build())
const ilpHandler = jest.fn().mockImplementation((ctx, next) => {
ctx.response.rawReply = rawReply
next()
})
const next = jest.fn()
const middleware = createIlpPacketMiddleware(ilpHandler, { getRawBody })

await expect(middleware(ctx, next)).resolves.toBeUndefined()
expect(incrementCounterWithTransactionAmountSpy).not.toHaveBeenCalled()
expect(incrementCounterSpy).not.toHaveBeenCalled()
})

it('should not gather telemetry if request.prepare.amount is 0', async () => {
const incrementCounterWithTransactionAmountSpy = jest
.spyOn(services.telemetry, 'incrementCounterWithTransactionAmount')
.mockImplementation(() => Promise.resolve())

const incrementCounterSpy = jest
.spyOn(services.telemetry, 'incrementCounter')
.mockImplementation(() => Promise.resolve())

const prepare = IlpPrepareFactory.build()
prepare.amount = '0'
const getRawBody = async (_req: Readable) => serializeIlpPrepare(prepare)
const rawReply = serializeIlpReject(IlpRejectFactory.build())
const ilpHandler = jest.fn().mockImplementation((ctx, next) => {
ctx.response.rawReply = rawReply
next()
})
const next = jest.fn()
const middleware = createIlpPacketMiddleware(ilpHandler, { getRawBody })

await expect(middleware(ctx, next)).resolves.toBeUndefined()
expect(incrementCounterWithTransactionAmountSpy).not.toHaveBeenCalled()
expect(incrementCounterSpy).not.toHaveBeenCalled()
})

it('should gather amount and packet count telemetry if response.fulfill is defined', async () => {
const incrementCounterWithTransactionAmountSpy = jest
.spyOn(services.telemetry, 'incrementCounterWithTransactionAmount')
.mockImplementation(() => Promise.resolve())

const incrementCounterSpy = jest
.spyOn(services.telemetry, 'incrementCounter')
.mockImplementation(() => Promise.resolve())

const prepare = IlpPrepareFactory.build()
const getRawBody = async (_req: Readable) => serializeIlpPrepare(prepare)
const rawReply = serializeIlpFulfill(IlpFulfillFactory.build())
const ilpHandler = jest.fn().mockImplementation((ctx, next) => {
ctx.response.rawReply = rawReply
next()
})
const next = jest.fn()
const middleware = createIlpPacketMiddleware(ilpHandler, { getRawBody })

await expect(middleware(ctx, next)).resolves.toBeUndefined()
expect(incrementCounterWithTransactionAmountSpy).toHaveBeenCalledTimes(1)
expect(incrementCounterSpy).toHaveBeenCalledTimes(2)
})

it('should only gather packet count telemetry if response.fulfill undefined', async () => {
const incrementCounterWithTransactionAmountSpy = jest
.spyOn(services.telemetry, 'incrementCounterWithTransactionAmount')
.mockImplementation(() => Promise.resolve())

const incrementCounterSpy = jest
.spyOn(services.telemetry, 'incrementCounter')
.mockImplementation(() => Promise.resolve())

const prepare = IlpPrepareFactory.build()
const getRawBody = async (_req: Readable) => serializeIlpPrepare(prepare)
const rawReply = serializeIlpReject(IlpRejectFactory.build())
const ilpHandler = jest.fn().mockImplementation((ctx, next) => {
ctx.response.rawReply = rawReply
next()
})
const next = jest.fn()
const middleware = createIlpPacketMiddleware(ilpHandler, { getRawBody })

await expect(middleware(ctx, next)).resolves.toBeUndefined()
expect(incrementCounterWithTransactionAmountSpy).not.toHaveBeenCalled()
expect(incrementCounterSpy).toHaveBeenCalledTimes(2)
})
})

0 comments on commit d4bb021

Please sign in to comment.