Search in sources :

Example 1 with ExceptionRunnable

use of org.wildfly.common.function.ExceptionRunnable in project wildfly by wildfly.

the class CompositeContextualizerTestCase method test.

@Test
public void test() {
    Contextualizer contextualizer1 = mock(Contextualizer.class);
    Contextualizer contextualizer2 = mock(Contextualizer.class);
    Contextualizer contextualizer3 = mock(Contextualizer.class);
    Contextualizer contextualizer = new CompositeContextualizer(contextualizer1, contextualizer2, contextualizer3);
    Runnable runner = mock(Runnable.class);
    Runnable contextualRunner1 = mock(Runnable.class);
    Runnable contextualRunner2 = mock(Runnable.class);
    Runnable contextualRunner3 = mock(Runnable.class);
    when(contextualizer1.contextualize(runner)).thenReturn(contextualRunner1);
    when(contextualizer2.contextualize(contextualRunner1)).thenReturn(contextualRunner2);
    when(contextualizer3.contextualize(contextualRunner2)).thenReturn(contextualRunner3);
    assertSame(contextualRunner3, contextualizer.contextualize(runner));
    ExceptionRunnable<Exception> exceptionRunner = mock(ExceptionRunnable.class);
    ExceptionRunnable<Exception> contextualExceptionRunner1 = mock(ExceptionRunnable.class);
    ExceptionRunnable<Exception> contextualExceptionRunner2 = mock(ExceptionRunnable.class);
    ExceptionRunnable<Exception> contextualExceptionRunner3 = mock(ExceptionRunnable.class);
    when(contextualizer1.contextualize(exceptionRunner)).thenReturn(contextualExceptionRunner1);
    when(contextualizer2.contextualize(contextualExceptionRunner1)).thenReturn(contextualExceptionRunner2);
    when(contextualizer3.contextualize(contextualExceptionRunner2)).thenReturn(contextualExceptionRunner3);
    assertSame(contextualExceptionRunner3, contextualizer.contextualize(exceptionRunner));
    Callable<Object> caller = mock(Callable.class);
    Callable<Object> contextualCaller1 = mock(Callable.class);
    Callable<Object> contextualCaller2 = mock(Callable.class);
    Callable<Object> contextualCaller3 = mock(Callable.class);
    when(contextualizer1.contextualize(caller)).thenReturn(contextualCaller1);
    when(contextualizer2.contextualize(contextualCaller1)).thenReturn(contextualCaller2);
    when(contextualizer3.contextualize(contextualCaller2)).thenReturn(contextualCaller3);
    assertSame(contextualCaller3, contextualizer.contextualize(caller));
    Supplier<Object> supplier = mock(Supplier.class);
    Supplier<Object> contextualSupplier1 = mock(Supplier.class);
    Supplier<Object> contextualSupplier2 = mock(Supplier.class);
    Supplier<Object> contextualSupplier3 = mock(Supplier.class);
    when(contextualizer1.contextualize(supplier)).thenReturn(contextualSupplier1);
    when(contextualizer2.contextualize(contextualSupplier1)).thenReturn(contextualSupplier2);
    when(contextualizer3.contextualize(contextualSupplier2)).thenReturn(contextualSupplier3);
    assertSame(contextualSupplier3, contextualizer.contextualize(supplier));
    ExceptionSupplier<Object, Exception> exceptionSupplier = mock(ExceptionSupplier.class);
    ExceptionSupplier<Object, Exception> contextualExceptionSupplier1 = mock(ExceptionSupplier.class);
    ExceptionSupplier<Object, Exception> contextualExceptionSupplier2 = mock(ExceptionSupplier.class);
    ExceptionSupplier<Object, Exception> contextualExceptionSupplier3 = mock(ExceptionSupplier.class);
    when(contextualizer1.contextualize(exceptionSupplier)).thenReturn(contextualExceptionSupplier1);
    when(contextualizer2.contextualize(contextualExceptionSupplier1)).thenReturn(contextualExceptionSupplier2);
    when(contextualizer3.contextualize(contextualExceptionSupplier2)).thenReturn(contextualExceptionSupplier3);
    assertSame(contextualExceptionSupplier3, contextualizer.contextualize(exceptionSupplier));
}
Also used : ExceptionRunnable(org.wildfly.common.function.ExceptionRunnable) Test(org.junit.Test)

Example 2 with ExceptionRunnable

use of org.wildfly.common.function.ExceptionRunnable in project wildfly by wildfly.

the class ContextReferenceExecutorTestCase method test.

@Test
public void test() throws Exception {
    Object original = new Object();
    Object target = new Object();
    Object result = new Object();
    AtomicReference<Object> resultRef = new AtomicReference<>();
    ContextReference<Object> contextRef = new AtomicContextReference<>(original);
    Contextualizer contextualizer = new ContextReferenceExecutor<>(target, contextRef);
    Runnable runner = new Runnable() {

        @Override
        public void run() {
            assertSame(target, contextRef.get());
            resultRef.set(result);
        }
    };
    assertSame(original, contextRef.get());
    contextualizer.contextualize(runner).run();
    assertSame(original, contextRef.get());
    assertSame(result, resultRef.get());
    resultRef.set(null);
    ExceptionRunnable<Exception> exceptionRunner = new ExceptionRunnable<Exception>() {

        @Override
        public void run() throws Exception {
            assertSame(target, contextRef.get());
            resultRef.set(result);
        }
    };
    assertSame(original, contextRef.get());
    contextualizer.contextualize(exceptionRunner).run();
    assertSame(original, contextRef.get());
    assertSame(result, resultRef.get());
    resultRef.set(null);
    Callable<Object> caller = new Callable<Object>() {

        @Override
        public Object call() {
            assertSame(target, contextRef.get());
            return result;
        }
    };
    assertSame(original, contextRef.get());
    assertSame(result, contextualizer.contextualize(caller).call());
    assertSame(original, contextRef.get());
    Supplier<Object> supplier = new Supplier<Object>() {

        @Override
        public Object get() {
            assertSame(target, contextRef.get());
            return result;
        }
    };
    assertSame(original, contextRef.get());
    assertSame(result, contextualizer.contextualize(supplier).get());
    assertSame(original, contextRef.get());
    ExceptionSupplier<Object, Exception> exceptionSupplier = new ExceptionSupplier<Object, Exception>() {

        @Override
        public Object get() {
            assertSame(target, contextRef.get());
            return result;
        }
    };
    assertSame(original, contextRef.get());
    assertSame(result, contextualizer.contextualize(exceptionSupplier).get());
    assertSame(original, contextRef.get());
}
Also used : ExceptionRunnable(org.wildfly.common.function.ExceptionRunnable) ExceptionSupplier(org.wildfly.common.function.ExceptionSupplier) AtomicReference(java.util.concurrent.atomic.AtomicReference) Callable(java.util.concurrent.Callable) ExceptionRunnable(org.wildfly.common.function.ExceptionRunnable) Supplier(java.util.function.Supplier) ExceptionSupplier(org.wildfly.common.function.ExceptionSupplier) Test(org.junit.Test)

Example 3 with ExceptionRunnable

use of org.wildfly.common.function.ExceptionRunnable in project wildfly by wildfly.

the class CommandDispatcherTransport method broadcast.

private void broadcast(Command<Void, CommandDispatcherTransport> command) throws WorkException {
    CommandDispatcher<CommandDispatcherTransport> dispatcher = this.dispatcher;
    ExceptionRunnable<WorkException> task = new ExceptionRunnable<WorkException>() {

        @Override
        public void run() throws WorkException {
            try {
                for (Map.Entry<Node, CompletionStage<Void>> entry : dispatcher.executeOnGroup(command).entrySet()) {
                    // Verify that command executed successfully on all nodes
                    try {
                        entry.getValue().toCompletableFuture().join();
                    } catch (CancellationException e) {
                    // Ignore
                    } catch (CompletionException e) {
                        throw new WorkException(e);
                    }
                }
            } catch (CommandDispatcherException e) {
                throw new WorkException(e);
            }
        }
    };
    this.executor.execute(task);
}
Also used : ExceptionRunnable(org.wildfly.common.function.ExceptionRunnable) CancellationException(java.util.concurrent.CancellationException) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) WorkException(javax.resource.spi.work.WorkException) Node(org.wildfly.clustering.group.Node) CompletionException(java.util.concurrent.CompletionException) HashMap(java.util.HashMap) Map(java.util.Map) CompletionStage(java.util.concurrent.CompletionStage)

Example 4 with ExceptionRunnable

use of org.wildfly.common.function.ExceptionRunnable in project wildfly by wildfly.

the class StampedLockServiceExecutorTestCase method testExecuteExceptionRunnable.

@SuppressWarnings("unchecked")
@Test
public void testExecuteExceptionRunnable() throws Exception {
    ServiceExecutor executor = new StampedLockServiceExecutor();
    ExceptionRunnable<Exception> executeTask = mock(ExceptionRunnable.class);
    executor.execute(executeTask);
    // Task should run
    verify(executeTask).run();
    reset(executeTask);
    doThrow(new Exception()).when(executeTask).run();
    try {
        executor.execute(executeTask);
        fail("Should have thrown an exception");
    } catch (Exception e) {
        assertNotNull(e);
    }
    reset(executeTask);
    Runnable closeTask = mock(Runnable.class);
    executor.close(closeTask);
    verify(closeTask).run();
    reset(closeTask);
    executor.close(closeTask);
    // Close task should only run once
    verify(closeTask, never()).run();
    executor.execute(executeTask);
    // Task should no longer run since service is closed
    verify(executeTask, never()).run();
}
Also used : ExceptionRunnable(org.wildfly.common.function.ExceptionRunnable) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

ExceptionRunnable (org.wildfly.common.function.ExceptionRunnable)4 Test (org.junit.Test)3 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1 CancellationException (java.util.concurrent.CancellationException)1 CompletionException (java.util.concurrent.CompletionException)1 CompletionStage (java.util.concurrent.CompletionStage)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Supplier (java.util.function.Supplier)1 WorkException (javax.resource.spi.work.WorkException)1 CommandDispatcherException (org.wildfly.clustering.dispatcher.CommandDispatcherException)1 Node (org.wildfly.clustering.group.Node)1 ExceptionSupplier (org.wildfly.common.function.ExceptionSupplier)1