Search in sources :

Example 1 with InvocationSuccessFunction

use of org.infinispan.interceptors.InvocationSuccessFunction in project infinispan by infinispan.

the class AsyncInterceptorChainInvocationTest method testAsyncStageComposeAsyncStage.

public void testAsyncStageComposeAsyncStage() throws Exception {
    CompletableFuture<Object> f1 = new CompletableFuture<>();
    CompletableFuture<Object> f2 = new CompletableFuture<>();
    CompletableFuture<Object> f3 = new CompletableFuture<>();
    AsyncInterceptorChain chain = newInterceptorChain(new BaseAsyncInterceptor() {

        @Override
        public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
            return invokeNextAndHandle(ctx, command, (rCtx, rCommand, rv, t) -> {
                InvocationSuccessFunction function = (rCtx1, rCommand1, rv1) -> asyncValue(f3);
                return asyncValue(f2).addCallback(rCtx, rCommand, function);
            });
        }
    }, new BaseAsyncInterceptor() {

        @Override
        public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
            return asyncValue(f1);
        }
    });
    InvocationContext context = newInvocationContext();
    CompletableFuture<Object> invokeFuture = chain.invokeAsync(context, testCommand);
    assertFalse(invokeFuture.isDone());
    f1.complete("v1");
    assertFalse(invokeFuture.isDone());
    f2.complete("v2");
    assertFalse(invokeFuture.isDone());
    f3.complete("v3");
    assertEquals("v3", invokeFuture.get(10, SECONDS));
}
Also used : VisitableCommand(org.infinispan.commands.VisitableCommand) AsyncInterceptor(org.infinispan.interceptors.AsyncInterceptor) GetKeyValueCommand(org.infinispan.commands.read.GetKeyValueCommand) SingleKeyNonTxInvocationContext(org.infinispan.context.impl.SingleKeyNonTxInvocationContext) TimeoutException(java.util.concurrent.TimeoutException) AssertJUnit.assertFalse(org.testng.AssertJUnit.assertFalse) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.testng.annotations.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) InvocationSuccessFunction(org.infinispan.interceptors.InvocationSuccessFunction) BaseAsyncInterceptor(org.infinispan.interceptors.BaseAsyncInterceptor) ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future) InvocationContext(org.infinispan.context.InvocationContext) AbstractInfinispanTest(org.infinispan.test.AbstractInfinispanTest) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) TestException(org.infinispan.test.TestException) Exceptions.expectExecutionException(org.infinispan.commons.test.Exceptions.expectExecutionException) VisitableCommand(org.infinispan.commands.VisitableCommand) Pattern(java.util.regex.Pattern) LockControlCommand(org.infinispan.commands.control.LockControlCommand) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) AssertJUnit.assertEquals(org.testng.AssertJUnit.assertEquals) SECONDS(java.util.concurrent.TimeUnit.SECONDS) CompletableFuture(java.util.concurrent.CompletableFuture) BaseAsyncInterceptor(org.infinispan.interceptors.BaseAsyncInterceptor) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) InvocationSuccessFunction(org.infinispan.interceptors.InvocationSuccessFunction) SingleKeyNonTxInvocationContext(org.infinispan.context.impl.SingleKeyNonTxInvocationContext) InvocationContext(org.infinispan.context.InvocationContext)

Example 2 with InvocationSuccessFunction

use of org.infinispan.interceptors.InvocationSuccessFunction in project infinispan by infinispan.

the class AsyncInterceptorChainInvocationTest method testDeadlockWithAsyncStage.

public void testDeadlockWithAsyncStage() throws Exception {
    CompletableFuture<Object> f1 = new CompletableFuture<>();
    CompletableFuture<Object> f2 = new CompletableFuture<>();
    AsyncInterceptorChain chain = newInterceptorChain(new BaseAsyncInterceptor() {

        @Override
        public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
            return invokeNextThenApply(ctx, command, (rCtx, rCommand, rv) -> rv + " " + awaitFuture(f2));
        }
    }, new BaseAsyncInterceptor() {

        @Override
        public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
            // Add a handler to force the return value to be a full AsyncInvocationStage
            InvocationSuccessFunction function = (rCtx, rCommand, rv) -> rv;
            return asyncValue(f1).addCallback(ctx, command, function);
        }
    });
    InvocationContext context = newInvocationContext();
    CompletableFuture<Object> invokeFuture = chain.invokeAsync(context, testCommand);
    assertFalse(invokeFuture.isDone());
    Future<Boolean> fork = fork(() -> f1.complete("v1"));
    Thread.sleep(100);
    assertFalse(fork.isDone());
    assertFalse(invokeFuture.isDone());
    f2.complete("v2");
    fork.get(10, SECONDS);
    assertEquals("v1 v2", invokeFuture.getNow(null));
}
Also used : AsyncInterceptor(org.infinispan.interceptors.AsyncInterceptor) GetKeyValueCommand(org.infinispan.commands.read.GetKeyValueCommand) SingleKeyNonTxInvocationContext(org.infinispan.context.impl.SingleKeyNonTxInvocationContext) TimeoutException(java.util.concurrent.TimeoutException) AssertJUnit.assertFalse(org.testng.AssertJUnit.assertFalse) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.testng.annotations.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) InvocationSuccessFunction(org.infinispan.interceptors.InvocationSuccessFunction) BaseAsyncInterceptor(org.infinispan.interceptors.BaseAsyncInterceptor) ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future) InvocationContext(org.infinispan.context.InvocationContext) AbstractInfinispanTest(org.infinispan.test.AbstractInfinispanTest) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) TestException(org.infinispan.test.TestException) Exceptions.expectExecutionException(org.infinispan.commons.test.Exceptions.expectExecutionException) VisitableCommand(org.infinispan.commands.VisitableCommand) Pattern(java.util.regex.Pattern) LockControlCommand(org.infinispan.commands.control.LockControlCommand) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) AssertJUnit.assertEquals(org.testng.AssertJUnit.assertEquals) SECONDS(java.util.concurrent.TimeUnit.SECONDS) BaseAsyncInterceptor(org.infinispan.interceptors.BaseAsyncInterceptor) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) InvocationSuccessFunction(org.infinispan.interceptors.InvocationSuccessFunction) VisitableCommand(org.infinispan.commands.VisitableCommand) CompletableFuture(java.util.concurrent.CompletableFuture) SingleKeyNonTxInvocationContext(org.infinispan.context.impl.SingleKeyNonTxInvocationContext) InvocationContext(org.infinispan.context.InvocationContext)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 SECONDS (java.util.concurrent.TimeUnit.SECONDS)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Pattern (java.util.regex.Pattern)2 VisitableCommand (org.infinispan.commands.VisitableCommand)2 LockControlCommand (org.infinispan.commands.control.LockControlCommand)2 GetKeyValueCommand (org.infinispan.commands.read.GetKeyValueCommand)2 Exceptions.expectExecutionException (org.infinispan.commons.test.Exceptions.expectExecutionException)2 InvocationContext (org.infinispan.context.InvocationContext)2 SingleKeyNonTxInvocationContext (org.infinispan.context.impl.SingleKeyNonTxInvocationContext)2 AsyncInterceptor (org.infinispan.interceptors.AsyncInterceptor)2 AsyncInterceptorChain (org.infinispan.interceptors.AsyncInterceptorChain)2 BaseAsyncInterceptor (org.infinispan.interceptors.BaseAsyncInterceptor)2 InvocationSuccessFunction (org.infinispan.interceptors.InvocationSuccessFunction)2 AbstractInfinispanTest (org.infinispan.test.AbstractInfinispanTest)2 TestException (org.infinispan.test.TestException)2 CompletableFutures (org.infinispan.util.concurrent.CompletableFutures)2