-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
51 lines (34 loc) · 1.81 KB
/
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
import * as net from 'net';
import * as stream from 'stream';
import { expect } from 'chai';
import { makeDestroyable } from './index';
const delay = (durationMs: number) => new Promise((resolve) => setTimeout(resolve, durationMs));
describe("Destroyable server", () => {
it("can shut down a server even with active connections", async () => {
const rawServer = new net.Server((_socket) => { /* Do nothing, keep socket open forever */ });
const server = makeDestroyable(rawServer);
await new Promise((resolve) => server.listen(resolve));
// Connect to the server, opening a socket that we never close
net.connect({ host: '127.0.0.1', port: (server.address() as any).port });
await delay(100);
server.destroy(); // Without this or with just `.close()`, the process doesn't exit
});
it("can shut down a server with duplicate connections", async () => {
// This can happen when using a server (like Mockttp) that handles its own tunnelled connections,
// which means it may have multiple connections on the same incoming ip:port.
const rawServer = new net.Server((_socket) => { /* Do nothing, keep socket open forever */ });
const server = makeDestroyable(rawServer);
const stream1 = new stream.PassThrough();
const stream2 = new stream.PassThrough();
[stream1, stream2].forEach((stream) => {
Object.assign(stream, { remoteAddress: '1.2.3.4', remotePort: 1234 });
});
server.emit('connection', stream1);
server.emit('connection', stream2);
expect(stream1.destroyed).to.equal(false);
expect(stream2.destroyed).to.equal(false);
server.destroy();
// expect(stream1.destroyed).to.equal(true);
expect(stream2.destroyed).to.equal(true);
});
});