Skip to content

Commit

Permalink
Simplified test
Browse files Browse the repository at this point in the history
Simpplified invocation completion
Doc fix
  • Loading branch information
jhalterman committed Jul 6, 2015
1 parent 54f1a72 commit 2731121
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -56,7 +56,7 @@ Recurrent.get(invocation -> {
if (failure == null)
invocation.complete(result);
else if (!invocation.retry(failure))
log.error("Connection attempts failed", failure)
log.error("Connection attempts failed", failure);
}
}, retryPolicy, executor));
```
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -51,7 +51,7 @@
<dependency>
<groupId>net.jodah</groupId>
<artifactId>concurrentunit</artifactId>
<version>0.3.5</version>
<version>0.3.6-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
22 changes: 14 additions & 8 deletions src/main/java/net/jodah/recurrent/Invocation.java
Expand Up @@ -13,7 +13,7 @@
*/
public class Invocation {
private RetryPolicy retryPolicy;
private RecurrentFuture<?> future;
private RecurrentFuture<Object> future;
private long startTime;

/** Count of retry attempts */
Expand All @@ -23,19 +23,26 @@ public class Invocation {
/** Indicates whether a retry has been requested */
volatile boolean retryRequested;

@SuppressWarnings("unchecked")
Invocation(RetryPolicy retryPolicy, RecurrentFuture<?> future) {
this.retryPolicy = retryPolicy;
this.future = future;
this.future = (RecurrentFuture<Object>) future;
waitTime = retryPolicy.getDelay().toNanos();
startTime = System.nanoTime();
}

/**
* Completes the invocation.
*/
@SuppressWarnings("unchecked")
public void complete() {
future.complete(null, null);
}

/**
* Completes the invocation.
*/
public void complete(Object result) {
((RecurrentFuture<Object>) future).complete(result, null);
future.complete(result, null);
}

/**
Expand All @@ -62,7 +69,6 @@ public boolean retry(Throwable failure) {
return retryInternal(failure);
}

@SuppressWarnings("unchecked")
private boolean retryInternal(Throwable failure) {
if (retryRequested)
return true;
Expand All @@ -73,13 +79,13 @@ private boolean retryInternal(Throwable failure) {
retryRequested = true;
return true;
}

if (failure == null)
failure = new RuntimeException("Retry invocations exceeded");
((RecurrentFuture<Object>) future).complete(null, failure);
future.complete(null, failure);
return false;
}

/**
* Returns the wait time.
*/
Expand Down
17 changes: 8 additions & 9 deletions src/test/java/net/jodah/recurrent/RecurrentTest.java
Expand Up @@ -88,7 +88,6 @@ private void assertRunWithExecutor(Object runnable) throws Throwable {
: Recurrent.run((ContextualRunnable) runnable, new RetryPolicy(), executor);

// Then
waiter.expectResume();
future.whenComplete((result, failure) -> {
waiter.assertNull(result);
waiter.assertNull(failure);
Expand All @@ -107,7 +106,6 @@ private void assertRunWithExecutor(Object runnable) throws Throwable {
: Recurrent.run((ContextualRunnable) runnable, retryTwice, executor);

// Then
waiter.expectResume();
future2.whenComplete((result, failure) -> {
waiter.assertNull(result);
waiter.assertTrue(failure instanceof SocketException);
Expand Down Expand Up @@ -162,7 +160,6 @@ private void assertGetWithExecutor(Object callable) throws Throwable {
: Recurrent.get((ContextualCallable<Boolean>) callable, new RetryPolicy(), executor);

// Then
waiter.expectResume();
future.whenComplete((result, failure) -> {
waiter.assertTrue(result);
waiter.assertNull(failure);
Expand All @@ -182,7 +179,6 @@ private void assertGetWithExecutor(Object callable) throws Throwable {
: Recurrent.get((ContextualCallable) callable, retryTwice, executor);

// Then
waiter.expectResume();
future2.whenComplete((result, failure) -> {
waiter.assertNull(result);
waiter.assertTrue(failure instanceof SocketException);
Expand Down Expand Up @@ -223,7 +219,6 @@ private void assertGetFuture(Object callable) throws Throwable {
: Recurrent.future((ContextualCallable) callable, new RetryPolicy(), executor);

// Then
waiter.expectResume();
future.whenComplete((result, failure) -> {
waiter.assertTrue(result);
waiter.assertNull(failure);
Expand All @@ -243,7 +238,6 @@ private void assertGetFuture(Object callable) throws Throwable {
: Recurrent.future((ContextualCallable) callable, retryTwice, executor);

// Then
waiter.expectResume();
future2.whenComplete((result, failure) -> {
waiter.assertNull(result);
waiter.assertTrue(matches(failure, CompletionException.class, SocketException.class));
Expand Down Expand Up @@ -296,14 +290,19 @@ public void shouldCancelFuture() throws Throwable {
future.cancel(true);
assertTrue(future.isCancelled());
}

public void shouldManuallyRetryAndComplete() throws Throwable {
Recurrent.get((ctx) -> {
if (ctx.getRetryCount() < 2)
ctx.retry();
else
ctx.complete(true);
return null;
} , retryAlways, executor);
return true;
} , retryAlways, executor).whenComplete((result, failure) -> {
waiter.assertTrue(result);
waiter.assertNull(failure);
waiter.resume();
});
waiter.await(3000);
}
}
Expand Up @@ -78,7 +78,6 @@ public void shouldHandleConcurrentWaiters() throws Throwable {
circuit.open();

final Waiter waiter = new Waiter();
waiter.expectResumes(3);
for (int i = 0; i < 3; i++)
new Thread(new Runnable() {
@Override
Expand All @@ -97,7 +96,6 @@ public void shouldInterruptWaiters() throws Throwable {
circuit.open();

final Waiter waiter = new Waiter();
waiter.expectResumes(3);
for (int i = 0; i < 3; i++)
new Thread(new Runnable() {
@Override
Expand All @@ -116,7 +114,6 @@ public void shouldNotBlockOpenWhenSyncAcquired() throws Throwable {
circuit.open();

final Waiter waiter = new Waiter();
waiter.expectResume();
new Thread(new Runnable() {
@Override
public void run() {
Expand Down

0 comments on commit 2731121

Please sign in to comment.