Search in sources :

Example 1 with RequestHandler

use of io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler in project zeebe by zeebe-io.

the class ActivateJobsTest method shouldSendRejectionWithoutRetrying.

@Test
public void shouldSendRejectionWithoutRetrying() {
    // given
    final RejectionType rejectionType = RejectionType.INVALID_ARGUMENT;
    final AtomicInteger callCounter = new AtomicInteger();
    brokerClient.registerHandler(BrokerActivateJobsRequest.class, (RequestHandler<BrokerRequest<?>, BrokerResponse<?>>) request -> {
        callCounter.incrementAndGet();
        return new BrokerRejectionResponse<>(new BrokerRejection(Intent.UNKNOWN, 1, rejectionType, "expected"));
    });
    final ActivateJobsRequest request = ActivateJobsRequest.newBuilder().setType("").setMaxJobsToActivate(1).build();
    // when/then
    assertThatThrownBy(() -> {
        final Iterator<ActivateJobsResponse> responseIterator = client.activateJobs(request);
        responseIterator.hasNext();
    }).isInstanceOf(StatusRuntimeException.class).extracting(t -> ((StatusRuntimeException) t).getStatus().getCode()).isEqualTo(Status.INVALID_ARGUMENT.getCode());
    assertThat(callCounter).hasValue(1);
}
Also used : BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) Protocol(io.camunda.zeebe.protocol.Protocol) Arrays(java.util.Arrays) BrokerRejectionResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejectionResponse) BufferUtil.wrapString(io.camunda.zeebe.util.buffer.BufferUtil.wrapString) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GatewayTest(io.camunda.zeebe.gateway.api.util.GatewayTest) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) Status(io.grpc.Status) BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) Parameterized(org.junit.runners.Parameterized) Intent(io.camunda.zeebe.protocol.record.intent.Intent) RequestHandler(io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler) Iterator(java.util.Iterator) BrokerRejection(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejection) JsonUtil(io.camunda.zeebe.test.util.JsonUtil) Test(org.junit.Test) StatusRuntimeException(io.grpc.StatusRuntimeException) List(java.util.List) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) RejectionType(io.camunda.zeebe.protocol.record.RejectionType) GatewayCfg(io.camunda.zeebe.gateway.impl.configuration.GatewayCfg) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord) BrokerRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerRequest) BufferUtil(io.camunda.zeebe.util.buffer.BufferUtil) BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) RejectionType(io.camunda.zeebe.protocol.record.RejectionType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) Iterator(java.util.Iterator) BrokerRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerRequest) BrokerRejection(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejection) GatewayTest(io.camunda.zeebe.gateway.api.util.GatewayTest) Test(org.junit.Test)

Example 2 with RequestHandler

use of io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler in project zeebe by camunda-cloud.

the class LongPollingActivateJobsTest method shouldRepeatActivateJobsRequestAgainstBrokersIfNewJobsArriveWhileIteratingThroughBrokersTheFirstTime.

@Test
public void shouldRepeatActivateJobsRequestAgainstBrokersIfNewJobsArriveWhileIteratingThroughBrokersTheFirstTime() {
    // given
    // a request with timeout
    final InflightActivateJobsRequest request = new InflightActivateJobsRequest(getNextRequestId(), ActivateJobsRequest.newBuilder().setType(TYPE).setMaxJobsToActivate(15).setRequestTimeout(500).build(), spy(ServerStreamObserver.class));
    /* and a request handler that simulates the following:
        - on the first round no broker has any jobs
        - about midway through iterating the brokers one of the brokers that has already been visited reports new jobs being available
        - these jobs are available, when the brokers are asked a second time
    */
    brokerClient.registerHandler(BrokerActivateJobsRequest.class, new RequestHandler<BrokerActivateJobsRequest, BrokerResponse<JobBatchRecord>>() {

        private final ActivateJobsStub noJobsAvailableStub = new ActivateJobsStub();

        private final ActivateJobsStub jobsAvailableStub = new ActivateJobsStub();

        private final Map<Integer, Integer> requestsPerPartitionCount = new HashMap<>();

        {
            jobsAvailableStub.addAvailableJobs(TYPE, 10);
        }

        @Override
        public BrokerResponse<JobBatchRecord> handle(final BrokerActivateJobsRequest request) throws Exception {
            final int partitionId = request.getPartitionId();
            final int requestsPerPartition = requestsPerPartitionCount.computeIfAbsent(partitionId, key -> 0);
            if (requestsPerPartition == 0) {
                requestsPerPartitionCount.put(partitionId, requestsPerPartition + 1);
                if (partitionId == 3) {
                    brokerClient.notifyJobsAvailable(TYPE);
                }
                return noJobsAvailableStub.handle(request);
            } else {
                return jobsAvailableStub.handle(request);
            }
        }
    });
    // when
    handler.activateJobs(request);
    waitUntil(request::isCompleted);
    // then
    assertThat(request.isTimedOut()).isFalse();
    final ArgumentCaptor<ActivateJobsResponse> responseArgumentCaptor = ArgumentCaptor.forClass(ActivateJobsResponse.class);
    verify(request.getResponseObserver()).onNext(responseArgumentCaptor.capture());
    final ActivateJobsResponse response = responseArgumentCaptor.getValue();
    assertThat(response.getJobsList()).hasSize(10);
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServerStreamObserver(io.camunda.zeebe.gateway.grpc.ServerStreamObserver) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) Mockito.doThrow(org.mockito.Mockito.doThrow) StreamObserver(io.grpc.stub.StreamObserver) Duration(java.time.Duration) Map(java.util.Map) Mockito.atLeast(org.mockito.Mockito.atLeast) ActorSchedulerRule(io.camunda.zeebe.util.sched.testing.ActorSchedulerRule) Actor(io.camunda.zeebe.util.sched.Actor) Mockito.doReturn(org.mockito.Mockito.doReturn) ControlledActorClock(io.camunda.zeebe.util.sched.clock.ControlledActorClock) BrokerRejection(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejection) BrokerError(io.camunda.zeebe.gateway.impl.broker.response.BrokerError) Collectors(java.util.stream.Collectors) BrokerFailJobRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerFailJobRequest) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) TestUtil.waitUntil(io.camunda.zeebe.test.util.TestUtil.waitUntil) BrokerErrorResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerErrorResponse) Awaitility(org.awaitility.Awaitility) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) BrokerRejectionException(io.camunda.zeebe.gateway.cmd.BrokerRejectionException) BrokerRejectionResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejectionResponse) ErrorCode(io.camunda.zeebe.protocol.record.ErrorCode) StatusException(io.grpc.StatusException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) AtomicReference(java.util.concurrent.atomic.AtomicReference) Mockito.timeout(org.mockito.Mockito.timeout) Code(io.grpc.Status.Code) ArgumentCaptor(org.mockito.ArgumentCaptor) ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) Either(io.camunda.zeebe.util.Either) BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) Intent(io.camunda.zeebe.protocol.record.intent.Intent) Before(org.junit.Before) StubbedBrokerClient(io.camunda.zeebe.gateway.api.util.StubbedBrokerClient) RequestHandler(io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) InflightActivateJobsRequest(io.camunda.zeebe.gateway.impl.job.InflightActivateJobsRequest) LongPollingActivateJobsHandler(io.camunda.zeebe.gateway.impl.job.LongPollingActivateJobsHandler) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) Rule(org.junit.Rule) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) RejectionType(io.camunda.zeebe.protocol.record.RejectionType) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord) InflightActivateJobsRequest(io.camunda.zeebe.gateway.impl.job.InflightActivateJobsRequest) HashMap(java.util.HashMap) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) BrokerRejectionException(io.camunda.zeebe.gateway.cmd.BrokerRejectionException) StatusException(io.grpc.StatusException) BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) ServerStreamObserver(io.camunda.zeebe.gateway.grpc.ServerStreamObserver) Test(org.junit.Test)

Example 3 with RequestHandler

use of io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler in project zeebe by zeebe-io.

the class LongPollingActivateJobsTest method shouldRepeatActivateJobsRequestAgainstBrokersIfNewJobsArriveWhileIteratingThroughBrokersTheFirstTime.

@Test
public void shouldRepeatActivateJobsRequestAgainstBrokersIfNewJobsArriveWhileIteratingThroughBrokersTheFirstTime() {
    // given
    // a request with timeout
    final InflightActivateJobsRequest request = new InflightActivateJobsRequest(getNextRequestId(), ActivateJobsRequest.newBuilder().setType(TYPE).setMaxJobsToActivate(15).setRequestTimeout(500).build(), spy(ServerStreamObserver.class));
    /* and a request handler that simulates the following:
        - on the first round no broker has any jobs
        - about midway through iterating the brokers one of the brokers that has already been visited reports new jobs being available
        - these jobs are available, when the brokers are asked a second time
    */
    brokerClient.registerHandler(BrokerActivateJobsRequest.class, new RequestHandler<BrokerActivateJobsRequest, BrokerResponse<JobBatchRecord>>() {

        private final ActivateJobsStub noJobsAvailableStub = new ActivateJobsStub();

        private final ActivateJobsStub jobsAvailableStub = new ActivateJobsStub();

        private final Map<Integer, Integer> requestsPerPartitionCount = new HashMap<>();

        {
            jobsAvailableStub.addAvailableJobs(TYPE, 10);
        }

        @Override
        public BrokerResponse<JobBatchRecord> handle(final BrokerActivateJobsRequest request) throws Exception {
            final int partitionId = request.getPartitionId();
            final int requestsPerPartition = requestsPerPartitionCount.computeIfAbsent(partitionId, key -> 0);
            if (requestsPerPartition == 0) {
                requestsPerPartitionCount.put(partitionId, requestsPerPartition + 1);
                if (partitionId == 3) {
                    brokerClient.notifyJobsAvailable(TYPE);
                }
                return noJobsAvailableStub.handle(request);
            } else {
                return jobsAvailableStub.handle(request);
            }
        }
    });
    // when
    handler.activateJobs(request);
    waitUntil(request::isCompleted);
    // then
    assertThat(request.isTimedOut()).isFalse();
    final ArgumentCaptor<ActivateJobsResponse> responseArgumentCaptor = ArgumentCaptor.forClass(ActivateJobsResponse.class);
    verify(request.getResponseObserver()).onNext(responseArgumentCaptor.capture());
    final ActivateJobsResponse response = responseArgumentCaptor.getValue();
    assertThat(response.getJobsList()).hasSize(10);
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServerStreamObserver(io.camunda.zeebe.gateway.grpc.ServerStreamObserver) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) Mockito.doThrow(org.mockito.Mockito.doThrow) StreamObserver(io.grpc.stub.StreamObserver) Duration(java.time.Duration) Map(java.util.Map) Mockito.atLeast(org.mockito.Mockito.atLeast) ActorSchedulerRule(io.camunda.zeebe.util.sched.testing.ActorSchedulerRule) Actor(io.camunda.zeebe.util.sched.Actor) Mockito.doReturn(org.mockito.Mockito.doReturn) ControlledActorClock(io.camunda.zeebe.util.sched.clock.ControlledActorClock) BrokerRejection(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejection) BrokerError(io.camunda.zeebe.gateway.impl.broker.response.BrokerError) Collectors(java.util.stream.Collectors) BrokerFailJobRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerFailJobRequest) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) TestUtil.waitUntil(io.camunda.zeebe.test.util.TestUtil.waitUntil) BrokerErrorResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerErrorResponse) Awaitility(org.awaitility.Awaitility) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) BrokerRejectionException(io.camunda.zeebe.gateway.cmd.BrokerRejectionException) BrokerRejectionResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejectionResponse) ErrorCode(io.camunda.zeebe.protocol.record.ErrorCode) StatusException(io.grpc.StatusException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) AtomicReference(java.util.concurrent.atomic.AtomicReference) Mockito.timeout(org.mockito.Mockito.timeout) Code(io.grpc.Status.Code) ArgumentCaptor(org.mockito.ArgumentCaptor) ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) Either(io.camunda.zeebe.util.Either) BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) Intent(io.camunda.zeebe.protocol.record.intent.Intent) Before(org.junit.Before) StubbedBrokerClient(io.camunda.zeebe.gateway.api.util.StubbedBrokerClient) RequestHandler(io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) InflightActivateJobsRequest(io.camunda.zeebe.gateway.impl.job.InflightActivateJobsRequest) LongPollingActivateJobsHandler(io.camunda.zeebe.gateway.impl.job.LongPollingActivateJobsHandler) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) Rule(org.junit.Rule) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) RejectionType(io.camunda.zeebe.protocol.record.RejectionType) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord) InflightActivateJobsRequest(io.camunda.zeebe.gateway.impl.job.InflightActivateJobsRequest) HashMap(java.util.HashMap) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) BrokerRejectionException(io.camunda.zeebe.gateway.cmd.BrokerRejectionException) StatusException(io.grpc.StatusException) BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) ServerStreamObserver(io.camunda.zeebe.gateway.grpc.ServerStreamObserver) Test(org.junit.Test)

Example 4 with RequestHandler

use of io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler in project zeebe by camunda-cloud.

the class ActivateJobsTest method shouldSendRejectionWithoutRetrying.

@Test
public void shouldSendRejectionWithoutRetrying() {
    // given
    final RejectionType rejectionType = RejectionType.INVALID_ARGUMENT;
    final AtomicInteger callCounter = new AtomicInteger();
    brokerClient.registerHandler(BrokerActivateJobsRequest.class, (RequestHandler<BrokerRequest<?>, BrokerResponse<?>>) request -> {
        callCounter.incrementAndGet();
        return new BrokerRejectionResponse<>(new BrokerRejection(Intent.UNKNOWN, 1, rejectionType, "expected"));
    });
    final ActivateJobsRequest request = ActivateJobsRequest.newBuilder().setType("").setMaxJobsToActivate(1).build();
    // when/then
    assertThatThrownBy(() -> {
        final Iterator<ActivateJobsResponse> responseIterator = client.activateJobs(request);
        responseIterator.hasNext();
    }).isInstanceOf(StatusRuntimeException.class).extracting(t -> ((StatusRuntimeException) t).getStatus().getCode()).isEqualTo(Status.INVALID_ARGUMENT.getCode());
    assertThat(callCounter).hasValue(1);
}
Also used : BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) Protocol(io.camunda.zeebe.protocol.Protocol) Arrays(java.util.Arrays) BrokerRejectionResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejectionResponse) BufferUtil.wrapString(io.camunda.zeebe.util.buffer.BufferUtil.wrapString) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GatewayTest(io.camunda.zeebe.gateway.api.util.GatewayTest) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) Status(io.grpc.Status) BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) Parameterized(org.junit.runners.Parameterized) Intent(io.camunda.zeebe.protocol.record.intent.Intent) RequestHandler(io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler) Iterator(java.util.Iterator) BrokerRejection(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejection) JsonUtil(io.camunda.zeebe.test.util.JsonUtil) Test(org.junit.Test) StatusRuntimeException(io.grpc.StatusRuntimeException) List(java.util.List) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) RejectionType(io.camunda.zeebe.protocol.record.RejectionType) GatewayCfg(io.camunda.zeebe.gateway.impl.configuration.GatewayCfg) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord) BrokerRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerRequest) BufferUtil(io.camunda.zeebe.util.buffer.BufferUtil) BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) RejectionType(io.camunda.zeebe.protocol.record.RejectionType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) Iterator(java.util.Iterator) BrokerRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerRequest) BrokerRejection(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejection) GatewayTest(io.camunda.zeebe.gateway.api.util.GatewayTest) Test(org.junit.Test)

Example 5 with RequestHandler

use of io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler in project zeebe by camunda.

the class LongPollingActivateJobsTest method shouldRepeatActivateJobsRequestAgainstBrokersIfNewJobsArriveWhileIteratingThroughBrokersTheFirstTime.

@Test
public void shouldRepeatActivateJobsRequestAgainstBrokersIfNewJobsArriveWhileIteratingThroughBrokersTheFirstTime() {
    // given
    // a request with timeout
    final InflightActivateJobsRequest request = new InflightActivateJobsRequest(getNextRequestId(), ActivateJobsRequest.newBuilder().setType(TYPE).setMaxJobsToActivate(15).setRequestTimeout(500).build(), spy(ServerStreamObserver.class));
    /* and a request handler that simulates the following:
        - on the first round no broker has any jobs
        - about midway through iterating the brokers one of the brokers that has already been visited reports new jobs being available
        - these jobs are available, when the brokers are asked a second time
    */
    brokerClient.registerHandler(BrokerActivateJobsRequest.class, new RequestHandler<BrokerActivateJobsRequest, BrokerResponse<JobBatchRecord>>() {

        private final ActivateJobsStub noJobsAvailableStub = new ActivateJobsStub();

        private final ActivateJobsStub jobsAvailableStub = new ActivateJobsStub();

        private final Map<Integer, Integer> requestsPerPartitionCount = new HashMap<>();

        {
            jobsAvailableStub.addAvailableJobs(TYPE, 10);
        }

        @Override
        public BrokerResponse<JobBatchRecord> handle(final BrokerActivateJobsRequest request) throws Exception {
            final int partitionId = request.getPartitionId();
            final int requestsPerPartition = requestsPerPartitionCount.computeIfAbsent(partitionId, key -> 0);
            if (requestsPerPartition == 0) {
                requestsPerPartitionCount.put(partitionId, requestsPerPartition + 1);
                if (partitionId == 3) {
                    brokerClient.notifyJobsAvailable(TYPE);
                }
                return noJobsAvailableStub.handle(request);
            } else {
                return jobsAvailableStub.handle(request);
            }
        }
    });
    // when
    handler.activateJobs(request);
    waitUntil(request::isCompleted);
    // then
    assertThat(request.isTimedOut()).isFalse();
    final ArgumentCaptor<ActivateJobsResponse> responseArgumentCaptor = ArgumentCaptor.forClass(ActivateJobsResponse.class);
    verify(request.getResponseObserver()).onNext(responseArgumentCaptor.capture());
    final ActivateJobsResponse response = responseArgumentCaptor.getValue();
    assertThat(response.getJobsList()).hasSize(10);
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServerStreamObserver(io.camunda.zeebe.gateway.grpc.ServerStreamObserver) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) Mockito.doThrow(org.mockito.Mockito.doThrow) StreamObserver(io.grpc.stub.StreamObserver) Duration(java.time.Duration) Map(java.util.Map) Mockito.atLeast(org.mockito.Mockito.atLeast) ActorSchedulerRule(io.camunda.zeebe.util.sched.testing.ActorSchedulerRule) Actor(io.camunda.zeebe.util.sched.Actor) Mockito.doReturn(org.mockito.Mockito.doReturn) ControlledActorClock(io.camunda.zeebe.util.sched.clock.ControlledActorClock) BrokerRejection(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejection) BrokerError(io.camunda.zeebe.gateway.impl.broker.response.BrokerError) Collectors(java.util.stream.Collectors) BrokerFailJobRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerFailJobRequest) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) TestUtil.waitUntil(io.camunda.zeebe.test.util.TestUtil.waitUntil) BrokerErrorResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerErrorResponse) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) BrokerRejectionException(io.camunda.zeebe.gateway.cmd.BrokerRejectionException) BrokerRejectionResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejectionResponse) ErrorCode(io.camunda.zeebe.protocol.record.ErrorCode) StatusException(io.grpc.StatusException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) AtomicReference(java.util.concurrent.atomic.AtomicReference) Mockito.timeout(org.mockito.Mockito.timeout) Code(io.grpc.Status.Code) ArgumentCaptor(org.mockito.ArgumentCaptor) ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) Either(io.camunda.zeebe.util.Either) BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) Intent(io.camunda.zeebe.protocol.record.intent.Intent) Before(org.junit.Before) StubbedBrokerClient(io.camunda.zeebe.gateway.api.util.StubbedBrokerClient) RequestHandler(io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) InflightActivateJobsRequest(io.camunda.zeebe.gateway.impl.job.InflightActivateJobsRequest) LongPollingActivateJobsHandler(io.camunda.zeebe.gateway.impl.job.LongPollingActivateJobsHandler) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) Rule(org.junit.Rule) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) ActorControl(io.camunda.zeebe.util.sched.ActorControl) RejectionType(io.camunda.zeebe.protocol.record.RejectionType) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord) InflightActivateJobsRequest(io.camunda.zeebe.gateway.impl.job.InflightActivateJobsRequest) HashMap(java.util.HashMap) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) BrokerRejectionException(io.camunda.zeebe.gateway.cmd.BrokerRejectionException) StatusException(io.grpc.StatusException) BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) ServerStreamObserver(io.camunda.zeebe.gateway.grpc.ServerStreamObserver) Test(org.junit.Test)

Aggregations

RequestHandler (io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler)6 BrokerActivateJobsRequest (io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest)6 BrokerRejection (io.camunda.zeebe.gateway.impl.broker.response.BrokerRejection)6 BrokerRejectionResponse (io.camunda.zeebe.gateway.impl.broker.response.BrokerRejectionResponse)6 BrokerResponse (io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse)6 ActivateJobsRequest (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest)6 ActivateJobsResponse (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse)6 ActivatedJob (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob)6 JobBatchRecord (io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord)6 RejectionType (io.camunda.zeebe.protocol.record.RejectionType)6 Intent (io.camunda.zeebe.protocol.record.intent.Intent)6 Duration (java.time.Duration)6 List (java.util.List)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 Test (org.junit.Test)6 GatewayTest (io.camunda.zeebe.gateway.api.util.GatewayTest)3 StubbedBrokerClient (io.camunda.zeebe.gateway.api.util.StubbedBrokerClient)3 BrokerRejectionException (io.camunda.zeebe.gateway.cmd.BrokerRejectionException)3 ServerStreamObserver (io.camunda.zeebe.gateway.grpc.ServerStreamObserver)3 BrokerFailJobRequest (io.camunda.zeebe.gateway.impl.broker.request.BrokerFailJobRequest)3