38 lines
888 B
JavaScript
38 lines
888 B
JavaScript
require('bare-process/global')
|
|
const test = require('brittle')
|
|
const { HyperP2PSlaBudget, PROTOCOL } = require('../index.js')
|
|
|
|
test('exports', (t) => {
|
|
t.ok(HyperP2PSlaBudget)
|
|
t.is(PROTOCOL, 'sla-budget/v1')
|
|
})
|
|
|
|
test('allocate consume remaining', async (t) => {
|
|
const m = new HyperP2PSlaBudget()
|
|
m.allocate('api', 10)
|
|
t.ok(m.consume('api', 3))
|
|
t.is(m.remaining('api'), 7)
|
|
await m.close()
|
|
})
|
|
|
|
test('validation', async (t) => {
|
|
const m = new HyperP2PSlaBudget()
|
|
try { m.allocate('', 1) } catch (e) { t.ok(e) }
|
|
await m.close()
|
|
})
|
|
|
|
test('reject over consume', async (t) => {
|
|
const m = new HyperP2PSlaBudget()
|
|
m.allocate('x', 1)
|
|
t.not(m.consume('x', 5))
|
|
await m.close()
|
|
})
|
|
|
|
test('getStats', async (t) => {
|
|
const m = new HyperP2PSlaBudget()
|
|
m.allocate('s', 2)
|
|
t.is(m.getStats().protocol, 'sla-budget/v1')
|
|
t.is(m.getStats().allocated, 1)
|
|
await m.close()
|
|
})
|