File tree Expand file tree Collapse file tree 4 files changed +31
-8
lines changed Expand file tree Collapse file tree 4 files changed +31
-8
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ import {
2020 tick ,
2121 useTmpdir ,
2222 waitEval ,
23+ waitMongoshCompletionResults ,
2324} from '../test/repl-helpers' ;
2425import type { MongoshIOProvider , MongoshNodeReplOptions } from './mongosh-repl' ;
2526import 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 } ;
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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+
7897const 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} ;
Original file line number Diff line number Diff 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._
You can’t perform that action at this time.
0 commit comments