Search in sources :

Example 1 with PermitManager

use of org.apache.distributedlog.common.util.PermitManager in project bookkeeper by apache.

the class TestPermitManager method testUnlimitedPermitManager.

@Test(timeout = 60000)
public void testUnlimitedPermitManager() {
    PermitManager pm = PermitManager.UNLIMITED_PERMIT_MANAGER;
    List<PermitManager.Permit> permits = new ArrayList<PermitManager.Permit>();
    for (int i = 0; i < 10; i++) {
        permits.add(pm.acquirePermit());
    }
    for (int i = 0; i < 10; i++) {
        assertTrue(permits.get(i).isAllowed());
        pm.releasePermit(permits.get(i));
    }
    PermitManager.Permit permit = pm.acquirePermit();
    pm.disallowObtainPermits(permit);
    pm.releasePermit(permit);
    for (int i = 0; i < 10; i++) {
        permits.add(pm.acquirePermit());
    }
    for (int i = 0; i < 10; i++) {
        assertTrue(permits.get(i).isAllowed());
        pm.releasePermit(permits.get(i));
    }
}
Also used : PermitManager(org.apache.distributedlog.common.util.PermitManager) LimitedPermitManager(org.apache.distributedlog.zk.LimitedPermitManager) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with PermitManager

use of org.apache.distributedlog.common.util.PermitManager 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

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