@@ -3,7 +3,7 @@ import { handleRetriesFor, retryAfter } from '#js/util/Retries.js';
33import { MathJax as MJX , MathJaxObject } from '#js/components/global.js' ;
44
55/**
6- * Add the legacy MathJax.CallBack for teting v2-style restarts
6+ * Add the legacy MathJax.CallBack for testing v2-style restarts
77 */
88type MathJaxGlobal = MathJaxObject & {
99 /* eslint-disable @typescript-eslint/no-unsafe-function-type */
@@ -40,34 +40,33 @@ describe('handleRetriesFor() and retryAfter()', () => {
4040
4141 /********************************************************************************/
4242
43- test ( 'handleRetriesFor().then called after 3 retries' , ( ) => {
43+ test ( 'handleRetriesFor().then called after 3 retries' , async ( ) => {
4444 let n = 0 ;
45- handleRetriesFor ( ( ) => {
45+ const result = await handleRetriesFor ( ( ) => {
4646 if ( ++ n < 3 ) {
4747 const p = new Promise < void > ( ( ok , _fail ) => {
48- setTimeout ( ( ) => ok ( ) , 1 ) ;
48+ setTimeout ( ok , 1 ) ;
4949 } ) ;
5050 retryAfter ( p ) ;
5151 }
5252 return 'success' ;
53- } ) . then ( ( result : string ) => {
54- expect ( result ) . toBe ( 'success' ) ;
55- expect ( n ) . toBe ( 3 ) ;
5653 } ) ;
54+ expect ( result ) . toBe ( 'success' ) ;
55+ expect ( n ) . toBe ( 3 ) ;
5756 } ) ;
5857
5958 /********************************************************************************/
6059
61- test ( 'handleRetriesFor().catch called for fail on 3rd retry' , ( ) => {
60+ test ( 'handleRetriesFor().catch called for fail on 3rd retry' , async ( ) => {
6261 let n = 0 ;
63- handleRetriesFor ( ( ) => {
62+ await handleRetriesFor ( ( ) => {
6463 if ( ++ n < 3 ) {
6564 const p = new Promise < void > ( ( ok , fail ) => {
6665 setTimeout ( ( ) => ( n < 2 ? ok ( ) : fail ( 'fail' ) ) , 1 ) ;
6766 } ) ;
6867 retryAfter ( p ) ;
6968 }
70- return 'success' ;
69+ throw 'success' ;
7170 } ) . catch ( ( result : string ) => {
7271 expect ( result ) . toBe ( 'fail' ) ;
7372 expect ( n ) . toBe ( 2 ) ;
@@ -76,12 +75,12 @@ describe('handleRetriesFor() and retryAfter()', () => {
7675
7776 /********************************************************************************/
7877
79- test ( 'handleRetriesFor().catch called for error on 3rd retry' , ( ) => {
78+ test ( 'handleRetriesFor().catch called for error on 3rd retry' , async ( ) => {
8079 let n = 0 ;
81- handleRetriesFor ( ( ) => {
80+ await handleRetriesFor ( ( ) => {
8281 if ( ++ n < 3 ) {
8382 const p = new Promise < void > ( ( ok , _fail ) => {
84- setTimeout ( ( ) => ok ( ) , 1 ) ;
83+ setTimeout ( ok , 1 ) ;
8584 } ) ;
8685 retryAfter ( p ) ;
8786 }
@@ -94,28 +93,27 @@ describe('handleRetriesFor() and retryAfter()', () => {
9493
9594 /********************************************************************************/
9695
97- test ( 'v2 retry' , ( ) => {
96+ test ( 'v2 retry' , async ( ) => {
9897 let n = 0 ;
99- handleRetriesFor ( ( ) => {
98+ const result = await handleRetriesFor ( ( ) => {
10099 if ( ++ n < 3 ) {
101100 throw Object . assign ( new Error ( 'restart' ) , {
102101 restart : MathJax . Callback . mock ( ) , // mark this error as a v2 restart
103102 } ) ;
104103 }
105104 return 'success' ;
106- } ) . then ( ( result : string ) => {
107- expect ( result ) . toBe ( 'success' ) ;
108- expect ( n ) . toBe ( 3 ) ;
109105 } ) ;
106+ expect ( result ) . toBe ( 'success' ) ;
107+ expect ( n ) . toBe ( 3 ) ;
110108 } ) ;
111109
112110 /********************************************************************************/
113111
114- test ( 'handleRetriedFor () async success' , ( ) => {
112+ test ( 'handleRetriesFor () async success' , ( ) => {
115113 expect (
116114 handleRetriesFor ( async ( ) => {
117115 const wait = new Promise ( ( ok , _fail ) =>
118- setTimeout ( ( ) => ok ( 'success' ) , 1 )
116+ setTimeout ( ok , 1 , 'success' )
119117 ) ;
120118 return await wait ;
121119 } )
@@ -124,11 +122,11 @@ describe('handleRetriesFor() and retryAfter()', () => {
124122
125123 /********************************************************************************/
126124
127- test ( 'handleRetriedFor () async fails' , ( ) => {
125+ test ( 'handleRetriesFor () async fails' , ( ) => {
128126 expect (
129127 handleRetriesFor ( async ( ) => {
130128 const wait = new Promise ( ( _ok , fail ) =>
131- setTimeout ( ( ) => fail ( 'fail' ) , 1 )
129+ setTimeout ( fail , 1 , 'fail' )
132130 ) ;
133131 return await wait ;
134132 } )
@@ -137,20 +135,74 @@ describe('handleRetriesFor() and retryAfter()', () => {
137135
138136 /********************************************************************************/
139137
140- test ( 'handleRetriedFor () async with retry' , ( ) => {
138+ test ( 'handleRetriesFor () async with retry' , async ( ) => {
141139 let n = 0 ;
142- handleRetriesFor ( async ( ) => {
140+ const result = await handleRetriesFor ( async ( ) => {
143141 if ( ++ n < 3 ) {
144142 await new Promise < void > ( ( ok , _fail ) => setTimeout ( ok , 1 ) ) ;
145143 const p = new Promise < void > ( ( ok , _fail ) => {
146- setTimeout ( ( ) => ok ( ) , 1 ) ;
144+ setTimeout ( ok , 1 ) ;
147145 } ) ;
148146 retryAfter ( p ) ;
149147 }
150148 return 'success' ;
151- } ) . then ( ( result : string ) => {
149+ } ) ;
150+ expect ( result ) . toBe ( 'success' ) ;
151+ expect ( n ) . toBe ( 3 ) ;
152+ } ) ;
153+
154+ /********************************************************************************/
155+
156+ test ( 'retryAfter() without restart code' , async ( ) => {
157+ let n = 0 ;
158+ const result = await handleRetriesFor ( ( ) => {
159+ if ( n ++ ) return 'success' ;
160+ retryAfter ( Promise . resolve ( ) ) ;
161+ return 'failed' ;
162+ } )
163+ expect ( result ) . toBe ( 'success' ) ;
164+ expect ( n ) . toBe ( 2 ) ;
165+ } ) ;
166+
167+ /********************************************************************************/
168+
169+ test ( 'retryAfter() with restart code' , async ( ) => {
170+ let n = 0 ;
171+ const result = await handleRetriesFor ( ( ) => {
172+ if ( n ++ ) return 'failed' ;
173+ retryAfter ( Promise . resolve ( ) , ( ) => 'success' ) ;
174+ return 'failed' ;
175+ } ) ;
176+ expect ( result ) . toBe ( 'success' ) ;
177+ expect ( n ) . toBe ( 1 ) ;
178+ } ) ;
179+
180+ /********************************************************************************/
181+
182+ test ( 'retryAfter() throws' , async ( ) => {
183+ let n = 0 ;
184+ await handleRetriesFor ( ( ) => {
185+ if ( n ++ ) throw 'failed' ;
186+ retryAfter ( Promise . reject ( 'success' ) ) ;
187+ throw 'failed' ;
188+ } ) . catch ( ( result : string ) => {
152189 expect ( result ) . toBe ( 'success' ) ;
153- expect ( n ) . toBe ( 3 ) ;
190+ expect ( n ) . toBe ( 1 ) ;
191+ } ) ;
192+ } ) ;
193+
194+ /********************************************************************************/
195+
196+ test ( 'retryAfter() nested' , async ( ) => {
197+ let n = 0 ;
198+ await handleRetriesFor ( ( ) => {
199+ retryAfter ( new Promise < void > ( ( _ok , _fail ) => {
200+ retryAfter ( n ++ < 3 ? Promise . resolve ( ) : Promise . reject ( 'success' ) )
201+ } ) ) ;
202+ throw 'failed' ;
203+ } ) . catch ( ( result : string ) => {
204+ expect ( result ) . toBe ( 'success' ) ;
205+ expect ( n ) . toBe ( 4 ) ;
154206 } ) ;
155207 } ) ;
156208
0 commit comments