Search in sources :

Example 1 with InCacheMode

use of org.infinispan.test.fwk.InCacheMode in project infinispan by infinispan.

the class PutForExternalReadTest method testKeyOnlyWrittenOnceOnOriginator.

// This test executes PFER on cache1, and expects that it will be relayed to cache2 == primary
// and then sent to cache1 again for backup. In scattered cache there's only one RPC.
@InCacheMode({ CacheMode.DIST_SYNC, CacheMode.REPL_SYNC })
public void testKeyOnlyWrittenOnceOnOriginator() throws Exception {
    final Cache<MagicKey, String> cache1 = cache(0, CACHE_NAME);
    final Cache<MagicKey, String> cache2 = cache(1, CACHE_NAME);
    final CyclicBarrier barrier = new CyclicBarrier(2);
    cache1.getAdvancedCache().getAsyncInterceptorChain().addInterceptor(new BaseAsyncInterceptor() {

        @Override
        public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
            if (command instanceof PutKeyValueCommand) {
                if (!ctx.isOriginLocal()) {
                    // wait first before the check
                    TestBlocking.await(barrier, 10, TimeUnit.SECONDS);
                    // and once more after the check
                    TestBlocking.await(barrier, 10, TimeUnit.SECONDS);
                }
            }
            return invokeNext(ctx, command);
        }
    }, 0);
    final MagicKey myKey = new MagicKey(cache2);
    cache1.putForExternalRead(myKey, value);
    // Verify that the key was not written on the origin by the time it was looped back
    barrier.await(10, TimeUnit.SECONDS);
    assertNull(cache1.get(myKey));
    // Verify that the key is written on the origin afterwards
    barrier.await(10, TimeUnit.SECONDS);
    eventually(() -> value.equals(cache1.get(myKey)) && value.equals(cache2.get(myKey)));
}
Also used : VisitableCommand(org.infinispan.commands.VisitableCommand) BaseAsyncInterceptor(org.infinispan.interceptors.BaseAsyncInterceptor) InvocationContext(org.infinispan.context.InvocationContext) MagicKey(org.infinispan.distribution.MagicKey) CyclicBarrier(java.util.concurrent.CyclicBarrier) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) InCacheMode(org.infinispan.test.fwk.InCacheMode)

Example 2 with InCacheMode

use of org.infinispan.test.fwk.InCacheMode in project infinispan by infinispan.

the class MultipleCacheManagersTest method defaultFactory.

@Factory
public Object[] defaultFactory() {
    try {
        // Ignore any inherited factory() method and only run methods defined in the current class
        Method factory = getClass().getMethod("factory");
        if (factory.getDeclaringClass() == getClass()) {
            if (getClass().getAnnotation(InCacheMode.class) != null || getClass().getAnnotation(InTransactionMode.class) != null) {
                return new Object[] { new TestFrameworkFailure<>(getClass(), new IllegalStateException("Tests with factory() methods ignore @InCacheMode and @InTransactionMode annotations, " + "please remove them.")) };
            }
            Object[] instances = factory();
            for (int i = 0; i < instances.length; i++) {
                if (instances[i].getClass() != getClass()) {
                    instances[i] = new TestFrameworkFailure<>(getClass(), "%s.factory() creates instances of %s", getClass().getName(), instances[i].getClass().getName());
                }
            }
            return instances;
        }
    } catch (NoSuchMethodException e) {
        throw new IllegalStateException("Every class should have factory method, at least inherited", e);
    }
    List<Consumer<MultipleCacheManagersTest>[]> allModifiers;
    try {
        Consumer<MultipleCacheManagersTest>[] cacheModeModifiers = getModifiers(InCacheMode.class, InCacheMode::value, MultipleCacheManagersTest::cacheMode);
        Consumer<MultipleCacheManagersTest>[] transactionModifiers = getModifiers(InTransactionMode.class, InTransactionMode::value, (t, m) -> t.transactional(m.isTransactional()));
        allModifiers = asList(cacheModeModifiers, transactionModifiers);
    } catch (Exception e) {
        return new Object[] { new TestFrameworkFailure<>(getClass(), e) };
    }
    int numTests = allModifiers.stream().mapToInt(m -> m.length).reduce(1, (m1, m2) -> m1 * m2);
    Object[] tests = new Object[numTests];
    tests[0] = this;
    Constructor<? extends MultipleCacheManagersTest> ctor;
    try {
        ctor = getClass().getConstructor();
    } catch (NoSuchMethodException e) {
        return new Object[] { new TestFrameworkFailure<>(getClass(), "Missing no-arg constructor in %s", getClass().getName()) };
    }
    for (int i = 1; i < tests.length; ++i) {
        try {
            tests[i] = ctor.newInstance();
        } catch (Exception e) {
            return new Object[] { new TestFrameworkFailure<>(getClass(), e) };
        }
    }
    int stride = 1;
    for (Consumer<MultipleCacheManagersTest>[] modifiers : allModifiers) {
        applyModifiers(tests, modifiers, stride);
        stride *= modifiers.length;
    }
    return tests;
}
Also used : Arrays(java.util.Arrays) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) Cache(org.infinispan.Cache) AfterMethod(org.testng.annotations.AfterMethod) AssertJUnit.assertTrue(org.testng.AssertJUnit.assertTrue) LockManager(org.infinispan.util.concurrent.locks.LockManager) AdvancedCache(org.infinispan.AdvancedCache) ConfigurationBuilderHolder(org.infinispan.configuration.parsing.ConfigurationBuilderHolder) CacheContainer(org.infinispan.manager.CacheContainer) Arrays.asList(java.util.Arrays.asList) InTransactionMode(org.infinispan.test.fwk.InTransactionMode) InCacheMode(org.infinispan.test.fwk.InCacheMode) Method(java.lang.reflect.Method) Transaction(javax.transaction.Transaction) MagicKey(org.infinispan.distribution.MagicKey) IdentityHashMap(java.util.IdentityHashMap) IMethodInstance(org.testng.IMethodInstance) Predicate(java.util.function.Predicate) BeforeClass(org.testng.annotations.BeforeClass) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) TransportFlags(org.infinispan.test.fwk.TransportFlags) LockingMode(org.infinispan.transaction.LockingMode) TestFrameworkFailure(org.infinispan.test.fwk.TestFrameworkFailure) TestResourceTracker.getCurrentTestShortName(org.infinispan.commons.test.TestResourceTracker.getCurrentTestShortName) List(java.util.List) Stream(java.util.stream.Stream) Annotation(java.lang.annotation.Annotation) TestCacheManagerFactory.createClusteredCacheManager(org.infinispan.test.fwk.TestCacheManagerFactory.createClusteredCacheManager) TestSelector(org.infinispan.test.fwk.TestSelector) TestResourceTracker(org.infinispan.commons.test.TestResourceTracker) PrivateGlobalConfigurationBuilder(org.infinispan.configuration.internal.PrivateGlobalConfigurationBuilder) Constructor(java.lang.reflect.Constructor) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BiPredicate(java.util.function.BiPredicate) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) SerializationContextInitializer(org.infinispan.protostream.SerializationContextInitializer) RollbackException(javax.transaction.RollbackException) BiConsumer(java.util.function.BiConsumer) Address(org.infinispan.remoting.transport.Address) AfterClass(org.testng.annotations.AfterClass) StorageType(org.infinispan.configuration.cache.StorageType) DataContainer(org.infinispan.container.DataContainer) Factory(org.testng.annotations.Factory) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) TransactionMode(org.infinispan.transaction.TransactionMode) BiasAcquisition(org.infinispan.configuration.cache.BiasAcquisition) XAResourceAdapter(org.infinispan.distribution.rehash.XAResourceAdapter) Consumer(java.util.function.Consumer) CacheMode(org.infinispan.configuration.cache.CacheMode) IsolationLevel(org.infinispan.util.concurrent.IsolationLevel) SystemException(javax.transaction.SystemException) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) Configuration(org.infinispan.configuration.cache.Configuration) TransactionManager(javax.transaction.TransactionManager) Collections(java.util.Collections) TransactionTable(org.infinispan.transaction.impl.TransactionTable) InTransactionMode(org.infinispan.test.fwk.InTransactionMode) AfterMethod(org.testng.annotations.AfterMethod) Method(java.lang.reflect.Method) BeforeMethod(org.testng.annotations.BeforeMethod) RollbackException(javax.transaction.RollbackException) SystemException(javax.transaction.SystemException) BiConsumer(java.util.function.BiConsumer) Consumer(java.util.function.Consumer) InCacheMode(org.infinispan.test.fwk.InCacheMode) TestFrameworkFailure(org.infinispan.test.fwk.TestFrameworkFailure) Factory(org.testng.annotations.Factory)

Aggregations

MagicKey (org.infinispan.distribution.MagicKey)2 Annotation (java.lang.annotation.Annotation)1 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Arrays.asList (java.util.Arrays.asList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 IdentityHashMap (java.util.IdentityHashMap)1 List (java.util.List)1 Set (java.util.Set)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 BiConsumer (java.util.function.BiConsumer)1 BiPredicate (java.util.function.BiPredicate)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 Supplier (java.util.function.Supplier)1 Stream (java.util.stream.Stream)1