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); } }
public static void handedOutTimestamps(TimestampRange range) { long count = range.getUpperBound() - range.getLowerBound() + 1L; logger.trace("Handing out {} timestamps, taking us to {}.", SafeArg.of("count", count), SafeArg.of("rangeUpperBound", range.getUpperBound())); }
@Test public void canRequestTimestampRangeWithGetFreshTimestamps() { int expectedNumTimestamps = 5; TimestampRange range = timestampService.getFreshTimestamps(expectedNumTimestamps); Assertions.assertThat((int) range.size()) .withFailMessage("Expected %d timestamps, got %d timestamps. (The returned range was: %d-%d)", expectedNumTimestamps, range.size(), range.getLowerBound(), range.getUpperBound()) .isGreaterThanOrEqualTo(1) .isLessThanOrEqualTo(expectedNumTimestamps); }
@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()); } }
public TimestampRange incrementBy(long delta) { TimestampRange range = timestamp.incrementBy(delta); upperLimit.increaseToAtLeast(range.getUpperBound()); return range; }
@Override public long getFreshTimestamp() { TimestampRange range = getFreshTimestamps(1); return range.getLowerBound(); }
@Test public void shouldHandOutRangesOfTheCorrectSize() { assertThat(timestamp.incrementBy(10).size(), is(10L)); }
@Test public void shouldHaveASizeThatInludesBothEndpoints() { assertThat(TimestampRange.createInclusiveRange(10, 12).size()).isEqualTo(3L); }
@Test public void shouldIncreaseTheValueToAHigherNumber() { TimestampRange range = timestamp.incrementBy(10); assertThat(range.getUpperBound(), is(INITIAL_TIMESTAMP + 10)); }
@Override public long getFreshTimestamp() { return getFreshTimestamps(1).getLowerBound(); }
modulus); long lowerBoundResidue = LongMath.mod(range.getLowerBound(), modulus); long shift = residue < lowerBoundResidue ? modulus + residue - lowerBoundResidue : residue - lowerBoundResidue; long candidate = range.getLowerBound() + shift; return range.getUpperBound() >= candidate ? OptionalLong.of(candidate) : OptionalLong.empty();
@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); }
@Test public void canRequestTimestampRangeWithGetFreshTimestamps() { int expectedNumTimestamps = 5; TimestampRange range = timestampService.getFreshTimestamps(expectedNumTimestamps); Assertions.assertThat((int) range.size()) .withFailMessage("Expected %d timestamps, got %d timestamps. (The returned range was: %d-%d)", expectedNumTimestamps, range.size(), range.getLowerBound(), range.getUpperBound()) .isGreaterThanOrEqualTo(1) .isLessThanOrEqualTo(expectedNumTimestamps); }
@Test public void shouldHaveASizeOf1WhenCreatedWithTheSameUpperAndLowerBounds() { assertThat(TimestampRange.createInclusiveRange(10, 10).size()).isEqualTo(1L); }
@Test public void handleConcurrentlyIncreasingTheValue() throws InterruptedException { for (int i = 0; i < 100; i++) { executor.submit(() -> timestamp.incrementBy(1)); } waitForExecutorToFinish(); assertThat(timestamp.incrementBy(1).getUpperBound(), is(INITIAL_TIMESTAMP + 101)); }
@Test public void canFastForwardToANewMinimumTimestamp() { long newMinimum = 2 * UPPER_LIMIT; timestamp.increaseTo(newMinimum); assertThat(timestamp.incrementBy(1).getLowerBound(), is(newMinimum + 1L)); verify(upperLimit).increaseToAtLeast(longThat(is(greaterThan(newMinimum)))); }
@Test public void timestampRangesAreReturnedInNonOverlappingOrder() { TimestampRange timestampRange1 = timestampService.getFreshTimestamps(10); TimestampRange timestampRange2 = timestampService.getFreshTimestamps(10); long firstUpperBound = timestampRange1.getUpperBound(); long secondLowerBound = timestampRange2.getLowerBound(); Assertions.assertThat(firstUpperBound).isLessThan(secondLowerBound); }
private static TimestampRange single(int ts) { return TimestampRange.createInclusiveRange(ts, ts); }
@Test public void shouldIncreaseUpperLimitWhenHandingOutNewTimestamps() { assertThat( timestamp.incrementBy(INITIAL_REMAINING_TIMESTAMPS + 10).getUpperBound(), is(UPPER_LIMIT + 10)); verify(upperLimit).increaseToAtLeast(UPPER_LIMIT + 10); }
@Override public long getFreshTimestamp() { TimestampRange range = getFreshTimestamps(1); return range.getLowerBound(); }