Skip to content

Commit

Permalink
Improve SegmentChannelFactory testing. (#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdcove2 authored Feb 2, 2024
1 parent d1f2082 commit 995b71e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static class SegmentChannelFactoryImpl implements SeekableByteChannelFac

private SegmentChannelFactoryImpl(final SeekableByteChannelFactory seekableByteChannelFactory, final long start,
final long length) {
Validate.notNull(seekableByteChannelFactory, "Required: seekableByteChannelFactory not null!");
Validate.isTrue(seekableByteChannelFactory != null, "Required: seekableByteChannelFactory != null!");
Validate.isTrue(start >= 0, "Required: start >= 0!");
Validate.isTrue(length >= 0, "Required: length >= 0!");
try (SeekableByteChannel sbc = seekableByteChannelFactory.create()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import emissary.test.core.junit5.UnitTest;

import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class SegmentChannelFactoryTest extends UnitTest {
Expand All @@ -20,14 +26,21 @@ void testCreate() throws IOException {
new Random(0).nextBytes(bytes);

final SeekableByteChannelFactory sbcf = InMemoryChannelFactory.create(bytes);
final ExceptionChannelFactory ecf = new ExceptionChannelFactory();

assertThrows(NullPointerException.class, () -> SegmentChannelFactory.create(null, 0, 0));
assertThrows(IllegalArgumentException.class, () -> SegmentChannelFactory.create(null, 0, 0));
assertThrows(IllegalArgumentException.class, () -> SegmentChannelFactory.create(sbcf, -1, 0));
assertThrows(IllegalArgumentException.class, () -> SegmentChannelFactory.create(sbcf, 0, -1));
assertThrows(IllegalArgumentException.class, () -> SegmentChannelFactory.create(sbcf, bytes.length + 1, 0));
assertThrows(IllegalArgumentException.class, () -> SegmentChannelFactory.create(sbcf, 0, bytes.length + 1));
assertThrows(IllegalArgumentException.class,
() -> SegmentChannelFactory.create(new ExceptionChannelFactory(), 0, 0));
assertThrows(IllegalArgumentException.class, () -> SegmentChannelFactory.create(ecf, 0, 0));

final CheckCloseChannelFactory cccf = new CheckCloseChannelFactory();

try (SeekableByteChannel sbc = SegmentChannelFactory.create(cccf, 0, 0).create()) {
}

assertEquals(List.of(true, true), cccf.isClosedList);

// Test all start and length combinations.
for (int start = 0; start <= bytes.length; start++) {
Expand Down Expand Up @@ -61,4 +74,35 @@ protected long sizeImpl() throws IOException {
};
}
}

private static class CheckCloseChannelFactory implements SeekableByteChannelFactory {
private final AtomicInteger instanceNumber = new AtomicInteger(0);

public final List<Boolean> isClosedList = Collections.synchronizedList(new ArrayList<>());

@Override
public SeekableByteChannel create() {
LoggerFactory.getLogger(SegmentChannelFactoryTest.class).info("CHECKCLOSE", new Throwable("CHECKCLOSE"));
isClosedList.add(false);

return new AbstractSeekableByteChannel() {
final int myInstanceNumber = instanceNumber.getAndIncrement();

@Override
protected void closeImpl() throws IOException {
isClosedList.set(myInstanceNumber, true);
}

@Override
protected int readImpl(ByteBuffer byteBuffer) throws IOException {
return 0;
}

@Override
protected long sizeImpl() throws IOException {
return 1;
}
};
}
}
}

0 comments on commit 995b71e

Please sign in to comment.