Skip to content

Commit 44e8c4b

Browse files
committed
test: fix race condition in repl history test
Previously, there were cases when the `end` event could be emitted before the `flushHistory` event. This change makes the next test fire off after the specified event based on the `test`
1 parent 847459c commit 44e8c4b

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

test/sequential/test-repl-persistent-history.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,35 +74,41 @@ const oldHistoryPath = path.join(fixtures, 'old-repl-history-file.json');
7474
const tests = [{
7575
env: { NODE_REPL_HISTORY: '' },
7676
test: [UP],
77-
expected: [prompt, replDisabled, prompt]
77+
expected: [prompt, replDisabled, prompt],
78+
event: 'end'
7879
},
7980
{
8081
env: { NODE_REPL_HISTORY: '',
8182
NODE_REPL_HISTORY_FILE: oldHistoryPath },
8283
test: [UP],
83-
expected: [prompt, replDisabled, prompt]
84+
expected: [prompt, replDisabled, prompt],
85+
event: 'end'
8486
},
8587
{
8688
env: { NODE_REPL_HISTORY: historyPath },
8789
test: [UP, CLEAR],
88-
expected: [prompt, prompt + '\'you look fabulous today\'', prompt]
90+
expected: [prompt, prompt + '\'you look fabulous today\'', prompt],
91+
event: 'end'
8992
},
9093
{
9194
env: { NODE_REPL_HISTORY: historyPath,
9295
NODE_REPL_HISTORY_FILE: oldHistoryPath },
9396
test: [UP, CLEAR],
94-
expected: [prompt, prompt + '\'you look fabulous today\'', prompt]
97+
expected: [prompt, prompt + '\'you look fabulous today\'', prompt],
98+
event: 'end'
9599
},
96100
{
97101
env: { NODE_REPL_HISTORY: historyPath,
98102
NODE_REPL_HISTORY_FILE: '' },
99103
test: [UP, CLEAR],
100-
expected: [prompt, prompt + '\'you look fabulous today\'', prompt]
104+
expected: [prompt, prompt + '\'you look fabulous today\'', prompt],
105+
event: 'end'
101106
},
102107
{
103108
env: {},
104109
test: [UP],
105-
expected: [prompt]
110+
expected: [prompt],
111+
event: 'end'
106112
},
107113
{
108114
env: { NODE_REPL_HISTORY_FILE: oldHistoryPath },
@@ -123,13 +129,15 @@ const tests = [{
123129
const historyCopy = fs.readFileSync(historyPath, 'utf8');
124130
assert.strictEqual(historyCopy, '\'you look fabulous today\'' + os.EOL +
125131
'\'Stay Fresh~\'' + os.EOL);
126-
}
132+
},
133+
event: 'flushHistory'
127134
},
128135
{
129136
env: {},
130137
test: [UP, UP, ENTER],
131138
expected: [prompt, prompt + '\'42\'', prompt + '\'=^.^=\'', '\'=^.^=\'\n',
132-
prompt]
139+
prompt],
140+
event: 'flushHistory'
133141
},
134142
{ // Make sure this is always the last test, since we change os.homedir()
135143
before: function mockHomedirFailure() {
@@ -140,7 +148,8 @@ const tests = [{
140148
},
141149
env: {},
142150
test: [UP],
143-
expected: [prompt, homedirErr, prompt, replDisabled, prompt]
151+
expected: [prompt, homedirErr, prompt, replDisabled, prompt],
152+
event: 'end'
144153
}];
145154

146155

@@ -180,9 +189,9 @@ function runTest() {
180189
}, function(err, repl) {
181190
if (err) throw err;
182191

183-
if (after) repl.on('close', after);
192+
if (after) repl.on(opts.event, after);
184193

185-
repl.on('close', function() {
194+
repl.on(opts.event, function() {
186195
// Ensure everything that we expected was output
187196
assert.strictEqual(expected.length, 0);
188197
setImmediate(runTest);

0 commit comments

Comments
 (0)