QueueCircuitBreakerImpl implements QueueCircuitBreaker, RuleChangesObserver, Refreshable { @Override public Future<Void> reOpenCircuit(HttpRequest queuedRequest) { Future<Void> future = Future.future(); PatternAndCircuitHash patternAndCircuitHash = getPatternAndCircuitHashFromRequest(queuedRequest); if (patternAndCircuitHash != null) { log.info("About to reopen circuit " + patternAndCircuitHash.getPattern().pattern()); queueCircuitBreakerStorage.reOpenCircuit(patternAndCircuitHash).setHandler(event -> { if (event.failed()) { future.fail(event.cause()); return; } log.info("circuit '" + patternAndCircuitHash.getPattern().pattern() + "' has been reopened"); future.complete(); }); } else { failWithNoRuleToCircuitMappingMessage(future, null, queuedRequest); } return future; } QueueCircuitBreakerImpl(Vertx vertx, Lock lock, String redisquesAddress, QueueCircuitBreakerStorage queueCircuitBreakerStorage, RuleProvider ruleProvider, QueueCircuitBreakerRulePatternToCircuitMapping ruleToCircuitMapping, QueueCircuitBreakerConfigurationResourceManager configResourceManager, Handler<HttpServerRequest> queueCircuitBreakerHttpRequestHandler, int requestHandlerPort); @Override void rulesChanged(List<Rule> rules); @Override void refresh(); @Override boolean isCircuitCheckEnabled(); @Override boolean isStatisticsUpdateEnabled(); @Override Future<QueueCircuitState> handleQueuedRequest(String queueName, HttpRequest queuedRequest); @Override Future<Void> updateStatistics(String queueName, HttpRequest queuedRequest, QueueResponseType queueResponseType); @Override Future<Void> closeCircuit(HttpRequest queuedRequest); @Override Future<Void> closeAllCircuits(); @Override Future<Void> reOpenCircuit(HttpRequest queuedRequest); @Override Future<Void> lockQueue(String queueName, HttpRequest queuedRequest); @Override Future<String> unlockNextQueue(); @Override Future<Long> setOpenCircuitsToHalfOpen(); @Override Future<Long> unlockSampleQueues(); @Override Future<String> unlockQueue(String queueName); static final String OPEN_TO_HALF_OPEN_TASK_LOCK; static final String UNLOCK_QUEUES_TASK_LOCK; static final String UNLOCK_SAMPLE_QUEUES_TASK_LOCK; }
@Test public void testReOpenCircuit(TestContext context){ Async async = context.async(); HttpRequest req = new HttpRequest(HttpMethod.PUT, "/playground/circuitBreaker/test", MultiMap.caseInsensitiveMultiMap(), null); Mockito.when(ruleToCircuitMapping.getCircuitFromRequestUri(anyString())) .thenReturn(new PatternAndCircuitHash(Pattern.compile("/someCircuit"), "someCircuitHash")); Mockito.when(queueCircuitBreakerStorage.reOpenCircuit(any(PatternAndCircuitHash.class))) .thenReturn(Future.succeededFuture()); queueCircuitBreaker.reOpenCircuit(req).setHandler(event -> { context.assertTrue(event.succeeded()); verify(queueCircuitBreakerStorage, times(1)).reOpenCircuit(any(PatternAndCircuitHash.class)); async.complete(); }); async.awaitSuccess(); }
@Test public void testReOpenCircuitFailingStorage(TestContext context){ Async async = context.async(); HttpRequest req = new HttpRequest(HttpMethod.PUT, "/playground/circuitBreaker/test", MultiMap.caseInsensitiveMultiMap(), null); Mockito.when(ruleToCircuitMapping.getCircuitFromRequestUri(anyString())) .thenReturn(new PatternAndCircuitHash(Pattern.compile("/someCircuit"), "someCircuitHash")); Mockito.when(queueCircuitBreakerStorage.reOpenCircuit(any(PatternAndCircuitHash.class))) .thenReturn(Future.failedFuture("unable to re-open circuit")); queueCircuitBreaker.reOpenCircuit(req).setHandler(event -> { context.assertTrue(event.failed()); context.assertTrue(event.cause().getMessage().contains("unable to re-open circuit")); verify(queueCircuitBreakerStorage, times(1)).reOpenCircuit(any(PatternAndCircuitHash.class)); async.complete(); }); async.awaitSuccess(); }