org.redisson.api.RPermitExpirableSemaphore Java Examples

The following examples show how to use org.redisson.api.RPermitExpirableSemaphore. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedissonPermitExpirableSemaphoreTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testConcurrency_SingleInstance() throws InterruptedException {
    final AtomicInteger lockedCounter = new AtomicInteger();

    RPermitExpirableSemaphore s = redisson.getPermitExpirableSemaphore("test");
    s.trySetPermits(1);

    int iterations = 100;
    testSingleInstanceConcurrency(iterations, r -> {
        RPermitExpirableSemaphore s1 = redisson.getPermitExpirableSemaphore("test");
        try {
            String permitId = s1.acquire();
        int value = lockedCounter.get();
        lockedCounter.set(value + 1);
        s1.release(permitId);
        }catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    });

    assertThat(lockedCounter.get()).isEqualTo(iterations);
}
 
Example #2
Source File: RedissonPermitExpirableSemaphoreTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testAvailablePermits() throws InterruptedException {
    RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("test-semaphore");
    assertThat(semaphore.trySetPermits(2)).isTrue();
    Assert.assertEquals(2, semaphore.availablePermits());
    String acquire1 = semaphore.tryAcquire(200, 1000, TimeUnit.MILLISECONDS);
    assertThat(acquire1).isNotNull();
    String acquire2 = semaphore.tryAcquire(200, 1000, TimeUnit.MILLISECONDS);
    assertThat(acquire2).isNotNull();
    String acquire3 = semaphore.tryAcquire(200, 1000, TimeUnit.MILLISECONDS);
    assertThat(acquire3).isNull();
    Assert.assertEquals(0, semaphore.availablePermits());
    Thread.sleep(1100);
    String acquire4 = semaphore.tryAcquire(200, 1000, TimeUnit.MILLISECONDS);
    assertThat(acquire4).isNotNull();
    Thread.sleep(1100);
    Assert.assertEquals(2, semaphore.availablePermits());
}
 
Example #3
Source File: RedissonPermitExpirableSemaphoreTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateLeaseTime() throws InterruptedException {
    RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("test");
    semaphore.trySetPermits(1);
    assertThat(semaphore.updateLeaseTime("123", 1, TimeUnit.SECONDS)).isFalse();
    String id = semaphore.acquire();
    assertThat(semaphore.updateLeaseTime(id, 1, TimeUnit.SECONDS)).isTrue();
    Thread.sleep(1200);
    assertThat(semaphore.updateLeaseTime(id, 1, TimeUnit.SECONDS)).isFalse();
    String id2 = semaphore.tryAcquire(1, 1, TimeUnit.SECONDS);
    assertThat(semaphore.updateLeaseTime(id2, 3, TimeUnit.SECONDS)).isTrue();
    Thread.sleep(2800);
    assertThat(semaphore.availablePermits()).isZero();
    Thread.sleep(500);
    assertThat(semaphore.availablePermits()).isOne();
    assertThat(semaphore.updateLeaseTime(id2, 2, TimeUnit.SECONDS)).isFalse();
}
 
Example #4
Source File: RedissonPermitExpirableSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testConcurrencyLoop_MultiInstance() throws InterruptedException {
    final int iterations = 100;
    final AtomicInteger lockedCounter = new AtomicInteger();

    RPermitExpirableSemaphore s = redisson.getPermitExpirableSemaphore("test");
    s.trySetPermits(1);

    testMultiInstanceConcurrency(16, r -> {
        for (int i = 0; i < iterations; i++) {
            try {
                String permitId = r.getPermitExpirableSemaphore("test").acquire();
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int value = lockedCounter.get();
                lockedCounter.set(value + 1);
                r.getPermitExpirableSemaphore("test").release(permitId);
            }catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });

    assertThat(lockedCounter.get()).isEqualTo(16 * iterations);
}
 
Example #5
Source File: RedissonPermitExpirableSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddPermits() throws InterruptedException {
    RPermitExpirableSemaphore s = redisson.getPermitExpirableSemaphore("test");
    s.trySetPermits(10);
    
    s.addPermits(5);
    assertThat(s.availablePermits()).isEqualTo(15);
    s.addPermits(-10);
    assertThat(s.availablePermits()).isEqualTo(5);
}
 
Example #6
Source File: RedissonPermitExpirableSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testTrySetPermits() {
    RPermitExpirableSemaphore s = redisson.getPermitExpirableSemaphore("test");
    assertThat(s.trySetPermits(10)).isTrue();
    assertThat(s.availablePermits()).isEqualTo(10);
    assertThat(s.trySetPermits(15)).isFalse();
    assertThat(s.availablePermits()).isEqualTo(10);
}
 
Example #7
Source File: RedissonPermitExpirableSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpiration() throws InterruptedException {
    RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("some-key");
    semaphore.trySetPermits(1);
    semaphore.expire(3, TimeUnit.SECONDS);
    semaphore.tryAcquire(1, 1, TimeUnit.SECONDS);
    Thread.sleep(4100);
    assertThat(redisson.getKeys().count()).isZero();
}
 
Example #8
Source File: RedissonSetMultimapValues.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RPermitExpirableSemaphore getPermitExpirableSemaphore(V value) {
    return set.getPermitExpirableSemaphore(value);
}
 
Example #9
Source File: RedissonPermitExpirableSemaphoreTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test(expected = RedisException.class)
public void testReleaseWithoutPermits() {
    RPermitExpirableSemaphore s = redisson.getPermitExpirableSemaphore("test");
    s.release("1234");
}
 
Example #10
Source File: RedissonPermitExpirableSemaphoreTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void testNotExistent() {
    RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("testSemaphoreForNPE");
    Assert.assertEquals(0, semaphore.availablePermits());        
}
 
Example #11
Source File: RedissonMap.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RPermitExpirableSemaphore getPermitExpirableSemaphore(K key) {
    String lockName = getLockByMapKey(key, "permitexpirablesemaphore");
    return new RedissonPermitExpirableSemaphore(commandExecutor, lockName);
}
 
Example #12
Source File: TracingRSetCache.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RPermitExpirableSemaphore getPermitExpirableSemaphore(V value) {
  return new TracingRPermitExpirableSemaphore(cache.getPermitExpirableSemaphore(value),
      tracingRedissonHelper);
}
 
Example #13
Source File: RedissonSet.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RPermitExpirableSemaphore getPermitExpirableSemaphore(V value) {
    String lockName = getLockByValue(value, "permitexpirablesemaphore");
    return new RedissonPermitExpirableSemaphore(commandExecutor, lockName);
}
 
Example #14
Source File: RedissonSetCache.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RPermitExpirableSemaphore getPermitExpirableSemaphore(V value) {
    String lockName = getLockByValue(value, "permitexpirablesemaphore");
    return new RedissonPermitExpirableSemaphore(commandExecutor, lockName);
}
 
Example #15
Source File: RedissonTransactionalMap.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RPermitExpirableSemaphore getPermitExpirableSemaphore(K key) {
    throw new UnsupportedOperationException("getPermitExpirableSemaphore method is not supported in transaction");
}
 
Example #16
Source File: RedissonTransactionalSet.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RPermitExpirableSemaphore getPermitExpirableSemaphore(V value) {
    throw new UnsupportedOperationException("getPermitExpirableSemaphore method is not supported in transaction");
}
 
Example #17
Source File: RedissonTransactionalMapCache.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RPermitExpirableSemaphore getPermitExpirableSemaphore(K key) {
    throw new UnsupportedOperationException("getPermitExpirableSemaphore method is not supported in transaction");
}
 
Example #18
Source File: TracingRPermitExpirableSemaphore.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
public TracingRPermitExpirableSemaphore(RPermitExpirableSemaphore semaphore,
    TracingRedissonHelper tracingRedissonHelper) {
  super(semaphore, tracingRedissonHelper);
  this.semaphore = semaphore;
  this.tracingRedissonHelper = tracingRedissonHelper;
}
 
Example #19
Source File: TracingRMultimap.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RPermitExpirableSemaphore getPermitExpirableSemaphore(K key) {
  return new TracingRPermitExpirableSemaphore(map.getPermitExpirableSemaphore(key),
      tracingRedissonHelper);
}
 
Example #20
Source File: TracingRMap.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RPermitExpirableSemaphore getPermitExpirableSemaphore(K key) {
  return new TracingRPermitExpirableSemaphore(map.getPermitExpirableSemaphore(key),
      tracingRedissonHelper);
}
 
Example #21
Source File: TracingRSet.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RPermitExpirableSemaphore getPermitExpirableSemaphore(V value) {
  return new TracingRPermitExpirableSemaphore(set.getPermitExpirableSemaphore(value),
      tracingRedissonHelper);
}
 
Example #22
Source File: TracingRedissonClient.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RPermitExpirableSemaphore getPermitExpirableSemaphore(String name) {
  return new TracingRPermitExpirableSemaphore(redissonClient.getPermitExpirableSemaphore(name),
      tracingRedissonHelper);
}