@@ -109,17 +109,20 @@ describe('middleware and engine to stream', () => {
109109
110110const RECONNECTED = 'CONNECTED' ;
111111describe ( 'retry logic in middleware connected to a port' , ( ) => {
112- it ( 'retries requests on reconnect message' , async ( ) => {
112+ let engineA : JsonRpcEngine | undefined ;
113+ let messages : any [ ] = [ ] ;
114+ let messageConsumer : any ;
115+ beforeEach ( ( ) => {
113116 // create guest
114- const engineA = new JsonRpcEngine ( ) ;
117+ engineA = new JsonRpcEngine ( ) ;
115118 const jsonRpcConnection = createStreamMiddleware ( {
116119 retryOnMessage : RECONNECTED ,
117120 } ) ;
118121 engineA . push ( jsonRpcConnection . middleware ) ;
119122
120123 // create port
121- let messageConsumer = noop ;
122- const messages : any [ ] = [ ] ;
124+ messageConsumer = noop ;
125+ messages = [ ] ;
123126 const extensionPort = {
124127 onMessage : {
125128 addListener : ( cb : any ) => {
@@ -143,15 +146,17 @@ describe('retry logic in middleware connected to a port', () => {
143146 clientSideStream
144147 . pipe ( connectionStream as unknown as Duplex )
145148 . pipe ( clientSideStream ) ;
149+ } ) ;
146150
151+ it ( 'retries requests on reconnect message' , async ( ) => {
147152 // request and expected result
148153 const req1 = { id : 1 , jsonrpc, method : 'test' } ;
149154 const req2 = { id : 2 , jsonrpc, method : 'test' } ;
150155 const res = { id : 1 , jsonrpc, result : 'test' } ;
151156
152157 // Initially sent once
153- const responsePromise1 = engineA . handle ( req1 ) ;
154- engineA . handle ( req2 ) ;
158+ const responsePromise1 = engineA ? .handle ( req1 ) ;
159+ engineA ? .handle ( req2 ) ;
155160 await artificialDelay ( ) ;
156161
157162 expect ( messages ) . toHaveLength ( 2 ) ;
@@ -179,4 +184,59 @@ describe('retry logic in middleware connected to a port', () => {
179184
180185 expect ( messages ) . toHaveLength ( 5 ) ;
181186 } ) ;
187+
188+ it ( 'throw error when requests are retried more than 3 times' , async ( ) => {
189+ // request and expected result
190+ const req = { id : 1 , jsonrpc, method : 'test' } ;
191+
192+ // Initially sent once, message count at 1
193+ engineA ?. handle ( req ) ;
194+ await artificialDelay ( ) ;
195+ expect ( messages ) . toHaveLength ( 1 ) ;
196+
197+ // Reconnected, gets sent again message count increased to 2
198+ messageConsumer ( {
199+ method : RECONNECTED ,
200+ } ) ;
201+ await artificialDelay ( ) ;
202+ expect ( messages ) . toHaveLength ( 2 ) ;
203+
204+ // Reconnected, gets sent again message count increased to 3
205+ messageConsumer ( {
206+ method : RECONNECTED ,
207+ } ) ;
208+ await artificialDelay ( ) ;
209+ expect ( messages ) . toHaveLength ( 3 ) ;
210+
211+ // Reconnected, gets sent again message count increased to 4
212+ messageConsumer ( {
213+ method : RECONNECTED ,
214+ } ) ;
215+ await artificialDelay ( ) ;
216+ expect ( messages ) . toHaveLength ( 4 ) ;
217+
218+ // Reconnected, error is thrrown when trying to resend request more that 3 times
219+ expect ( ( ) => {
220+ messageConsumer ( {
221+ method : RECONNECTED ,
222+ } ) ;
223+ } ) . toThrow ( 'StreamMiddleware - Retry limit exceeded for request id' ) ;
224+ } ) ;
225+
226+ it ( 'does not retry if the request has no id' , async ( ) => {
227+ // request and expected result
228+ const req = { id : undefined , jsonrpc, method : 'test' } ;
229+
230+ // Initially sent once, message count at 1
231+ engineA ?. handle ( req ) ;
232+ await artificialDelay ( ) ;
233+ expect ( messages ) . toHaveLength ( 1 ) ;
234+
235+ // Reconnected, but request is not re-submitted
236+ messageConsumer ( {
237+ method : RECONNECTED ,
238+ } ) ;
239+ await artificialDelay ( ) ;
240+ expect ( messages ) . toHaveLength ( 1 ) ;
241+ } ) ;
182242} ) ;
0 commit comments