@Test public void testSleepOnClientDelegation() { ThrottlingPolicyFactory factory = new ThrottlingPolicyFactory(); SharedLimiterKey res1key = new SharedLimiterKey("res1"); Map<String, String> configMap = ImmutableMap.<String, String>builder() .put(BrokerConfigurationKeyGenerator.generateKey(factory, res1key, null, ThrottlingPolicyFactory.POLICY_KEY), TestWaitPolicy.class.getName()) .build(); ThrottlingGuiceServletConfig guiceServletConfig = new ThrottlingGuiceServletConfig(); Sleeper.MockSleeper sleeper = guiceServletConfig.mockSleeper(); guiceServletConfig.initialize(ConfigFactory.parseMap(configMap)); Injector injector = guiceServletConfig.getInjector(); LimiterServerResource limiterServer = injector.getInstance(LimiterServerResource.class); PermitRequest request = new PermitRequest(); request.setPermits(5); request.setResource(res1key.getResourceLimitedPath()); request.setVersion(ThrottlingProtocolVersion.BASE.ordinal()); PermitAllocation allocation = limiterServer.getSync(new ComplexResourceKey<>(request, new EmptyRecord())); Assert.assertEquals((long) allocation.getPermits(), 5); Assert.assertEquals((long) allocation.getWaitForPermitUseMillis(GetMode.DEFAULT), 0); Assert.assertTrue(sleeper.getRequestedSleeps().isEmpty()); request.setPermits(20); request.setVersion(ThrottlingProtocolVersion.BASE.ordinal()); allocation = limiterServer.getSync(new ComplexResourceKey<>(request, new EmptyRecord())); Assert.assertEquals((long) allocation.getPermits(), 20); Assert.assertEquals((long) allocation.getWaitForPermitUseMillis(GetMode.DEFAULT), 0); Assert.assertEquals((long) sleeper.getRequestedSleeps().peek(), 10); sleeper.reset(); request.setVersion(ThrottlingProtocolVersion.WAIT_ON_CLIENT.ordinal()); request.setPermits(20); allocation = limiterServer.getSync(new ComplexResourceKey<>(request, new EmptyRecord())); Assert.assertEquals((long) allocation.getPermits(), 20); Assert.assertEquals((long) allocation.getWaitForPermitUseMillis(GetMode.DEFAULT), 10); Assert.assertTrue(sleeper.getRequestedSleeps().isEmpty()); }
public PermitAllocation getSync(ComplexResourceKey<PermitRequest, EmptyRecord> key) { try { FutureCallback<PermitAllocation> callback = new FutureCallback<>(); get(key, callback); return callback.get(); } catch (ExecutionException ee) { Throwable t = ee.getCause(); if (t instanceof RestLiServiceException) { throw (RestLiServiceException) t; } else { throw new RuntimeException(t); } } catch (InterruptedException ie) { throw new RuntimeException(ie); } }
LimiterServerResource extends ComplexKeyResourceAsyncTemplate<PermitRequest, EmptyRecord, PermitAllocation> { public PermitAllocation getSync(ComplexResourceKey<PermitRequest, EmptyRecord> key) { try { FutureCallback<PermitAllocation> callback = new FutureCallback<>(); get(key, callback); return callback.get(); } catch (ExecutionException ee) { Throwable t = ee.getCause(); if (t instanceof RestLiServiceException) { throw (RestLiServiceException) t; } else { throw new RuntimeException(t); } } catch (InterruptedException ie) { throw new RuntimeException(ie); } } }
LimiterServerResource extends ComplexKeyResourceAsyncTemplate<PermitRequest, EmptyRecord, PermitAllocation> { public PermitAllocation getSync(ComplexResourceKey<PermitRequest, EmptyRecord> key) { try { FutureCallback<PermitAllocation> callback = new FutureCallback<>(); get(key, callback); return callback.get(); } catch (ExecutionException ee) { Throwable t = ee.getCause(); if (t instanceof RestLiServiceException) { throw (RestLiServiceException) t; } else { throw new RuntimeException(t); } } catch (InterruptedException ie) { throw new RuntimeException(ie); } }  }
LimiterServerResource extends ComplexKeyResourceAsyncTemplate<PermitRequest, EmptyRecord, PermitAllocation> { public PermitAllocation getSync(ComplexResourceKey<PermitRequest, EmptyRecord> key) { try { FutureCallback<PermitAllocation> callback = new FutureCallback<>(); get(key, callback); return callback.get(); } catch (ExecutionException ee) { Throwable t = ee.getCause(); if (t instanceof RestLiServiceException) { throw (RestLiServiceException) t; } else { throw new RuntimeException(t); } } catch (InterruptedException ie) { throw new RuntimeException(ie); } }  @Override @RestMethod.Get void get(
      ComplexResourceKey<PermitRequest, EmptyRecord> key,
      @CallbackParam final Callback<PermitAllocation> callback); PermitAllocation getSync(ComplexResourceKey<PermitRequest, EmptyRecord> key); }
LimiterServerResource extends ComplexKeyResourceAsyncTemplate<PermitRequest, EmptyRecord, PermitAllocation> { public PermitAllocation getSync(ComplexResourceKey<PermitRequest, EmptyRecord> key) { try { FutureCallback<PermitAllocation> callback = new FutureCallback<>(); get(key, callback); return callback.get(); } catch (ExecutionException ee) { Throwable t = ee.getCause(); if (t instanceof RestLiServiceException) { throw (RestLiServiceException) t; } else { throw new RuntimeException(t); } } catch (InterruptedException ie) { throw new RuntimeException(ie); } }  @Override @RestMethod.Get void get(
      ComplexResourceKey<PermitRequest, EmptyRecord> key,
      @CallbackParam final Callback<PermitAllocation> callback); PermitAllocation getSync(ComplexResourceKey<PermitRequest, EmptyRecord> key); static final long TIMEOUT_MILLIS; static final String BROKER_INJECT_NAME; static final String METRIC_CONTEXT_INJECT_NAME; static final String REQUEST_TIMER_INJECT_NAME; static final String LEADER_FINDER_INJECT_NAME; static final String REQUEST_TIMER_NAME; static final String PERMITS_REQUESTED_METER_NAME; static final String PERMITS_GRANTED_METER_NAME; static final String LIMITER_TIMER_NAME; static final String RESOURCE_ID_TAG; static final String LOCATION_301; }