RedissonCountDownLatch few bugs fixed, tests added
parent
207d89b34b
commit
6486428f91
@ -0,0 +1,37 @@
|
||||
package org.redisson.misc.internal;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
public class ThreadLocalSemaphore {
|
||||
|
||||
private final ThreadLocal<Semaphore> semaphore;
|
||||
private final Set<Semaphore> allValues = Collections.newSetFromMap(new ConcurrentHashMap<Semaphore, Boolean>());
|
||||
|
||||
public ThreadLocalSemaphore() {
|
||||
semaphore = new ThreadLocal<Semaphore>() {
|
||||
@Override protected Semaphore initialValue() {
|
||||
Semaphore value = new Semaphore(1);
|
||||
allValues.add(value);
|
||||
return value;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public Semaphore get() {
|
||||
return semaphore.get();
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
allValues.remove(get());
|
||||
semaphore.remove();
|
||||
}
|
||||
|
||||
public Collection<Semaphore> getAll() {
|
||||
return allValues;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package org.redisson;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.redisson.core.RCountDownLatch;
|
||||
|
||||
public class RedissonCountDownLatchConcurrentTest {
|
||||
|
||||
@Test
|
||||
public void testSingleCountDownAwait_SingleInstance() throws InterruptedException {
|
||||
int iterations = Runtime.getRuntime().availableProcessors()*2;
|
||||
|
||||
Redisson redisson = Redisson.create();
|
||||
final RCountDownLatch latch = redisson.getCountDownLatch("latch");
|
||||
latch.trySetCount(iterations);
|
||||
|
||||
ScheduledExecutorService executor = Executors.newScheduledThreadPool(iterations);
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
executor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
Assert.fail();
|
||||
}
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
executor = Executors.newScheduledThreadPool(iterations);
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
executor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
latch.await();
|
||||
} catch (InterruptedException e) {
|
||||
Assert.fail();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
executor.shutdown();
|
||||
executor.awaitTermination(10, TimeUnit.SECONDS);
|
||||
|
||||
redisson.shutdown();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package org.redisson;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.redisson.core.RCountDownLatch;
|
||||
|
||||
public class RedissonCountDownLatchTest {
|
||||
|
||||
@Test
|
||||
public void testCountDown() throws InterruptedException {
|
||||
Redisson redisson = Redisson.create();
|
||||
RCountDownLatch latch = redisson.getCountDownLatch("latch");
|
||||
latch.trySetCount(1);
|
||||
latch.countDown();
|
||||
latch.await();
|
||||
latch.countDown();
|
||||
latch.await();
|
||||
latch.countDown();
|
||||
latch.await();
|
||||
|
||||
RCountDownLatch latch1 = redisson.getCountDownLatch("latch1");
|
||||
latch1.trySetCount(1);
|
||||
latch1.countDown();
|
||||
latch1.countDown();
|
||||
latch1.await();
|
||||
|
||||
RCountDownLatch latch2 = redisson.getCountDownLatch("latch2");
|
||||
latch2.trySetCount(1);
|
||||
latch2.countDown();
|
||||
latch2.await();
|
||||
latch2.await();
|
||||
|
||||
RCountDownLatch latch3 = redisson.getCountDownLatch("latch3");
|
||||
latch3.await();
|
||||
|
||||
RCountDownLatch latch4 = redisson.getCountDownLatch("latch4");
|
||||
latch4.countDown();
|
||||
latch4.await();
|
||||
|
||||
redisson.shutdown();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue