2018-01-23 15:54:39 +01:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const test = require('tape')
|
|
|
|
|
|
|
|
const createThrottledClient = require('../throttle')
|
|
|
|
const vbbProfile = require('../p/vbb')
|
|
|
|
|
2018-07-19 21:55:54 +02:00
|
|
|
const userAgent = 'public-transport/hafas-client:test'
|
2018-01-23 15:54:39 +01:00
|
|
|
const spichernstr = '900000042101'
|
|
|
|
|
|
|
|
test('throttle works', (t) => {
|
|
|
|
let calls = 0
|
|
|
|
const transformReqBody = (body) => {
|
|
|
|
calls++
|
|
|
|
return vbbProfile.transformReqBody(body)
|
|
|
|
}
|
|
|
|
const mockProfile = Object.assign({}, vbbProfile, {transformReqBody})
|
|
|
|
|
2018-07-19 21:55:54 +02:00
|
|
|
const client = createThrottledClient(mockProfile, userAgent, 2, 1000)
|
2018-01-23 15:54:39 +01:00
|
|
|
for (let i = 0; i < 10; i++) client.departures(spichernstr, {duration: 1})
|
|
|
|
|
|
|
|
t.plan(3)
|
|
|
|
setTimeout(() => t.equal(calls, 2), 500)
|
|
|
|
setTimeout(() => t.equal(calls, 4), 1500)
|
|
|
|
setTimeout(() => t.equal(calls, 6), 2500)
|
|
|
|
})
|
|
|
|
|
|
|
|
// todo
|