Skip to content

Commit

Permalink
fix(tests): client retries requests that require synchronization
Browse files Browse the repository at this point in the history
- workaround for the problem that client and broker are not synchronized,
  i.e. the client is not aware when a the broker has reached a certain state
  that is able to serve the next request

related to #23, #30
  • Loading branch information
ThorbenLindhauer committed Jul 11, 2016
1 parent 75ed978 commit 94548f8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.camunda.bpm.broker.it;

import java.util.concurrent.Callable;
import java.util.function.Function;

import uk.co.real_logic.agrona.LangUtil;

public class TestUtil
{

public static final int MAX_RETRIES = 5;

public static <T> Invocation<T> doRepeatedly(Callable<T> callable)
{
return new Invocation<>(callable);
}

public static class Invocation<T>
{
protected Callable<T> callable;

public Invocation(Callable<T> callable)
{
this.callable = callable;
}

public T until(Function<T, Boolean> condition)
{
int numTries = 0;

T result = null;

do
{
try
{
if (numTries > 0)
{
Thread.sleep(100L);
}

result = callable.call();
}
catch (Exception e)
{
LangUtil.rethrowUnchecked(e);
}
numTries++;
}
while (numTries < MAX_RETRIES && !condition.apply(result));

return result;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.camunda.bpm.broker.it.ClientRule;
import org.camunda.bpm.broker.it.EmbeddedBrokerRule;
import org.camunda.bpm.broker.it.TestUtil;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.tngp.client.ProcessService;
Expand All @@ -14,7 +15,6 @@
import org.camunda.tngp.client.cmd.LockedTasksBatch;
import org.camunda.tngp.client.cmd.WorkflowInstance;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -80,7 +80,7 @@ public void shouldStartProcessWithServiceTask()
}

@Test
public void shouldPollAndLockServiceTask()
public void shouldPollAndLockServiceTask() throws InterruptedException
{
final TngpClient client = clientRule.getClient();
final ProcessService workflowService = client.processes();
Expand All @@ -91,16 +91,17 @@ public void shouldPollAndLockServiceTask()
.execute();

// when
// TODO: the task is not guaranteed to exist yet

final LockedTasksBatch tasksBatch = client
.tasks()
.pollAndLock()
.taskQueueId(0)
.taskType("foo")
.lockTime(100000L)
.maxTasks(1)
.execute();
final LockedTasksBatch tasksBatch = TestUtil.doRepeatedly(() ->
client
.tasks()
.pollAndLock()
.taskQueueId(0)
.taskType("foo")
.lockTime(100000L)
.maxTasks(1)
.execute())
.until(
(tasks) -> !tasks.getLockedTasks().isEmpty());

// then
assertThat(tasksBatch.getLockedTasks()).hasSize(1);
Expand All @@ -109,7 +110,6 @@ public void shouldPollAndLockServiceTask()
}

@Test
@Ignore
public void shouldNotLockServiceTaskOfDifferentType()
{
final TngpClient client = clientRule.getClient();
Expand All @@ -125,16 +125,17 @@ public void shouldNotLockServiceTaskOfDifferentType()
.execute();

// when
// TODO: the task is not guaranteed to exist yet

final LockedTasksBatch tasksBatch = client
.tasks()
.pollAndLock()
.taskQueueId(0)
.taskType("bar")
.lockTime(100000L)
.maxTasks(2)
.execute();
final LockedTasksBatch tasksBatch = TestUtil.doRepeatedly(() ->
client
.tasks()
.pollAndLock()
.taskQueueId(0)
.taskType("bar")
.lockTime(100000L)
.maxTasks(2)
.execute())
.until(
(tasks) -> !tasks.getLockedTasks().isEmpty());

// then
assertThat(tasksBatch.getLockedTasks()).hasSize(1);
Expand Down

0 comments on commit 94548f8

Please sign in to comment.