Skip to content

Commit

Permalink
test: make SessionOutput Promise tolerant
Browse files Browse the repository at this point in the history
If a test case would use Promises for linesUntil, sometimes we would
emit lines events while there was no listener attached to it. This
commit changes SessionOutput to make it a little more resilient: now
line events will only be emitted when the SessionOutput is waiting. When
wait is called (via linesUntil, for example), it'll retrieve all lines
(via line events) on the buffer until it reaches a line matching the
regexp.  If no lines match the regexp, we'll continue as before waiting
for data from lldb and buffering it.

PR-URL: #311
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
mmarchini committed Jan 14, 2020
1 parent fd9d2b0 commit 82d28c5
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ function SessionOutput(session, stream, timeout) {
this.timeout = timeout || 10000;
this.session = session;

stream.on('data', (data) => {
buf += data;

this.flush = function flush() {
for (;;) {
// NOTE(mmarchini): don't emit line events while not waiting, otherwise
// we might miss something.
if (!this.waiting)
break

let index = buf.indexOf('\n');

if (index === -1)
Expand All @@ -62,6 +65,11 @@ function SessionOutput(session, stream, timeout) {
else
this.emit('line', line);
}
}

stream.on('data', (data) => {
buf += data;
this.flush();
});

// Ignore errors
Expand Down Expand Up @@ -129,6 +137,7 @@ SessionOutput.prototype.wait = function wait(regexp, callback, allLines) {
}, this.timeout).unref();

this.on('line', onLine);
this.flush();
};

SessionOutput.prototype.waitBreak = function waitBreak(callback) {
Expand Down

0 comments on commit 82d28c5

Please sign in to comment.