2016-07-14 14:03:04 +02:00
|
|
|
/*
|
2021-01-08 17:55:38 +01:00
|
|
|
* Copyright (C) 2009-2021 Lightbend Inc. <https://www.lightbend.com>
|
2016-07-14 14:03:04 +02:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2016-07-14 14:03:04 +02:00
|
|
|
package akka.pattern;
|
|
|
|
|
|
|
|
|
|
import akka.actor.*;
|
|
|
|
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
|
|
|
|
import akka.testkit.AkkaSpec;
|
2018-04-11 16:47:36 +02:00
|
|
|
import akka.util.JavaDurationConverters;
|
2016-07-14 14:03:04 +02:00
|
|
|
import org.junit.ClassRule;
|
|
|
|
|
import org.junit.Test;
|
2020-01-11 15:14:21 +03:00
|
|
|
import org.scalatestplus.junit.JUnitSuite;
|
2016-07-14 14:03:04 +02:00
|
|
|
import scala.compat.java8.FutureConverters;
|
|
|
|
|
import scala.concurrent.Await;
|
|
|
|
|
|
2017-02-13 07:27:42 -05:00
|
|
|
import java.util.Optional;
|
2016-07-14 14:03:04 +02:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
import java.util.concurrent.CompletionStage;
|
2017-02-13 07:27:42 -05:00
|
|
|
import java.util.function.BiFunction;
|
2018-04-11 16:47:36 +02:00
|
|
|
import java.time.Duration;
|
2016-07-14 14:03:04 +02:00
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
|
|
|
|
|
public class CircuitBreakerTest extends JUnitSuite {
|
|
|
|
|
|
|
|
|
|
@ClassRule
|
2019-01-12 04:00:53 +08:00
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource =
|
|
|
|
|
new AkkaJUnitActorSystemResource("JavaAPI", AkkaSpec.testConf());
|
2016-07-14 14:03:04 +02:00
|
|
|
|
|
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void useCircuitBreakerWithCompletableFuture() throws Exception {
|
2018-04-11 16:47:36 +02:00
|
|
|
final Duration fiveSeconds = Duration.ofSeconds(5);
|
|
|
|
|
final Duration fiveHundredMillis = Duration.ofMillis(500);
|
2019-01-12 04:00:53 +08:00
|
|
|
final CircuitBreaker breaker =
|
|
|
|
|
new CircuitBreaker(
|
|
|
|
|
system.dispatcher(), system.scheduler(), 1, fiveSeconds, fiveHundredMillis);
|
2016-07-14 14:03:04 +02:00
|
|
|
|
|
|
|
|
final CompletableFuture<String> f = new CompletableFuture<>();
|
|
|
|
|
f.complete("hello");
|
|
|
|
|
final CompletionStage<String> res = breaker.callWithCircuitBreakerCS(() -> f);
|
2019-01-12 04:00:53 +08:00
|
|
|
assertEquals(
|
|
|
|
|
"hello",
|
|
|
|
|
Await.result(
|
|
|
|
|
FutureConverters.toScala(res), JavaDurationConverters.asFiniteDuration(fiveSeconds)));
|
2016-07-14 14:03:04 +02:00
|
|
|
}
|
|
|
|
|
|
2017-02-13 07:27:42 -05:00
|
|
|
@Test
|
|
|
|
|
public void useCircuitBreakerWithCompletableFutureAndCustomDefineFailure() throws Exception {
|
2018-04-11 16:47:36 +02:00
|
|
|
final Duration fiveSeconds = Duration.ofSeconds(5);
|
|
|
|
|
final Duration fiveHundredMillis = Duration.ofMillis(500);
|
2019-01-12 04:00:53 +08:00
|
|
|
final CircuitBreaker breaker =
|
|
|
|
|
new CircuitBreaker(
|
|
|
|
|
system.dispatcher(), system.scheduler(), 1, fiveSeconds, fiveHundredMillis);
|
2017-02-13 07:27:42 -05:00
|
|
|
|
|
|
|
|
final BiFunction<Optional<String>, Optional<Throwable>, java.lang.Boolean> fn =
|
2019-01-12 04:00:53 +08:00
|
|
|
(result, err) -> (result.isPresent() && result.get().equals("hello"));
|
2017-02-13 07:27:42 -05:00
|
|
|
|
|
|
|
|
final CompletableFuture<String> f = new CompletableFuture<>();
|
|
|
|
|
f.complete("hello");
|
|
|
|
|
final CompletionStage<String> res = breaker.callWithCircuitBreakerCS(() -> f, fn);
|
2019-01-12 04:00:53 +08:00
|
|
|
assertEquals(
|
|
|
|
|
"hello",
|
|
|
|
|
Await.result(
|
|
|
|
|
FutureConverters.toScala(res), JavaDurationConverters.asFiniteDuration(fiveSeconds)));
|
2017-02-13 07:27:42 -05:00
|
|
|
assertEquals(1, breaker.currentFailureCount());
|
|
|
|
|
}
|
2016-07-14 14:03:04 +02:00
|
|
|
}
|