use of org.infinispan.counter.api.CounterManager in project infinispan by infinispan.
the class RemoveCounterTest method assertCounterRemove.
private void assertCounterRemove(String name, TestCounter counter, Factory factory) {
CounterManager manager = counterManager(0);
counter.remove();
assertTrue(cache(0, CounterModuleLifecycle.COUNTER_CACHE_NAME).isEmpty());
TestCounter anotherCounter = factory.get(manager, name);
assertTrue(counter.isSame(anotherCounter));
}
use of org.infinispan.counter.api.CounterManager in project infinispan by infinispan.
the class RemoveCounterTest method testCounterRemove.
private void testCounterRemove(Factory factory, String counterName) {
CounterManager manager = counterManager(0);
factory.define(manager, counterName);
TestCounter counter = factory.get(manager, counterName);
assertEquals(10, counter.getValue());
assertTrue(counter.isSame(factory.get(manager, counterName)));
assertCounterRemove(counterName, counter, factory);
counter.increment();
assertEquals(11, counter.getValue());
assertCounterRemove(counterName, counter, factory);
counter.decrement();
assertEquals(9, counter.getValue());
assertCounterRemove(counterName, counter, factory);
counter.reset();
assertEquals(10, counter.getValue());
}
use of org.infinispan.counter.api.CounterManager in project infinispan by infinispan.
the class RemoveCounterTest method testCounterManagerRemoveNonExisting.
private void testCounterManagerRemoveNonExisting(Factory factory, String counterName) {
// similar to testCounterManagerRemove but the remove() will be invoked in the CounterManager where the counter instance doesn't exist,
CounterManager manager = counterManager(0);
factory.define(manager, counterName);
TestCounter counter = factory.get(manager, counterName);
assertEquals(10, counter.getValue());
assertTrue(counter.isSame(factory.get(manager, counterName)));
counter = assertCounterManagerRemove(counterName, counter, factory, 1);
counter.increment();
assertEquals(11, counter.getValue());
counter = assertCounterManagerRemove(counterName, counter, factory, 1);
counter.decrement();
assertEquals(9, counter.getValue());
counter = assertCounterManagerRemove(counterName, counter, factory, 1);
counter.reset();
assertEquals(10, counter.getValue());
}
use of org.infinispan.counter.api.CounterManager in project infinispan by infinispan.
the class RestartCounterTest method testRuntimeCounters.
public void testRuntimeCounters() {
final CounterManager counterManager = counterManager(0);
// while the retries during state transfer aren't fixed, we need to wait for the cache to start everywhere
waitForClusterToForm(CounterModuleLifecycle.COUNTER_CACHE_NAME);
incrementAll(defaultCounters, counterManager);
incrementAll(defaultCounters, counterManager);
otherCounters.forEach(counterDefinition -> counterDefinition.define(counterManager));
incrementAll(otherCounters, counterManager);
assertCounterValue(defaultCounters, counterManager, 2, 2);
assertCounterValue(otherCounters, counterManager, 1, 1);
shutdownAndRestart();
// recreate the counter manager.
final CounterManager counterManager2 = counterManager(0);
Collection<CounterDefinition> othersPersisted = otherCounters.stream().filter(counterDefinition -> counterDefinition.storage == Storage.PERSISTENT).collect(Collectors.toList());
Collection<CounterDefinition> otherVolatile = otherCounters.stream().filter(counterDefinition -> counterDefinition.storage == Storage.VOLATILE).collect(Collectors.toList());
assertDefined(defaultCounters);
assertDefined(othersPersisted);
assertNotDefined(otherVolatile);
assertCounterValue(defaultCounters, counterManager2, 0, 2);
assertCounterValue(othersPersisted, counterManager2, -1, /*doesn't mather*/
1);
incrementAll(defaultCounters, counterManager2);
incrementAll(othersPersisted, counterManager2);
assertCounterValue(defaultCounters, counterManager2, 1, 3);
assertCounterValue(othersPersisted, counterManager2, -1, /*doesn't mather*/
2);
}
use of org.infinispan.counter.api.CounterManager 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);
}
}
Aggregations