public static ResumeToken bytes(byte[] token) { return new ResumeToken(token); }
public void sent(Frame frame) { if (ResumeUtil.isTracked(frame)) { frames.put(currentPosition, frame.copy()); bufferSize += strategy.cost(frame); currentPosition += ResumeUtil.offset(frame); if (frames.size() > maxBufferSize) { Frame f = frames.remove(first(frames)); bufferSize -= strategy.cost(f); } } }
public static ResumeToken generate() { return new ResumeToken(getBytesFromUUID(UUID.randomUUID())); }
@Test public void supportsFrameCountBuffers() { cache = new ResumeCache(ResumePositionCounter.size(), 100); assertEquals(0, cache.getRemotePosition()); assertEquals(0, cache.getCurrentPosition()); assertEquals(0, cache.getEarliestResendPosition()); assertEquals(0, cache.size()); cache.sent(STREAM); assertEquals(0, cache.getRemotePosition()); assertEquals(14, cache.getCurrentPosition()); assertEquals(0, cache.getEarliestResendPosition()); assertEquals(14, cache.size()); cache.updateRemotePosition(14); assertEquals(14, cache.getRemotePosition()); assertEquals(14, cache.getCurrentPosition()); assertEquals(14, cache.getEarliestResendPosition()); assertEquals(0, cache.size()); cache.sent(CANCEL); assertEquals(14, cache.getRemotePosition()); assertEquals(20, cache.getCurrentPosition()); assertEquals(14, cache.getEarliestResendPosition()); assertEquals(6, cache.size());
@Test public void updatesPositions() { assertEquals(0, cache.getRemotePosition()); assertEquals(0, cache.getCurrentPosition()); assertEquals(0, cache.getEarliestResendPosition()); assertEquals(0, cache.size()); cache.sent(STREAM); assertEquals(0, cache.getRemotePosition()); assertEquals(14, cache.getCurrentPosition()); assertEquals(0, cache.getEarliestResendPosition()); assertEquals(1, cache.size()); cache.updateRemotePosition(14); assertEquals(14, cache.getRemotePosition()); assertEquals(14, cache.getCurrentPosition()); assertEquals(14, cache.getEarliestResendPosition()); assertEquals(0, cache.size()); cache.sent(CANCEL); assertEquals(14, cache.getRemotePosition()); assertEquals(20, cache.getCurrentPosition()); assertEquals(14, cache.getEarliestResendPosition()); assertEquals(1, cache.size()); cache.updateRemotePosition(20);
@Test public void supportsZeroBuffer() { cache = new ResumeCache(ResumePositionCounter.frames(), 0); cache.sent(STREAM); cache.sent(STREAM); cache.sent(STREAM); assertEquals(0, cache.getRemotePosition()); assertEquals(42, cache.getCurrentPosition()); assertEquals(42, cache.getEarliestResendPosition()); assertEquals(0, cache.size()); }
@Test public void testFromUuid() { UUID x = UUID.fromString("3bac9870-3873-403a-99f4-9728aa8c7860"); ResumeToken t = ResumeToken.bytes(ResumeToken.getBytesFromUUID(x)); ResumeToken t2 = ResumeToken.bytes(ResumeToken.getBytesFromUUID(x)); assertEquals("3bac98703873403a99f49728aa8c7860", t.toString()); assertEquals(t.hashCode(), t2.hashCode()); assertEquals(t, t2); } }
public static boolean isTracked(Frame frame) { return isTracked(frame.getType()); }
@Test public void startsEmpty() { Flux<Frame> x = cache.resend(0); assertEquals(0L, (long) x.count().block()); cache.updateRemotePosition(0); }
public Flux<Frame> resend(int remotePosition) { updateRemotePosition(remotePosition); if (remotePosition == currentPosition) { return Flux.empty(); } List<Frame> resend = new ArrayList<>(); for (Map.Entry<Integer, Frame> cachePosition : frames.entrySet()) { if (remotePosition < cachePosition.getKey()) { resend.add(cachePosition.getValue()); } // TODO error handling } return Flux.fromIterable(resend); }
public int getEarliestResendPosition() { if (frames.isEmpty()) { return currentPosition; } else { return first(frames); } }
public void updateRemotePosition(int remotePosition) { if (remotePosition > currentPosition) { throw new IllegalStateException( "Remote ahead of " + lastRemotePosition + " , expected " + remotePosition); } if (remotePosition == lastRemotePosition) { return; } if (remotePosition < lastRemotePosition) { throw new IllegalStateException( "Remote position moved back from " + lastRemotePosition + " to " + remotePosition); } lastRemotePosition = remotePosition; Iterator<Map.Entry<Integer, Frame>> positions = frames.entrySet().iterator(); while (positions.hasNext()) { Map.Entry<Integer, Frame> cachePosition = positions.next(); if (cachePosition.getKey() <= remotePosition) { positions.remove(); bufferSize -= strategy.cost(cachePosition.getValue()); cachePosition.getValue().release(); } // TODO check for a bad position } }
@Test(expected = IllegalStateException.class) public void failsForFutureResend() { cache.resend(1); }
public void sent(Frame frame) { if (ResumeUtil.isTracked(frame)) { frames.put(currentPosition, frame.copy()); bufferSize += strategy.cost(frame); currentPosition += ResumeUtil.offset(frame); if (frames.size() > maxBufferSize) { Frame f = frames.remove(first(frames)); bufferSize -= strategy.cost(f); } } }
@Test public void testUnsupportedTypes() { assertFalse(ResumeUtil.isTracked(FrameType.METADATA_PUSH)); assertFalse(ResumeUtil.isTracked(FrameType.RESUME)); assertFalse(ResumeUtil.isTracked(FrameType.RESUME_OK)); assertFalse(ResumeUtil.isTracked(FrameType.SETUP)); assertFalse(ResumeUtil.isTracked(FrameType.EXT)); assertFalse(ResumeUtil.isTracked(FrameType.KEEPALIVE)); }
@Test(expected = IllegalStateException.class) public void failsForFutureUpdatePosition() { cache.updateRemotePosition(1); }
public static ResumeToken bytes(byte[] token) { return new ResumeToken(token); }
public int getEarliestResendPosition() { if (frames.isEmpty()) { return currentPosition; } else { return first(frames); } }
@Test public void testSupportedTypes() { assertTrue(ResumeUtil.isTracked(FrameType.REQUEST_STREAM)); assertTrue(ResumeUtil.isTracked(FrameType.REQUEST_CHANNEL)); assertTrue(ResumeUtil.isTracked(FrameType.REQUEST_RESPONSE)); assertTrue(ResumeUtil.isTracked(FrameType.REQUEST_N)); assertTrue(ResumeUtil.isTracked(FrameType.CANCEL)); assertTrue(ResumeUtil.isTracked(FrameType.ERROR)); assertTrue(ResumeUtil.isTracked(FrameType.REQUEST_FNF)); assertTrue(ResumeUtil.isTracked(FrameType.PAYLOAD)); }