-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlb_custom.test.ts
109 lines (93 loc) · 2.66 KB
/
lb_custom.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import test from 'ava';
import * as Brakes from 'brakes';
import * as _ from 'lodash';
import * as grpc from 'grpc';
import { Resolver, Address, Watcher, Update, UpdateOp } from '../src/naming';
import { RoundRobinBalancer } from '../src/lb';
import { GRPCHelperClient } from '../src/common';
import { EventEmitter } from 'events';
import { ClientFactory } from '../src';
class CustomResoler extends EventEmitter implements Resolver {
resolve(target: string): Watcher {
const watcher = new CustomWatcher(async function(): Promise<Address[]> {
return [];
});
this.on('next', () => watcher.emit('next'));
return watcher;
}
}
class CustomWatcher extends EventEmitter implements Watcher {
private resolveAddrs: () => Promise<Address[]>;
private updates: Update[] = [{
addr: 'localhost:1111',
op: UpdateOp.ADD,
}, {
addr: 'localhost:2222',
op: UpdateOp.ADD,
}, {
addr: 'localhost:3333',
op: UpdateOp.ADD,
}, {
addr: 'localhost:1111',
op: UpdateOp.DEL,
}, {
addr: 'localhost:4444',
op: UpdateOp.ADD,
}, {
addr: 'localhost:0',
op: 3,
}];
constructor(resolveAddrs: () => Promise<Address[]>) {
super();
this.resolveAddrs = resolveAddrs;
}
public async next(): Promise<Update[]> {
return new Promise<Update[]>(resolve => {
this.once('next', () => {
if (this.updates.length) {
resolve([this.updates.shift()]);
}
});
});
}
public async close(): Promise<void> {
}
}
class ClientCreator implements ClientFactory {
createClient(addr: string) {
return <GRPCHelperClient>{
address: addr,
connected: true,
brake: new Brakes(),
};
}
closeClient() {
}
}
async function testlb(t, resolver, lb, addrs) {
return new Promise(resolve => {
lb.once('change', clients => {
const resAddrs = _.map(clients, 'address');
t.log(resAddrs);
t.deepEqual(resAddrs.sort(), addrs);
resolve();
});
resolver.emit('next');
});
}
test('#lb with custom resolver', async t => {
const resolver = new CustomResoler();
const lb = new RoundRobinBalancer(resolver, new ClientCreator());
lb.start('test');
await testlb(t, resolver, lb, ['localhost:1111']);
await testlb(t, resolver, lb, ['localhost:1111', 'localhost:2222']);
await testlb(t, resolver, lb, ['localhost:1111', 'localhost:2222', 'localhost:3333']);
await testlb(t, resolver, lb, ['localhost:2222', 'localhost:3333']);
await testlb(t, resolver, lb, ['localhost:2222', 'localhost:3333', 'localhost:4444']);
await lb.waitForReady();
lb.on('error', (e) => {
t.is(e.message, 'unknwon update op, 3');
});
resolver.emit('next');
lb.close();
});