use of org.apache.distributedlog.zk.LimitedPermitManager in project bookkeeper by apache.
the class TestPermitManager method testLimitedPermitManager.
@Test(timeout = 60000)
public void testLimitedPermitManager() {
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
PermitManager pm = new LimitedPermitManager(1, 0, TimeUnit.SECONDS, executorService);
PermitManager.Permit permit1 = pm.acquirePermit();
PermitManager.Permit permit2 = pm.acquirePermit();
assertTrue(permit1.isAllowed());
assertFalse(permit2.isAllowed());
pm.releasePermit(permit2);
PermitManager.Permit permit3 = pm.acquirePermit();
assertFalse(permit3.isAllowed());
pm.releasePermit(permit3);
pm.releasePermit(permit1);
PermitManager.Permit permit4 = pm.acquirePermit();
assertTrue(permit4.isAllowed());
pm.releasePermit(permit4);
PermitManager pm2 = new LimitedPermitManager(2, 0, TimeUnit.SECONDS, executorService);
PermitManager.Permit permit5 = pm2.acquirePermit();
PermitManager.Permit permit6 = pm2.acquirePermit();
assertTrue(permit5.isAllowed());
assertTrue(permit6.isAllowed());
assertTrue(pm2.disallowObtainPermits(permit5));
assertFalse(pm2.disallowObtainPermits(permit6));
pm2.releasePermit(permit5);
pm2.releasePermit(permit6);
PermitManager.Permit permit7 = pm2.acquirePermit();
assertFalse(permit7.isAllowed());
pm2.releasePermit(permit7);
pm2.allowObtainPermits();
PermitManager.Permit permit8 = pm2.acquirePermit();
assertTrue(permit8.isAllowed());
pm2.releasePermit(permit2);
}
Aggregations