Skip to content

Commit 36332eb

Browse files
apapirovskiMylesBorins
authored andcommitted
readline: use Date.now() and move test to parallel
The readline module wants a truthy time while using Timer.now() doesn't necessarily guarantee that early on in the process' life. It also doesn't actually resolve the timing issues experienced in an earlier issue. Instead, this PR fixes the related tests and moves them back to parallel. Refs: #14674 PR-URL: #18563 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 0277993 commit 36332eb

File tree

3 files changed

+81
-114
lines changed

3 files changed

+81
-114
lines changed

lib/readline.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ const {
4848
kClearScreenDown
4949
} = CSI;
5050

51-
const { now } = process.binding('timer_wrap').Timer;
52-
5351
const kHistorySize = 30;
5452
const kMincrlfDelay = 100;
5553
// \r\n, \n, or \r followed by something other than \n
@@ -411,7 +409,7 @@ Interface.prototype._normalWrite = function(b) {
411409
}
412410
var string = this._decoder.write(b);
413411
if (this._sawReturnAt &&
414-
now() - this._sawReturnAt <= this.crlfDelay) {
412+
Date.now() - this._sawReturnAt <= this.crlfDelay) {
415413
string = string.replace(/^\n/, '');
416414
this._sawReturnAt = 0;
417415
}
@@ -424,7 +422,7 @@ Interface.prototype._normalWrite = function(b) {
424422
this._line_buffer = null;
425423
}
426424
if (newPartContainsEnding) {
427-
this._sawReturnAt = string.endsWith('\r') ? now() : 0;
425+
this._sawReturnAt = string.endsWith('\r') ? Date.now() : 0;
428426

429427
// got one or more newlines; process into "line" events
430428
var lines = string.split(lineEnding);
@@ -917,14 +915,14 @@ Interface.prototype._ttyWrite = function(s, key) {
917915

918916
switch (key.name) {
919917
case 'return': // carriage return, i.e. \r
920-
this._sawReturnAt = now();
918+
this._sawReturnAt = Date.now();
921919
this._line();
922920
break;
923921

924922
case 'enter':
925923
// When key interval > crlfDelay
926924
if (this._sawReturnAt === 0 ||
927-
now() - this._sawReturnAt > this.crlfDelay) {
925+
Date.now() - this._sawReturnAt > this.crlfDelay) {
928926
this._line();
929927
}
930928
this._sawReturnAt = 0;

test/parallel/test-readline-interface.js

+77
Original file line numberDiff line numberDiff line change
@@ -869,3 +869,80 @@ function isWarned(emitter) {
869869
assert.strictEqual(rl._prompt, '$ ');
870870
}
871871
});
872+
873+
// For the purposes of the following tests, we do not care about the exact
874+
// value of crlfDelay, only that the behaviour conforms to what's expected.
875+
// Setting it to Infinity allows the test to succeed even under extreme
876+
// CPU stress.
877+
const crlfDelay = Infinity;
878+
879+
[ true, false ].forEach(function(terminal) {
880+
// sending multiple newlines at once that does not end with a new line
881+
// and a `end` event(last line is)
882+
883+
// \r\n should emit one line event, not two
884+
{
885+
const fi = new FakeInput();
886+
const rli = new readline.Interface(
887+
{
888+
input: fi,
889+
output: fi,
890+
terminal: terminal,
891+
crlfDelay
892+
}
893+
);
894+
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
895+
let callCount = 0;
896+
rli.on('line', function(line) {
897+
assert.strictEqual(line, expectedLines[callCount]);
898+
callCount++;
899+
});
900+
fi.emit('data', expectedLines.join('\r\n'));
901+
assert.strictEqual(callCount, expectedLines.length - 1);
902+
rli.close();
903+
}
904+
905+
// \r\n should emit one line event when split across multiple writes.
906+
{
907+
const fi = new FakeInput();
908+
const rli = new readline.Interface({
909+
input: fi,
910+
output: fi,
911+
terminal: terminal,
912+
crlfDelay
913+
});
914+
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
915+
let callCount = 0;
916+
rli.on('line', function(line) {
917+
assert.strictEqual(line, expectedLines[callCount]);
918+
callCount++;
919+
});
920+
expectedLines.forEach(function(line) {
921+
fi.emit('data', `${line}\r`);
922+
fi.emit('data', '\n');
923+
});
924+
assert.strictEqual(callCount, expectedLines.length);
925+
rli.close();
926+
}
927+
928+
// Emit one line event when the delay between \r and \n is
929+
// over the default crlfDelay but within the setting value.
930+
{
931+
const fi = new FakeInput();
932+
const delay = 125;
933+
const rli = new readline.Interface({
934+
input: fi,
935+
output: fi,
936+
terminal: terminal,
937+
crlfDelay
938+
});
939+
let callCount = 0;
940+
rli.on('line', () => callCount++);
941+
fi.emit('data', '\r');
942+
setTimeout(common.mustCall(() => {
943+
fi.emit('data', '\n');
944+
assert.strictEqual(callCount, 1);
945+
rli.close();
946+
}), delay);
947+
}
948+
});

test/sequential/test-readline-interface.js

-108
This file was deleted.

0 commit comments

Comments
 (0)