|
25 | 25 |
|
26 | 26 | public class PreallocatedCircuitBreakerServiceTests extends ESTestCase { |
27 | 27 | public void testUseNotPreallocated() { |
28 | | - try (HierarchyCircuitBreakerService real = real()) { |
29 | | - try (PreallocatedCircuitBreakerService preallocated = preallocateRequest(real, 1024)) { |
30 | | - CircuitBreaker b = preallocated.getBreaker(CircuitBreaker.REQUEST); |
31 | | - b.addEstimateBytesAndMaybeBreak(100, "test"); |
32 | | - b.addWithoutBreaking(-100); |
33 | | - } |
34 | | - assertThat(real.getBreaker(CircuitBreaker.REQUEST).getUsed(), equalTo(0L)); |
| 28 | + HierarchyCircuitBreakerService real = real(); |
| 29 | + try (PreallocatedCircuitBreakerService preallocated = preallocateRequest(real, 1024)) { |
| 30 | + CircuitBreaker b = preallocated.getBreaker(CircuitBreaker.REQUEST); |
| 31 | + b.addEstimateBytesAndMaybeBreak(100, "test"); |
| 32 | + b.addWithoutBreaking(-100); |
35 | 33 | } |
| 34 | + assertThat(real.getBreaker(CircuitBreaker.REQUEST).getUsed(), equalTo(0L)); |
36 | 35 | } |
37 | 36 |
|
38 | 37 | public void testUseLessThanPreallocated() { |
39 | | - try (HierarchyCircuitBreakerService real = real()) { |
40 | | - try (PreallocatedCircuitBreakerService preallocated = preallocateRequest(real, 1024)) { |
41 | | - CircuitBreaker b = preallocated.getBreaker(CircuitBreaker.REQUEST); |
42 | | - b.addEstimateBytesAndMaybeBreak(100, "test"); |
43 | | - b.addWithoutBreaking(-100); |
44 | | - } |
45 | | - assertThat(real.getBreaker(CircuitBreaker.REQUEST).getUsed(), equalTo(0L)); |
| 38 | + HierarchyCircuitBreakerService real = real(); |
| 39 | + try (PreallocatedCircuitBreakerService preallocated = preallocateRequest(real, 1024)) { |
| 40 | + CircuitBreaker b = preallocated.getBreaker(CircuitBreaker.REQUEST); |
| 41 | + b.addEstimateBytesAndMaybeBreak(100, "test"); |
| 42 | + b.addWithoutBreaking(-100); |
46 | 43 | } |
| 44 | + assertThat(real.getBreaker(CircuitBreaker.REQUEST).getUsed(), equalTo(0L)); |
47 | 45 | } |
48 | 46 |
|
49 | 47 | public void testCloseIsIdempotent() { |
50 | | - try (HierarchyCircuitBreakerService real = real()) { |
51 | | - try (PreallocatedCircuitBreakerService preallocated = preallocateRequest(real, 1024)) { |
52 | | - CircuitBreaker b = preallocated.getBreaker(CircuitBreaker.REQUEST); |
53 | | - b.addEstimateBytesAndMaybeBreak(100, "test"); |
54 | | - b.addWithoutBreaking(-100); |
55 | | - preallocated.close(); |
56 | | - assertThat(real.getBreaker(CircuitBreaker.REQUEST).getUsed(), equalTo(0L)); |
57 | | - } // Closes again which should do nothing |
| 48 | + HierarchyCircuitBreakerService real = real(); |
| 49 | + try (PreallocatedCircuitBreakerService preallocated = preallocateRequest(real, 1024)) { |
| 50 | + CircuitBreaker b = preallocated.getBreaker(CircuitBreaker.REQUEST); |
| 51 | + b.addEstimateBytesAndMaybeBreak(100, "test"); |
| 52 | + b.addWithoutBreaking(-100); |
| 53 | + preallocated.close(); |
58 | 54 | assertThat(real.getBreaker(CircuitBreaker.REQUEST).getUsed(), equalTo(0L)); |
59 | | - } |
| 55 | + } // Closes again which should do nothing |
| 56 | + assertThat(real.getBreaker(CircuitBreaker.REQUEST).getUsed(), equalTo(0L)); |
60 | 57 | } |
61 | 58 |
|
62 | 59 | public void testUseMoreThanPreallocated() { |
63 | | - try (HierarchyCircuitBreakerService real = real()) { |
64 | | - try (PreallocatedCircuitBreakerService preallocated = preallocateRequest(real, 1024)) { |
65 | | - CircuitBreaker b = preallocated.getBreaker(CircuitBreaker.REQUEST); |
66 | | - b.addEstimateBytesAndMaybeBreak(2048, "test"); |
67 | | - b.addWithoutBreaking(-2048); |
68 | | - } |
69 | | - assertThat(real.getBreaker(CircuitBreaker.REQUEST).getUsed(), equalTo(0L)); |
| 60 | + HierarchyCircuitBreakerService real = real(); |
| 61 | + try (PreallocatedCircuitBreakerService preallocated = preallocateRequest(real, 1024)) { |
| 62 | + CircuitBreaker b = preallocated.getBreaker(CircuitBreaker.REQUEST); |
| 63 | + b.addEstimateBytesAndMaybeBreak(2048, "test"); |
| 64 | + b.addWithoutBreaking(-2048); |
70 | 65 | } |
| 66 | + assertThat(real.getBreaker(CircuitBreaker.REQUEST).getUsed(), equalTo(0L)); |
71 | 67 | } |
72 | 68 |
|
73 | 69 | public void testPreallocateMoreThanRemains() { |
74 | | - try (HierarchyCircuitBreakerService real = real()) { |
75 | | - long limit = real.getBreaker(CircuitBreaker.REQUEST).getLimit(); |
76 | | - Exception e = expectThrows(CircuitBreakingException.class, () -> preallocateRequest(real, limit + 1024)); |
77 | | - assertThat(e.getMessage(), startsWith("[request] Data too large, data for [preallocate[test]] would be [")); |
78 | | - } |
| 70 | + HierarchyCircuitBreakerService real = real(); |
| 71 | + long limit = real.getBreaker(CircuitBreaker.REQUEST).getLimit(); |
| 72 | + Exception e = expectThrows(CircuitBreakingException.class, () -> preallocateRequest(real, limit + 1024)); |
| 73 | + assertThat(e.getMessage(), startsWith("[request] Data too large, data for [preallocate[test]] would be [")); |
79 | 74 | } |
80 | 75 |
|
81 | 76 | public void testRandom() { |
82 | | - try (HierarchyCircuitBreakerService real = real()) { |
83 | | - CircuitBreaker realBreaker = real.getBreaker(CircuitBreaker.REQUEST); |
84 | | - long preallocatedBytes = randomLongBetween(1, (long) (realBreaker.getLimit() * .8)); |
85 | | - try (PreallocatedCircuitBreakerService preallocated = preallocateRequest(real, preallocatedBytes)) { |
86 | | - CircuitBreaker b = preallocated.getBreaker(CircuitBreaker.REQUEST); |
87 | | - boolean usedPreallocated = false; |
88 | | - long current = 0; |
89 | | - for (int i = 0; i < 10000; i++) { |
90 | | - if (current >= preallocatedBytes) { |
91 | | - usedPreallocated = true; |
92 | | - } |
93 | | - if (usedPreallocated) { |
94 | | - assertThat(realBreaker.getUsed(), equalTo(current)); |
95 | | - } else { |
96 | | - assertThat(realBreaker.getUsed(), equalTo(preallocatedBytes)); |
97 | | - } |
98 | | - if (current > 0 && randomBoolean()) { |
99 | | - long delta = randomLongBetween(-Math.min(current, realBreaker.getLimit() / 100), 0); |
100 | | - b.addWithoutBreaking(delta); |
101 | | - current += delta; |
102 | | - continue; |
103 | | - } |
104 | | - long delta = randomLongBetween(0, realBreaker.getLimit() / 100); |
105 | | - if (randomBoolean()) { |
106 | | - b.addWithoutBreaking(delta); |
107 | | - current += delta; |
108 | | - continue; |
109 | | - } |
110 | | - if (current + delta < realBreaker.getLimit()) { |
111 | | - b.addEstimateBytesAndMaybeBreak(delta, "test"); |
112 | | - current += delta; |
113 | | - continue; |
114 | | - } |
115 | | - Exception e = expectThrows(CircuitBreakingException.class, () -> b.addEstimateBytesAndMaybeBreak(delta, "test")); |
116 | | - assertThat(e.getMessage(), startsWith("[request] Data too large, data for [test] would be [")); |
| 77 | + HierarchyCircuitBreakerService real = real(); |
| 78 | + CircuitBreaker realBreaker = real.getBreaker(CircuitBreaker.REQUEST); |
| 79 | + long preallocatedBytes = randomLongBetween(1, (long) (realBreaker.getLimit() * .8)); |
| 80 | + try (PreallocatedCircuitBreakerService preallocated = preallocateRequest(real, preallocatedBytes)) { |
| 81 | + CircuitBreaker b = preallocated.getBreaker(CircuitBreaker.REQUEST); |
| 82 | + boolean usedPreallocated = false; |
| 83 | + long current = 0; |
| 84 | + for (int i = 0; i < 10000; i++) { |
| 85 | + if (current >= preallocatedBytes) { |
| 86 | + usedPreallocated = true; |
| 87 | + } |
| 88 | + if (usedPreallocated) { |
| 89 | + assertThat(realBreaker.getUsed(), equalTo(current)); |
| 90 | + } else { |
| 91 | + assertThat(realBreaker.getUsed(), equalTo(preallocatedBytes)); |
| 92 | + } |
| 93 | + if (current > 0 && randomBoolean()) { |
| 94 | + long delta = randomLongBetween(-Math.min(current, realBreaker.getLimit() / 100), 0); |
| 95 | + b.addWithoutBreaking(delta); |
| 96 | + current += delta; |
| 97 | + continue; |
117 | 98 | } |
118 | | - b.addWithoutBreaking(-current); |
| 99 | + long delta = randomLongBetween(0, realBreaker.getLimit() / 100); |
| 100 | + if (randomBoolean()) { |
| 101 | + b.addWithoutBreaking(delta); |
| 102 | + current += delta; |
| 103 | + continue; |
| 104 | + } |
| 105 | + if (current + delta < realBreaker.getLimit()) { |
| 106 | + b.addEstimateBytesAndMaybeBreak(delta, "test"); |
| 107 | + current += delta; |
| 108 | + continue; |
| 109 | + } |
| 110 | + Exception e = expectThrows(CircuitBreakingException.class, () -> b.addEstimateBytesAndMaybeBreak(delta, "test")); |
| 111 | + assertThat(e.getMessage(), startsWith("[request] Data too large, data for [test] would be [")); |
119 | 112 | } |
120 | | - assertThat(real.getBreaker(CircuitBreaker.REQUEST).getUsed(), equalTo(0L)); |
| 113 | + b.addWithoutBreaking(-current); |
121 | 114 | } |
| 115 | + assertThat(real.getBreaker(CircuitBreaker.REQUEST).getUsed(), equalTo(0L)); |
122 | 116 | } |
123 | 117 |
|
124 | 118 | private HierarchyCircuitBreakerService real() { |
|
0 commit comments