flowController.reserve( 1, outstandingMessage.receivedMessage().getMessage().getSerializedSize()); } catch (FlowController.MaxOutstandingElementCountReachedException
@Test public void testExtension_GiveUp() throws Exception { dispatcher.processReceivedMessages(Collections.singletonList(TEST_MESSAGE), NOOP_RUNNABLE); dispatcher.extendDeadlines(); assertThat(sentModAcks) .contains(ModAckItem.of(TEST_MESSAGE.getAckId(), Subscriber.MIN_ACK_DEADLINE_SECONDS)); sentModAcks.clear(); // If we run extendDeadlines after totalExpiration, we shouldn't send anything. // In particular, don't send negative modacks. clock.advance(1, TimeUnit.DAYS); dispatcher.extendDeadlines(); assertThat(sentModAcks).isEmpty(); // We should be able to reserve another item in the flow controller and not block shutdown flowController.reserve(1, 0); dispatcher.stop(); }
@Override public void reserve(long elements, long bytes) throws FlowControlException { super.reserve(elements, bytes); this.elementsReserved += elements; this.bytesReserved += bytes; this.callsToReserve += 1; }
public void reserve(T batch) throws FlowControlException { Preconditions.checkNotNull(batch); int elements = Ints.checkedCast(elementCounter.count(batch)); int bytes = Ints.checkedCast(byteCounter.count(batch)); flowController.reserve(elements, bytes); }
public void reserve(T batch) throws FlowControlException { Preconditions.checkNotNull(batch); int elements = Ints.checkedCast(elementCounter.count(batch)); int bytes = Ints.checkedCast(byteCounter.count(batch)); flowController.reserve(elements, bytes); }
@Override public void run() { try { permitsReserved.set(null); flowController.reserve(maxElementCount, maxNumBytes); } catch (FlowController.FlowControlException e) { throw new AssertionError(e); } } });
private void testRejectedReserveRelease( FlowController flowController, int maxElementCount, int maxNumBytes, Class<? extends FlowController.FlowControlException> expectedException) throws FlowController.FlowControlException { flowController.reserve(1, 1); try { flowController.reserve(maxElementCount, maxNumBytes); fail("Should thrown a FlowController.FlowControlException"); } catch (FlowController.FlowControlException e) { assertTrue(expectedException.isInstance(e)); } flowController.release(1, 1); flowController.reserve(maxElementCount, maxNumBytes); } }
private static void testBlockingReserveRelease( final FlowController flowController, final int maxElementCount, final int maxNumBytes) throws Exception { flowController.reserve(1, 1); final SettableFuture<?> permitsReserved = SettableFuture.create(); Future<?> finished = Executors.newCachedThreadPool() .submit( new Runnable() { @Override public void run() { try { permitsReserved.set(null); flowController.reserve(maxElementCount, maxNumBytes); } catch (FlowController.FlowControlException e) { throw new AssertionError(e); } } }); permitsReserved.get(); flowController.release(1, 1); finished.get(); }
@Test public void testRestoreAfterFail() throws FlowController.FlowControlException { FlowController flowController = new FlowController( FlowControlSettings.newBuilder() .setMaxOutstandingElementCount(2L) .setMaxOutstandingRequestBytes(1L) .setLimitExceededBehavior(LimitExceededBehavior.ThrowException) .build()); flowController.reserve(1, 1); try { flowController.reserve(1, 1); throw new IllegalStateException("flowController should not have any bytes left"); } catch (FlowController.MaxOutstandingRequestBytesReachedException e) { // Ignore. } flowController.reserve(1, 0); }
@Test public void testInvalidArguments() throws Exception { FlowController flowController = new FlowController( FlowControlSettings.newBuilder() .setMaxOutstandingElementCount(10L) .setMaxOutstandingRequestBytes(10L) .setLimitExceededBehavior(LimitExceededBehavior.Block) .build()); flowController.reserve(0, 0); try { flowController.reserve(-1, 1); fail("Must have thrown an illegal argument error"); } catch (IllegalArgumentException expected) { // Expected } try { flowController.reserve(1, -1); fail("Must have thrown an illegal argument error"); } catch (IllegalArgumentException expected) { // Expected } }
@Test public void testReserveRelease_noLimits_ok() throws Exception { FlowController flowController = new FlowController( FlowControlSettings.newBuilder() .setLimitExceededBehavior(LimitExceededBehavior.Block) .build()); flowController.reserve(1, 1); flowController.release(1, 1); }
@Test public void testReserveRelease_ok() throws Exception { FlowController flowController = new FlowController( FlowControlSettings.newBuilder() .setMaxOutstandingElementCount(10L) .setMaxOutstandingRequestBytes(10L) .setLimitExceededBehavior(LimitExceededBehavior.Block) .build()); flowController.reserve(1, 1); flowController.release(1, 1); }
@Test public void testReserveRelease_ignore_ok() throws Exception { FlowController flowController = new FlowController( FlowControlSettings.newBuilder() .setMaxOutstandingElementCount(1L) .setMaxOutstandingRequestBytes(1L) .setLimitExceededBehavior(LimitExceededBehavior.Ignore) .build()); flowController.reserve(1, 1); flowController.release(1, 1); }