Search in sources :

Example 1 with ExceptionRunnable

use of org.infinispan.commons.test.ExceptionRunnable in project infinispan by infinispan.

the class JdbcStringBasedStoreTxFunctionalTest method testWithPassivation.

public void testWithPassivation(Method m) throws Exception {
    ConfigurationBuilder base = getDefaultCacheConfiguration();
    base.persistence().passivation(true);
    ExceptionRunnable runnable = () -> TestingUtil.defineConfiguration(cacheManager, m.getName(), configureCacheLoader(base, m.getName(), false).build());
    // transactional and shared don't mix with passivation
    if (transactionalConfig || sharedConfig) {
        Exceptions.expectException(CacheConfigurationException.class, runnable);
    } else {
        runnable.run();
    }
}
Also used : ExceptionRunnable(org.infinispan.commons.test.ExceptionRunnable) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) JdbcStringBasedStoreConfigurationBuilder(org.infinispan.persistence.jdbc.configuration.JdbcStringBasedStoreConfigurationBuilder)

Example 2 with ExceptionRunnable

use of org.infinispan.commons.test.ExceptionRunnable in project infinispan by infinispan.

the class AbstractInfinispanTest method getTestThreadFactory.

/**
 * This should normally not be used, use the {@code fork(Runnable|Callable|ExceptionRunnable)}
 * method when an executor is required.
 *
 * Although if you want a limited set of threads this could still be useful for something like
 * {@link java.util.concurrent.Executors#newFixedThreadPool(int, java.util.concurrent.ThreadFactory)} or
 * {@link java.util.concurrent.Executors#newSingleThreadExecutor(java.util.concurrent.ThreadFactory)}
 *
 * @param prefix The prefix starting for the thread factory
 * @return A thread factory that will use the same naming schema as the other methods
 */
protected ThreadFactory getTestThreadFactory(final String prefix) {
    final String className = getClass().getSimpleName();
    return new ThreadFactory() {

        private final AtomicInteger counter = new AtomicInteger(0);

        @Override
        public Thread newThread(Runnable r) {
            String threadName = prefix + "-" + counter.incrementAndGet() + "," + className;
            Thread thread = new Thread(r, threadName);
            TestResourceTracker.addResource(AbstractInfinispanTest.this.getTestName(), new ThreadCleaner(thread));
            return thread;
        }
    };
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExceptionRunnable(org.infinispan.commons.test.ExceptionRunnable)

Example 3 with ExceptionRunnable

use of org.infinispan.commons.test.ExceptionRunnable in project infinispan by infinispan.

the class AbstractInfinispanTest method runConcurrently.

/**
 * This will run two or more tasks concurrently.
 *
 * It synchronizes before starting at approximately the same time by ensuring they all start before
 * allowing the tasks to proceed.
 *
 * @param tasks The tasks to run
 * @throws InterruptedException Thrown if this thread is interrupted
 * @throws ExecutionException Thrown if one of the callables throws any kind of Throwable.  The
 *         thrown Throwable will be wrapped by this exception
 * @throws TimeoutException If one of the tasks doesn't complete within the timeout
 */
protected void runConcurrently(long timeout, TimeUnit timeUnit, ExceptionRunnable... tasks) throws Exception {
    if (tasks == null || tasks.length < 2) {
        throw new IllegalArgumentException("Need at least 2 tasks to run concurrently");
    }
    long deadlineNanos = System.nanoTime() + TimeUnit.NANOSECONDS.convert(timeout, timeUnit);
    List<Future<Void>> futures = new ArrayList<>(tasks.length);
    CyclicBarrier barrier = new CyclicBarrier(tasks.length);
    for (ExceptionRunnable task : tasks) {
        futures.add(testExecutor.submit(new ConcurrentCallable(task, barrier)));
    }
    List<Exception> exceptions = new ArrayList<>();
    for (Future<Void> future : futures) {
        try {
            future.get(deadlineNanos - System.nanoTime(), TimeUnit.NANOSECONDS);
        } catch (Exception e) {
            futures.forEach(f -> f.cancel(true));
            exceptions.add(e);
        }
    }
    if (!exceptions.isEmpty()) {
        Exception exception = exceptions.remove(0);
        for (Exception e : exceptions) {
            exception.addSuppressed(e);
        }
        throw exception;
    }
}
Also used : ExceptionRunnable(org.infinispan.commons.test.ExceptionRunnable) Arrays(java.util.Arrays) Listeners(org.testng.annotations.Listeners) LogFactory(org.infinispan.util.logging.LogFactory) TimeoutException(java.util.concurrent.TimeoutException) AfterMethod(org.testng.annotations.AfterMethod) MethodInstance(org.testng.internal.MethodInstance) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ThreadFactory(java.util.concurrent.ThreadFactory) ExceptionRunnable(org.infinispan.commons.test.ExceptionRunnable) FunctionalMap(org.infinispan.functional.FunctionalMap) CyclicBarrier(java.util.concurrent.CyclicBarrier) IdentityHashMap(java.util.IdentityHashMap) BasicCacheContainer(org.infinispan.commons.api.BasicCacheContainer) IMethodInstance(org.testng.IMethodInstance) ITestContext(org.testng.ITestContext) SynchronousQueue(java.util.concurrent.SynchronousQueue) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) BeforeClass(org.testng.annotations.BeforeClass) Objects(java.util.Objects) LockSupport(java.util.concurrent.locks.LockSupport) List(java.util.List) Stream(java.util.stream.Stream) Type(java.lang.reflect.Type) FakeTestClass(org.infinispan.test.fwk.FakeTestClass) Modifier(java.lang.reflect.Modifier) TestSelector(org.infinispan.test.fwk.TestSelector) IMethodInterceptor(org.testng.IMethodInterceptor) TestResourceTracker(org.infinispan.commons.test.TestResourceTracker) RequestRepository(org.infinispan.remoting.transport.impl.RequestRepository) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) NamedTestMethod(org.infinispan.test.fwk.NamedTestMethod) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) Supplier(java.util.function.Supplier) TestNGException(org.testng.TestNGException) Retention(java.lang.annotation.Retention) ArrayList(java.util.ArrayList) BasicCache(org.infinispan.commons.api.BasicCache) Log(org.infinispan.util.logging.Log) BasePartitionHandlingTest(org.infinispan.partitionhandling.BasePartitionHandlingTest) ExecutorService(java.util.concurrent.ExecutorService) Protocol(org.jgroups.stack.Protocol) AsyncInterceptor(org.infinispan.interceptors.AsyncInterceptor) AfterClass(org.testng.annotations.AfterClass) EmbeddedTimeService(org.infinispan.util.EmbeddedTimeService) Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType) Field(java.lang.reflect.Field) AssertJUnit.fail(org.testng.AssertJUnit.fail) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ChainMethodInterceptor(org.infinispan.test.fwk.ChainMethodInterceptor) ParameterizedType(java.lang.reflect.ParameterizedType) TestNGLongTestsHook(org.infinispan.commons.test.TestNGLongTestsHook) TransactionManager(javax.transaction.TransactionManager) TimeService(org.infinispan.commons.time.TimeService) RetentionPolicy(java.lang.annotation.RetentionPolicy) ArrayList(java.util.ArrayList) TimeoutException(java.util.concurrent.TimeoutException) TestNGException(org.testng.TestNGException) ExecutionException(java.util.concurrent.ExecutionException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Future(java.util.concurrent.Future)

Example 4 with ExceptionRunnable

use of org.infinispan.commons.test.ExceptionRunnable in project infinispan by infinispan.

the class JdbcStringBasedStoreTxFunctionalTest method testWithPurgeOnStartup.

public void testWithPurgeOnStartup(Method m) throws Exception {
    ConfigurationBuilder base = getDefaultCacheConfiguration();
    ExceptionRunnable runnable = () -> TestingUtil.defineConfiguration(cacheManager, m.getName(), configureCacheLoader(base, m.getName(), true).build());
    // shared doesn't mix with purgeOnStartup
    if (sharedConfig) {
        Exceptions.expectException(CacheConfigurationException.class, runnable);
    } else {
        runnable.run();
    }
}
Also used : ExceptionRunnable(org.infinispan.commons.test.ExceptionRunnable) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) JdbcStringBasedStoreConfigurationBuilder(org.infinispan.persistence.jdbc.configuration.JdbcStringBasedStoreConfigurationBuilder)

Aggregations

ExceptionRunnable (org.infinispan.commons.test.ExceptionRunnable)4 ThreadFactory (java.util.concurrent.ThreadFactory)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)2 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)2 ElementType (java.lang.annotation.ElementType)1 Retention (java.lang.annotation.Retention)1 RetentionPolicy (java.lang.annotation.RetentionPolicy)1 Target (java.lang.annotation.Target)1 MethodHandles (java.lang.invoke.MethodHandles)1 Field (java.lang.reflect.Field)1 Modifier (java.lang.reflect.Modifier)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 List (java.util.List)1