Search in sources :

Example 1 with StrongTestCounter

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);
    }
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) ArrayList(java.util.ArrayList) StrongTestCounter(org.infinispan.counter.util.StrongTestCounter) Future(java.util.concurrent.Future) CounterManager(org.infinispan.counter.api.CounterManager) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 2 with StrongTestCounter

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));
    }
}
Also used : ArrayList(java.util.ArrayList) CounterManager(org.infinispan.counter.api.CounterManager) CyclicBarrier(java.util.concurrent.CyclicBarrier) StrongTestCounter(org.infinispan.counter.util.StrongTestCounter) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 3 with StrongTestCounter

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());
    }
}
Also used : StrongTestCounter(org.infinispan.counter.util.StrongTestCounter)

Example 4 with StrongTestCounter

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);
    }
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) ArrayList(java.util.ArrayList) StrongTestCounter(org.infinispan.counter.util.StrongTestCounter) Future(java.util.concurrent.Future) CounterManager(org.infinispan.counter.api.CounterManager) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Example 5 with StrongTestCounter

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);
}
Also used : StrongTestCounter(org.infinispan.counter.util.StrongTestCounter) CounterManager(org.infinispan.counter.api.CounterManager)

Aggregations

StrongTestCounter (org.infinispan.counter.util.StrongTestCounter)8 CounterManager (org.infinispan.counter.api.CounterManager)4 ArrayList (java.util.ArrayList)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 Future (java.util.concurrent.Future)3 AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Test (org.testng.annotations.Test)1