Search in sources :

Example 1 with Actor

use of io.camunda.zeebe.util.sched.Actor in project zeebe by camunda.

the class SyncLogStreamBuilder method build.

public SyncLogStream build() {
    final var scheduler = Objects.requireNonNull(actorSchedulingService, "must provide an actor scheduling service through SyncLogStreamBuilder#withActorSchedulingService");
    final var buildFuture = new CompletableActorFuture<SyncLogStream>();
    scheduler.submitActor(new Actor() {

        @Override
        protected void onActorStarting() {
            actor.runOnCompletionBlockingCurrentPhase(buildAsync(), (logStream, t) -> {
                if (t == null) {
                    buildFuture.complete(new SyncLogStream(logStream));
                } else {
                    buildFuture.completeExceptionally(t);
                }
            });
        }
    });
    return buildFuture.join();
}
Also used : Objects(java.util.Objects) LogStreamBuilder(io.camunda.zeebe.logstreams.log.LogStreamBuilder) LogStorage(io.camunda.zeebe.logstreams.storage.LogStorage) LogStream(io.camunda.zeebe.logstreams.log.LogStream) CompletableActorFuture(io.camunda.zeebe.util.sched.future.CompletableActorFuture) ActorFuture(io.camunda.zeebe.util.sched.future.ActorFuture) ActorSchedulingService(io.camunda.zeebe.util.sched.ActorSchedulingService) Actor(io.camunda.zeebe.util.sched.Actor) CompletableActorFuture(io.camunda.zeebe.util.sched.future.CompletableActorFuture) Actor(io.camunda.zeebe.util.sched.Actor)

Example 2 with Actor

use of io.camunda.zeebe.util.sched.Actor in project zeebe by camunda.

the class ActorFutureTest method shouldInvokeCallbackOnFutureCompletion.

@Test
public void shouldInvokeCallbackOnFutureCompletion() {
    // given
    final CompletableActorFuture<Void> future = new CompletableActorFuture<>();
    final AtomicInteger callbackInvocations = new AtomicInteger(0);
    final Actor waitingActor = new Actor() {

        @Override
        protected void onActorStarted() {
            actor.runOnCompletion(future, (r, t) -> callbackInvocations.incrementAndGet());
        }
    };
    final Actor completingActor = new Actor() {

        @Override
        protected void onActorStarted() {
            future.complete(null);
        }
    };
    schedulerRule.submitActor(waitingActor);
    schedulerRule.workUntilDone();
    // when
    schedulerRule.submitActor(completingActor);
    schedulerRule.workUntilDone();
    // then
    assertThat(callbackInvocations).hasValue(1);
}
Also used : CompletableActorFuture(io.camunda.zeebe.util.sched.future.CompletableActorFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Actor(io.camunda.zeebe.util.sched.Actor) Test(org.junit.Test)

Example 3 with Actor

use of io.camunda.zeebe.util.sched.Actor in project zeebe by camunda.

the class ActorFutureTest method shouldInvokeCallbackOnBlockPhaseForFutureCompletion.

@Test
public void shouldInvokeCallbackOnBlockPhaseForFutureCompletion() {
    // given
    final CompletableActorFuture<Void> future = new CompletableActorFuture<>();
    final AtomicInteger callbackInvocations = new AtomicInteger(0);
    final Actor waitingActor = new Actor() {

        @Override
        protected void onActorStarted() {
            actor.runOnCompletionBlockingCurrentPhase(future, (r, t) -> callbackInvocations.incrementAndGet());
        }
    };
    final Actor completingActor = new Actor() {

        @Override
        protected void onActorStarted() {
            future.complete(null);
        }
    };
    schedulerRule.submitActor(waitingActor);
    schedulerRule.workUntilDone();
    // when
    schedulerRule.submitActor(completingActor);
    schedulerRule.workUntilDone();
    // then
    assertThat(callbackInvocations).hasValue(1);
}
Also used : CompletableActorFuture(io.camunda.zeebe.util.sched.future.CompletableActorFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Actor(io.camunda.zeebe.util.sched.Actor) Test(org.junit.Test)

Example 4 with Actor

use of io.camunda.zeebe.util.sched.Actor in project zeebe by camunda.

the class ActorFutureTest method shouldInvokeCallbackOnEmptyFutureList.

@Test
public void shouldInvokeCallbackOnEmptyFutureList() {
    // given
    final List<ActorFuture<Void>> futures = Collections.emptyList();
    final List<Throwable> invocations = new ArrayList<>();
    final Actor waitingActor = new Actor() {

        @Override
        protected void onActorStarted() {
            actor.runOnCompletion(futures, t -> {
                invocations.add(t);
            });
        }
    };
    schedulerRule.submitActor(waitingActor);
    schedulerRule.workUntilDone();
    // then
    assertThat(invocations).hasSize(1).containsNull();
}
Also used : ActorFuture(io.camunda.zeebe.util.sched.future.ActorFuture) CompletableActorFuture(io.camunda.zeebe.util.sched.future.CompletableActorFuture) Actor(io.camunda.zeebe.util.sched.Actor) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with Actor

use of io.camunda.zeebe.util.sched.Actor in project zeebe by camunda.

the class ActorFutureTest method shouldInvokeCallbackOnCompletedFuture.

@Test
public void shouldInvokeCallbackOnCompletedFuture() {
    // given
    final AtomicReference<String> futureResult = new AtomicReference<>();
    schedulerRule.submitActor(new Actor() {

        @Override
        protected void onActorStarted() {
            actor.runOnCompletion(CompletableActorFuture.completed("foo"), (r, t) -> futureResult.set(r));
        }
    });
    // when
    schedulerRule.workUntilDone();
    // then
    assertThat(futureResult.get()).isEqualTo("foo");
}
Also used : Arrays(java.util.Arrays) AbstractThrowableAssert(org.assertj.core.api.AbstractThrowableAssert) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ActorFuture(io.camunda.zeebe.util.sched.future.ActorFuture) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) ControlledActorSchedulerRule(io.camunda.zeebe.util.sched.testing.ControlledActorSchedulerRule) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Rule(org.junit.Rule) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableActorFuture(io.camunda.zeebe.util.sched.future.CompletableActorFuture) BiConsumer(java.util.function.BiConsumer) ActorThread(io.camunda.zeebe.util.sched.ActorThread) Actor(io.camunda.zeebe.util.sched.Actor) Collections(java.util.Collections) Actor(io.camunda.zeebe.util.sched.Actor) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Aggregations

Actor (io.camunda.zeebe.util.sched.Actor)75 Test (org.junit.Test)69 CompletableActorFuture (io.camunda.zeebe.util.sched.future.CompletableActorFuture)30 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)24 ArrayList (java.util.ArrayList)18 ActorCondition (io.camunda.zeebe.util.sched.ActorCondition)15 ActorThread (io.camunda.zeebe.util.sched.ActorThread)12 ActorFuture (io.camunda.zeebe.util.sched.future.ActorFuture)12 ActorThreadGroup (io.camunda.zeebe.util.sched.ActorThreadGroup)9 ExecutionException (java.util.concurrent.ExecutionException)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 ControlledActorSchedulerRule (io.camunda.zeebe.util.sched.testing.ControlledActorSchedulerRule)6 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 List (java.util.List)6 TimeUnit (java.util.concurrent.TimeUnit)6 TimeoutException (java.util.concurrent.TimeoutException)6 BiConsumer (java.util.function.BiConsumer)6 AbstractThrowableAssert (org.assertj.core.api.AbstractThrowableAssert)6