use of org.infinispan.counter.util.StrongTestCounter in project infinispan by infinispan.
the class StrongCounterTest method testCompareAndSwapConcurrent.
public void testCompareAndSwapConcurrent(Method method) throws ExecutionException, InterruptedException, TimeoutException {
// local mode will have 8 concurrent thread, cluster mode will have 8 concurrent threads (4 nodes, 2 threads per node)
final int numThreadsPerNode = clusterSize() == 1 ? 8 : 2;
final int totalThreads = clusterSize() * numThreadsPerNode;
final List<Future<Boolean>> workers = new ArrayList<>(totalThreads);
final String counterName = method.getName();
final CyclicBarrier barrier = new CyclicBarrier(totalThreads);
final AtomicIntegerArray retValues = new AtomicIntegerArray(totalThreads);
final long maxIterations = 100;
for (int i = 0; i < totalThreads; ++i) {
final int threadIndex = i;
final int cmIndex = i % clusterSize();
workers.add(fork(() -> {
long iteration = 0;
final long initialValue = 0;
long previousValue = initialValue;
CounterManager manager = counterManager(cmIndex);
StrongTestCounter counter = createCounter(manager, counterName, initialValue);
while (iteration < maxIterations) {
assertEquals(previousValue, counter.getValue());
long update = previousValue + 1;
barrier.await();
// all threads calling compareAndSet at the same time, only one should succeed
long ret = counter.compareAndSwap(previousValue, update);
boolean success = ret == previousValue;
previousValue = success ? update : ret;
retValues.set(threadIndex, success ? 1 : 0);
barrier.await();
assertUnique(retValues, iteration);
++iteration;
}
return true;
}));
}
for (Future<?> w : workers) {
w.get(1, TimeUnit.MINUTES);
}
}
use of org.infinispan.counter.util.StrongTestCounter in project infinispan by infinispan.
the class StrongCounterTest method testUniqueReturnValues.
public void testUniqueReturnValues(Method method) throws ExecutionException, InterruptedException, TimeoutException {
// local mode will have 8 concurrent thread, cluster mode will have 8 concurrent threads (4 nodes, 2 threads per node)
final int numThreadsPerNode = clusterSize() == 1 ? 8 : 2;
final int totalThreads = clusterSize() * numThreadsPerNode;
final List<Future<List<Long>>> workers = new ArrayList<>(totalThreads);
final String counterName = method.getName();
final CyclicBarrier barrier = new CyclicBarrier(totalThreads);
final long counterLimit = 1000;
for (int i = 0; i < totalThreads; ++i) {
final int cmIndex = i % clusterSize();
workers.add(fork(() -> {
List<Long> retValues = new LinkedList<>();
CounterManager manager = counterManager(cmIndex);
StrongTestCounter counter = createCounter(manager, counterName, 0);
long lastRet = 0;
barrier.await();
while (lastRet < counterLimit) {
lastRet = counter.addAndGet(1);
retValues.add(lastRet);
}
return retValues;
}));
}
final Set<Long> uniqueValuesCheck = new HashSet<>();
for (Future<List<Long>> w : workers) {
List<Long> returnValues = w.get(1, TimeUnit.MINUTES);
for (Long l : returnValues) {
assertTrue(format("Duplicated value %d", l), uniqueValuesCheck.add(l));
}
}
for (long l = 1; l < (counterLimit + 3); ++l) {
assertTrue(format("Value %d does not exists!", l), uniqueValuesCheck.contains(l));
}
}
use of org.infinispan.counter.util.StrongTestCounter in project infinispan by infinispan.
the class StrongCounterTest method testCompareAndSwap.
public void testCompareAndSwap(Method method) {
final String counterName = method.getName();
TestContext context = new TestContext();
context.printSeed(counterName);
long expect = context.random.nextLong();
long value = context.random.nextLong();
StrongTestCounter counter = createCounter(counterManager(0), counterName, expect);
for (int i = 0; i < 10; ++i) {
assertEquals(expect, counter.compareAndSwap(expect, value));
assertEquals(value, counter.getValue());
expect = value;
value = context.random.nextLong();
}
for (int i = 0; i < 10; ++i) {
long notExpected = context.random.nextLong();
while (expect == notExpected) {
notExpected = context.random.nextLong();
}
assertEquals(expect, counter.compareAndSwap(notExpected, value));
assertEquals(expect, counter.getValue());
}
}
use of org.infinispan.counter.util.StrongTestCounter in project infinispan by infinispan.
the class StrongCounterTest method testCompareAndSetConcurrent.
@Test(groups = "unstable", description = "ISPN-8786")
public void testCompareAndSetConcurrent(Method method) throws ExecutionException, InterruptedException, TimeoutException {
// local mode will have 8 concurrent thread, cluster mode will have 8 concurrent threads (4 nodes, 2 threads per node)
final int numThreadsPerNode = clusterSize() == 1 ? 8 : 2;
final int totalThreads = clusterSize() * numThreadsPerNode;
final List<Future<Boolean>> workers = new ArrayList<>(totalThreads);
final String counterName = method.getName();
final CyclicBarrier barrier = new CyclicBarrier(totalThreads);
final AtomicIntegerArray retValues = new AtomicIntegerArray(totalThreads);
final long maxIterations = 100;
for (int i = 0; i < totalThreads; ++i) {
final int threadIndex = i;
final int cmIndex = i % clusterSize();
workers.add(fork(() -> {
long iteration = 0;
final long initialValue = 0;
long previousValue = initialValue;
CounterManager manager = counterManager(cmIndex);
StrongTestCounter counter = createCounter(manager, counterName, initialValue);
while (iteration < maxIterations) {
assertEquals(previousValue, counter.getValue());
long update = previousValue + 1;
barrier.await();
// all threads calling compareAndSet at the same time, only one should succeed
boolean ret = counter.compareAndSet(previousValue, update);
if (ret) {
previousValue = update;
} else {
previousValue = counter.getValue();
}
retValues.set(threadIndex, ret ? 1 : 0);
barrier.await();
assertUnique(retValues, iteration);
++iteration;
}
return true;
}));
}
for (Future<?> w : workers) {
w.get(1, TimeUnit.MINUTES);
}
}
use of org.infinispan.counter.util.StrongTestCounter in project infinispan by infinispan.
the class BoundedCounterTest method testSimpleThreshold.
public void testSimpleThreshold(Method method) {
CounterManager counterManager = counterManager(0);
counterManager.defineCounter(method.getName(), CounterConfiguration.builder(CounterType.BOUNDED_STRONG).lowerBound(-1).upperBound(1).build());
StrongTestCounter counter = new StrongTestCounter(counterManager.getStrongCounter(method.getName()));
addAndAssertResult(counter, 1, 1);
assertOutOfBoundsAdd(counter, 1, 1);
addAndAssertResult(counter, -1, 0);
addAndAssertResult(counter, -1, -1);
assertOutOfBoundsAdd(counter, -1, -1);
counter.reset();
assertOutOfBoundsAdd(counter, 2, 1);
assertOutOfBoundsAdd(counter, -3, -1);
}
Aggregations