Java 11 migrate remaining q-r (#1121)
* Moves queue-load-leveling to Java 11 * Moves reactor to Java 11 * Moves reader-writer-lock to Java 11 * Moves repository to Java 11 * Moves resource-acquisition-is-initialization to Java 11 * Moves retry to Java 11 * Moves role-object to Java 11
This commit is contained in:
committed by
Ilkka Seppälä
parent
cd2a2e7711
commit
20ea465b7f
@ -90,14 +90,14 @@ public final class App {
|
||||
}
|
||||
|
||||
private static void errorWithRetry() throws Exception {
|
||||
final Retry<String> retry = new Retry<>(
|
||||
final var retry = new Retry<>(
|
||||
new FindCustomer("123", new CustomerNotFoundException("not found")),
|
||||
3, //3 attempts
|
||||
100, //100 ms delay between attempts
|
||||
e -> CustomerNotFoundException.class.isAssignableFrom(e.getClass())
|
||||
);
|
||||
op = retry;
|
||||
final String customerId = op.perform();
|
||||
final var customerId = op.perform();
|
||||
LOG.info(String.format(
|
||||
"However, retrying the operation while ignoring a recoverable error will eventually yield "
|
||||
+ "the result %s after a number of attempts %s", customerId, retry.attempts()
|
||||
@ -105,14 +105,14 @@ public final class App {
|
||||
}
|
||||
|
||||
private static void errorWithRetryExponentialBackoff() throws Exception {
|
||||
final RetryExponentialBackoff<String> retry = new RetryExponentialBackoff<>(
|
||||
final var retry = new RetryExponentialBackoff<>(
|
||||
new FindCustomer("123", new CustomerNotFoundException("not found")),
|
||||
6, //6 attempts
|
||||
30000, //30 s max delay between attempts
|
||||
e -> CustomerNotFoundException.class.isAssignableFrom(e.getClass())
|
||||
);
|
||||
op = retry;
|
||||
final String customerId = op.perform();
|
||||
final var customerId = op.perform();
|
||||
LOG.info(String.format(
|
||||
"However, retrying the operation while ignoring a recoverable error will eventually yield "
|
||||
+ "the result %s after a number of attempts %s", customerId, retry.attempts()
|
||||
|
@ -100,8 +100,8 @@ public final class RetryExponentialBackoff<T> implements BusinessOperation<T> {
|
||||
}
|
||||
|
||||
try {
|
||||
long testDelay = (long) Math.pow(2, this.attempts()) * 1000 + RANDOM.nextInt(1000);
|
||||
long delay = testDelay < this.maxDelay ? testDelay : maxDelay;
|
||||
var testDelay = (long) Math.pow(2, this.attempts()) * 1000 + RANDOM.nextInt(1000);
|
||||
var delay = Math.min(testDelay, this.maxDelay);
|
||||
Thread.sleep(delay);
|
||||
} catch (InterruptedException f) {
|
||||
//ignore
|
||||
|
@ -23,12 +23,12 @@
|
||||
|
||||
package com.iluwatar.retry;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link FindCustomer}.
|
||||
*
|
||||
@ -40,33 +40,29 @@ public class FindCustomerTest {
|
||||
*/
|
||||
@Test
|
||||
public void noExceptions() throws Exception {
|
||||
assertThat(
|
||||
new FindCustomer("123").perform(),
|
||||
is("123")
|
||||
);
|
||||
assertThat(new FindCustomer("123").perform(), is("123"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws the given exception.
|
||||
*
|
||||
*
|
||||
* @throws Exception the expected exception
|
||||
*/
|
||||
@Test
|
||||
public void oneException() {
|
||||
assertThrows(BusinessException.class, () -> {
|
||||
new FindCustomer("123", new BusinessException("test")).perform();
|
||||
});
|
||||
var findCustomer = new FindCustomer("123", new BusinessException("test"));
|
||||
assertThrows(BusinessException.class, findCustomer::perform);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should first throw the given exceptions, then return the given result.
|
||||
*
|
||||
*
|
||||
* @throws Exception not an expected exception
|
||||
*/
|
||||
@Test
|
||||
public void resultAfterExceptions() throws Exception {
|
||||
final BusinessOperation<String> op = new FindCustomer(
|
||||
"123",
|
||||
final var op = new FindCustomer(
|
||||
"123",
|
||||
new CustomerNotFoundException("not found"),
|
||||
new DatabaseNotAvailableException("not available")
|
||||
);
|
||||
@ -81,9 +77,6 @@ public class FindCustomerTest {
|
||||
//ignore
|
||||
}
|
||||
|
||||
assertThat(
|
||||
op.perform(),
|
||||
is("123")
|
||||
);
|
||||
assertThat(op.perform(), is("123"));
|
||||
}
|
||||
}
|
||||
|
@ -23,26 +23,27 @@
|
||||
|
||||
package com.iluwatar.retry;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.hamcrest.CoreMatchers.hasItem;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link Retry}.
|
||||
*
|
||||
* @author George Aristy (george.aristy@gmail.com)
|
||||
*/
|
||||
public class RetryExponentialBackoffTest {
|
||||
/**
|
||||
* Should contain all errors thrown.
|
||||
*/
|
||||
/**
|
||||
* Should contain all errors thrown.
|
||||
*/
|
||||
@Test
|
||||
public void errors() throws Exception {
|
||||
final BusinessException e = new BusinessException("unhandled");
|
||||
final RetryExponentialBackoff<String> retry = new RetryExponentialBackoff<>(
|
||||
() -> {
|
||||
throw e;
|
||||
public void errors() {
|
||||
final var e = new BusinessException("unhandled");
|
||||
final var retry = new RetryExponentialBackoff<String>(
|
||||
() -> {
|
||||
throw e;
|
||||
},
|
||||
2,
|
||||
0
|
||||
@ -53,22 +54,19 @@ public class RetryExponentialBackoffTest {
|
||||
//ignore
|
||||
}
|
||||
|
||||
assertThat(
|
||||
retry.errors(),
|
||||
hasItem(e)
|
||||
);
|
||||
assertThat(retry.errors(), hasItem(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* No exceptions will be ignored, hence final number of attempts should be 1 even if we're asking
|
||||
* it to attempt twice.
|
||||
*/
|
||||
/**
|
||||
* No exceptions will be ignored, hence final number of attempts should be 1 even if we're asking
|
||||
* it to attempt twice.
|
||||
*/
|
||||
@Test
|
||||
public void attempts() {
|
||||
final BusinessException e = new BusinessException("unhandled");
|
||||
final RetryExponentialBackoff<String> retry = new RetryExponentialBackoff<>(
|
||||
() -> {
|
||||
throw e;
|
||||
final var e = new BusinessException("unhandled");
|
||||
final var retry = new RetryExponentialBackoff<String>(
|
||||
() -> {
|
||||
throw e;
|
||||
},
|
||||
2,
|
||||
0
|
||||
@ -79,22 +77,19 @@ public class RetryExponentialBackoffTest {
|
||||
//ignore
|
||||
}
|
||||
|
||||
assertThat(
|
||||
retry.attempts(),
|
||||
is(1)
|
||||
);
|
||||
assertThat(retry.attempts(), is(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Final number of attempts should be equal to the number of attempts asked because we are
|
||||
* asking it to ignore the exception that will be thrown.
|
||||
*/
|
||||
/**
|
||||
* Final number of attempts should be equal to the number of attempts asked because we are asking
|
||||
* it to ignore the exception that will be thrown.
|
||||
*/
|
||||
@Test
|
||||
public void ignore() throws Exception {
|
||||
final BusinessException e = new CustomerNotFoundException("customer not found");
|
||||
final RetryExponentialBackoff<String> retry = new RetryExponentialBackoff<>(
|
||||
() -> {
|
||||
throw e;
|
||||
public void ignore() {
|
||||
final var e = new CustomerNotFoundException("customer not found");
|
||||
final var retry = new RetryExponentialBackoff<String>(
|
||||
() -> {
|
||||
throw e;
|
||||
},
|
||||
2,
|
||||
0,
|
||||
@ -106,9 +101,6 @@ public class RetryExponentialBackoffTest {
|
||||
//ignore
|
||||
}
|
||||
|
||||
assertThat(
|
||||
retry.attempts(),
|
||||
is(2)
|
||||
);
|
||||
assertThat(retry.attempts(), is(2));
|
||||
}
|
||||
}
|
||||
|
@ -23,12 +23,12 @@
|
||||
|
||||
package com.iluwatar.retry;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.hasItem;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link Retry}.
|
||||
*
|
||||
@ -40,10 +40,11 @@ public class RetryTest {
|
||||
*/
|
||||
@Test
|
||||
public void errors() {
|
||||
final BusinessException e = new BusinessException("unhandled");
|
||||
final Retry<String> retry = new Retry<>(
|
||||
final var e = new BusinessException("unhandled");
|
||||
final var retry = new Retry<String>(
|
||||
() -> {
|
||||
throw e; },
|
||||
throw e;
|
||||
},
|
||||
2,
|
||||
0
|
||||
);
|
||||
@ -53,10 +54,7 @@ public class RetryTest {
|
||||
//ignore
|
||||
}
|
||||
|
||||
assertThat(
|
||||
retry.errors(),
|
||||
hasItem(e)
|
||||
);
|
||||
assertThat(retry.errors(), hasItem(e));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -65,10 +63,11 @@ public class RetryTest {
|
||||
*/
|
||||
@Test
|
||||
public void attempts() {
|
||||
final BusinessException e = new BusinessException("unhandled");
|
||||
final Retry<String> retry = new Retry<>(
|
||||
final var e = new BusinessException("unhandled");
|
||||
final var retry = new Retry<String>(
|
||||
() -> {
|
||||
throw e; },
|
||||
throw e;
|
||||
},
|
||||
2,
|
||||
0
|
||||
);
|
||||
@ -78,22 +77,20 @@ public class RetryTest {
|
||||
//ignore
|
||||
}
|
||||
|
||||
assertThat(
|
||||
retry.attempts(),
|
||||
is(1)
|
||||
);
|
||||
assertThat(retry.attempts(), is(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Final number of attempts should be equal to the number of attempts asked because we are
|
||||
* asking it to ignore the exception that will be thrown.
|
||||
* Final number of attempts should be equal to the number of attempts asked because we are asking
|
||||
* it to ignore the exception that will be thrown.
|
||||
*/
|
||||
@Test
|
||||
public void ignore() throws Exception {
|
||||
final BusinessException e = new CustomerNotFoundException("customer not found");
|
||||
final Retry<String> retry = new Retry<>(
|
||||
public void ignore() {
|
||||
final var e = new CustomerNotFoundException("customer not found");
|
||||
final var retry = new Retry<String>(
|
||||
() -> {
|
||||
throw e; },
|
||||
throw e;
|
||||
},
|
||||
2,
|
||||
0,
|
||||
ex -> CustomerNotFoundException.class.isAssignableFrom(ex.getClass())
|
||||
@ -104,10 +101,7 @@ public class RetryTest {
|
||||
//ignore
|
||||
}
|
||||
|
||||
assertThat(
|
||||
retry.attempts(),
|
||||
is(2)
|
||||
);
|
||||
assertThat(retry.attempts(), is(2));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user