NetworkUsageCollector implements Collector { @Override public void initialize(MetricRegistry registry) { if (!isTrafficStatsAPISupported()) { return; } registerCachedGaugesForUploadedBytes(registry, false); registerCachedGaugeForDownloadedBytes(registry, false); registerCachedGaugesForUploadedBytes(registry, true); registerCachedGaugeForDownloadedBytes(registry, true); } NetworkUsageCollector(AppTrafficStats appTrafficStats, MetricNamesGenerator metricNamesGenerator,
      long samplingInterval, TimeUnit timeUnit, App app); @Override void initialize(MetricRegistry registry);  }
@Test public void doesNotReportBytesUploadedIfItIsTheFirstTimeTheInfoIsCollected() { givenTheUploadedBytesAre(ANY_AMOUNT_OF_BYTES); collector.initialize(registry); Gauge<Long> gauge = bytesUploadedCaptor.getValue(); assertNull(gauge.getValue()); }
@Test public void doesNotReportBytesDownloadedIfItIsTheFirstTimeTheInfoIsCollected() { givenTheDownloadedBytesAre(ANY_AMOUNT_OF_BYTES); collector.initialize(registry); Gauge<Long> gauge = bytesDownloadedCaptor.getValue(); assertNull(gauge.getValue()); }
@Test public void collectsTheDifferenceOfBytesUploadedBetweenTwoGetValueCalls() { givenTheUploadedBytesAre(ANY_AMOUNT_OF_BYTES); collector.initialize(registry); Gauge<Long> gauge = bytesUploadedCaptor.getValue(); gauge.getValue(); givenTheUploadedBytesAre(ANY_AMOUNT_OF_BYTES + ANY_DELTA); Long expectedValue = gauge.getValue(); assertEquals(ANY_DELTA, expectedValue, COMPARATION_DELTA); }
@Test public void collectsTheDifferenceOfBytesDownloadedBetweenTwoGetValueCalls() { givenTheDownloadedBytesAre(ANY_AMOUNT_OF_BYTES); collector.initialize(registry); Gauge<Long> gauge = bytesDownloadedCaptor.getValue(); gauge.getValue(); givenTheDownloadedBytesAre(ANY_AMOUNT_OF_BYTES + ANY_DELTA); Long expectedValue = gauge.getValue(); assertEquals(ANY_DELTA, expectedValue, COMPARATION_DELTA); }