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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations