Skip to content

Commit 3543c55

Browse files
committed
test: make sure WriteWrap tests are actually async
PR-URL: #18676 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent fa691f7 commit 3543c55

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

test/async-hooks/test-writewrap.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function checkDestroyedWriteWraps(n, stage) {
4747
}
4848

4949
function onconnection(conn) {
50+
conn.write('hi'); // Let the client know we're ready.
5051
conn.resume();
5152
//
5253
// Server received client connection
@@ -60,15 +61,35 @@ function onconnect() {
6061
//
6162
checkDestroyedWriteWraps(0, 'client connected');
6263

64+
this.once('data', common.mustCall(ondata));
65+
}
66+
67+
function ondata() {
6368
//
64-
// Destroying client socket
69+
// Writing data to client socket
6570
//
66-
this.write('f'.repeat(128000), () => onafterwrite(this));
71+
const write = () => {
72+
let writeFinished = false;
73+
this.write('f'.repeat(1280000), () => {
74+
writeFinished = true;
75+
});
76+
process.nextTick(() => {
77+
if (writeFinished) {
78+
// Synchronous finish, write more data immediately.
79+
writeFinished = false;
80+
write();
81+
} else {
82+
// Asynchronous write; this is what we are here for.
83+
onafterwrite(this);
84+
}
85+
});
86+
};
87+
write();
6788
}
6889

6990
function onafterwrite(self) {
7091
checkDestroyedWriteWraps(1, 'client destroyed');
71-
self.destroy();
92+
self.end();
7293

7394
checkDestroyedWriteWraps(1, 'client destroyed');
7495

test/sequential/test-async-wrap-getasyncid.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ if (common.hasCrypto) { // eslint-disable-line crypto-check
195195
const handle = new tcp_wrap.TCP(tcp_wrap.constants.SOCKET);
196196
const req = new tcp_wrap.TCPConnectWrap();
197197
const sreq = new stream_wrap.ShutdownWrap();
198-
const wreq = new stream_wrap.WriteWrap();
199198
testInitialized(handle, 'TCP');
200199
testUninitialized(req, 'TCPConnectWrap');
201200
testUninitialized(sreq, 'ShutdownWrap');
@@ -204,20 +203,25 @@ if (common.hasCrypto) { // eslint-disable-line crypto-check
204203
handle.close();
205204
});
206205

207-
wreq.handle = handle;
208-
wreq.oncomplete = common.mustCall(() => {
209-
handle.shutdown(sreq);
210-
testInitialized(sreq, 'ShutdownWrap');
211-
});
212-
wreq.async = true;
213-
214-
req.oncomplete = common.mustCall(() => {
215-
// Use a long string to make sure the write happens asynchronously.
206+
req.oncomplete = common.mustCall(writeData);
207+
function writeData() {
208+
const wreq = new stream_wrap.WriteWrap();
209+
wreq.handle = handle;
210+
wreq.oncomplete = () => {
211+
handle.shutdown(sreq);
212+
testInitialized(sreq, 'ShutdownWrap');
213+
};
216214
const err = handle.writeLatin1String(wreq, 'hi'.repeat(100000));
217215
if (err)
218216
throw new Error(`write failed: ${getSystemErrorName(err)}`);
217+
if (!wreq.async) {
218+
testUninitialized(wreq, 'WriteWrap');
219+
// Synchronous finish. Write more data until we hit an
220+
// asynchronous write.
221+
return writeData();
222+
}
219223
testInitialized(wreq, 'WriteWrap');
220-
});
224+
}
221225
req.address = common.localhostIPv4;
222226
req.port = server.address().port;
223227
const err = handle.connect(req, req.address, req.port);

0 commit comments

Comments
 (0)