diff --git a/server/src/main/java/com/hedera/block/server/mediator/MediatorConfig.java b/server/src/main/java/com/hedera/block/server/mediator/MediatorConfig.java index 9618d0e9e..646e5ec14 100644 --- a/server/src/main/java/com/hedera/block/server/mediator/MediatorConfig.java +++ b/server/src/main/java/com/hedera/block/server/mediator/MediatorConfig.java @@ -43,6 +43,10 @@ public record MediatorConfig(@ConfigProperty(defaultValue = "1024") int ringBuff throw new IllegalArgumentException("Ring buffer size must be greater than 0"); } + if ((ringBufferSize & (ringBufferSize - 1)) != 0) { + throw new IllegalArgumentException("Ring buffer size must be a power of 2"); + } + LOGGER.log(INFO, "Mediator configuration mediator.ringBufferSize: " + ringBufferSize); } } diff --git a/server/src/main/java/com/hedera/block/server/notifier/NotifierConfig.java b/server/src/main/java/com/hedera/block/server/notifier/NotifierConfig.java index 0b5c1dd70..1b72085a5 100644 --- a/server/src/main/java/com/hedera/block/server/notifier/NotifierConfig.java +++ b/server/src/main/java/com/hedera/block/server/notifier/NotifierConfig.java @@ -43,6 +43,10 @@ public record NotifierConfig(@ConfigProperty(defaultValue = "1024") int ringBuff throw new IllegalArgumentException("Ring buffer size must be greater than 0"); } + if ((ringBufferSize & (ringBufferSize - 1)) != 0) { + throw new IllegalArgumentException("Ring buffer size must be a power of 2"); + } + LOGGER.log(INFO, "Notifier configuration notifier.ringBufferSize: " + ringBufferSize); } } diff --git a/server/src/test/java/com/hedera/block/server/mediator/MediatorConfigTest.java b/server/src/test/java/com/hedera/block/server/mediator/MediatorConfigTest.java index 5cb42cb62..8586c6d5d 100644 --- a/server/src/test/java/com/hedera/block/server/mediator/MediatorConfigTest.java +++ b/server/src/test/java/com/hedera/block/server/mediator/MediatorConfigTest.java @@ -19,6 +19,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.util.stream.IntStream; import org.junit.jupiter.api.Test; public class MediatorConfigTest { @@ -35,4 +36,25 @@ public void testMediatorConfig_negativeRingBufferSize() { assertThrows(IllegalArgumentException.class, () -> new MediatorConfig(-1)); assertEquals("Ring buffer size must be greater than 0", exception.getMessage()); } + + @Test + public void testMediatorConfig_powerOf2Values() { + + int[] powerOf2Values = IntStream.iterate(2, n -> n * 2).limit(30).toArray(); + + // Test the power of 2 values + for (int powerOf2Value : powerOf2Values) { + MediatorConfig mediatorConfig = new MediatorConfig(powerOf2Value); + assertEquals(powerOf2Value, mediatorConfig.ringBufferSize()); + } + + // Test the non-power of 2 values + for (int powerOf2Value : powerOf2Values) { + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> new MediatorConfig(powerOf2Value + 1)); + assertEquals("Ring buffer size must be a power of 2", exception.getMessage()); + } + } } diff --git a/server/src/test/java/com/hedera/block/server/notifier/NotifierConfigTest.java b/server/src/test/java/com/hedera/block/server/notifier/NotifierConfigTest.java index d01c45ebc..329da42e0 100644 --- a/server/src/test/java/com/hedera/block/server/notifier/NotifierConfigTest.java +++ b/server/src/test/java/com/hedera/block/server/notifier/NotifierConfigTest.java @@ -19,6 +19,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.util.stream.IntStream; import org.junit.jupiter.api.Test; public class NotifierConfigTest { @@ -35,4 +36,24 @@ public void testNotifierConfig_negativeRingBufferSize() { assertThrows(IllegalArgumentException.class, () -> new NotifierConfig(-1)); assertEquals("Ring buffer size must be greater than 0", exception.getMessage()); } + + @Test + public void testMediatorConfig_powerOf2Values() { + + int[] powerOf2Values = IntStream.iterate(2, n -> n * 2).limit(30).toArray(); + + for (int powerOf2Value : powerOf2Values) { + NotifierConfig notifierConfig = new NotifierConfig(powerOf2Value); + assertEquals(powerOf2Value, notifierConfig.ringBufferSize()); + } + + // Test the non-power of 2 values + for (int powerOf2Value : powerOf2Values) { + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> new NotifierConfig(powerOf2Value + 1)); + assertEquals("Ring buffer size must be a power of 2", exception.getMessage()); + } + } }