Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix race condition in repl history test #2358

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions test/sequential/test-repl-persistent-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,35 +74,41 @@ const oldHistoryPath = path.join(fixtures, 'old-repl-history-file.json');
const tests = [{
env: { NODE_REPL_HISTORY: '' },
test: [UP],
expected: [prompt, replDisabled, prompt]
expected: [prompt, replDisabled, prompt],
event: 'end'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this works? I don't think the repl actually emits this at all. Console.log something in on('close',...) / on(opts.event,...) and make sure it calls the same number as the number of tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, use this patch, which I am adding to #2356:

diff --git a/test/sequential/test-repl-persistent-history.js b/test/sequential/test-repl-persistent-history.js
index 8d550f6..27aca28 100644
--- a/test/sequential/test-repl-persistent-history.js
+++ b/test/sequential/test-repl-persistent-history.js
@@ -144,6 +144,13 @@ const tests = [{
 }];


+var testsNotRan = tests.length;
+
+process.on('beforeExit', function() {
+  assert.strictEqual(testsNotRan, 0);
+});
+
+
 // Copy our fixture to the tmp directory
 fs.createReadStream(historyFixturePath)
   .pipe(fs.createWriteStream(historyPath)).on('unpipe', runTest);
@@ -185,6 +192,7 @@ function runTest() {
     repl.on('close', function() {
       // Ensure everything that we expected was output
       assert.strictEqual(expected.length, 0);
+      testsNotRan--;
       setImmediate(runTest);
     });

EDIT: FIXED

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The end event worked fine for me. To really see it, you can run with the NODE_DEBUG=repl. It makes me wonder if we should defer emitting close until after flushHistory though?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes me wonder if we should defer emitting close until after flushHistory though?

That's what I was thinking.

I can't find where the 'end' event is emitted on the repl/readline though..

},
{
env: { NODE_REPL_HISTORY: '',
NODE_REPL_HISTORY_FILE: oldHistoryPath },
test: [UP],
expected: [prompt, replDisabled, prompt]
expected: [prompt, replDisabled, prompt],
event: 'end'
},
{
env: { NODE_REPL_HISTORY: historyPath },
test: [UP, CLEAR],
expected: [prompt, prompt + '\'you look fabulous today\'', prompt]
expected: [prompt, prompt + '\'you look fabulous today\'', prompt],
event: 'end'
},
{
env: { NODE_REPL_HISTORY: historyPath,
NODE_REPL_HISTORY_FILE: oldHistoryPath },
test: [UP, CLEAR],
expected: [prompt, prompt + '\'you look fabulous today\'', prompt]
expected: [prompt, prompt + '\'you look fabulous today\'', prompt],
event: 'end'
},
{
env: { NODE_REPL_HISTORY: historyPath,
NODE_REPL_HISTORY_FILE: '' },
test: [UP, CLEAR],
expected: [prompt, prompt + '\'you look fabulous today\'', prompt]
expected: [prompt, prompt + '\'you look fabulous today\'', prompt],
event: 'end'
},
{
env: {},
test: [UP],
expected: [prompt]
expected: [prompt],
event: 'end'
},
{
env: { NODE_REPL_HISTORY_FILE: oldHistoryPath },
Expand All @@ -123,13 +129,15 @@ const tests = [{
const historyCopy = fs.readFileSync(historyPath, 'utf8');
assert.strictEqual(historyCopy, '\'you look fabulous today\'' + os.EOL +
'\'Stay Fresh~\'' + os.EOL);
}
},
event: 'flushHistory'
},
{
env: {},
test: [UP, UP, ENTER],
expected: [prompt, prompt + '\'42\'', prompt + '\'=^.^=\'', '\'=^.^=\'\n',
prompt]
prompt],
event: 'flushHistory'
},
{ // Make sure this is always the last test, since we change os.homedir()
before: function mockHomedirFailure() {
Expand All @@ -140,7 +148,8 @@ const tests = [{
},
env: {},
test: [UP],
expected: [prompt, homedirErr, prompt, replDisabled, prompt]
expected: [prompt, homedirErr, prompt, replDisabled, prompt],
event: 'end'
}];


Expand Down Expand Up @@ -180,9 +189,9 @@ function runTest() {
}, function(err, repl) {
if (err) throw err;

if (after) repl.on('close', after);
if (after) repl.on(opts.event, after);

repl.on('close', function() {
repl.on(opts.event, function() {
// Ensure everything that we expected was output
assert.strictEqual(expected.length, 0);
setImmediate(runTest);
Expand Down