Skip to content

Commit 1faae90

Browse files
apapirovskigibfahn
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 <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent cbd698a commit 1faae90

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
@@ -47,8 +47,6 @@ const {
4747
kClearScreenDown
4848
} = CSI;
4949

50-
const { now } = process.binding('timer_wrap').Timer;
51-
5250
const kHistorySize = 30;
5351
const kMincrlfDelay = 100;
5452
// \r\n, \n, or \r followed by something other than \n
@@ -400,7 +398,7 @@ Interface.prototype._normalWrite = function(b) {
400398
}
401399
var string = this._decoder.write(b);
402400
if (this._sawReturnAt &&
403-
now() - this._sawReturnAt <= this.crlfDelay) {
401+
Date.now() - this._sawReturnAt <= this.crlfDelay) {
404402
string = string.replace(/^\n/, '');
405403
this._sawReturnAt = 0;
406404
}
@@ -413,7 +411,7 @@ Interface.prototype._normalWrite = function(b) {
413411
this._line_buffer = null;
414412
}
415413
if (newPartContainsEnding) {
416-
this._sawReturnAt = string.endsWith('\r') ? now() : 0;
414+
this._sawReturnAt = string.endsWith('\r') ? Date.now() : 0;
417415

418416
// got one or more newlines; process into "line" events
419417
var lines = string.split(lineEnding);
@@ -908,14 +906,14 @@ Interface.prototype._ttyWrite = function(s, key) {
908906

909907
switch (key.name) {
910908
case 'return': // carriage return, i.e. \r
911-
this._sawReturnAt = now();
909+
this._sawReturnAt = Date.now();
912910
this._line();
913911
break;
914912

915913
case 'enter':
916914
// When key interval > crlfDelay
917915
if (this._sawReturnAt === 0 ||
918-
now() - this._sawReturnAt > this.crlfDelay) {
916+
Date.now() - this._sawReturnAt > this.crlfDelay) {
919917
this._line();
920918
}
921919
this._sawReturnAt = 0;

test/parallel/test-readline-interface.js

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

test/sequential/test-readline-interface.js

-108
This file was deleted.

0 commit comments

Comments
 (0)