Skip to content

Commit

Permalink
test: make sure WriteWrap tests are actually async
Browse files Browse the repository at this point in the history
PR-URL: #18676
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax committed Feb 27, 2018
1 parent fa691f7 commit 3543c55
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
27 changes: 24 additions & 3 deletions test/async-hooks/test-writewrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');

Expand Down
26 changes: 15 additions & 11 deletions test/sequential/test-async-wrap-getasyncid.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,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');
Expand All @@ -204,20 +203,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);
Expand Down

0 comments on commit 3543c55

Please sign in to comment.