@@ -160,6 +160,94 @@ describe('DefaultEventsService', () => {
160160 await ( service as any ) . shutdown ( ) ;
161161 } ) ;
162162
163+ it ( 'should wait an poll on timeout' , async ( ) => {
164+ const logger = mockServices . logger . mock ( ) ;
165+ const service = DefaultEventsService . create ( { logger } ) . forPlugin ( 'a' , {
166+ auth : mockServices . auth ( ) ,
167+ logger,
168+ discovery : mockServices . discovery ( ) ,
169+ lifecycle : mockServices . lifecycle . mock ( ) ,
170+ } ) ;
171+
172+ let callCount = 0 ;
173+
174+ let blockingController : ReadableStreamDefaultController ;
175+ const blockingStream = new ReadableStream ( {
176+ start ( controller ) {
177+ blockingController = controller ;
178+ } ,
179+ } ) ;
180+
181+ mswServer . use (
182+ rest . put (
183+ 'http://localhost:0/api/events/bus/v1/subscriptions/a.tester' ,
184+ ( _req , res , ctx ) => res ( ctx . status ( 200 ) ) ,
185+ ) ,
186+ // The first and third calls result in a blocking 202 that is resolved after 100ms
187+ // The second and fourth calls result in a 200 with an event
188+ // The fifth call blocks until the end of the test
189+ // No more than 5 calls should be made
190+ rest . get (
191+ 'http://localhost:0/api/events/bus/v1/subscriptions/a.tester/events' ,
192+ ( _req , res , ctx ) => {
193+ callCount += 1 ;
194+ if ( callCount === 1 || callCount === 3 ) {
195+ return res (
196+ ctx . status ( 202 ) ,
197+ ctx . body (
198+ new ReadableStream ( {
199+ start ( controller ) {
200+ setTimeout ( ( ) => controller . close ( ) , 100 ) ;
201+ } ,
202+ } ) ,
203+ ) ,
204+ ) ;
205+ } else if ( callCount === 2 || callCount === 4 ) {
206+ return res (
207+ ctx . status ( 200 ) ,
208+ ctx . json ( {
209+ events : [ { topic : 'test' , payload : { callCount } } ] ,
210+ } ) ,
211+ ) ;
212+ } else if ( callCount === 5 ) {
213+ return res ( ctx . status ( 202 ) , ctx . body ( blockingStream ) ) ;
214+ }
215+ throw new Error ( `events endpoint called too many times` ) ;
216+ } ,
217+ ) ,
218+ ) ;
219+
220+ const event = await new Promise ( resolve => {
221+ const events = new Array < EventParams > ( ) ;
222+ service . subscribe ( {
223+ id : 'tester' ,
224+ topics : [ 'test' ] ,
225+ async onEvent ( newEvent ) {
226+ events . push ( newEvent ) ;
227+ if ( events . length === 2 ) {
228+ resolve ( events ) ;
229+ }
230+ } ,
231+ } ) ;
232+ } ) ;
233+
234+ expect ( event ) . toEqual ( [
235+ { topic : 'test' , eventPayload : { callCount : 2 } } ,
236+ { topic : 'test' , eventPayload : { callCount : 4 } } ,
237+ ] ) ;
238+
239+ // Wait to make sure no additional calls happen
240+ await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
241+
242+ expect ( callCount ) . toBe ( 5 ) ;
243+
244+ // Internal call to clean up subscriptions
245+ await ( service as any ) . shutdown ( ) ;
246+
247+ // Close the stream for the 5th call so that we don't leave the request hanging
248+ blockingController ! . close ( ) ;
249+ } ) ;
250+
163251 it ( 'should not read events from bus if disabled' , async ( ) => {
164252 const logger = mockServices . logger . mock ( ) ;
165253 const service = DefaultEventsService . create ( {
0 commit comments