### Question:
TimeSeriesRepositoryImpl extends AbstractScheduledService implements TimeSeriesRepository { @VisibleForTesting synchronized void runSampler(Clock clock) { timestamps.add(clock.nowMillis()); long startNanos = clock.nowNanos(); for (RecordingStat<? extends Number> var : statRegistry.getStats()) { timeSeries.getUnchecked(var.getName()).addSample(var.sample()); } scrapeDuration.accumulate( Amount.of(clock.nowNanos() - startNanos, Time.NANOSECONDS).as(Time.MICROSECONDS)); } @Inject  TimeSeriesRepositoryImpl(
      StatRegistry statRegistry,
      @Named(SAMPLE_PERIOD) Amount<Long, Time> samplePeriod,
      @Named(SAMPLE_RETENTION_PERIOD) final Amount<Long, Time> retentionPeriod,
      BuildInfo buildInfo); @Override synchronized Set<String> getAvailableSeries(); @Override synchronized TimeSeries get(String name); @Override synchronized Iterable<Number> getTimestamps(); static final String SAMPLE_PERIOD; static final String SAMPLE_RETENTION_PERIOD; }### Answer:
@Test public void testSamplesInOrder() { RecordingStat<Integer> statA = mockedStat(); RecordingStat<Integer> statB = mockedStat(); RecordingStat<Integer> statC = mockedStat(); RecordingStat<Integer> statD = mockedStat(); expect(statRegistry.getStats()) .andReturn(ImmutableList.<RecordingStat<? extends Number>>of(statB, statA, statC, statD)); expect(statB.getName()).andReturn("statB"); expect(statB.sample()).andReturn(1); expect(statA.getName()).andReturn("statA"); expect(statA.sample()).andReturn(2); expect(statC.getName()).andReturn("statC"); expect(statC.sample()).andReturn(3); expect(statD.getName()).andReturn("statD"); expect(statD.sample()).andReturn(4); control.replay(); repo.runSampler(clock); }
@Test public void testDelayedExport() throws InterruptedException { RecordingStat<Integer> earlyExport = mockedStat(); for (int i = 1; i <= 4; i++) { expect(statRegistry.getStats()) .andReturn(ImmutableList.<RecordingStat<? extends Number>>of(earlyExport)); expect(earlyExport.getName()).andReturn("early"); expect(earlyExport.sample()).andReturn(i * 2); } RecordingStat<Integer> delayedExport = mockedStat(); expect(statRegistry.getStats()) .andReturn(ImmutableList.<RecordingStat<? extends Number>>of(earlyExport, delayedExport)); expect(earlyExport.getName()).andReturn("early"); expect(earlyExport.sample()).andReturn(10); expect(delayedExport.getName()).andReturn("delayed"); expect(delayedExport.sample()).andReturn(100); control.replay(); clock.setNowMillis(1000); for (int i = 0; i < 4; i++) { repo.runSampler(clock); clock.waitFor(1000); } expectTimestamps(1000L, 2000L, 3000L, 4000L); expectSeriesData("early", 2, 4, 6, 8); repo.runSampler(clock); expectTimestamps(1000L, 2000L, 3000L, 4000L, 5000L); expectSeriesData("early", 2, 4, 6, 8, 10); expectSeriesData("delayed", 0L, 0L, 0L, 0L, 100); }