|
21 | 21 |
|
22 | 22 | import com.carrotsearch.hppc.BitMixer;
|
23 | 23 | import com.carrotsearch.hppc.IntHashSet;
|
| 24 | +import org.elasticsearch.common.breaker.CircuitBreaker; |
| 25 | +import org.elasticsearch.common.breaker.CircuitBreakingException; |
| 26 | +import org.elasticsearch.common.breaker.NoopCircuitBreaker; |
24 | 27 | import org.elasticsearch.common.util.BigArrays;
|
25 | 28 | import org.elasticsearch.search.aggregations.metrics.HyperLogLogPlusPlus;
|
| 29 | +import org.elasticsearch.indices.breaker.CircuitBreakerService; |
26 | 30 | import org.elasticsearch.test.ESTestCase;
|
27 | 31 |
|
| 32 | +import java.util.concurrent.atomic.AtomicLong; |
| 33 | + |
28 | 34 | import static org.elasticsearch.search.aggregations.metrics.HyperLogLogPlusPlus.MAX_PRECISION;
|
29 | 35 | import static org.elasticsearch.search.aggregations.metrics.HyperLogLogPlusPlus.MIN_PRECISION;
|
30 | 36 | import static org.hamcrest.Matchers.closeTo;
|
| 37 | +import static org.hamcrest.Matchers.equalTo; |
| 38 | +import static org.mockito.Mockito.mock; |
| 39 | +import static org.mockito.Mockito.when; |
31 | 40 |
|
32 | 41 | public class HyperLogLogPlusPlusTests extends ESTestCase {
|
33 | 42 | public void testEncodeDecode() {
|
@@ -127,4 +136,40 @@ public void testPrecisionFromThreshold() {
|
127 | 136 | assertEquals(18, HyperLogLogPlusPlus.precisionFromThreshold(100000));
|
128 | 137 | assertEquals(18, HyperLogLogPlusPlus.precisionFromThreshold(1000000));
|
129 | 138 | }
|
| 139 | + |
| 140 | + public void testCircuitBreakerOnConstruction() { |
| 141 | + int whenToBreak = randomInt(10); |
| 142 | + AtomicLong total = new AtomicLong(); |
| 143 | + CircuitBreakerService breakerService = mock(CircuitBreakerService.class); |
| 144 | + when(breakerService.getBreaker(CircuitBreaker.REQUEST)).thenReturn(new NoopCircuitBreaker(CircuitBreaker.REQUEST) { |
| 145 | + private int countDown = whenToBreak; |
| 146 | + @Override |
| 147 | + public double addEstimateBytesAndMaybeBreak(long bytes, String label) throws CircuitBreakingException { |
| 148 | + if (countDown-- == 0) { |
| 149 | + throw new CircuitBreakingException("test error", bytes, Long.MAX_VALUE, Durability.TRANSIENT); |
| 150 | + } |
| 151 | + total.addAndGet(bytes); |
| 152 | + return total.get(); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public long addWithoutBreaking(long bytes) { |
| 157 | + total.addAndGet(bytes); |
| 158 | + return total.get(); |
| 159 | + } |
| 160 | + }); |
| 161 | + BigArrays bigArrays = new BigArrays(null, breakerService, CircuitBreaker.REQUEST).withCircuitBreaking(); |
| 162 | + final int p = randomIntBetween(HyperLogLogPlusPlus.MIN_PRECISION, HyperLogLogPlusPlus.MAX_PRECISION); |
| 163 | + try { |
| 164 | + for (int i = 0; i < whenToBreak + 1; ++i) { |
| 165 | + final HyperLogLogPlusPlus subject = new HyperLogLogPlusPlus(p, bigArrays, 0); |
| 166 | + subject.close(); |
| 167 | + } |
| 168 | + fail("Must fail"); |
| 169 | + } catch (CircuitBreakingException e) { |
| 170 | + // OK |
| 171 | + } |
| 172 | + |
| 173 | + assertThat(total.get(), equalTo(0L)); |
| 174 | + } |
130 | 175 | }
|
0 commit comments