2022-04-08 16:35:11 +02:00
|
|
|
import { Multiaddr } from '@multiformats/multiaddr'
|
|
|
|
import { expect } from 'aegir/utils/chai.js'
|
|
|
|
import { Status } from '../../../src/circuit/v2/pb/index.js'
|
|
|
|
import { ReservationStore } from '../../../src/circuit/v2/reservation-store.js'
|
|
|
|
import { createPeerId } from '../../utils/creators/peer.js'
|
2022-03-21 19:07:19 +01:00
|
|
|
|
|
|
|
/* eslint-env mocha */
|
|
|
|
|
|
|
|
describe('Circuit v2 - reservation store', function () {
|
|
|
|
it('should add reservation', async function () {
|
|
|
|
const store = new ReservationStore(2)
|
2022-04-08 16:35:11 +02:00
|
|
|
const peer = await createPeerId()
|
2022-03-21 19:07:19 +01:00
|
|
|
const result = await store.reserve(peer, new Multiaddr())
|
|
|
|
expect(result.status).to.equal(Status.OK)
|
|
|
|
expect(result.expire).to.not.be.undefined()
|
|
|
|
expect(await store.hasReservation(peer)).to.be.true()
|
|
|
|
})
|
|
|
|
it('should add reservation if peer already has reservation', async function () {
|
|
|
|
const store = new ReservationStore(1)
|
2022-04-08 16:35:11 +02:00
|
|
|
const peer = await createPeerId()
|
2022-03-21 19:07:19 +01:00
|
|
|
await store.reserve(peer, new Multiaddr())
|
|
|
|
const result = await store.reserve(peer, new Multiaddr())
|
|
|
|
expect(result.status).to.equal(Status.OK)
|
|
|
|
expect(result.expire).to.not.be.undefined()
|
|
|
|
expect(await store.hasReservation(peer)).to.be.true()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should fail to add reservation on exceeding limit', async function () {
|
|
|
|
const store = new ReservationStore(0)
|
2022-04-08 16:35:11 +02:00
|
|
|
const peer = await createPeerId()
|
2022-03-21 19:07:19 +01:00
|
|
|
const result = await store.reserve(peer, new Multiaddr())
|
|
|
|
expect(result.status).to.equal(Status.RESERVATION_REFUSED)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should remove reservation', async function () {
|
|
|
|
const store = new ReservationStore(10)
|
2022-04-08 16:35:11 +02:00
|
|
|
const peer = await createPeerId()
|
2022-03-21 19:07:19 +01:00
|
|
|
const result = await store.reserve(peer, new Multiaddr())
|
|
|
|
expect(result.status).to.equal(Status.OK)
|
|
|
|
expect(await store.hasReservation(peer)).to.be.true()
|
|
|
|
await store.removeReservation(peer)
|
|
|
|
expect(await store.hasReservation(peer)).to.be.false()
|
|
|
|
await store.removeReservation(peer)
|
|
|
|
})
|
|
|
|
})
|