DefaultVersionService implements VersionService { @Override public HybridLogicalTimestamp nextVersion() { final long physicalNow = this.clock.timestampMicros(); if (localTime.get() >= physicalNow) { localCounter.incrementAndGet(); traceBackwardDrift(physicalNow, localTime.get()); } else { if (physicalNowTooFarAhead(physicalNow, localTime)) { localCounter.incrementAndGet(); traceForwardDrift(physicalNow, localTime.get()); } else { localTime.set(physicalNow); localCounter.set(INITIAL_COUNTER); } } return new HybridLogicalTimestamp(localTime.get(), localCounter.get()); } DefaultVersionService(Clock clock);   DefaultVersionService(); @Override HybridLogicalTimestamp nextVersion(); @Override HybridLogicalTimestamp nextVersionUpdate(HybridLogicalTimestamp incoming);  }
@Test public void sendPhysicalTimeInThePast() { final long inThePast = 1489018784131L; final DefaultVersionService.Clock clock = () -> inThePast; DefaultVersionService v = new DefaultVersionService(clock); final DefaultVersionService.HybridLogicalTimestamp send = v.nextVersion(); final DefaultVersionService.HybridLogicalTimestamp send1 = v.nextVersion(); assertEquals(send.logicalTime(), send1.logicalTime()); assertTrue(send.counter() < send1.counter()); assertSame(send.counter(), send1.counter() - 1); }
@Test public void sendPhysicalTimeInTheFuture() { final int[] calls = {0}; final DefaultVersionService.Clock clock = () -> { if (calls[0]++ == 0) { return (System.currentTimeMillis() * 1000L); } return (System.currentTimeMillis() * 1000L) + DefaultVersionService.MAX_SYSTEM_TIME_FORWARD_DRIFT + 1; }; DefaultVersionService v = new DefaultVersionService(clock); final DefaultVersionService.HybridLogicalTimestamp send = v.nextVersion(); final DefaultVersionService.HybridLogicalTimestamp send1 = v.nextVersion(); assertEquals(send.logicalTime(), send1.logicalTime()); assertTrue(send.counter() < send1.counter()); assertSame(send.counter(), send1.counter() - 1); }
@Test public void sendPhysicalTime() { final long[] epoch = {System.currentTimeMillis() * 1000L}; final DefaultVersionService.Clock clock = () -> epoch[0]++; DefaultVersionService v = new DefaultVersionService(clock); final DefaultVersionService.HybridLogicalTimestamp send = v.nextVersion(); final DefaultVersionService.HybridLogicalTimestamp send1 = v.nextVersion(); assertTrue(send.logicalTime() < send1.logicalTime()); assertEquals(send.counter(), send1.counter()); assertEquals(send.counter(), send1.counter()); }