Skip to content

Commit

Permalink
Add simple test
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 authored and ScottyPoi committed Dec 17, 2024
1 parent 9285866 commit d245527
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions packages/portalnetwork/test/wire/utp/utp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import debug from 'debug'
import { assert, describe, it } from 'vitest'

import {
FindContentReadRequest,
NetworkId,
Packet,
PacketType,
PortalNetwork,
PortalNetworkUTP,
ReadSocket,
RequestCode,
UtpSocketType,
encodeWithVariantPrefix,
Expand All @@ -19,6 +21,7 @@ import { ContentWriter } from '../../../src/wire/utp/Socket/ContentWriter.js'
import { WriteSocket } from '../../../src/wire/utp/Socket/WriteSocket.js'

import { ENR } from '@chainsafe/enr'
import { RequestManager } from '../../../src/wire/utp/PortalNetworkUtp/requestManager.js'

const sampleSize = 50000
const enr = ENR.decodeTxt(
Expand Down Expand Up @@ -211,3 +214,76 @@ describe('PortalNetworkUTP test', async () => {
)
})
})
describe('RequestManager', () => {
it('should order packets correctly', async () => {
const client = await PortalNetwork.create({
bindAddress: enr.getLocationMultiaddr('udp')!.nodeAddress().address,
})
const mgr = new RequestManager(enr.nodeId, debug('test'))
const req1 = new FindContentReadRequest({
network: client.network()['0x500b']!,
socket: new ReadSocket({
utp: client.uTP,
networkId: NetworkId.HistoryNetwork,
enr,
connectionId: 0,
sndId: 0,
rcvId: 0,
seqNr: 0,
ackNr: 0,
type: UtpSocketType.READ,
logger: debug('test'),
}),
connectionId: 0,
requestManager: mgr,
requestCode: RequestCode.FINDCONTENT_READ,
contentKeys: [],
content: Buffer.from('test'),
})
const packet1 = Packet.fromOpts({
header: {
seqNr: 0,
pType: PacketType.ST_DATA,
version: 1,
connectionId: 0,
extension: 0,
timestampMicroseconds: 0,
timestampDifferenceMicroseconds: 0,
wndSize: 0,
ackNr: 123,
},
})
const packet2 = Packet.fromOpts({
header: {
seqNr: 0,
pType: PacketType.ST_SYN,
version: 1,
connectionId: 0,
extension: 0,
timestampMicroseconds: 0,
timestampDifferenceMicroseconds: 0,
wndSize: 0,
ackNr: 121,
},
})
const packet3 = Packet.fromOpts({
header: {
seqNr: 0,
pType: PacketType.ST_DATA,
version: 1,
connectionId: 0,
extension: 0,
timestampMicroseconds: 0,
timestampDifferenceMicroseconds: 0,
wndSize: 0,
ackNr: 125,
},
})
void mgr.handleNewRequest(req1.connectionId, req1)
mgr.masterPacketQueue.push(packet2)
mgr.currentPacket = packet3
void mgr.handlePacket(packet1.encode())
assert.equal(mgr.masterPacketQueue.length, 2)
assert.deepEqual(mgr.masterPacketQueue[0], packet2)
})
})

0 comments on commit d245527

Please sign in to comment.