本文整理汇总了Java中io.vertx.circuitbreaker.CircuitBreakerOptions.getMaxFailures方法的典型用法代码示例。如果您正苦于以下问题:Java CircuitBreakerOptions.getMaxFailures方法的具体用法?Java CircuitBreakerOptions.getMaxFailures怎么用?Java CircuitBreakerOptions.getMaxFailures使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.vertx.circuitbreaker.CircuitBreakerOptions
的用法示例。
在下文中一共展示了CircuitBreakerOptions.getMaxFailures方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testExceptionOnSynchronousCode
import io.vertx.circuitbreaker.CircuitBreakerOptions; //导入方法依赖的package包/类
@Test
@Repeat(5)
public void testExceptionOnSynchronousCode() {
AtomicBoolean called = new AtomicBoolean(false);
CircuitBreakerOptions options = new CircuitBreakerOptions()
.setFallbackOnFailure(false)
.setResetTimeout(-1);
breaker = CircuitBreaker.create("test", vertx, options)
.fallback(t -> {
called.set(true);
return "fallback";
});
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.execute(v -> {
throw new RuntimeException("oh no, but this is expected");
});
}
await().until(() -> breaker.state() == CircuitBreakerState.OPEN ||
breaker.state() == CircuitBreakerState.HALF_OPEN);
assertThat(called.get()).isEqualTo(false);
AtomicBoolean spy = new AtomicBoolean();
breaker.execute(v -> spy.set(true));
assertThat(spy.get()).isEqualTo(false);
assertThat(called.get()).isEqualTo(true);
}
示例2: testFailureOnAsynchronousCode
import io.vertx.circuitbreaker.CircuitBreakerOptions; //导入方法依赖的package包/类
@Test
public void testFailureOnAsynchronousCode() {
AtomicBoolean called = new AtomicBoolean(false);
AtomicReference<String> result = new AtomicReference<>();
CircuitBreakerOptions options = new CircuitBreakerOptions().setResetTimeout(-1);
breaker = CircuitBreaker.create("test", vertx, options)
.fallback(v -> {
called.set(true);
return "fallback";
});
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.<String>execute(
future -> vertx.setTimer(100, l -> future.fail("expected failure"))
).setHandler(ar -> result.set(ar.result()));
}
await().until(() -> breaker.state() == CircuitBreakerState.OPEN);
assertThat(called.get()).isEqualTo(false);
AtomicBoolean spy = new AtomicBoolean();
breaker.<String>execute(
future -> vertx.setTimer(100, l -> {
future.fail("expected failure");
spy.set(true);
}))
.setHandler(ar -> result.set(ar.result()));
await().untilAtomic(called, is(true));
assertThat(spy.get()).isEqualTo(false);
assertThat(result.get()).isEqualTo("fallback");
}
示例3: testResetAttempt
import io.vertx.circuitbreaker.CircuitBreakerOptions; //导入方法依赖的package包/类
@Test
@Repeat(5)
public void testResetAttempt() {
AtomicBoolean called = new AtomicBoolean(false);
CircuitBreakerOptions options = new CircuitBreakerOptions().setResetTimeout(100);
breaker = CircuitBreaker.create("test", vertx, options)
.fallback(v -> {
called.set(true);
return "fallback";
});
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.execute(v -> {
throw new RuntimeException("oh no, but this is expected");
});
}
await().until(() -> breaker.state() == CircuitBreakerState.OPEN || breaker.state() == CircuitBreakerState.HALF_OPEN);
assertThat(called.get()).isEqualTo(false);
await().until(() -> breaker.state() == CircuitBreakerState.HALF_OPEN);
AtomicBoolean spy = new AtomicBoolean();
breaker.execute(v -> {
spy.set(true);
v.complete();
});
assertThat(spy.get()).isEqualTo(true);
assertThat(called.get()).isEqualTo(false);
await().until(() -> breaker.state() == CircuitBreakerState.CLOSED);
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
}
示例4: testResetAttemptThatFails
import io.vertx.circuitbreaker.CircuitBreakerOptions; //导入方法依赖的package包/类
@Test
@Repeat(5)
public void testResetAttemptThatFails() {
AtomicBoolean called = new AtomicBoolean(false);
CircuitBreakerOptions options = new CircuitBreakerOptions()
.setResetTimeout(100)
.setFallbackOnFailure(true);
breaker = CircuitBreaker.create("test", vertx, options)
.fallback(v -> {
called.set(true);
return "fallback";
});
await().until(() -> breaker.state() == CircuitBreakerState.CLOSED);
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.execute(v -> {
throw new RuntimeException("oh no, but this is expected");
});
}
await().until(() -> breaker.state() == CircuitBreakerState.OPEN || breaker.state() == CircuitBreakerState.HALF_OPEN);
assertThat(called.get()).isEqualTo(true);
await().until(() -> breaker.state() == CircuitBreakerState.HALF_OPEN);
called.set(false);
AtomicReference<String> result = new AtomicReference<>();
breaker.<String>execute(v -> {
throw new RuntimeException("oh no, but this is expected");
}).setHandler(ar -> result.set(ar.result()));
await().until(called::get);
await().until(() -> breaker.state() == CircuitBreakerState.OPEN || breaker.state() == CircuitBreakerState.HALF_OPEN);
assertThat(result.get()).isEqualTo("fallback");
}
示例5: testTimeout
import io.vertx.circuitbreaker.CircuitBreakerOptions; //导入方法依赖的package包/类
@Test
public void testTimeout() {
AtomicBoolean called = new AtomicBoolean(false);
CircuitBreakerOptions options = new CircuitBreakerOptions().setTimeout(100);
breaker = CircuitBreaker.create("test", vertx, options)
.fallback(v -> {
called.set(true);
return "fallback";
});
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
AtomicInteger failureCount = new AtomicInteger();
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.<String>execute(v -> {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
v.complete("done");
}).setHandler(ar -> {
if (ar.failed()) failureCount.incrementAndGet();
});
}
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.OPEN);
assertThat(called.get()).isEqualTo(false);
assertThat(failureCount.get()).isEqualTo(options.getMaxFailures());
AtomicBoolean spy = new AtomicBoolean();
AtomicReference<String> result = new AtomicReference<>();
breaker.<String>execute(v -> {
spy.set(true);
v.complete();
})
.setHandler(ar -> result.set(ar.result()));
assertThat(spy.get()).isEqualTo(false);
assertThat(called.get()).isEqualTo(true);
assertThat(result.get()).isEqualTo("fallback");
}
示例6: testTimeoutWithFallbackCalled
import io.vertx.circuitbreaker.CircuitBreakerOptions; //导入方法依赖的package包/类
@Test
public void testTimeoutWithFallbackCalled() {
AtomicBoolean called = new AtomicBoolean(false);
CircuitBreakerOptions options = new CircuitBreakerOptions().setTimeout(100)
.setResetTimeout(5000)
.setFallbackOnFailure(true);
breaker = CircuitBreaker.create("test", vertx, options)
.fallback(v -> {
called.set(true);
return "fallback";
});
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
AtomicInteger count = new AtomicInteger();
for (int i = 0; i < options.getMaxFailures() + 3; i++) {
breaker.execute(v -> {
try {
Thread.sleep(500);
v.complete("done");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
v.fail(e);
}
}).setHandler(ar -> {
if (ar.result().equals("fallback")) {
count.incrementAndGet();
}
});
}
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.OPEN);
assertThat(called.get()).isEqualTo(true);
assertThat(count.get()).isEqualTo(options.getMaxFailures() + 3);
}
示例7: testResetAttemptOnTimeout
import io.vertx.circuitbreaker.CircuitBreakerOptions; //导入方法依赖的package包/类
@Test
public void testResetAttemptOnTimeout() {
AtomicBoolean called = new AtomicBoolean(false);
AtomicBoolean hasBeenOpened = new AtomicBoolean(false);
CircuitBreakerOptions options = new CircuitBreakerOptions()
.setResetTimeout(100)
.setTimeout(10)
.setFallbackOnFailure(true);
breaker = CircuitBreaker.create("test", vertx, options)
.fallback(v -> {
called.set(true);
return "fallback";
})
.openHandler(v -> hasBeenOpened.set(true));
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.execute(future -> {
// Do nothing with the future, this is a very bad thing.
});
}
await().untilAtomic(hasBeenOpened, is(true));
assertThat(called.get()).isEqualTo(true);
await().until(() -> breaker.state() == CircuitBreakerState.HALF_OPEN);
called.set(false);
breaker.execute(Future::complete);
await().until(() -> breaker.state() == CircuitBreakerState.CLOSED);
await().untilAtomic(called, is(false));
}
示例8: testThatOnlyOneRequestIsCheckedInHalfOpen
import io.vertx.circuitbreaker.CircuitBreakerOptions; //导入方法依赖的package包/类
@Test
public void testThatOnlyOneRequestIsCheckedInHalfOpen() {
AtomicBoolean called = new AtomicBoolean(false);
AtomicBoolean hasBeenOpened = new AtomicBoolean(false);
CircuitBreakerOptions options = new CircuitBreakerOptions()
.setResetTimeout(1000)
.setFallbackOnFailure(true);
breaker = CircuitBreaker.create("test", vertx, options)
.fallback(v -> {
called.set(true);
return "fallback";
})
.openHandler(v -> hasBeenOpened.set(true));
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.execute(future -> future.fail("expected failure"));
}
await().untilAtomic(hasBeenOpened, is(true));
assertThat(called.get()).isEqualTo(true);
await().until(() -> breaker.state() == CircuitBreakerState.HALF_OPEN);
called.set(false);
AtomicInteger fallbackCalled = new AtomicInteger();
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.executeWithFallback(
future -> vertx.setTimer(500, l -> future.complete()),
v -> {
fallbackCalled.incrementAndGet();
return "fallback";
});
}
await().until(() -> breaker.state() == CircuitBreakerState.CLOSED);
assertThat(fallbackCalled.get()).isEqualTo(options.getMaxFailures() - 1);
}
示例9: testWhenFallbackThrowsAnException
import io.vertx.circuitbreaker.CircuitBreakerOptions; //导入方法依赖的package包/类
@Test
public void testWhenFallbackThrowsAnException() {
CircuitBreakerOptions options = new CircuitBreakerOptions()
.setResetTimeout(5000)
.setFallbackOnFailure(true);
breaker = CircuitBreaker.create("test", vertx, options);
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
List<AsyncResult<String>> results = new ArrayList<>();
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.<String>executeWithFallback(
future -> future.fail("expected failure"),
t -> {
throw new RuntimeException("boom");
})
.setHandler(results::add);
}
await().until(() -> results.size() == options.getMaxFailures());
results.forEach(ar -> {
assertThat(ar.failed()).isTrue();
assertThat(ar.cause()).isNotNull().hasMessage("boom");
});
results.clear();
await().until(() -> breaker.state() == CircuitBreakerState.OPEN);
breaker.<String>executeWithFallback(
future -> future.fail("expected failure"),
t -> {
throw new RuntimeException("boom");
})
.setHandler(results::add);
await().until(() -> results.size() == 1);
results.forEach(ar -> {
assertThat(ar.failed()).isTrue();
assertThat(ar.cause()).isNotNull().hasMessage("boom");
});
}
示例10: testTimeout
import io.vertx.circuitbreaker.CircuitBreakerOptions; //导入方法依赖的package包/类
@Test
public void testTimeout() {
CircuitBreakerOptions options = new CircuitBreakerOptions().setTimeout(100).setMaxFailures(2);
breaker = CircuitBreaker.create("test", vertx, options);
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
AtomicInteger count = new AtomicInteger();
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.execute(future ->
client.getNow(8080, "localhost", "/long", response -> {
count.incrementAndGet();
future.complete();
}));
}
await().untilAtomic(count, is(options.getMaxFailures()));
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.OPEN);
Future<String> result = Future.future();
breaker.executeAndReportWithFallback(result, future ->
client.getNow(8080, "localhost", "/long", response -> {
System.out.println("Got response");
future.complete();
}), v -> "fallback");
await().until(() -> result.result().equals("fallback"));
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.OPEN);
}
示例11: testResetAttemptThatFailsOnTimeout
import io.vertx.circuitbreaker.CircuitBreakerOptions; //导入方法依赖的package包/类
@Test
@Repeat(10)
public void testResetAttemptThatFailsOnTimeout() {
AtomicBoolean called = new AtomicBoolean(false);
AtomicBoolean hasBeenOpened = new AtomicBoolean(false);
CircuitBreakerOptions options = new CircuitBreakerOptions()
.setResetTimeout(100)
.setTimeout(10)
.setFallbackOnFailure(true);
breaker = CircuitBreaker.create("test", vertx, options)
.fallback(v -> {
called.set(true);
return "fallback";
})
.openHandler(v -> hasBeenOpened.set(true));
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.execute(future -> {
// Do nothing with the future, this is a very bad thing.
});
}
await().untilAtomic(hasBeenOpened, is(true));
assertThat(called.get()).isEqualTo(true);
await().until(() -> breaker.state() == CircuitBreakerState.HALF_OPEN);
hasBeenOpened.set(false);
called.set(false);
breaker.execute(future -> {
// Do nothing with the future, this is a very bad thing.
});
// Failed again, open circuit
await().until(() -> breaker.state() == CircuitBreakerState.OPEN || breaker.state() == CircuitBreakerState.HALF_OPEN);
await().untilAtomic(called, is(true));
await().untilAtomic(hasBeenOpened, is(true));
hasBeenOpened.set(false);
called.set(false);
breaker.execute(future -> {
// Do nothing with the future, this is a very bad thing.
});
// Failed again, open circuit
await().until(() -> breaker.state() == CircuitBreakerState.OPEN || breaker.state() == CircuitBreakerState.HALF_OPEN);
await().untilAtomic(called, is(true));
await().untilAtomic(hasBeenOpened, is(true));
hasBeenOpened.set(false);
called.set(false);
hasBeenOpened.set(false);
called.set(false);
await().until(() -> breaker.state() == CircuitBreakerState.CLOSED || breaker.state() == CircuitBreakerState.HALF_OPEN);
// If HO - need to get next request executed and wait until we are closed
breaker.execute(Future::complete);
await().until(() -> {
if (breaker.state() == CircuitBreakerState.CLOSED) {
return true;
} else {
breaker.execute(Future::complete);
return false;
}
});
called.set(false);
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.execute(f -> f.complete(null));
}
await().until(() -> breaker.state() == CircuitBreakerState.CLOSED);
await().untilAtomic(hasBeenOpened, is(false));
}
示例12: testFailureWhenThereIsNoFallback
import io.vertx.circuitbreaker.CircuitBreakerOptions; //导入方法依赖的package包/类
@Test
public void testFailureWhenThereIsNoFallback() {
CircuitBreakerOptions options = new CircuitBreakerOptions()
.setResetTimeout(50000)
.setTimeout(300)
.setFallbackOnFailure(true);
breaker = CircuitBreaker.create("test", vertx, options);
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
List<AsyncResult<String>> results = new ArrayList<>();
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.<String>execute(future -> future.fail("expected failure"))
.setHandler(results::add);
}
await().until(() -> results.size() == options.getMaxFailures());
results.forEach(ar -> {
assertThat(ar.failed()).isTrue();
assertThat(ar.cause()).isNotNull().hasMessage("expected failure");
});
results.clear();
await().until(() -> breaker.state() == CircuitBreakerState.OPEN);
breaker.<String>execute(future -> future.fail("expected failure"))
.setHandler(results::add);
await().until(() -> results.size() == 1);
results.forEach(ar -> {
assertThat(ar.failed()).isTrue();
assertThat(ar.cause()).isNotNull().hasMessage("open circuit");
});
((CircuitBreakerImpl) breaker).reset(true);
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
results.clear();
breaker.<String>execute(future -> {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// Ignored.
}
})
.setHandler(results::add);
await().until(() -> results.size() == 1);
results.forEach(ar -> {
assertThat(ar.failed()).isTrue();
assertThat(ar.cause()).isNotNull().hasMessage("operation timeout");
});
}
示例13: testTheExceptionReceivedByFallback
import io.vertx.circuitbreaker.CircuitBreakerOptions; //导入方法依赖的package包/类
@Test
public void testTheExceptionReceivedByFallback() {
CircuitBreakerOptions options = new CircuitBreakerOptions()
.setResetTimeout(50000)
.setTimeout(300)
.setFallbackOnFailure(true);
List<Throwable> failures = new ArrayList<>();
breaker = CircuitBreaker.create("test", vertx, options)
.fallback(failures::add);
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.<String>execute(future -> future.fail("expected failure"));
}
await().until(() -> failures.size() == options.getMaxFailures());
failures.forEach(ar -> assertThat(ar).isNotNull().hasMessage("expected failure"));
failures.clear();
((CircuitBreakerImpl) breaker).reset(true);
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
failures.clear();
breaker.<String>execute(future -> {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// Ignored.
}
});
await().until(() -> failures.size() == 1);
failures.forEach(ar -> assertThat(ar).isNotNull().hasMessage("operation timeout"));
((CircuitBreakerImpl) breaker).reset(true);
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
failures.clear();
breaker.<String>execute(future -> {
throw new RuntimeException("boom");
});
await().until(() -> failures.size() == 1);
failures.forEach(ar -> assertThat(ar).isNotNull().hasMessage("boom"));
}