use of io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest in project zeebe by camunda.
the class ActivateJobsTest method shouldMapRequestAndResponse.
@Test
public void shouldMapRequestAndResponse() {
// given
final ActivateJobsStub stub = new ActivateJobsStub();
stub.registerWith(brokerClient);
final String jobType = "testJob";
final String worker = "testWorker";
final int maxJobsToActivate = 13;
final Duration timeout = Duration.ofMinutes(12);
final List<String> fetchVariables = Arrays.asList("foo", "bar", "baz");
final ActivateJobsRequest request = ActivateJobsRequest.newBuilder().setType(jobType).setWorker(worker).setMaxJobsToActivate(maxJobsToActivate).setTimeout(timeout.toMillis()).addAllFetchVariable(fetchVariables).build();
stub.addAvailableJobs(jobType, maxJobsToActivate);
// when
final Iterator<ActivateJobsResponse> responses = client.activateJobs(request);
// then
assertThat(responses.hasNext()).isTrue();
final ActivateJobsResponse response = responses.next();
assertThat(response.getJobsCount()).isEqualTo(maxJobsToActivate);
for (int i = 0; i < maxJobsToActivate; i++) {
final ActivatedJob job = response.getJobs(i);
assertThat(job.getKey()).isEqualTo(Protocol.encodePartitionId(Protocol.START_PARTITION_ID, i));
assertThat(job.getType()).isEqualTo(jobType);
assertThat(job.getWorker()).isEqualTo(worker);
assertThat(job.getRetries()).isEqualTo(stub.getRetries());
assertThat(job.getDeadline()).isEqualTo(stub.getDeadline());
assertThat(job.getProcessInstanceKey()).isEqualTo(stub.getProcessInstanceKey());
assertThat(job.getBpmnProcessId()).isEqualTo(stub.getBpmnProcessId());
assertThat(job.getProcessDefinitionVersion()).isEqualTo(stub.getProcessDefinitionVersion());
assertThat(job.getProcessDefinitionKey()).isEqualTo(stub.getProcessDefinitionKey());
assertThat(job.getElementId()).isEqualTo(stub.getElementId());
assertThat(job.getElementInstanceKey()).isEqualTo(stub.getElementInstanceKey());
JsonUtil.assertEquality(job.getCustomHeaders(), stub.getCustomHeaders());
JsonUtil.assertEquality(job.getVariables(), stub.getVariables());
}
final BrokerActivateJobsRequest brokerRequest = brokerClient.getSingleBrokerRequest();
final JobBatchRecord brokerRequestValue = brokerRequest.getRequestWriter();
assertThat(brokerRequestValue.getMaxJobsToActivate()).isEqualTo(maxJobsToActivate);
assertThat(brokerRequestValue.getTypeBuffer()).isEqualTo(wrapString(jobType));
assertThat(brokerRequestValue.getTimeout()).isEqualTo(timeout.toMillis());
assertThat(brokerRequestValue.getWorkerBuffer()).isEqualTo(wrapString(worker));
assertThat(brokerRequestValue.variables()).extracting(v -> BufferUtil.bufferAsString(v.getValue())).containsExactlyInAnyOrderElementsOf(fetchVariables);
}
use of io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest in project zeebe by camunda.
the class LongPollingActivateJobsTest method shouldReturnJobsIfSomeBrokersHaveJobsWhileOthersReturnResourceExhaustionResponse.
@Test
public void shouldReturnJobsIfSomeBrokersHaveJobsWhileOthersReturnResourceExhaustionResponse() {
// given
final InflightActivateJobsRequest request = new InflightActivateJobsRequest(getNextRequestId(), ActivateJobsRequest.newBuilder().setType(TYPE).setMaxJobsToActivate(15).setRequestTimeout(500).build(), spy(ServerStreamObserver.class));
brokerClient.registerHandler(BrokerActivateJobsRequest.class, new RequestHandler<BrokerActivateJobsRequest, BrokerResponse<?>>() {
private final ActivateJobsStub jobsAvailableStub = new ActivateJobsStub();
{
jobsAvailableStub.addAvailableJobs(TYPE, 10);
}
@Override
public BrokerResponse<?> handle(final BrokerActivateJobsRequest request) throws Exception {
final int partitionId = request.getPartitionId();
if (partitionId == 4) {
return new BrokerErrorResponse<>(new BrokerError(ErrorCode.RESOURCE_EXHAUSTED, "backpressure"));
} 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.impl.broker.request.BrokerActivateJobsRequest in project zeebe by zeebe-io.
the class ActivateJobsTest method shouldMapRequestAndResponse.
@Test
public void shouldMapRequestAndResponse() {
// given
final ActivateJobsStub stub = new ActivateJobsStub();
stub.registerWith(brokerClient);
final String jobType = "testJob";
final String worker = "testWorker";
final int maxJobsToActivate = 13;
final Duration timeout = Duration.ofMinutes(12);
final List<String> fetchVariables = Arrays.asList("foo", "bar", "baz");
final ActivateJobsRequest request = ActivateJobsRequest.newBuilder().setType(jobType).setWorker(worker).setMaxJobsToActivate(maxJobsToActivate).setTimeout(timeout.toMillis()).addAllFetchVariable(fetchVariables).build();
stub.addAvailableJobs(jobType, maxJobsToActivate);
// when
final Iterator<ActivateJobsResponse> responses = client.activateJobs(request);
// then
assertThat(responses.hasNext()).isTrue();
final ActivateJobsResponse response = responses.next();
assertThat(response.getJobsCount()).isEqualTo(maxJobsToActivate);
for (int i = 0; i < maxJobsToActivate; i++) {
final ActivatedJob job = response.getJobs(i);
assertThat(job.getKey()).isEqualTo(Protocol.encodePartitionId(Protocol.START_PARTITION_ID, i));
assertThat(job.getType()).isEqualTo(jobType);
assertThat(job.getWorker()).isEqualTo(worker);
assertThat(job.getRetries()).isEqualTo(stub.getRetries());
assertThat(job.getDeadline()).isEqualTo(stub.getDeadline());
assertThat(job.getProcessInstanceKey()).isEqualTo(stub.getProcessInstanceKey());
assertThat(job.getBpmnProcessId()).isEqualTo(stub.getBpmnProcessId());
assertThat(job.getProcessDefinitionVersion()).isEqualTo(stub.getProcessDefinitionVersion());
assertThat(job.getProcessDefinitionKey()).isEqualTo(stub.getProcessDefinitionKey());
assertThat(job.getElementId()).isEqualTo(stub.getElementId());
assertThat(job.getElementInstanceKey()).isEqualTo(stub.getElementInstanceKey());
JsonUtil.assertEquality(job.getCustomHeaders(), stub.getCustomHeaders());
JsonUtil.assertEquality(job.getVariables(), stub.getVariables());
}
final BrokerActivateJobsRequest brokerRequest = brokerClient.getSingleBrokerRequest();
final JobBatchRecord brokerRequestValue = brokerRequest.getRequestWriter();
assertThat(brokerRequestValue.getMaxJobsToActivate()).isEqualTo(maxJobsToActivate);
assertThat(brokerRequestValue.getTypeBuffer()).isEqualTo(wrapString(jobType));
assertThat(brokerRequestValue.getTimeout()).isEqualTo(timeout.toMillis());
assertThat(brokerRequestValue.getWorkerBuffer()).isEqualTo(wrapString(worker));
assertThat(brokerRequestValue.variables()).extracting(v -> BufferUtil.bufferAsString(v.getValue())).containsExactlyInAnyOrderElementsOf(fetchVariables);
}
use of io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest in project zeebe by zeebe-io.
the class LongPollingActivateJobsTest method shouldCancelTimerOnResourceExhausted.
@Test
public void shouldCancelTimerOnResourceExhausted() {
// given
final InflightActivateJobsRequest request = getLongPollingActivateJobsRequest();
brokerClient.registerHandler(BrokerActivateJobsRequest.class, new RequestHandler<BrokerActivateJobsRequest, BrokerResponse<?>>() {
private int count = 0;
/*
* First execution of the request (count < partitionCount) -> don't activate jobs
* Second execution of the request (count >= partitionCount) -> fail immediately
*/
@Override
public BrokerResponse<?> handle(final BrokerActivateJobsRequest request) throws Exception {
if (count >= partitionsCount) {
return new BrokerErrorResponse<>(new BrokerError(ErrorCode.RESOURCE_EXHAUSTED, "backpressure"));
}
count += 1;
return activateJobsStub.handle(request);
}
});
// when
handler.activateJobs(request);
waitUntil(request::hasScheduledTimer);
brokerClient.notifyJobsAvailable(TYPE);
Awaitility.await().until(request::isAborted);
// then
final ArgumentCaptor<Throwable> throwableCaptor = ArgumentCaptor.forClass(Throwable.class);
verify(request.getResponseObserver(), times(1)).onError(throwableCaptor.capture());
assertThat(throwableCaptor.getValue()).isInstanceOf(StatusException.class);
assertThat(request.hasScheduledTimer()).isFalse();
}
use of io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest in project zeebe by zeebe-io.
the class LongPollingActivateJobsTest method shouldCancelTimerOnBrokerRejectionException.
@Test
public void shouldCancelTimerOnBrokerRejectionException() {
// given
final InflightActivateJobsRequest request = getLongPollingActivateJobsRequest();
brokerClient.registerHandler(BrokerActivateJobsRequest.class, new RequestHandler<BrokerActivateJobsRequest, BrokerResponse<?>>() {
private int count = 0;
/*
* First execution of the request (count < partitionCount) -> don't activate jobs
* Second execution of the request (count >= partitionCount) -> fail immediately
*/
@Override
public BrokerResponse<?> handle(final BrokerActivateJobsRequest request) throws Exception {
if (count >= partitionsCount) {
return new BrokerRejectionResponse<>(new BrokerRejection(Intent.UNKNOWN, 1, RejectionType.INVALID_ARGUMENT, "expected"));
}
count += 1;
return activateJobsStub.handle(request);
}
});
// when
handler.activateJobs(request);
waitUntil(request::hasScheduledTimer);
brokerClient.notifyJobsAvailable(TYPE);
Awaitility.await().until(request::isAborted);
// then
final ArgumentCaptor<Throwable> throwableCaptor = ArgumentCaptor.forClass(Throwable.class);
verify(request.getResponseObserver(), times(1)).onError(throwableCaptor.capture());
assertThat(throwableCaptor.getValue()).isInstanceOf(BrokerRejectionException.class);
assertThat(request.hasScheduledTimer()).isFalse();
}
Aggregations