Skip to content

Commit

Permalink
fix package of DequeueStatisticCollectorTest
Browse files Browse the repository at this point in the history
add more testes for failure cases
simplify code as suggested in PR
  • Loading branch information
steniobhz committed Sep 25, 2024
1 parent 6973703 commit 824bbb9
Showing 1 changed file with 54 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.swisspush.redisques;
package org.swisspush.redisques.util;

import io.vertx.core.Future;
import io.vertx.core.Vertx;
Expand All @@ -10,8 +10,6 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.swisspush.redisques.util.DequeueStatistic;
import org.swisspush.redisques.util.DequeueStatisticCollector;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -33,6 +31,7 @@ public void setUp() {
sharedData = mock(SharedData.class);
asyncMap = mock(AsyncMap.class);

// Mock sharedData.getAsyncMap to return asyncMap
doAnswer(invocation -> {
io.vertx.core.Handler<io.vertx.core.AsyncResult<AsyncMap<String, DequeueStatistic>>> handler = invocation.getArgument(1);
handler.handle(Future.succeededFuture(asyncMap));
Expand All @@ -41,19 +40,19 @@ public void setUp() {

when(vertx.sharedData()).thenReturn(sharedData);

// Set up the enabled and disabled DequeueStatisticCollector
// Initialize DequeueStatisticCollector with enabled/disabled stats collection
dequeueStatisticCollectorEnabled = new DequeueStatisticCollector(vertx, true);
dequeueStatisticCollectorDisabled = new DequeueStatisticCollector(vertx, false);
}

@Test
public void testGetAllDequeueStatisticsEnabled(TestContext context) {
// Mocking asyncMap.entries() to return a non-empty map
// Mock asyncMap.entries() to return a non-empty map
Map<String, DequeueStatistic> dequeueStats = new HashMap<>();
dequeueStats.put("queue1", new DequeueStatistic());
when(asyncMap.entries()).thenReturn(Future.succeededFuture(dequeueStats));

// Test for when dequeue statistics are enabled
// Test when dequeue statistics are enabled
Async async = context.async();
dequeueStatisticCollectorEnabled.getAllDequeueStatistics().onComplete(result -> {
context.assertTrue(result.succeeded());
Expand All @@ -68,7 +67,7 @@ public void testGetAllDequeueStatisticsEnabled(TestContext context) {

@Test
public void testGetAllDequeueStatisticsDisabled(TestContext context) {
// Test for when dequeue statistics are disabled
// Test when dequeue statistics are disabled
Async async = context.async();
dequeueStatisticCollectorDisabled.getAllDequeueStatistics().onComplete(result -> {
context.assertTrue(result.succeeded());
Expand All @@ -77,7 +76,53 @@ public void testGetAllDequeueStatisticsDisabled(TestContext context) {
});

// Verify that sharedData and asyncMap were NOT used
verify(sharedData, never()).getAsyncMap(anyString(), any());
verify(asyncMap, never()).entries();
verifyNoInteractions(sharedData);
verifyNoInteractions(asyncMap);
}

@Test
public void testGetAllDequeueStatisticsAsyncMapFailure(TestContext context) {
// Simulate failure in sharedData.getAsyncMap
doAnswer(invocation -> {
io.vertx.core.Handler<io.vertx.core.AsyncResult<AsyncMap<String, DequeueStatistic>>> handler = invocation.getArgument(1);
handler.handle(Future.failedFuture(new RuntimeException("Failed to retrieve async map")));
return null;
}).when(sharedData).getAsyncMap(anyString(), any());

// Test when asyncMap retrieval fails
Async async = context.async();
dequeueStatisticCollectorEnabled.getAllDequeueStatistics().onComplete(result -> {
context.assertTrue(result.failed());
context.assertEquals("Failed to retrieve async map", result.cause().getMessage());
async.complete();
});

// Verify that sharedData.getAsyncMap was used, but asyncMap.entries() was not
verify(sharedData, times(1)).getAsyncMap(anyString(), any());
verifyNoInteractions(asyncMap);
}

@Test
public void testGetAllDequeueStatisticsEntriesFailure(TestContext context) {
// Simulate success in sharedData.getAsyncMap, but failure in asyncMap.entries
doAnswer(invocation -> {
io.vertx.core.Handler<io.vertx.core.AsyncResult<AsyncMap<String, DequeueStatistic>>> handler = invocation.getArgument(1);
handler.handle(Future.succeededFuture(asyncMap));
return null;
}).when(sharedData).getAsyncMap(anyString(), any());

when(asyncMap.entries()).thenReturn(Future.failedFuture(new RuntimeException("Failed to retrieve entries")));

// Test when asyncMap.entries fails
Async async = context.async();
dequeueStatisticCollectorEnabled.getAllDequeueStatistics().onComplete(result -> {
context.assertTrue(result.failed());
context.assertEquals("Failed to retrieve entries", result.cause().getMessage());
async.complete();
});

// Verify that sharedData.getAsyncMap and asyncMap.entries were used correctly
verify(sharedData, times(1)).getAsyncMap(anyString(), any());
verify(asyncMap, times(1)).entries();
}
}

0 comments on commit 824bbb9

Please sign in to comment.