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:
parent
e1271c07c3
commit
0ed9ea861b
@ -47,6 +47,7 @@ function checkDestroyedWriteWraps(n, stage) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onconnection(conn) {
|
function onconnection(conn) {
|
||||||
|
conn.write('hi'); // Let the client know we're ready.
|
||||||
conn.resume();
|
conn.resume();
|
||||||
//
|
//
|
||||||
// Server received client connection
|
// Server received client connection
|
||||||
@ -60,15 +61,35 @@ function onconnect() {
|
|||||||
//
|
//
|
||||||
checkDestroyedWriteWraps(0, 'client connected');
|
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) {
|
function onafterwrite(self) {
|
||||||
checkDestroyedWriteWraps(1, 'client destroyed');
|
checkDestroyedWriteWraps(1, 'client destroyed');
|
||||||
self.destroy();
|
self.end();
|
||||||
|
|
||||||
checkDestroyedWriteWraps(1, 'client destroyed');
|
checkDestroyedWriteWraps(1, 'client destroyed');
|
||||||
|
|
||||||
|
@ -197,7 +197,6 @@ if (common.hasCrypto) { // eslint-disable-line crypto-check
|
|||||||
const handle = new tcp_wrap.TCP(tcp_wrap.constants.SOCKET);
|
const handle = new tcp_wrap.TCP(tcp_wrap.constants.SOCKET);
|
||||||
const req = new tcp_wrap.TCPConnectWrap();
|
const req = new tcp_wrap.TCPConnectWrap();
|
||||||
const sreq = new stream_wrap.ShutdownWrap();
|
const sreq = new stream_wrap.ShutdownWrap();
|
||||||
const wreq = new stream_wrap.WriteWrap();
|
|
||||||
testInitialized(handle, 'TCP');
|
testInitialized(handle, 'TCP');
|
||||||
testUninitialized(req, 'TCPConnectWrap');
|
testUninitialized(req, 'TCPConnectWrap');
|
||||||
testUninitialized(sreq, 'ShutdownWrap');
|
testUninitialized(sreq, 'ShutdownWrap');
|
||||||
@ -206,20 +205,25 @@ if (common.hasCrypto) { // eslint-disable-line crypto-check
|
|||||||
handle.close();
|
handle.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
req.oncomplete = common.mustCall(writeData);
|
||||||
|
function writeData() {
|
||||||
|
const wreq = new stream_wrap.WriteWrap();
|
||||||
wreq.handle = handle;
|
wreq.handle = handle;
|
||||||
wreq.oncomplete = common.mustCall(() => {
|
wreq.oncomplete = () => {
|
||||||
handle.shutdown(sreq);
|
handle.shutdown(sreq);
|
||||||
testInitialized(sreq, 'ShutdownWrap');
|
testInitialized(sreq, 'ShutdownWrap');
|
||||||
});
|
};
|
||||||
wreq.async = true;
|
|
||||||
|
|
||||||
req.oncomplete = common.mustCall(() => {
|
|
||||||
// Use a long string to make sure the write happens asynchronously.
|
|
||||||
const err = handle.writeLatin1String(wreq, 'hi'.repeat(100000));
|
const err = handle.writeLatin1String(wreq, 'hi'.repeat(100000));
|
||||||
if (err)
|
if (err)
|
||||||
throw new Error(`write failed: ${getSystemErrorName(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');
|
testInitialized(wreq, 'WriteWrap');
|
||||||
});
|
}
|
||||||
req.address = common.localhostIPv4;
|
req.address = common.localhostIPv4;
|
||||||
req.port = server.address().port;
|
req.port = server.address().port;
|
||||||
const err = handle.connect(req, req.address, req.port);
|
const err = handle.connect(req, req.address, req.port);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user