LimitedRemote implements Remote { @Override public ListenableFuture<Integer> post( final String path, final String shardKey, final Map json) { return tasks.add(new Callable<ListenableFuture<Integer>>() { @Override public ListenableFuture<Integer> call() throws Exception { return inner.post(path, shardKey, json); } }); } LimitedRemote(Remote inner, int maxConcurrency, int highWaterMark); @Override ListenableFuture<Integer> post(
            final String path,
            final String shardKey,
            final Map json); @Override boolean shutdown(long timeout, TimeUnit timeUnit);  }
@Test public void createMeterTest() { outer.post(url, "foo", json); verify(inner).post(url, "foo", json); }
@Test public void blockMeterTest() throws Exception { when(inner.post(anyString(), anyString(), anyMap())).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { return SettableFuture.<Integer>create(); } }); outer.post(url, "foo", json); outer.post(url, "foo", json2); verify(inner, times(1)).post(eq(url), eq("foo"), anyMap()); }
@Test public void overflowMeterTest() throws Exception { when(inner.post(anyString(), anyString(), anyMap())).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { return SettableFuture.<Integer>create(); } }); outer.post(url, "foo", json); outer.post(url, "foo", json2); outer.post(url, "foo", json3); ListenableFuture<Integer> overflow = outer.post(url, "foo", json4); assert (overflow.isDone()); exception.expect(ExecutionException.class); overflow.get(); }