@@ -23,11 +23,9 @@ jest.unstable_mockModule('@actions/core', () => ({
2323// Dynamic imports after mocking
2424const { RetryHelper} = await import ( '../src/retry-helper.js' )
2525
26- let retryHelper : any
27-
2826describe ( 'retry-helper tests' , ( ) => {
2927 beforeAll ( ( ) => {
30- retryHelper = new RetryHelper ( 3 , 0 , 0 )
28+ // @actions /core is mocked at module load above; nothing to set up here.
3129 } )
3230
3331 beforeEach ( ( ) => {
@@ -40,14 +38,22 @@ describe('retry-helper tests', () => {
4038 } )
4139
4240 it ( 'first attempt succeeds' , async ( ) => {
41+ const retryHelper : any = new RetryHelper ( 3 , 1 , 10 )
42+ const sleep = jest . fn ( ) . mockResolvedValue ( undefined )
43+ retryHelper . sleep = sleep
44+
4345 const actual = await retryHelper . execute ( async ( ) => {
4446 return 'some result'
4547 } )
4648 expect ( actual ) . toBe ( 'some result' )
4749 expect ( info ) . toHaveLength ( 0 )
50+ expect ( sleep ) . not . toHaveBeenCalled ( )
4851 } )
4952
5053 it ( 'second attempt succeeds' , async ( ) => {
54+ const retryHelper : any = new RetryHelper ( 3 , 1 , 10 )
55+ const sleep = jest . fn ( ) . mockResolvedValue ( undefined )
56+ retryHelper . sleep = sleep
5157 let attempts = 0
5258 const actual = await retryHelper . execute ( ( ) => {
5359 if ( ++ attempts == 1 ) {
@@ -60,10 +66,15 @@ describe('retry-helper tests', () => {
6066 expect ( actual ) . toBe ( 'some result' )
6167 expect ( info ) . toHaveLength ( 2 )
6268 expect ( info [ 0 ] ) . toBe ( 'some error' )
63- expect ( info [ 1 ] ) . toMatch ( / W a i t i n g .+ s e c o n d s b e f o r e t r y i n g a g a i n / )
69+ expect ( info [ 1 ] ) . toBe ( 'Waiting 1 seconds before trying again' )
70+ expect ( sleep ) . toHaveBeenCalledTimes ( 1 )
71+ expect ( sleep ) . toHaveBeenCalledWith ( 1 )
6472 } )
6573
6674 it ( 'third attempt succeeds' , async ( ) => {
75+ const retryHelper : any = new RetryHelper ( 3 , 1 , 10 )
76+ const sleep = jest . fn ( ) . mockResolvedValue ( undefined )
77+ retryHelper . sleep = sleep
6778 let attempts = 0
6879 const actual = await retryHelper . execute ( ( ) => {
6980 if ( ++ attempts < 3 ) {
@@ -76,12 +87,18 @@ describe('retry-helper tests', () => {
7687 expect ( actual ) . toBe ( 'some result' )
7788 expect ( info ) . toHaveLength ( 4 )
7889 expect ( info [ 0 ] ) . toBe ( 'some error 1' )
79- expect ( info [ 1 ] ) . toMatch ( / W a i t i n g . + s e c o n d s b e f o r e t r y i n g a g a i n / )
90+ expect ( info [ 1 ] ) . toBe ( ' Waiting 1 seconds before trying again' )
8091 expect ( info [ 2 ] ) . toBe ( 'some error 2' )
81- expect ( info [ 3 ] ) . toMatch ( / W a i t i n g .+ s e c o n d s b e f o r e t r y i n g a g a i n / )
92+ expect ( info [ 3 ] ) . toBe ( 'Waiting 2 seconds before trying again' )
93+ expect ( sleep ) . toHaveBeenCalledTimes ( 2 )
94+ expect ( sleep ) . toHaveBeenNthCalledWith ( 1 , 1 )
95+ expect ( sleep ) . toHaveBeenNthCalledWith ( 2 , 2 )
8296 } )
8397
8498 it ( 'all attempts fail succeeds' , async ( ) => {
99+ const retryHelper : any = new RetryHelper ( 3 , 1 , 10 )
100+ const sleep = jest . fn ( ) . mockResolvedValue ( undefined )
101+ retryHelper . sleep = sleep
85102 let attempts = 0
86103 let error : Error = null as unknown as Error
87104 try {
@@ -95,8 +112,42 @@ describe('retry-helper tests', () => {
95112 expect ( attempts ) . toBe ( 3 )
96113 expect ( info ) . toHaveLength ( 4 )
97114 expect ( info [ 0 ] ) . toBe ( 'some error 1' )
98- expect ( info [ 1 ] ) . toMatch ( / W a i t i n g . + s e c o n d s b e f o r e t r y i n g a g a i n / )
115+ expect ( info [ 1 ] ) . toBe ( ' Waiting 1 seconds before trying again' )
99116 expect ( info [ 2 ] ) . toBe ( 'some error 2' )
100- expect ( info [ 3 ] ) . toMatch ( / W a i t i n g .+ s e c o n d s b e f o r e t r y i n g a g a i n / )
117+ expect ( info [ 3 ] ) . toBe ( 'Waiting 2 seconds before trying again' )
118+ expect ( sleep ) . toHaveBeenCalledTimes ( 2 )
119+ expect ( sleep ) . toHaveBeenNthCalledWith ( 1 , 1 )
120+ expect ( sleep ) . toHaveBeenNthCalledWith ( 2 , 2 )
121+ } )
122+
123+ it ( 'server-side 500 errors are retried with exponential backoff' , async ( ) => {
124+ const retryHelper : any = new RetryHelper ( 4 , 2 , 10 )
125+ const sleep = jest . fn ( ) . mockResolvedValue ( undefined )
126+ retryHelper . sleep = sleep
127+ let attempts = 0
128+
129+ const actual = await retryHelper . execute ( ( ) => {
130+ if ( ++ attempts < 3 ) {
131+ const error : Error & { status ?: number } = new Error (
132+ `server error ${ attempts } `
133+ )
134+ error . status = 500
135+ throw error
136+ }
137+
138+ return Promise . resolve ( 'some result' )
139+ } )
140+
141+ expect ( actual ) . toBe ( 'some result' )
142+ expect ( attempts ) . toBe ( 3 )
143+ expect ( info ) . toEqual ( [
144+ 'server error 1' ,
145+ 'Waiting 2 seconds before trying again' ,
146+ 'server error 2' ,
147+ 'Waiting 4 seconds before trying again'
148+ ] )
149+ expect ( sleep ) . toHaveBeenCalledTimes ( 2 )
150+ expect ( sleep ) . toHaveBeenNthCalledWith ( 1 , 2 )
151+ expect ( sleep ) . toHaveBeenNthCalledWith ( 2 , 4 )
101152 } )
102153} )
0 commit comments