QueueCircuitBreakerImpl implements QueueCircuitBreaker, RuleChangesObserver, Refreshable { @Override public Future<String> unlockNextQueue() { log.debug("About to unlock the next queue"); Future<String> future = Future.future(); queueCircuitBreakerStorage.popQueueToUnlock().setHandler(event -> { if (event.failed()) { future.fail(event.cause().getMessage()); return; } String queueToUnlock = event.result(); if (queueToUnlock != null) { unlockQueue(queueToUnlock).setHandler(event1 -> { if (event1.failed()) { future.fail(event1.cause().getMessage()); return; } future.complete(event1.result()); }); } else { future.complete(null); } }); 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 testUnlockNextQueue(TestContext context){ Async async = context.async(2); Mockito.when(queueCircuitBreakerStorage.popQueueToUnlock()) .thenReturn(Future.succeededFuture("queue_1")); vertx.eventBus().consumer(Address.redisquesAddress(), (Message<JsonObject> event) -> { context.assertEquals("deleteLock", event.body().getString("operation")); event.reply(new JsonObject().put("status", "ok")); async.countDown(); }); queueCircuitBreaker.unlockNextQueue().setHandler(event -> { context.assertTrue(event.succeeded()); context.assertEquals("queue_1", event.result()); verify(queueCircuitBreakerStorage, times(1)).popQueueToUnlock(); async.countDown(); }); async.awaitSuccess(); }
@Test public void testUnlockNextQueueFailingRedisques(TestContext context){ Async async = context.async(2); Mockito.when(queueCircuitBreakerStorage.popQueueToUnlock()) .thenReturn(Future.succeededFuture("queue_1")); vertx.eventBus().consumer(Address.redisquesAddress(), (Message<JsonObject> event) -> { context.assertEquals("deleteLock", event.body().getString("operation")); event.reply(new JsonObject().put("status", "error")); async.countDown(); }); queueCircuitBreaker.unlockNextQueue().setHandler(event -> { context.assertTrue(event.failed()); context.assertTrue(event.cause().getMessage().equalsIgnoreCase("queue_1")); verify(queueCircuitBreakerStorage, times(1)).popQueueToUnlock(); async.countDown(); }); async.awaitSuccess(); }
@Test public void testUnlockNextQueueFailingStorage(TestContext context){ Async async = context.async(); Mockito.when(queueCircuitBreakerStorage.popQueueToUnlock()) .thenReturn(Future.failedFuture("unable to pop queueToUnlock from list")); vertx.eventBus().consumer(Address.redisquesAddress(), (Message<JsonObject> event) -> { context.fail("Redisques should not have been called when the storage failed"); }); queueCircuitBreaker.unlockNextQueue().setHandler(event -> { context.assertTrue(event.failed()); context.assertTrue(event.cause().getMessage().contains("unable to pop queueToUnlock from list")); verify(queueCircuitBreakerStorage, times(1)).popQueueToUnlock(); async.complete(); }); async.awaitSuccess(); }