2016-11-22 16:13:44 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const initHooks = require('./init-hooks');
|
|
|
|
const { checkInvocations } = require('./hook-checks');
|
2018-01-28 16:26:08 +00:00
|
|
|
const net = require('net');
|
2017-06-30 23:29:09 +00:00
|
|
|
|
2016-11-22 16:13:44 +00:00
|
|
|
const hooks = initHooks();
|
|
|
|
hooks.enable();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Creating server and listening on port
|
|
|
|
//
|
2018-01-28 16:26:08 +00:00
|
|
|
const server = net.createServer()
|
2016-11-22 16:13:44 +00:00
|
|
|
.on('listening', common.mustCall(onlistening))
|
2018-01-28 16:26:08 +00:00
|
|
|
.on('connection', common.mustCall(onconnection))
|
2017-11-22 22:39:38 +00:00
|
|
|
.listen(0);
|
2016-11-22 16:13:44 +00:00
|
|
|
|
2017-05-26 15:53:06 +00:00
|
|
|
assert.strictEqual(hooks.activitiesOfTypes('WRITEWRAP').length, 0);
|
2016-11-22 16:13:44 +00:00
|
|
|
|
|
|
|
function onlistening() {
|
2017-05-26 15:53:06 +00:00
|
|
|
assert.strictEqual(hooks.activitiesOfTypes('WRITEWRAP').length, 0);
|
2016-11-22 16:13:44 +00:00
|
|
|
//
|
|
|
|
// Creating client and connecting it to server
|
|
|
|
//
|
2018-01-28 16:26:08 +00:00
|
|
|
net
|
|
|
|
.connect(server.address().port)
|
|
|
|
.on('connect', common.mustCall(onconnect));
|
2016-11-22 16:13:44 +00:00
|
|
|
|
2017-05-26 15:53:06 +00:00
|
|
|
assert.strictEqual(hooks.activitiesOfTypes('WRITEWRAP').length, 0);
|
2016-11-22 16:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkDestroyedWriteWraps(n, stage) {
|
|
|
|
const as = hooks.activitiesOfTypes('WRITEWRAP');
|
2018-01-28 16:26:08 +00:00
|
|
|
assert.strictEqual(as.length, n,
|
|
|
|
`${as.length} out of ${n} WRITEWRAPs when ${stage}`);
|
2016-11-22 16:13:44 +00:00
|
|
|
|
|
|
|
function checkValidWriteWrap(w) {
|
2017-05-26 15:53:06 +00:00
|
|
|
assert.strictEqual(w.type, 'WRITEWRAP');
|
|
|
|
assert.strictEqual(typeof w.uid, 'number');
|
2017-06-14 10:39:53 +00:00
|
|
|
assert.strictEqual(typeof w.triggerAsyncId, 'number');
|
2016-11-22 16:13:44 +00:00
|
|
|
|
2017-05-26 15:53:06 +00:00
|
|
|
checkInvocations(w, { init: 1 }, `when ${stage}`);
|
2016-11-22 16:13:44 +00:00
|
|
|
}
|
|
|
|
as.forEach(checkValidWriteWrap);
|
|
|
|
}
|
|
|
|
|
2018-01-28 16:26:08 +00:00
|
|
|
function onconnection(conn) {
|
2018-02-08 20:29:30 +00:00
|
|
|
conn.write('hi'); // Let the client know we're ready.
|
2018-01-28 16:26:08 +00:00
|
|
|
conn.resume();
|
2016-11-22 16:13:44 +00:00
|
|
|
//
|
|
|
|
// Server received client connection
|
|
|
|
//
|
2018-01-28 16:26:08 +00:00
|
|
|
checkDestroyedWriteWraps(0, 'server got connection');
|
2016-11-22 16:13:44 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 16:26:08 +00:00
|
|
|
function onconnect() {
|
2016-11-22 16:13:44 +00:00
|
|
|
//
|
|
|
|
// Client connected to server
|
|
|
|
//
|
2018-01-28 16:26:08 +00:00
|
|
|
checkDestroyedWriteWraps(0, 'client connected');
|
2016-11-22 16:13:44 +00:00
|
|
|
|
2018-02-08 20:29:30 +00:00
|
|
|
this.once('data', common.mustCall(ondata));
|
|
|
|
}
|
|
|
|
|
|
|
|
function ondata() {
|
2016-11-22 16:13:44 +00:00
|
|
|
//
|
2018-02-08 20:29:30 +00:00
|
|
|
// Writing data to client socket
|
2016-11-22 16:13:44 +00:00
|
|
|
//
|
2018-02-08 20:29:30 +00:00
|
|
|
const write = () => {
|
|
|
|
let writeFinished = false;
|
|
|
|
this.write('f'.repeat(1280000), () => {
|
|
|
|
writeFinished = true;
|
|
|
|
});
|
|
|
|
process.nextTick(() => {
|
|
|
|
if (writeFinished) {
|
|
|
|
// Synchronous finish, write more data immediately.
|
|
|
|
writeFinished = false;
|
|
|
|
write();
|
|
|
|
} else {
|
|
|
|
// Asynchronous write; this is what we are here for.
|
|
|
|
onafterwrite(this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
write();
|
2018-01-28 16:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onafterwrite(self) {
|
|
|
|
checkDestroyedWriteWraps(1, 'client destroyed');
|
2018-02-08 20:29:30 +00:00
|
|
|
self.end();
|
2016-11-22 16:13:44 +00:00
|
|
|
|
2018-01-28 16:26:08 +00:00
|
|
|
checkDestroyedWriteWraps(1, 'client destroyed');
|
2016-11-22 16:13:44 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Closing server
|
|
|
|
//
|
|
|
|
server.close(common.mustCall(onserverClosed));
|
2018-01-28 16:26:08 +00:00
|
|
|
checkDestroyedWriteWraps(1, 'server closing');
|
2016-11-22 16:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onserverClosed() {
|
2018-01-28 16:26:08 +00:00
|
|
|
checkDestroyedWriteWraps(1, 'server closed');
|
2016-11-22 16:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
process.on('exit', onexit);
|
|
|
|
|
|
|
|
function onexit() {
|
|
|
|
hooks.disable();
|
|
|
|
hooks.sanityCheck('WRITEWRAP');
|
2018-01-28 16:26:08 +00:00
|
|
|
checkDestroyedWriteWraps(1, 'process exits');
|
2016-11-22 16:13:44 +00:00
|
|
|
}
|