@@ -10,7 +10,7 @@ import * as PingMaker from "../../../../src/core/pings/maker";
1010import Glean from "../../../../src/core/glean" ;
1111import CoreEvents from "../../../../src/core/events" ;
1212import Plugin from "../../../../src/plugins" ;
13- import type { JSONObject } from "../../../../src/core/utils" ;
13+ import type { JSONArray , JSONObject } from "../../../../src/core/utils" ;
1414import { Context } from "../../../../src/core/context" ;
1515import { stopGleanUploader } from "../../../utils" ;
1616import EventMetricType from "../../../../src/core/metrics/types/event" ;
@@ -273,4 +273,47 @@ describe("PingMaker", function() {
273273 labeled_counter : { "glean.error.invalid_value" : { "glean.restarted" : 1 } }
274274 } ) ;
275275 } ) ;
276+
277+ it ( "should delete trailing restarted events" , async function ( ) {
278+ const ping = new PingType ( {
279+ name : "aPing" ,
280+ includeClientId : true ,
281+ sendIfEmpty : true ,
282+ } ) ;
283+ const event = new EventMetricType ( {
284+ category : "test" ,
285+ name : "aEvent" ,
286+ sendInPings : [ "aPing" ] ,
287+ lifetime : Lifetime . Ping ,
288+ disabled : false
289+ } ) ;
290+
291+ // Record events
292+ for ( let i = 0 ; i < 6 ; i ++ ) {
293+ if ( i === 0 || i === 4 ) {
294+ // Record a non-restarted event
295+ event . record ( ) ;
296+ // Wait for recording action to complete.
297+ await event . testGetValue ( ) ;
298+ } else {
299+ // Record a restarted event (easiest way)
300+ //
301+ // Un-initialize and re-initialize manually instead of using testResetGlean
302+ // in order to have control over the startTime at initialization.
303+ await testUninitializeGlean ( false ) ;
304+ // Move the clock backwards by one hour.
305+ //
306+ // This will generate incoherent timestamps in events at collection time
307+ // and record an `InvalidValue` error for the `glean.restarted` event.
308+ Context . startTime . setTime ( Context . startTime . getTime ( ) - 1000 * 60 * 60 ) ;
309+ await testInitializeGlean ( testAppId , true ) ;
310+ }
311+ }
312+
313+ await PingMaker . collectAndStorePing ( "ident" , ping ) ;
314+ const allPings = Object . fromEntries ( await Context . pingsDatabase . getAllPings ( ) ) ;
315+ const eventsArray = allPings [ "ident" ] [ "payload" ] [ "events" ] as JSONArray ;
316+
317+ assert . equal ( 5 , eventsArray . length ) ;
318+ } ) ;
276319} ) ;
0 commit comments