-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathClientManager.test.js
More file actions
90 lines (73 loc) · 3.21 KB
/
Copy pathClientManager.test.js
File metadata and controls
90 lines (73 loc) · 3.21 KB
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
import assert from 'assert';
import net from 'net';
import ClientManager from './ClientManager';
describe('ClientManager', () => {
it('should construct with no tunnels', () => {
const manager = new ClientManager();
assert.equal(manager.stats.tunnels, 0);
});
it('should create a new client with random id', async () => {
const manager = new ClientManager();
const client = await manager.newClient(null, null);
assert(manager.hasClient(client.id));
manager.removeClient(client.id);
});
it('should create a new client with id', async () => {
const manager = new ClientManager();
const client = await manager.newClient('foobar', null);
assert(manager.hasClient('foobar'));
manager.removeClient('foobar');
});
it('should create a new client with random id if previous exists', async () => {
const manager = new ClientManager();
const clientA = await manager.newClient('foobar', null);
const clientB = await manager.newClient('foobar', null);
assert(clientA.id, 'foobar');
assert(manager.hasClient(clientB.id));
assert(clientB.id != clientA.id);
manager.removeClient(clientB.id);
manager.removeClient('foobar');
});
it('should remove client once it goes offline', async () => {
const manager = new ClientManager();
const client = await manager.newClient('foobar', null);
const socket = await new Promise((resolve) => {
const netClient = net.createConnection({ port: client.port }, () => {
resolve(netClient);
});
});
const closePromise = new Promise(resolve => socket.once('close', resolve));
socket.end();
await closePromise;
// should still have client - grace period has not expired
assert(manager.hasClient('foobar'));
// wait past grace period (1s)
await new Promise(resolve => setTimeout(resolve, 1500));
assert(!manager.hasClient('foobar'));
}).timeout(5000);
it('should remove correct client once it goes offline', async () => {
const manager = new ClientManager();
const clientFoo = await manager.newClient('foo', null);
const clientBar = await manager.newClient('bar', null);
const socket = await new Promise((resolve) => {
const netClient = net.createConnection({ port: clientFoo.port }, () => {
resolve(netClient);
});
});
await new Promise(resolve => setTimeout(resolve, 1500));
// foo should still be ok
assert(manager.hasClient('foo'));
// clientBar shound be removed - nothing connected to it
assert(!manager.hasClient('bar'));
manager.removeClient('foo');
socket.end();
}).timeout(5000);
it('should remove clients if they do not connect within 5 seconds', async () => {
const manager = new ClientManager();
const clientFoo = await manager.newClient('foo', null);
assert(manager.hasClient('foo'));
// wait past grace period (1s)
await new Promise(resolve => setTimeout(resolve, 1500));
assert(!manager.hasClient('foo'));
}).timeout(5000);
});