@@ -12,7 +12,7 @@ governing permissions and limitations under the License.
1212
1313const { setCurrentOrgId, context } = require ( '@adobe/aio-lib-ims' )
1414const { setStore } = require ( '@adobe/aio-lib-core-config' )
15- const { initSdk, getOutputFormat, columnWithArray, disableCliAuth, enableCliAuth, formatDuration, executeWithRetries } = require ( '../src/cloudmanager-helpers' )
15+ const { initSdk, getOutputFormat, columnWithArray, disableCliAuth, enableCliAuth, formatDuration, executeWithRetries, executeWithRetry } = require ( '../src/cloudmanager-helpers' )
1616const { init } = require ( '@adobe/aio-lib-cloudmanager' )
1717const { setDecodedToken, resetDecodedToken } = require ( 'jsonwebtoken' )
1818
@@ -196,3 +196,36 @@ describe('executeWithRetries', () => {
196196 expect ( mockFn . mock . calls . length ) . toEqual ( 6 )
197197 } )
198198} )
199+
200+ describe ( 'executeWithRetry' , ( ) => {
201+ afterEach ( ( ) => {
202+ jest . restoreAllMocks ( )
203+ } )
204+
205+ test ( 'should not retry when function succeed' , async ( ) => {
206+ const mockFn = jest . fn ( ) . mockResolvedValue ( 'success' )
207+
208+ executeWithRetry ( mockFn )
209+
210+ expect ( mockFn . mock . calls . length ) . toEqual ( 1 )
211+ } )
212+
213+ test ( 'should retry when error thrown' , async ( ) => {
214+ const mockFn = jest . fn ( )
215+ . mockRejectedValueOnce ( { } )
216+ . mockResolvedValue ( 'success' )
217+
218+ await executeWithRetry ( mockFn )
219+
220+ expect ( mockFn . mock . calls . length ) . toEqual ( 2 )
221+ } )
222+
223+ test ( 'should throw error after 3 attempts' , async ( ) => {
224+ const mockFn = jest . fn ( )
225+ . mockRejectedValue ( new Error ( ) )
226+
227+ await expect ( executeWithRetry ( mockFn ) ) . rejects . toThrow ( )
228+
229+ expect ( mockFn . mock . calls . length ) . toEqual ( 3 )
230+ } )
231+ } )
0 commit comments