1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.feed.client;
import ai.vespa.feed.client.FeedClient.CircuitBreaker;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicLong;
import static ai.vespa.feed.client.FeedClient.CircuitBreaker.State.CLOSED;
import static ai.vespa.feed.client.FeedClient.CircuitBreaker.State.HALF_OPEN;
import static ai.vespa.feed.client.FeedClient.CircuitBreaker.State.OPEN;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author jonmv
*/
class GracePeriodCircuitBreakerTest {
@Test
void testCircuitBreaker() {
AtomicLong now = new AtomicLong(0);
long SECOND = 1000;
CircuitBreaker breaker = new GracePeriodCircuitBreaker(now::get, Duration.ofSeconds(1), Duration.ofMinutes(1));
assertEquals(CLOSED, breaker.state(), "Initial state is closed");
now.addAndGet(100 * SECOND);
assertEquals(CLOSED, breaker.state(), "State is closed after some time without activity");
breaker.success();
assertEquals(CLOSED, breaker.state(), "State is closed after a success");
now.addAndGet(100 * SECOND);
assertEquals(CLOSED, breaker.state(), "State is closed some time after a success");
breaker.failure();
assertEquals(CLOSED, breaker.state(), "State is closed right after a failure");
now.addAndGet(SECOND);
assertEquals(CLOSED, breaker.state(), "State is closed until grace period has passed");
now.addAndGet(1);
assertEquals(HALF_OPEN, breaker.state(), "State is half-open when grace period has passed");
breaker.success();
assertEquals(CLOSED, breaker.state(), "State is closed after a new success");
breaker.failure();
now.addAndGet(60 * SECOND);
assertEquals(HALF_OPEN, breaker.state(), "State is half-open until doom period has passedd");
now.addAndGet(1);
assertEquals(OPEN, breaker.state(), "State is open when doom period has passed");
breaker.success();
assertEquals(OPEN, breaker.state(), "State remains open in spite of new successes");
}
}
|