QuotaManager { public UUID acquirePermit(Map<String, Object> bindingVariables) throws Exception { return acquirePermit(null, bindingVariables); } QuotaManager();  QuotaManager(QuotaManagerConfig config);  QuotaManager(File configFile); QuotaManagerConfig getConfig(); void loadConfiguration(File configFile); void loadConfiguration(QuotaManagerConfig config); UUID acquirePermit(Map<String, Object> bindingVariables); UUID acquirePermit(UUID permitID, Map<String, Object> bindingVariables); void releasePermit(UUID id); List<QuotaHandlerStatus> getStatus(); Object getPaceLockObject();  }
@Test public void testQuotaKeys() throws Exception { QuotaManagerConfig config = new QuotaManagerConfig("testManager"); List<Quota> quotas = new ArrayList<>(); Quota quota = new Quota(); quota.setPermits(1); quota.setQuotaKeyFunction("key"); quotas.add(quota); QuotaManager manager = new QuotaManager(config); for(int i=0;i<10;i++) { HashMap<String, Object> bindings = new HashMap<>(); bindings.put("key", "key"+i); UUID id = manager.acquirePermit(bindings); Assert.assertNotNull(id); } }
@Test public void testAcquireException() throws Exception { QuotaManagerConfig config = new QuotaManagerConfig("testManager"); Quota quota = new Quota(); quota.setPermits(1); quota.setAcquireTimeoutMs(0); quota.setQuotaKeyFunction("key"); List<Quota> quotas = new ArrayList<>(); quotas.add(quota); config.setQuotas(quotas); HashMap<String, Object> bindings = new HashMap<>(); bindings.put("key", "key1"); QuotaManager manager = new QuotaManager(config); boolean exceptionThrown = false; try { UUID id = manager.acquirePermit(bindings); Assert.assertNotNull(id); manager.acquirePermit(bindings); Assert.assertTrue(false); } catch (TimeoutException e) { exceptionThrown = true; } Assert.assertTrue(exceptionThrown); }
@Test public void testNullQuotaKey() throws Exception { QuotaManagerConfig config = new QuotaManagerConfig("testManager"); Quota quota = new Quota(); quota.setPermits(1); quota.setQuotaKeyFunction("(key=='key1')?key:null"); List<Quota> quotas = new ArrayList<>(); quotas.add(quota); config.setQuotas(quotas); HashMap<String, Object> bindings = new HashMap<>(); bindings.put("key", "key2"); QuotaManager manager = new QuotaManager(config); for(int i=0;i<100;i++) { UUID id = manager.acquirePermit(bindings); Assert.assertNotNull(id); UUID id2 = manager.acquirePermit(bindings); Assert.assertNotNull(id2); } }