@@ -559,7 +559,7 @@ describe('ChannelConnection', () => {
559559 } )
560560
561561 describe ( '.__handleOngoingRequestsNumberChange()' , ( ) => {
562- it ( 'should call channel.stopReceiveTimeout when requets number equals to 0' , ( ) => {
562+ it ( 'should call channel.stopReceiveTimeout when requests number equals to 0' , ( ) => {
563563 const channel = {
564564 stopReceiveTimeout : jest . fn ( ) . mockName ( 'stopReceiveTimeout' ) ,
565565 startReceiveTimeout : jest . fn ( ) . mockName ( 'startReceiveTimeout' )
@@ -571,7 +571,7 @@ describe('ChannelConnection', () => {
571571 expect ( channel . stopReceiveTimeout ) . toHaveBeenCalledTimes ( 1 )
572572 } )
573573
574- it ( 'should not call channel.startReceiveTimeout when requets number equals to 0' , ( ) => {
574+ it ( 'should not call channel.startReceiveTimeout when requests number equals to 0' , ( ) => {
575575 const channel = {
576576 stopReceiveTimeout : jest . fn ( ) . mockName ( 'stopReceiveTimeout' ) ,
577577 startReceiveTimeout : jest . fn ( ) . mockName ( 'startReceiveTimeout' )
@@ -585,7 +585,7 @@ describe('ChannelConnection', () => {
585585
586586 it . each ( [
587587 [ 1 ] , [ 2 ] , [ 3 ] , [ 5 ] , [ 8 ] , [ 13 ] , [ 3000 ]
588- ] ) ( 'should call channel.startReceiveTimeout when requets number equals to %d' , ( requests ) => {
588+ ] ) ( 'should call channel.startReceiveTimeout when requests number equals to %d' , ( requests ) => {
589589 const channel = {
590590 stopReceiveTimeout : jest . fn ( ) . mockName ( 'stopReceiveTimeout' ) ,
591591 startReceiveTimeout : jest . fn ( ) . mockName ( 'startReceiveTimeout' )
@@ -599,7 +599,7 @@ describe('ChannelConnection', () => {
599599
600600 it . each ( [
601601 [ 1 ] , [ 2 ] , [ 3 ] , [ 5 ] , [ 8 ] , [ 13 ] , [ 3000 ]
602- ] ) ( 'should not call channel.stopReceiveTimeout when requets number equals to %d' , ( requests ) => {
602+ ] ) ( 'should not call channel.stopReceiveTimeout when requests number equals to %d' , ( requests ) => {
603603 const channel = {
604604 stopReceiveTimeout : jest . fn ( ) . mockName ( 'stopReceiveTimeout' ) ,
605605 startReceiveTimeout : jest . fn ( ) . mockName ( 'startReceiveTimeout' )
@@ -610,6 +610,68 @@ describe('ChannelConnection', () => {
610610
611611 expect ( channel . stopReceiveTimeout ) . toHaveBeenCalledTimes ( 0 )
612612 } )
613+
614+ it . each ( [
615+ [ 0 ] , [ 1 ] , [ 2 ] , [ 3 ] , [ 5 ] , [ 8 ] , [ 13 ] , [ 3000 ]
616+ ] ) ( 'should not call channel.stopReceiveTimeout or startReceiveTimeout when requests number equals to %d and connection is idle' , ( requests ) => {
617+ const channel = {
618+ stopReceiveTimeout : jest . fn ( ) . mockName ( 'stopReceiveTimeout' ) ,
619+ startReceiveTimeout : jest . fn ( ) . mockName ( 'startReceiveTimeout' )
620+ }
621+ const protocol = {
622+ queueObserverIfProtocolIsNotBroken : jest . fn ( ( ) => { } )
623+ }
624+ const connection = spyOnConnectionChannel ( { channel, protocolSupplier : ( ) => protocol } )
625+ connection . _setIdle ( { } )
626+ channel . stopReceiveTimeout . mockClear ( )
627+
628+ connection . _handleOngoingRequestsNumberChange ( requests )
629+
630+ expect ( channel . stopReceiveTimeout ) . toHaveBeenCalledTimes ( 0 )
631+ expect ( channel . startReceiveTimeout ) . toHaveBeenCalledTimes ( 0 )
632+ } )
633+
634+ it . each ( [
635+ [ 1 ] , [ 2 ] , [ 3 ] , [ 5 ] , [ 8 ] , [ 13 ] , [ 3000 ]
636+ ] ) ( 'should call channel.startReceiveTimeout when requests number equals to %d and connection is not idle anymore' , ( requests ) => {
637+ const channel = {
638+ stopReceiveTimeout : jest . fn ( ) . mockName ( 'stopReceiveTimeout' ) ,
639+ startReceiveTimeout : jest . fn ( ) . mockName ( 'startReceiveTimeout' )
640+ }
641+ const protocol = {
642+ queueObserverIfProtocolIsNotBroken : jest . fn ( ( ) => { } ) ,
643+ updateCurrentObserver : jest . fn ( ( ) => { } )
644+ }
645+ const connection = spyOnConnectionChannel ( { channel, protocolSupplier : ( ) => protocol } )
646+ connection . _setIdle ( { } )
647+ connection . _unsetIdle ( )
648+ channel . stopReceiveTimeout . mockClear ( )
649+
650+ connection . _handleOngoingRequestsNumberChange ( requests )
651+
652+ expect ( channel . stopReceiveTimeout ) . toHaveBeenCalledTimes ( 0 )
653+ expect ( channel . startReceiveTimeout ) . toHaveBeenCalledTimes ( 1 )
654+ } )
655+
656+ it ( 'should call channel.stopReceiveTimeout when requests number equals to 0 and connection is not idle anymore' , ( ) => {
657+ const channel = {
658+ stopReceiveTimeout : jest . fn ( ) . mockName ( 'stopReceiveTimeout' ) ,
659+ startReceiveTimeout : jest . fn ( ) . mockName ( 'startReceiveTimeout' )
660+ }
661+ const protocol = {
662+ queueObserverIfProtocolIsNotBroken : jest . fn ( ( ) => { } ) ,
663+ updateCurrentObserver : jest . fn ( ( ) => { } )
664+ }
665+ const connection = spyOnConnectionChannel ( { channel, protocolSupplier : ( ) => protocol } )
666+ connection . _setIdle ( { } )
667+ connection . _unsetIdle ( )
668+ channel . stopReceiveTimeout . mockClear ( )
669+
670+ connection . _handleOngoingRequestsNumberChange ( 0 )
671+
672+ expect ( channel . stopReceiveTimeout ) . toHaveBeenCalledTimes ( 1 )
673+ expect ( channel . startReceiveTimeout ) . toHaveBeenCalledTimes ( 0 )
674+ } )
613675 } )
614676
615677 describe ( '.resetAndFlush()' , ( ) => {
@@ -1181,6 +1243,44 @@ describe('ChannelConnection', () => {
11811243 } )
11821244
11831245 describe ( '.hasOngoingObservableRequests()' , ( ) => {
1246+ it ( 'should return false if connection is idle' , ( ) => {
1247+ const protocol = {
1248+ hasOngoingObservableRequests : jest . fn ( ( ) => true ) ,
1249+ queueObserverIfProtocolIsNotBroken : jest . fn ( ( ) => { } )
1250+ }
1251+ const channel = {
1252+ stopReceiveTimeout : jest . fn ( ) . mockName ( 'stopReceiveTimeout' )
1253+ }
1254+
1255+ const connection = spyOnConnectionChannel ( { protocolSupplier : ( ) => protocol , channel } )
1256+ connection . _setIdle ( { } )
1257+
1258+ const result = connection . hasOngoingObservableRequests ( )
1259+
1260+ expect ( result ) . toBe ( false )
1261+ expect ( protocol . hasOngoingObservableRequests ) . not . toBeCalledWith ( )
1262+ } )
1263+
1264+ it ( 'should redirect request to the protocol when connection is not idle anymore' , ( ) => {
1265+ const protocol = {
1266+ hasOngoingObservableRequests : jest . fn ( ( ) => true ) ,
1267+ queueObserverIfProtocolIsNotBroken : jest . fn ( ( ) => { } ) ,
1268+ updateCurrentObserver : jest . fn ( ( ) => { } )
1269+ }
1270+ const channel = {
1271+ stopReceiveTimeout : jest . fn ( ) . mockName ( 'stopReceiveTimeout' )
1272+ }
1273+
1274+ const connection = spyOnConnectionChannel ( { protocolSupplier : ( ) => protocol , channel } )
1275+ connection . _setIdle ( { } )
1276+ connection . _unsetIdle ( )
1277+
1278+ const result = connection . hasOngoingObservableRequests ( )
1279+
1280+ expect ( result ) . toBe ( true )
1281+ expect ( protocol . hasOngoingObservableRequests ) . toBeCalledWith ( )
1282+ } )
1283+
11841284 it ( 'should call redirect request to the protocol' , ( ) => {
11851285 const protocol = {
11861286 hasOngoingObservableRequests : jest . fn ( ( ) => true )
@@ -1195,6 +1295,41 @@ describe('ChannelConnection', () => {
11951295 } )
11961296 } )
11971297
1298+ describe ( '._setIdle()' , ( ) => {
1299+ it ( 'should stop receive timeout and enqueue observer' , ( ) => {
1300+ const protocol = {
1301+ queueObserverIfProtocolIsNotBroken : jest . fn ( ( ) => { } )
1302+ }
1303+ const channel = {
1304+ stopReceiveTimeout : jest . fn ( ) . mockName ( 'stopReceiveTimeout' )
1305+ }
1306+ const observer = {
1307+ onComplete : ( ) => { }
1308+ }
1309+
1310+ const connection = spyOnConnectionChannel ( { protocolSupplier : ( ) => protocol , channel } )
1311+
1312+ connection . _setIdle ( observer )
1313+
1314+ expect ( channel . stopReceiveTimeout ) . toBeCalledTimes ( 1 )
1315+ expect ( protocol . queueObserverIfProtocolIsNotBroken ) . toBeCalledWith ( observer )
1316+ } )
1317+ } )
1318+
1319+ describe ( '._unsetIdle()' , ( ) => {
1320+ it ( 'should update current observer' , ( ) => {
1321+ const protocol = {
1322+ updateCurrentObserver : jest . fn ( ( ) => { } )
1323+ }
1324+
1325+ const connection = spyOnConnectionChannel ( { protocolSupplier : ( ) => protocol } )
1326+
1327+ connection . _unsetIdle ( )
1328+
1329+ expect ( protocol . updateCurrentObserver ) . toBeCalledTimes ( 1 )
1330+ } )
1331+ } )
1332+
11981333 function spyOnConnectionChannel ( {
11991334 channel,
12001335 errorHandler,
0 commit comments