-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_base.js
56 lines (42 loc) · 1.67 KB
/
test_base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* ------------------------------------------------------------------------ */
const ccxt = require ('./ccxt')
, assert = require ('assert')
, log = require ('ololog')
, ansi = require ('ansicolor').nice;
/* ------------------------------------------------------------------------ */
describe ('ccxt base code', () => {
it ('sleep() is robust', async () => {
for (let i = 0; i < 30; i++) {
const before = Date.now ()
await ccxt.sleep (10)
const now = Date.now ()
assert ((now - before) >= 10) // not too fast
assert ((now - before) < 20) // but not too slow...
}
})
it ('rate limiting works', async () => {
const calls = []
const rateLimit = 100
const exchange = new ccxt.Exchange ({
id: 'mock',
rateLimit,
enableRateLimit: true,
async executeRestRequest (...args) { calls.push ({ when: Date.now (), path: args[0], args }) }
})
await exchange.fetch ('foo')
await exchange.fetch ('bar')
await exchange.fetch ('baz')
await Promise.all ([
exchange.fetch ('qux'),
exchange.fetch ('zap'),
exchange.fetch ('lol')
])
assert.deepEqual (calls.map (x => x.path), ['foo', 'bar', 'baz', 'qux', 'zap', 'lol'])
calls.reduce ((prevTime, call) => {
log ('delta T:', call.when - prevTime)
assert ((call.when - prevTime) >= rateLimit)
return call.when
}, 0)
})
})
/* ------------------------------------------------------------------------ */