@@ -107,4 +107,175 @@ describe("PingType", function() {
107107 const storedPings = await Context . pingsDatabase [ "store" ] . _getWholeStore ( ) ;
108108 assert . strictEqual ( Object . keys ( storedPings ) . length , 0 ) ;
109109 } ) ;
110+
111+ it ( "runs a validator with no metrics tests" , async function ( ) {
112+ const ping = new PingType ( {
113+ name : "custom" ,
114+ includeClientId : true ,
115+ sendIfEmpty : false ,
116+ reasonCodes : [ "test" ]
117+ } ) ;
118+
119+ // We did not call the testing API yet, internals should be undefined.
120+ assert . strictEqual ( ping [ "resolveTestPromiseFunction" ] , undefined ) ;
121+ assert . strictEqual ( ping [ "testValidator" ] , undefined ) ;
122+
123+ let validatorRun = false ;
124+ const p = ping . testBeforeNextSubmit ( r => {
125+ assert . strictEqual ( r , "test" ) ;
126+ validatorRun = true ;
127+ return Promise . resolve ( ) ;
128+ } ) ;
129+
130+ // Internals should be defined after the API was called.
131+ assert . notStrictEqual ( ping [ "resolveTestPromiseFunction" ] , undefined ) ;
132+ assert . notStrictEqual ( ping [ "testValidator" ] , undefined ) ;
133+
134+ ping . submit ( "test" ) ;
135+ await p ;
136+
137+ assert . ok ( validatorRun ) ;
138+ } ) ;
139+
140+ it ( "runs a validator with metrics tests" , async function ( ) {
141+ const TEST_VALUE = 2908 ;
142+
143+ const ping = new PingType ( {
144+ name : "custom" ,
145+ includeClientId : true ,
146+ sendIfEmpty : false ,
147+ reasonCodes : [ "test" ]
148+ } ) ;
149+ const counter = new CounterMetricType ( {
150+ category : "aCategory" ,
151+ name : "aCounterMetric" ,
152+ sendInPings : [ "custom" ] ,
153+ lifetime : Lifetime . Ping ,
154+ disabled : false
155+ } ) ;
156+ counter . add ( TEST_VALUE ) ;
157+
158+ let validatorRun = false ;
159+ const p = ping . testBeforeNextSubmit ( async r => {
160+ assert . strictEqual ( r , "test" ) ;
161+ assert . strictEqual ( await counter . testGetValue ( ) , TEST_VALUE ) ;
162+ validatorRun = true ;
163+ } ) ;
164+
165+ ping . submit ( "test" ) ;
166+ await p ;
167+
168+ assert . ok ( validatorRun ) ;
169+ } ) ;
170+
171+ it ( "runs a validator multiple times on the same ping" , async function ( ) {
172+ const ping = new PingType ( {
173+ name : "custom" ,
174+ includeClientId : true ,
175+ sendIfEmpty : false ,
176+ reasonCodes : [ "test1" , "test2" ]
177+ } ) ;
178+ const counter = new CounterMetricType ( {
179+ category : "aCategory" ,
180+ name : "aCounterMetric" ,
181+ sendInPings : [ "custom" ] ,
182+ lifetime : Lifetime . Ping ,
183+ disabled : false
184+ } ) ;
185+
186+ for ( let i = 1 ; i < 3 ; i ++ ) {
187+ counter . add ( i ) ;
188+
189+ let validatorRun = false ;
190+ const testPromise = ping . testBeforeNextSubmit ( async r => {
191+ assert . strictEqual ( r , `test${ i } ` ) ;
192+ assert . strictEqual ( await counter . testGetValue ( ) , i ) ;
193+ validatorRun = true ;
194+ } ) ;
195+
196+ await new Promise ( r => setTimeout ( r , 100 ) ) ;
197+
198+ ping . submit ( `test${ i } ` ) ;
199+ await testPromise ;
200+
201+ assert . ok ( validatorRun ) ;
202+ }
203+ } ) ;
204+
205+ it ( "runs a validator multiple times fails when not awaiting" , function ( ) {
206+ const ping = new PingType ( {
207+ name : "custom" ,
208+ includeClientId : true ,
209+ sendIfEmpty : false
210+ } ) ;
211+
212+ assert . strictEqual ( ping [ "testValidator" ] , undefined ) ;
213+
214+ const testFunction = async ( ) => Promise . resolve ( ) ;
215+ void ping . testBeforeNextSubmit ( testFunction ) ;
216+ assert . strictEqual ( ping [ "testValidator" ] , testFunction ) ;
217+
218+ void ping . testBeforeNextSubmit ( ( ) => Promise . resolve ( ) ) ;
219+ assert . strictEqual ( ping [ "testValidator" ] , testFunction ) ;
220+ } ) ;
221+
222+ it ( "runs a validator that rejects" , async function ( ) {
223+ const ping = new PingType ( {
224+ name : "custom" ,
225+ includeClientId : true ,
226+ sendIfEmpty : false
227+ } ) ;
228+
229+ // eslint-disable-next-line @typescript-eslint/require-await
230+ const p = ping . testBeforeNextSubmit ( async ( ) => {
231+ throw new Error ( "This should reject!" ) ;
232+ } ) ;
233+
234+ ping . submit ( ) ;
235+
236+ await assert . rejects ( p ) ;
237+ } ) ;
238+
239+ it ( "runs a validator: sorry" , async function ( ) {
240+ const ping = new PingType ( {
241+ name : "custom" ,
242+ includeClientId : true ,
243+ sendIfEmpty : false ,
244+ reasonCodes : [ "test" ]
245+ } ) ;
246+
247+ const counter = new CounterMetricType ( {
248+ category : "aCategory" ,
249+ name : "aCounterMetric" ,
250+ sendInPings : [ "custom" ] ,
251+ lifetime : Lifetime . Ping ,
252+ disabled : false
253+ } ) ;
254+
255+ let validatorRun = false ;
256+
257+ const p = ping . testBeforeNextSubmit ( async ( ) => {
258+ await new Promise < void > ( resolve => {
259+ setTimeout ( ( ) => resolve ( ) , 100 ) ;
260+ } ) ;
261+ const value = await counter . testGetValue ( ) ;
262+ console . log ( "!!!" , value ) ;
263+ assert . strictEqual ( value , 100 ) ;
264+ validatorRun = true ;
265+ } ) ;
266+ ping . submit ( "test" ) ;
267+
268+ for ( let i = 0 ; i < 100 ; i ++ ) {
269+ counter . add ( ) ;
270+ Context . dispatcher . launch ( async ( ) => {
271+ await new Promise < void > ( resolve => {
272+ setTimeout ( ( ) => resolve ( ) , 10 ) ;
273+ } ) ;
274+ } ) ;
275+ }
276+
277+ await p ;
278+
279+ assert . ok ( validatorRun ) ;
280+ } ) ;
110281} ) ;
0 commit comments