Skip to content

Commit 60ae556

Browse files
addaleaxMylesBorins
authored andcommitted
test: refactor test-net-connect-buffer
- Use arrow functions, `common.mustCall()` - Remove redundant `console.log()`s/turn them into assertions - Use `common.expectsError()` PR-URL: #17710 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jon Moss <[email protected]>
1 parent c953967 commit 60ae556

File tree

1 file changed

+40
-60
lines changed

1 file changed

+40
-60
lines changed

test/parallel/test-net-connect-buffer.js

+40-60
Original file line numberDiff line numberDiff line change
@@ -20,68 +20,59 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const assert = require('assert');
2525
const net = require('net');
2626

27-
let dataWritten = false;
28-
let connectHappened = false;
29-
30-
const tcp = net.Server(function(s) {
27+
const tcp = net.Server(common.mustCall((s) => {
3128
tcp.close();
3229

33-
console.log('tcp server connection');
34-
3530
let buf = '';
31+
s.setEncoding('utf8');
3632
s.on('data', function(d) {
3733
buf += d;
3834
});
3935

4036
s.on('end', function() {
41-
console.error('SERVER: end', buf.toString());
37+
console.error('SERVER: end', buf);
4238
assert.strictEqual(buf, "L'État, c'est moi");
43-
console.log('tcp socket disconnect');
4439
s.end();
4540
});
41+
}));
4642

47-
s.on('error', function(e) {
48-
console.log(`tcp server-side error: ${e.message}`);
49-
process.exit(1);
50-
});
51-
});
52-
53-
tcp.listen(0, function() {
43+
tcp.listen(0, common.mustCall(function() {
5444
const socket = net.Stream({ highWaterMark: 0 });
5545

56-
console.log('Connecting to socket ');
57-
58-
socket.connect(this.address().port, function() {
59-
console.log('socket connected');
60-
connectHappened = true;
61-
});
62-
63-
console.log(`connecting = ${socket.connecting}`);
46+
let connected = false;
47+
socket.connect(this.address().port, common.mustCall(() => connected = true));
6448

65-
assert.strictEqual('opening', socket.readyState);
49+
assert.strictEqual(socket.connecting, true);
50+
assert.strictEqual(socket.readyState, 'opening');
6651

6752
// Make sure that anything besides a buffer or a string throws.
68-
[null,
69-
true,
70-
false,
71-
undefined,
72-
1,
73-
1.0,
74-
1 / 0,
75-
+Infinity,
76-
-Infinity,
77-
[],
78-
{}
79-
].forEach(function(v) {
80-
function f() {
81-
console.error('write', v);
82-
socket.write(v);
83-
}
84-
assert.throws(f, TypeError);
53+
common.expectsError(() => socket.write(null),
54+
{
55+
code: 'ERR_STREAM_NULL_VALUES',
56+
type: TypeError,
57+
message: 'May not write null values to stream'
58+
});
59+
[
60+
true,
61+
false,
62+
undefined,
63+
1,
64+
1.0,
65+
1 / 0,
66+
+Infinity,
67+
-Infinity,
68+
[],
69+
{}
70+
].forEach((v) => {
71+
common.expectsError(() => socket.write(v), {
72+
code: 'ERR_INVALID_ARG_TYPE',
73+
type: TypeError,
74+
message: 'The "chunk" argument must be one of type string or Buffer'
75+
});
8576
});
8677

8778
// Write a string that contains a multi-byte character sequence to test that
@@ -92,26 +83,15 @@ tcp.listen(0, function() {
9283
// We're still connecting at this point so the datagram is first pushed onto
9384
// the connect queue. Make sure that it's not added to `bytesWritten` again
9485
// when the actual write happens.
95-
const r = socket.write(a, function(er) {
86+
const r = socket.write(a, common.mustCall((er) => {
9687
console.error('write cb');
97-
dataWritten = true;
98-
assert.ok(connectHappened);
99-
console.error('socket.bytesWritten', socket.bytesWritten);
100-
//assert.strictEqual(socket.bytesWritten, Buffer.from(a + b).length);
101-
console.error('data written');
102-
});
103-
console.error('socket.bytesWritten', socket.bytesWritten);
104-
console.error('write returned', r);
88+
assert.ok(connected);
89+
assert.strictEqual(socket.bytesWritten, Buffer.from(a + b).length);
90+
}));
10591

10692
assert.strictEqual(socket.bytesWritten, Buffer.from(a).length);
107-
108-
assert.strictEqual(false, r);
93+
assert.strictEqual(r, false);
10994
socket.end(b);
11095

111-
assert.strictEqual('opening', socket.readyState);
112-
});
113-
114-
process.on('exit', function() {
115-
assert.ok(connectHappened);
116-
assert.ok(dataWritten);
117-
});
96+
assert.strictEqual(socket.readyState, 'opening');
97+
}));

0 commit comments

Comments
 (0)