Skip to content

Commit 7731cd8

Browse files
committed
better event based waiting
1 parent 4c2a001 commit 7731cd8

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

packages/cli-repl/src/mongosh-repl.spec.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
tick,
2121
useTmpdir,
2222
waitEval,
23+
waitMongoshCompletionResults,
2324
} from '../test/repl-helpers';
2425
import type { MongoshIOProvider, MongoshNodeReplOptions } from './mongosh-repl';
2526
import MongoshNodeRepl from './mongosh-repl';
@@ -364,12 +365,7 @@ describe('MongoshNodeRepl', function () {
364365
const tabtab = async () => {
365366
await tab();
366367
if (process.env.USE_NEW_AUTOCOMPLETE) {
367-
// TODO: This is because autocomplete() is async and will either list
368-
// databases or collections or sample documents, any of which takes time
369-
// to complete and can time out. There is probably a better way.
370-
await new Promise((resolve) => {
371-
setTimeout(resolve, 210);
372-
});
368+
await waitMongoshCompletionResults(bus);
373369
}
374370
await tab();
375371
};

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,11 @@ class MongoshNodeRepl implements EvaluationListener {
548548
}
549549
})(),
550550
]);
551-
this.bus.emit('mongosh:autocompletion-complete'); // For testing.
551+
this.bus.emit(
552+
'mongosh:autocompletion-complete',
553+
replResults,
554+
mongoshResults
555+
); // For testing.
552556

553557
// Sometimes the mongosh completion knows that what it is doing is right,
554558
// and that autocompletion based on inspecting the actual objects that

packages/cli-repl/test/repl-helpers.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ async function waitCompletion(bus: MongoshBus) {
7575
await tick();
7676
}
7777

78+
async function waitMongoshCompletionResults(bus: MongoshBus) {
79+
// Waiting for the completion results can "time out" if an async action such
80+
// as listing the databases or collections or loading the schema takes longer
81+
// than 200ms (at the time of writing), but by the next try or at least
82+
// eventually the action should complete and then the next autocomplete call
83+
// will return the cached result.
84+
let found = false;
85+
while (!found) {
86+
const [, mongoshResults] = await waitBus(
87+
bus,
88+
'mongosh:autocompletion-complete'
89+
);
90+
if (mongoshResults.length === 0) {
91+
found = true;
92+
}
93+
}
94+
await tick();
95+
}
96+
7897
const fakeTTYProps: Partial<ReadStream & WriteStream> = {
7998
isTTY: true,
8099
isRaw: true,
@@ -106,6 +125,7 @@ export {
106125
waitBus,
107126
waitEval,
108127
waitCompletion,
128+
waitMongoshCompletionResults,
109129
fakeTTYProps,
110130
readReplLogFile,
111131
};

packages/types/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,10 @@ export interface MongoshBusEventsMap extends ConnectEventMap {
335335
* Signals the completion of the autocomplete suggestion providers.
336336
* _ONLY AVAILABLE FOR TESTING._
337337
*/
338-
'mongosh:autocompletion-complete': () => void;
338+
'mongosh:autocompletion-complete': (
339+
resplResults: string[],
340+
mongoshResults: string[]
341+
) => void;
339342
/**
340343
* Signals the completion of the asynchronous interrupt handler in MongoshRepl. Not fired for interrupts of _synchronous_ code.
341344
* _ONLY AVAILABLE FOR TESTING._

0 commit comments

Comments
 (0)