WriteLimiter { public void acquire() throws OverCapacityException { if (!streamLimiter.acquire()) { throw new OverCapacityException(String.format("Stream write capacity exceeded for stream %s", streamName)); } try { if (!globalLimiter.acquire()) { throw new OverCapacityException("Global write capacity exceeded"); } } catch (OverCapacityException ex) { streamLimiter.release(1); throw ex; } } WriteLimiter(String streamName, PermitLimiter streamLimiter, PermitLimiter globalLimiter); void acquire(); void release(); void release(int permits);  }
@Test public void testStreamOnly() throws Exception { SimplePermitLimiter streamLimiter = createPermitLimiter(false, 1); SimplePermitLimiter globalLimiter = createPermitLimiter(false, Integer.MAX_VALUE); WriteLimiter limiter = new WriteLimiter("test", streamLimiter, globalLimiter); limiter.acquire(); try { limiter.acquire(); fail("should have thrown stream limit exception"); } catch (OverCapacityException ex) { } assertPermits(streamLimiter, 1, globalLimiter, 1); }
@Test public void testDarkmode() throws Exception { SimplePermitLimiter streamLimiter = createPermitLimiter(true, Integer.MAX_VALUE); SimplePermitLimiter globalLimiter = createPermitLimiter(true, 1); WriteLimiter limiter = new WriteLimiter("test", streamLimiter, globalLimiter); limiter.acquire(); limiter.acquire(); assertPermits(streamLimiter, 2, globalLimiter, 2); }
@Test public void testUnsetDisableFeatureBeforePermitsExceeded() throws Exception { SettableFeature feature = new SettableFeature("test", 0); SimplePermitLimiter streamLimiter = createPermitLimiter(false, 1, feature); SimplePermitLimiter globalLimiter = createPermitLimiter(false, Integer.MAX_VALUE, feature); WriteLimiter limiter = new WriteLimiter("test", streamLimiter, globalLimiter); limiter.acquire(); try { limiter.acquire(); fail("should have thrown stream limit exception"); } catch (OverCapacityException ex) { } assertPermits(streamLimiter, 1, globalLimiter, 1); feature.set(10000); limiter.acquire(); assertPermits(streamLimiter, 2, globalLimiter, 2); }