-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
75 lines (63 loc) · 1.91 KB
/
test.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const http = require('http');
const net = require('net');
const delay = (t, v) => {
return new Promise((resolve) => {
setTimeout(resolve.bind(null, v), t);
});
};
const log = (message) => {
console.log(`${(new Date()).toISOString()} - ${message}`);
};
const address = '127.0.0.1';
const port = 8000;
const pingAfterDelay = async (socket, delayInMS) => {
await(delay(delayInMS));
socket.write('GET / HTTP/1.1\r\n');
socket.write(`Host: ${address}\r\n\r\n`);
log('HTTP Request was Sent');
}
const runTestWithIntialDelay = async (delayInMS) => {
log(`Beginning Socket Timeout Test (${delayInMS / 1000}s delay)`);
return new Promise(async (resolve, reject) => {
try {
const socket = new net.Socket();
socket.setKeepAlive(false);
socket.setTimeout(delayInMS * 2);
socket.connect(port, address, () => {
pingAfterDelay(socket, delayInMS);
});
socket.on('timeout', () => {
log('Closing socket via the client');
socket.end();
resolve();
});
socket.on('data', (data) => {
console.log(`\n\n${data}\n\n`);
log('An HTTP Response was Received');
});
socket.on('error', (e) => {
log('An error was received' + e);
reject(e);
});
socket.on('close', () => {
log('Server Closed Connection');
resolve();
});
} catch (err) {
log('Unexpected Exception');
log(err);
reject(err);
}
})
};
const execute = async () => {
try {
await runTestWithIntialDelay(39 * 1000);
await runTestWithIntialDelay(41 * 1000);
await delay(5000);
process.exit(0);
} catch (e) {
process.exit(1);
}
}
execute();