QueueCircuitBreakerImpl implements QueueCircuitBreaker, RuleChangesObserver, Refreshable { @Override public Future<QueueCircuitState> handleQueuedRequest(String queueName, HttpRequest queuedRequest) { Future<QueueCircuitState> future = Future.future(); PatternAndCircuitHash patternAndCircuitHash = getPatternAndCircuitHashFromRequest(queuedRequest); if (patternAndCircuitHash != null) { this.queueCircuitBreakerStorage.getQueueCircuitState(patternAndCircuitHash).setHandler(event -> { if (event.failed()) { future.fail(event.cause()); } else { future.complete(event.result()); if (QueueCircuitState.OPEN == event.result()) { lockQueueSync(queueName, queuedRequest); } } }); } else { failWithNoRuleToCircuitMappingMessage(future, queueName, 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 testHandleQueuedRequest(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.getQueueCircuitState(any(PatternAndCircuitHash.class))) .thenReturn(Future.succeededFuture(QueueCircuitState.CLOSED)); Mockito.when(queueCircuitBreakerStorage.lockQueue(anyString(), any(PatternAndCircuitHash.class))) .thenReturn(Future.succeededFuture()); queueCircuitBreaker.handleQueuedRequest("someQueue", req).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertEquals(QueueCircuitState.CLOSED, event.result()); Mockito.when(queueCircuitBreakerStorage.getQueueCircuitState(any(PatternAndCircuitHash.class))) .thenReturn(Future.succeededFuture(QueueCircuitState.OPEN)); queueCircuitBreaker.handleQueuedRequest("someQueue", req).setHandler(event1 -> { context.assertTrue(event1.succeeded()); context.assertEquals(QueueCircuitState.OPEN, event1.result()); async.complete(); }); }); async.awaitSuccess(); }
@Test public void testHandleQueuedRequestNoCircuitMapping(TestContext context){ Async async = context.async(); HttpRequest req = new HttpRequest(HttpMethod.PUT, "/playground/circuitBreaker/test", MultiMap.caseInsensitiveMultiMap(), null); Mockito.when(ruleToCircuitMapping.getCircuitFromRequestUri(anyString())).thenReturn(null); queueCircuitBreaker.handleQueuedRequest("someQueue", req).setHandler(event -> { context.assertTrue(event.failed()); context.assertNotNull(event.cause()); context.assertTrue(event.cause().getMessage().contains("no rule to circuit mapping found for queue")); async.complete(); }); async.awaitSuccess(); }