use of io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse 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.protocol.GatewayOuterClass.ActivateJobsResponse in project zeebe by camunda.
the class ActivateJobsTest method shouldActivateJobsRoundRobin.
@Test
public void shouldActivateJobsRoundRobin() {
// given
final ActivateJobsStub stub = new ActivateJobsStub();
stub.registerWith(brokerClient);
final String type = "test";
final int maxJobsToActivate = 2;
final ActivateJobsRequest request = ActivateJobsRequest.newBuilder().setType(type).setMaxJobsToActivate(maxJobsToActivate).build();
for (int partitionOffset = 0; partitionOffset < 3; partitionOffset++) {
stub.addAvailableJobs(type, maxJobsToActivate);
// when
final Iterator<ActivateJobsResponse> responses = client.activateJobs(request);
// then
assertThat(responses.hasNext()).isTrue();
final ActivateJobsResponse response = responses.next();
for (final ActivatedJob activatedJob : response.getJobsList()) {
assertThat(Protocol.decodePartitionId(activatedJob.getKey())).isEqualTo(Protocol.START_PARTITION_ID + partitionOffset);
}
}
}
use of io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse 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.protocol.GatewayOuterClass.ActivateJobsResponse in project zeebe by camunda.
the class ResponseMapper method toActivateJobsResponse.
public static ActivateJobsResponse toActivateJobsResponse(final long key, final JobBatchRecord brokerResponse) {
final ActivateJobsResponse.Builder responseBuilder = ActivateJobsResponse.newBuilder();
final Iterator<LongValue> jobKeys = brokerResponse.jobKeys().iterator();
final Iterator<JobRecord> jobs = brokerResponse.jobs().iterator();
while (jobKeys.hasNext() && jobs.hasNext()) {
final LongValue jobKey = jobKeys.next();
final JobRecord job = jobs.next();
final ActivatedJob activatedJob = ActivatedJob.newBuilder().setKey(jobKey.getValue()).setType(bufferAsString(job.getTypeBuffer())).setBpmnProcessId(job.getBpmnProcessId()).setElementId(job.getElementId()).setProcessInstanceKey(job.getProcessInstanceKey()).setProcessDefinitionVersion(job.getProcessDefinitionVersion()).setProcessDefinitionKey(job.getProcessDefinitionKey()).setElementInstanceKey(job.getElementInstanceKey()).setCustomHeaders(bufferAsJson(job.getCustomHeadersBuffer())).setWorker(bufferAsString(job.getWorkerBuffer())).setRetries(job.getRetries()).setDeadline(job.getDeadline()).setVariables(bufferAsJson(job.getVariablesBuffer())).build();
responseBuilder.addJobs(activatedJob);
}
return responseBuilder.build();
}
use of io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse 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);
}
Aggregations