@@ -371,6 +371,36 @@ public void testGaugeCounterStartsAJobToConsumeTheGeneratedMetrics() throws Inte
371371    assertThat (recordedGaugeMetric .getSessionId ()).isEqualTo (testSessionId (1 ));
372372  }
373373
374+   @ Test 
375+   public  void  testGaugeCounterIsDecrementedWhenLogged () throws  InterruptedException  {
376+     int  priorGaugeCounter  = GaugeCounter .count ();
377+ 
378+     PerfSession  fakeSession  = createTestSession (1 );
379+     testGaugeManager .setApplicationProcessState (ApplicationProcessState .FOREGROUND );
380+     testGaugeManager .startCollectingGauges (fakeSession );
381+     GaugeCounter .setGaugeManager (testGaugeManager );
382+ 
383+     // There's no job to log the gauges. 
384+     assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
385+ 
386+     // Generate metrics that don't exceed the GaugeCounter.MAX_COUNT. 
387+     generateMetricsAndIncrementCounter (20 );
388+ 
389+     // There's still no job to log the gauges. 
390+     assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
391+ 
392+     generateMetricsAndIncrementCounter (10 );
393+ 
394+     assertThat (fakeScheduledExecutorService .isEmpty ()).isFalse ();
395+     assertThat (fakeScheduledExecutorService .getDelayToNextTask (TimeUnit .MILLISECONDS ))
396+         .isEqualTo (TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS );
397+ 
398+     assertThat (GaugeCounter .count ()).isEqualTo (priorGaugeCounter  + 30 );
399+     fakeScheduledExecutorService .simulateSleepExecutingAtMostOneTask ();
400+ 
401+     assertThat (GaugeCounter .count ()).isEqualTo (priorGaugeCounter );
402+   }
403+ 
374404  @ Test 
375405  public  void  testUpdateAppStateHandlesMultipleAppStates () {
376406    PerfSession  fakeSession  = createTestSession (1 );
0 commit comments