|
| 1 | +package org.swisspush.redisques.util; |
| 2 | + |
| 3 | +import io.vertx.core.Future; |
| 4 | +import io.vertx.core.Vertx; |
| 5 | +import io.vertx.core.shareddata.AsyncMap; |
| 6 | +import io.vertx.core.shareddata.SharedData; |
| 7 | +import io.vertx.ext.unit.Async; |
| 8 | +import io.vertx.ext.unit.TestContext; |
| 9 | +import io.vertx.ext.unit.junit.VertxUnitRunner; |
| 10 | +import org.junit.Before; |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.runner.RunWith; |
| 13 | + |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +import static org.mockito.Mockito.*; |
| 18 | + |
| 19 | +@RunWith(VertxUnitRunner.class) |
| 20 | +public class DequeueStatisticCollectorTest { |
| 21 | + |
| 22 | + private Vertx vertx; |
| 23 | + private SharedData sharedData; |
| 24 | + private AsyncMap<String, DequeueStatistic> asyncMap; |
| 25 | + private DequeueStatisticCollector dequeueStatisticCollectorEnabled; |
| 26 | + private DequeueStatisticCollector dequeueStatisticCollectorDisabled; |
| 27 | + |
| 28 | + @Before |
| 29 | + public void setUp() { |
| 30 | + vertx = mock(Vertx.class); |
| 31 | + sharedData = mock(SharedData.class); |
| 32 | + asyncMap = mock(AsyncMap.class); |
| 33 | + |
| 34 | + // Mock sharedData.getAsyncMap to return asyncMap |
| 35 | + doAnswer(invocation -> { |
| 36 | + io.vertx.core.Handler<io.vertx.core.AsyncResult<AsyncMap<String, DequeueStatistic>>> handler = invocation.getArgument(1); |
| 37 | + handler.handle(Future.succeededFuture(asyncMap)); |
| 38 | + return null; |
| 39 | + }).when(sharedData).getAsyncMap(anyString(), any()); |
| 40 | + |
| 41 | + when(vertx.sharedData()).thenReturn(sharedData); |
| 42 | + |
| 43 | + // Initialize DequeueStatisticCollector with enabled/disabled stats collection |
| 44 | + dequeueStatisticCollectorEnabled = new DequeueStatisticCollector(vertx, true); |
| 45 | + dequeueStatisticCollectorDisabled = new DequeueStatisticCollector(vertx, false); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testGetAllDequeueStatisticsEnabled(TestContext context) { |
| 50 | + // Mock asyncMap.entries() to return a non-empty map |
| 51 | + Map<String, DequeueStatistic> dequeueStats = new HashMap<>(); |
| 52 | + dequeueStats.put("queue1", new DequeueStatistic()); |
| 53 | + when(asyncMap.entries()).thenReturn(Future.succeededFuture(dequeueStats)); |
| 54 | + |
| 55 | + // Test when dequeue statistics are enabled |
| 56 | + Async async = context.async(); |
| 57 | + dequeueStatisticCollectorEnabled.getAllDequeueStatistics().onComplete(result -> { |
| 58 | + context.assertTrue(result.succeeded()); |
| 59 | + context.assertEquals(1, result.result().size()); |
| 60 | + async.complete(); |
| 61 | + }); |
| 62 | + |
| 63 | + // Verify that sharedData and asyncMap were used correctly |
| 64 | + verify(sharedData, times(1)).getAsyncMap(anyString(), any()); |
| 65 | + verify(asyncMap, times(1)).entries(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testGetAllDequeueStatisticsDisabled(TestContext context) { |
| 70 | + // Test when dequeue statistics are disabled |
| 71 | + Async async = context.async(); |
| 72 | + dequeueStatisticCollectorDisabled.getAllDequeueStatistics().onComplete(result -> { |
| 73 | + context.assertTrue(result.succeeded()); |
| 74 | + context.assertTrue(result.result().isEmpty()); |
| 75 | + async.complete(); |
| 76 | + }); |
| 77 | + |
| 78 | + // Verify that sharedData and asyncMap were NOT used |
| 79 | + verifyNoInteractions(sharedData); |
| 80 | + verifyNoInteractions(asyncMap); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testGetAllDequeueStatisticsAsyncMapFailure(TestContext context) { |
| 85 | + // Simulate failure in sharedData.getAsyncMap |
| 86 | + doAnswer(invocation -> { |
| 87 | + io.vertx.core.Handler<io.vertx.core.AsyncResult<AsyncMap<String, DequeueStatistic>>> handler = invocation.getArgument(1); |
| 88 | + handler.handle(Future.failedFuture(new RuntimeException("Failed to retrieve async map"))); |
| 89 | + return null; |
| 90 | + }).when(sharedData).getAsyncMap(anyString(), any()); |
| 91 | + |
| 92 | + // Test when asyncMap retrieval fails |
| 93 | + Async async = context.async(); |
| 94 | + dequeueStatisticCollectorEnabled.getAllDequeueStatistics().onComplete(result -> { |
| 95 | + context.assertTrue(result.failed()); |
| 96 | + context.assertEquals("Failed to retrieve async map", result.cause().getMessage()); |
| 97 | + async.complete(); |
| 98 | + }); |
| 99 | + |
| 100 | + // Verify that sharedData.getAsyncMap was used, but asyncMap.entries() was not |
| 101 | + verify(sharedData, times(1)).getAsyncMap(anyString(), any()); |
| 102 | + verifyNoInteractions(asyncMap); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testGetAllDequeueStatisticsEntriesFailure(TestContext context) { |
| 107 | + // Simulate success in sharedData.getAsyncMap, but failure in asyncMap.entries |
| 108 | + doAnswer(invocation -> { |
| 109 | + io.vertx.core.Handler<io.vertx.core.AsyncResult<AsyncMap<String, DequeueStatistic>>> handler = invocation.getArgument(1); |
| 110 | + handler.handle(Future.succeededFuture(asyncMap)); |
| 111 | + return null; |
| 112 | + }).when(sharedData).getAsyncMap(anyString(), any()); |
| 113 | + |
| 114 | + when(asyncMap.entries()).thenReturn(Future.failedFuture(new RuntimeException("Failed to retrieve entries"))); |
| 115 | + |
| 116 | + // Test when asyncMap.entries fails |
| 117 | + Async async = context.async(); |
| 118 | + dequeueStatisticCollectorEnabled.getAllDequeueStatistics().onComplete(result -> { |
| 119 | + context.assertTrue(result.failed()); |
| 120 | + context.assertEquals("Failed to retrieve entries", result.cause().getMessage()); |
| 121 | + async.complete(); |
| 122 | + }); |
| 123 | + |
| 124 | + // Verify that sharedData.getAsyncMap and asyncMap.entries were used correctly |
| 125 | + verify(sharedData, times(1)).getAsyncMap(anyString(), any()); |
| 126 | + verify(asyncMap, times(1)).entries(); |
| 127 | + } |
| 128 | +} |
0 commit comments