Skip to content

Commit

Permalink
fix: added power of 2 checks and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Peterson <[email protected]>
  • Loading branch information
mattp-swirldslabs committed Sep 17, 2024
1 parent 1efcb90 commit ebe9856
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
}
}
}

0 comments on commit ebe9856

Please sign in to comment.