private static TimestampRange createExclusiveRange(long start, long end) { checkArgument(end > start, "End is not ahead of start so cannot create an exclusive range"); return TimestampRange.createInclusiveRange(start, end - 1); } }
@Override public TimestampRange getFreshTimestamps(int timestampsToGet) { if (timestampsToGet <= 0) { throw new IllegalArgumentException("Argument must be positive: " + timestampsToGet); } long topValue = counter.addAndGet(timestampsToGet); return TimestampRange.createInclusiveRange(topValue - timestampsToGet + 1, topValue); }
private static TimestampRange single(int ts) { return TimestampRange.createInclusiveRange(ts, ts); }
private static TimestampRange range(int start, int end) { return TimestampRange.createInclusiveRange(start, end - 1); }
public TimestampRange incrementBy(long delta) { long upperBound = value.updateAndGet(current -> Math.addExact(current, delta)); long lowerBound = upperBound - delta + 1L; return TimestampRange.createInclusiveRange(lowerBound, upperBound); }
private static TimestampRange getFreshTimestampsFromDelegate(TimestampService timestampService, int timestamps) { if (timestamps == 1) { long timestamp = timestampService.getFreshTimestamp(); return TimestampRange.createInclusiveRange(timestamp, timestamp); } return timestampService.getFreshTimestamps(timestamps); }
@Override public TimestampRange getFreshTimestamps(int timestampsToGet) { if (timestampsToGet <= 0) { throw new IllegalArgumentException("Argument must be positive: " + timestampsToGet); } long timestampsToRequest = Math.min(timestampsToGet, MAX_TIMESTAMPS); long topValue = counter.addAndGet(timestampsToRequest); return TimestampRange.createInclusiveRange(topValue - timestampsToRequest + 1, topValue); } }
@Test public void shouldHaveTheSameSizeWhicheverWayRoundTheBoundsArePassed() { long upper = 10; long lower = 1; TimestampRange lowerUpper = TimestampRange.createInclusiveRange(lower, upper); TimestampRange upperLower = TimestampRange.createInclusiveRange(upper, lower); assertThat(lowerUpper.size()).isEqualTo(upperLower.size()); } }
@Test public void shouldHaveASizeThatInludesBothEndpoints() { assertThat(TimestampRange.createInclusiveRange(10, 12).size()).isEqualTo(3L); }
@Test public void shouldHaveASizeOf1WhenCreatedWithTheSameUpperAndLowerBounds() { assertThat(TimestampRange.createInclusiveRange(10, 10).size()).isEqualTo(1L); }
@Test public void getTimestampsDelegates() { int numTimestamps = 5; TimestampRange timestamps = TimestampRange.createInclusiveRange(1L, numTimestamps); when(timestampService.getFreshTimestamps(numTimestamps)).thenReturn(timestamps); assertThat(timelock.getFreshTimestamps(numTimestamps)).isEqualTo(timestamps); }
@Test public void returnsAbsentIfTimestampRangeDoesNotContainAnyValuesMatchingModulus() { TimestampRange oneTimestamp = TimestampRange.createInclusiveRange(77, 77); assertThat(TimestampRanges.getTimestampMatchingModulus(oneTimestamp, 6, 10)).isNotPresent(); } }
@Test public void freshTimestampsDelegatesToTimestampService() { int numTimestamps = 10; TimestampRange range = TimestampRange.createInclusiveRange(21L, 30L); when(timestampService.getFreshTimestamps(numTimestamps)).thenReturn(range); assertEquals(range, timelock.getFreshTimestamps(numTimestamps)); }
@Test public void getFreshTimestampsShouldFail() { TimestampRange timestampRange = TimestampRange.createInclusiveRange(1, 2); when(rawTimelockService.getFreshTimestamps(anyInt())).thenReturn(timestampRange); assertThrowsOnSecondCall(() -> timelockService.getFreshTimestamps(1)); }
private static TimestampRange createExclusiveRange(long start, long end) { checkArgument(end > start, "End is not ahead of start so cannot create an exclusive range"); return TimestampRange.createInclusiveRange(start, end - 1); } }
availableServer.stubFor(TIMELOCK_ONE_TIMESTAMP_MAPPING.willReturn(aResponse() .withStatus(200) .withBody(new ObjectMapper().writeValueAsString(TimestampRange.createInclusiveRange(88, 88))))); availableServer.stubFor(TIMELOCK_LOCK_MAPPING.willReturn(aResponse().withStatus(200).withBody("4")));
private static TimestampRange getFreshTimestampsFromDelegate(TimestampService timestampService, int timestamps) { if (timestamps == 1) { long timestamp = timestampService.getFreshTimestamp(); return TimestampRange.createInclusiveRange(timestamp, timestamp); } return timestampService.getFreshTimestamps(timestamps); }
public TimestampRange incrementBy(long delta) { long upperBound = value.updateAndGet(current -> Math.addExact(current, delta)); long lowerBound = upperBound - delta + 1L; return TimestampRange.createInclusiveRange(lowerBound, upperBound); }