test: make sure WriteWrap tests are actually async

PR-URL: https://github.com/nodejs/node/pull/18676
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Anna Henningsen 2018-02-08 21:29:30 +01:00
parent e1271c07c3
commit 0ed9ea861b
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
2 changed files with 39 additions and 14 deletions

View File

@ -47,6 +47,7 @@ function checkDestroyedWriteWraps(n, stage) {
}
function onconnection(conn) {
conn.write('hi'); // Let the client know we're ready.
conn.resume();
//
// Server received client connection
@ -60,15 +61,35 @@ function onconnect() {
//
checkDestroyedWriteWraps(0, 'client connected');
this.once('data', common.mustCall(ondata));
}
function ondata() {
//
// Destroying client socket
// Writing data to client socket
//
this.write('f'.repeat(128000), () => onafterwrite(this));
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();
}
function onafterwrite(self) {
checkDestroyedWriteWraps(1, 'client destroyed');
self.destroy();
self.end();
checkDestroyedWriteWraps(1, 'client destroyed');

View File

@ -197,7 +197,6 @@ if (common.hasCrypto) { // eslint-disable-line crypto-check
const handle = new tcp_wrap.TCP(tcp_wrap.constants.SOCKET);
const req = new tcp_wrap.TCPConnectWrap();
const sreq = new stream_wrap.ShutdownWrap();
const wreq = new stream_wrap.WriteWrap();
testInitialized(handle, 'TCP');
testUninitialized(req, 'TCPConnectWrap');
testUninitialized(sreq, 'ShutdownWrap');
@ -206,20 +205,25 @@ if (common.hasCrypto) { // eslint-disable-line crypto-check
handle.close();
});
wreq.handle = handle;
wreq.oncomplete = common.mustCall(() => {
handle.shutdown(sreq);
testInitialized(sreq, 'ShutdownWrap');
});
wreq.async = true;
req.oncomplete = common.mustCall(() => {
// Use a long string to make sure the write happens asynchronously.
req.oncomplete = common.mustCall(writeData);
function writeData() {
const wreq = new stream_wrap.WriteWrap();
wreq.handle = handle;
wreq.oncomplete = () => {
handle.shutdown(sreq);
testInitialized(sreq, 'ShutdownWrap');
};
const err = handle.writeLatin1String(wreq, 'hi'.repeat(100000));
if (err)
throw new Error(`write failed: ${getSystemErrorName(err)}`);
if (!wreq.async) {
testUninitialized(wreq, 'WriteWrap');
// Synchronous finish. Write more data until we hit an
// asynchronous write.
return writeData();
}
testInitialized(wreq, 'WriteWrap');
});
}
req.address = common.localhostIPv4;
req.port = server.address().port;
const err = handle.connect(req, req.address, req.port);