use of io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob 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.ActivatedJob 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.ActivatedJob in project zeebe by camunda.
the class ActivateJobsTest method shouldActivateJobs.
@Test
public void shouldActivateJobs() {
// given
final ActivatedJob activatedJob1 = ActivatedJob.newBuilder().setKey(12).setType("foo").setProcessInstanceKey(123).setBpmnProcessId("test1").setProcessDefinitionVersion(2).setProcessDefinitionKey(23).setElementId("foo").setElementInstanceKey(23213).setCustomHeaders("{\"version\": \"1\"}").setWorker("worker1").setRetries(34).setDeadline(1231).setVariables("{\"key\": \"val\"}").build();
final ActivatedJob activatedJob2 = ActivatedJob.newBuilder().setKey(42).setType("foo").setProcessInstanceKey(333).setBpmnProcessId("test3").setProcessDefinitionVersion(23).setProcessDefinitionKey(11).setElementId("bar").setElementInstanceKey(111).setCustomHeaders("{\"key\": \"value\"}").setWorker("worker1").setRetries(334).setDeadline(3131).setVariables("{\"bar\": 3}").build();
gatewayService.onActivateJobsRequest(activatedJob1, activatedJob2);
// when
final ActivateJobsResponse response = client.newActivateJobsCommand().jobType("foo").maxJobsToActivate(3).timeout(Duration.ofMillis(1000)).workerName("worker1").send().join();
// then
assertThat(response.getJobs()).hasSize(2);
io.camunda.zeebe.client.api.response.ActivatedJob job = response.getJobs().get(0);
assertThat(job.getKey()).isEqualTo(activatedJob1.getKey());
assertThat(job.getType()).isEqualTo(activatedJob1.getType());
assertThat(job.getBpmnProcessId()).isEqualTo(activatedJob1.getBpmnProcessId());
assertThat(job.getElementId()).isEqualTo(activatedJob1.getElementId());
assertThat(job.getElementInstanceKey()).isEqualTo(activatedJob1.getElementInstanceKey());
assertThat(job.getProcessDefinitionVersion()).isEqualTo(activatedJob1.getProcessDefinitionVersion());
assertThat(job.getProcessDefinitionKey()).isEqualTo(activatedJob1.getProcessDefinitionKey());
assertThat(job.getProcessInstanceKey()).isEqualTo(activatedJob1.getProcessInstanceKey());
assertThat(job.getCustomHeaders()).isEqualTo(fromJsonAsMap(activatedJob1.getCustomHeaders()));
assertThat(job.getWorker()).isEqualTo(activatedJob1.getWorker());
assertThat(job.getRetries()).isEqualTo(activatedJob1.getRetries());
assertThat(job.getDeadline()).isEqualTo(activatedJob1.getDeadline());
assertThat(job.getVariables()).isEqualTo(activatedJob1.getVariables());
job = response.getJobs().get(1);
assertThat(job.getKey()).isEqualTo(activatedJob2.getKey());
assertThat(job.getType()).isEqualTo(activatedJob2.getType());
assertThat(job.getBpmnProcessId()).isEqualTo(activatedJob2.getBpmnProcessId());
assertThat(job.getElementId()).isEqualTo(activatedJob2.getElementId());
assertThat(job.getElementInstanceKey()).isEqualTo(activatedJob2.getElementInstanceKey());
assertThat(job.getProcessDefinitionVersion()).isEqualTo(activatedJob2.getProcessDefinitionVersion());
assertThat(job.getProcessDefinitionKey()).isEqualTo(activatedJob2.getProcessDefinitionKey());
assertThat(job.getProcessInstanceKey()).isEqualTo(activatedJob2.getProcessInstanceKey());
assertThat(job.getCustomHeaders()).isEqualTo(fromJsonAsMap(activatedJob2.getCustomHeaders()));
assertThat(job.getWorker()).isEqualTo(activatedJob2.getWorker());
assertThat(job.getRetries()).isEqualTo(activatedJob2.getRetries());
assertThat(job.getDeadline()).isEqualTo(activatedJob2.getDeadline());
assertThat(job.getVariables()).isEqualTo(activatedJob2.getVariables());
final ActivateJobsRequest request = gatewayService.getLastRequest();
assertThat(request.getType()).isEqualTo("foo");
assertThat(request.getMaxJobsToActivate()).isEqualTo(3);
assertThat(request.getTimeout()).isEqualTo(1000);
assertThat(request.getWorker()).isEqualTo("worker1");
}
use of io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob 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.ActivatedJob 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