@@ -18,7 +18,7 @@ import {
18
18
TransitionObject ,
19
19
} from "@provablehq/sdk/%%NETWORK%%.js" ;
20
20
import { beaconPrivateKeyString } from "./data/account-data" ;
21
- import * as utils from "../src/utils" ;
21
+ import { retryWithBackoff } from "../src/utils" ;
22
22
23
23
async function catchError ( f : ( ) => Promise < any > ) : Promise < Error | null > {
24
24
try {
@@ -237,24 +237,52 @@ describe("NodeConnection", () => {
237
237
} ) ;
238
238
239
239
it ( "should retry failed transaction submissions and eventually give up" , async ( ) => {
240
- const client = new AleoNetworkClient ( "http://localhost:1234" ) ;
241
-
242
- let attemptCount = 0 ;
243
-
244
- // @ts -ignore override for testing
245
- client [ "sendPost" ] = async ( ) => {
240
+ const client = new AleoNetworkClient ( "http://localhost:1234" ) ;
241
+
242
+ let attemptCount = 0 ;
243
+
244
+ client [ "sendPost" ] = async ( ) => {
245
+ attemptCount ++ ;
246
+ console . warn ( `fake sendPost attempt ${ attemptCount } ` ) ;
247
+ const error = new Error ( "503 Service Unavailable" ) as any ;
248
+ error . status = 503 ;
249
+ throw error ;
250
+ } ;
251
+
252
+ try {
253
+ await retryWithBackoff ( ( ) =>
254
+ client [ "sendPost" ] ( "http://fakeurl" , { method : "POST" } ) , {
255
+ retryOnStatus : [ 503 ] ,
256
+ }
257
+ ) ;
258
+ throw new Error ( "Expected retryWithBackoff to fail" ) ;
259
+ } catch ( err : any ) {
260
+ expect ( err . message ) . to . include ( "503" ) ;
261
+ expect ( attemptCount ) . to . be . greaterThan ( 1 ) ;
262
+ }
263
+ } ) ;
264
+
265
+ it ( "should retry solution submission and eventually throw" , async ( ) => {
266
+ const client = new AleoNetworkClient ( "http://localhost:1234" ) ;
267
+
268
+ let attemptCount = 0 ;
269
+
270
+ client [ "sendPost" ] = async function ( ) {
246
271
attemptCount ++ ;
247
- console . warn ( `fake sendPost attempt ${ attemptCount } ` ) ;
248
- throw Object . assign ( new Error ( "503 Service Unavailable" ) , { status : 503 } ) ;
249
- } ;
250
-
251
- try {
252
- await client . submitTransaction ( "dummy_tx_string" ) ;
253
- throw new Error ( "Expected submitTransaction to fail" ) ;
254
- } catch ( err : any ) {
255
- expect ( err . message ) . to . include ( "503" ) ;
272
+ throw Object . assign ( new Error ( "Network error" ) , { status : 503 } ) ;
273
+ } ;
274
+
275
+ try {
276
+ await retryWithBackoff ( ( ) =>
277
+ client [ "sendPost" ] ( "http://fakeurl" , { method : "POST" } ) , {
278
+ retryOnStatus : [ 503 ] ,
279
+ }
280
+ ) ;
281
+ throw new Error ( "Expected sendPost to fail" ) ;
282
+ } catch ( err : any ) {
283
+ expect ( err . message ) . to . include ( "Network error" ) ;
256
284
expect ( attemptCount ) . to . be . greaterThan ( 1 ) ;
257
- }
285
+ }
258
286
} ) ;
259
287
} ) ;
260
288
0 commit comments