|
45 | 45 | import com.google.testing.timing.FakeScheduledExecutorService; |
46 | 46 | import java.util.Random; |
47 | 47 | import java.util.concurrent.TimeUnit; |
| 48 | +import org.junit.After; |
48 | 49 | import org.junit.Before; |
49 | 50 | import org.junit.Test; |
50 | 51 | import org.junit.runner.RunWith; |
@@ -126,6 +127,11 @@ public void setUp() { |
126 | 127 | new Lazy<>(() -> fakeMemoryGaugeCollector)); |
127 | 128 | } |
128 | 129 |
|
| 130 | + @After |
| 131 | + public void tearDown() { |
| 132 | + shadowOf(Looper.getMainLooper()).idle(); |
| 133 | + } |
| 134 | + |
129 | 135 | @Test |
130 | 136 | public void testStartCollectingGaugesStartsCollectingMetricsDefault() { |
131 | 137 | PerfSession fakeSession = createTestSession(1); |
@@ -347,7 +353,6 @@ public void testGaugeCounterStartsAJobToConsumeTheGeneratedMetrics() { |
347 | 353 | .isEqualTo(TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS); |
348 | 354 |
|
349 | 355 | fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask(); |
350 | | - shadowOf(Looper.getMainLooper()).idle(); |
351 | 356 |
|
352 | 357 | // Generate additional metrics, but doesn't start logging them as it hasn't met the threshold. |
353 | 358 | generateMetricsAndIncrementCounter(5); |
@@ -418,59 +423,59 @@ public void testUpdateAppStateHandlesMultipleAppStates() { |
418 | 423 | assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(1)); |
419 | 424 | } |
420 | 425 |
|
421 | | - // @Test |
422 | | - // public void testGaugeManagerHandlesMultipleSessionIds() { |
423 | | - // PerfSession fakeSession = createTestSession(1); |
424 | | - // fakeSession.setGaugeAndEventCollectionEnabled(true); |
425 | | - // testGaugeManager.setApplicationProcessState(ApplicationProcessState.BACKGROUND); |
426 | | - // testGaugeManager.startCollectingGauges(fakeSession); |
427 | | - // GaugeCounter.INSTANCE.setGaugeManager(testGaugeManager); |
428 | | - // |
429 | | - // // Generate metrics that don't exceed the GaugeCounter.MAX_COUNT. |
430 | | - // generateMetricsAndIncrementCounter(10); |
431 | | - // |
432 | | - // PerfSession updatedPerfSession = createTestSession(2); |
433 | | - // updatedPerfSession.setGaugeAndEventCollectionEnabled(true); |
434 | | - // |
435 | | - // // A new session and updated app state. |
436 | | - // testGaugeManager.startCollectingGauges(updatedPerfSession); |
437 | | - // testGaugeManager.setApplicationProcessState(ApplicationProcessState.FOREGROUND); |
438 | | - // |
439 | | - // assertThat(fakeScheduledExecutorService.isEmpty()).isFalse(); |
440 | | - // assertThat(fakeScheduledExecutorService.getDelayToNextTask(TimeUnit.MILLISECONDS)) |
441 | | - // .isEqualTo(TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS); |
442 | | - // |
443 | | - // fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask(); |
444 | | - // shadowOf(Looper.getMainLooper()).idle(); |
445 | | - // |
446 | | - // // Generate metrics for the new session. |
447 | | - // generateMetricsAndIncrementCounter(26); |
448 | | - // |
449 | | - // GaugeMetric recordedGaugeMetric = |
450 | | - // getLastRecordedGaugeMetric(ApplicationProcessState.BACKGROUND); |
451 | | - // |
452 | | - // // It flushes all metrics in the ConcurrentLinkedQueues. |
453 | | - // int recordedGaugeMetricsCount = |
454 | | - // recordedGaugeMetric.getAndroidMemoryReadingsCount() |
455 | | - // + recordedGaugeMetric.getCpuMetricReadingsCount(); |
456 | | - // assertThat(recordedGaugeMetricsCount).isEqualTo(10); |
457 | | - // |
458 | | - // assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(1)); |
459 | | - // |
460 | | - // // Simulate gauges collected in the new app state. |
461 | | - // fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask(); |
462 | | - // shadowOf(Looper.getMainLooper()).idle(); |
463 | | - // |
464 | | - // recordedGaugeMetric = getLastRecordedGaugeMetric(ApplicationProcessState.BACKGROUND); |
465 | | - // |
466 | | - // // Verify the metrics in the new app state. |
467 | | - // recordedGaugeMetricsCount = |
468 | | - // recordedGaugeMetric.getAndroidMemoryReadingsCount() |
469 | | - // + recordedGaugeMetric.getCpuMetricReadingsCount(); |
470 | | - // assertThat(recordedGaugeMetricsCount).isEqualTo(26); |
471 | | - // |
472 | | - // assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(2)); |
473 | | - // } |
| 426 | + @Test |
| 427 | + public void testGaugeManagerHandlesMultipleSessionIds() { |
| 428 | + PerfSession fakeSession = createTestSession(1); |
| 429 | + fakeSession.setGaugeAndEventCollectionEnabled(true); |
| 430 | + testGaugeManager.setApplicationProcessState(ApplicationProcessState.BACKGROUND); |
| 431 | + testGaugeManager.startCollectingGauges(fakeSession); |
| 432 | + GaugeCounter.INSTANCE.setGaugeManager(testGaugeManager); |
| 433 | + |
| 434 | + // Generate metrics that don't exceed the GaugeCounter.MAX_COUNT. |
| 435 | + generateMetricsAndIncrementCounter(10); |
| 436 | + |
| 437 | + PerfSession updatedPerfSession = createTestSession(2); |
| 438 | + updatedPerfSession.setGaugeAndEventCollectionEnabled(true); |
| 439 | + |
| 440 | + // A new session and updated app state. |
| 441 | + testGaugeManager.startCollectingGauges(updatedPerfSession); |
| 442 | + testGaugeManager.setApplicationProcessState(ApplicationProcessState.FOREGROUND); |
| 443 | + |
| 444 | + assertThat(fakeScheduledExecutorService.isEmpty()).isFalse(); |
| 445 | + assertThat(fakeScheduledExecutorService.getDelayToNextTask(TimeUnit.MILLISECONDS)) |
| 446 | + .isEqualTo(TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS); |
| 447 | + |
| 448 | + fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask(); |
| 449 | + shadowOf(Looper.getMainLooper()).idle(); |
| 450 | + |
| 451 | + // Generate metrics for the new session. |
| 452 | + generateMetricsAndIncrementCounter(26); |
| 453 | + |
| 454 | + GaugeMetric recordedGaugeMetric = |
| 455 | + getLastRecordedGaugeMetric(ApplicationProcessState.BACKGROUND); |
| 456 | + |
| 457 | + // It flushes all metrics in the ConcurrentLinkedQueues. |
| 458 | + int recordedGaugeMetricsCount = |
| 459 | + recordedGaugeMetric.getAndroidMemoryReadingsCount() |
| 460 | + + recordedGaugeMetric.getCpuMetricReadingsCount(); |
| 461 | + assertThat(recordedGaugeMetricsCount).isEqualTo(10); |
| 462 | + |
| 463 | + assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(1)); |
| 464 | + |
| 465 | + // Simulate gauges collected in the new app state. |
| 466 | + fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask(); |
| 467 | + shadowOf(Looper.getMainLooper()).idle(); |
| 468 | + |
| 469 | + recordedGaugeMetric = getLastRecordedGaugeMetric(ApplicationProcessState.FOREGROUND); |
| 470 | + |
| 471 | + // Verify the metrics in the new app state. |
| 472 | + recordedGaugeMetricsCount = |
| 473 | + recordedGaugeMetric.getAndroidMemoryReadingsCount() |
| 474 | + + recordedGaugeMetric.getCpuMetricReadingsCount(); |
| 475 | + assertThat(recordedGaugeMetricsCount).isEqualTo(26); |
| 476 | + |
| 477 | + assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(2)); |
| 478 | + } |
474 | 479 |
|
475 | 480 | @Test |
476 | 481 | public void testStopCollectingGaugesStopsCollectingAllGaugeMetrics() { |
|
0 commit comments