-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlb.test.ts
64 lines (48 loc) · 1.53 KB
/
lb.test.ts
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
57
58
59
60
61
62
63
64
import test from 'ava';
import * as Brakes from 'brakes';
import * as _ from 'lodash';
import { StaticResolver } from '../src/naming';
import { RoundRobinBalancer } from '../src/lb';
import { GRPCHelperClient } from '../src/common';
import { ClientFactory } from '../src';
class ClientCreator implements ClientFactory {
createClient(addr: string) {
return <GRPCHelperClient>{
address: addr,
connected: true,
brake: new Brakes(),
};
}
closeClient() {
}
}
async function testlb(t, lb, addrs) {
const resAddrs = _.uniq(_.map(_.times(6, () => lb.get()), 'address'));
t.log(resAddrs);
t.deepEqual(resAddrs.sort(), addrs);
}
test('#lb with static resolver', async t => {
const resolver = new StaticResolver();
const lb = new RoundRobinBalancer(resolver, new ClientCreator());
lb.start(['localhost:1111', 'localhost:2222', 'localhost:3333'].join(','));
await lb.waitForReady();
testlb(t, lb, ['localhost:1111', 'localhost:2222', 'localhost:3333']);
lb.down('localhost:1111');
testlb(t, lb, ['localhost:2222', 'localhost:3333']);
const down = lb.up('localhost:1111');
testlb(t, lb, ['localhost:1111', 'localhost:2222', 'localhost:3333']);
down();
testlb(t, lb, ['localhost:2222', 'localhost:3333']);
lb.close();
});
test('#lb no client available', async t => {
const resolver = new StaticResolver();
const lb = new RoundRobinBalancer(resolver, new ClientCreator());
lb.start('');
try {
lb.get();
} catch (e) {
t.is(e.message, 'no client available');
}
lb.close();
});