Search in sources :

Example 1 with LimitedPermitManager

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);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PermitManager(org.apache.distributedlog.common.util.PermitManager) LimitedPermitManager(org.apache.distributedlog.zk.LimitedPermitManager) LimitedPermitManager(org.apache.distributedlog.zk.LimitedPermitManager) Test(org.junit.Test)

Aggregations

ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 PermitManager (org.apache.distributedlog.common.util.PermitManager)1 LimitedPermitManager (org.apache.distributedlog.zk.LimitedPermitManager)1 Test (org.junit.Test)1