Skip to content

Commit

Permalink
Add slightly more coverage and apply IDE suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
centic9 committed Mar 19, 2023
1 parent 5a64df6 commit fcd2fd5
Showing 1 changed file with 70 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

public class BlockingSeekableRingBufferTest extends AbstractBlockingSeekableRingBufferTester {
Expand All @@ -19,70 +20,81 @@ protected SeekableRingBuffer<Chunk> getBlockingSeekableRingBuffer() {

@Test
public void testPersistence() throws IOException {
BlockingSeekableRingBuffer localBuffer = new BlockingSeekableRingBuffer(10);
for(byte i = 0;i < 15;i++) {
localBuffer.add(new Chunk(new byte[] { i }, "", 0));
try (BlockingSeekableRingBuffer localBuffer = new BlockingSeekableRingBuffer(10)) {
for (byte i = 0; i < 15; i++) {
localBuffer.add(new Chunk(new byte[]{i}, "", 0));
}

Stream stream = new Stream();
stream.setUrl("url1");
stream.setStreamType(Stream.StreamType.live);

// get the persistence
final BufferPersistenceDTO dto = localBuffer.toPersistence(stream, false, false);
assertNotNull(dto);
assertEquals("url1", dto.getStream().getUrl());

// check what next returns
final Chunk next = localBuffer.next();
assertEquals(new Chunk(new byte[]{6}, "", 0), next);

// check the local buffer
assertFalse(localBuffer.full());
assertFalse(localBuffer.empty());
assertEquals(8, localBuffer.size());
assertEquals(9, localBuffer.fill());

// check the DTO
assertEquals(6, dto.getNextGet());
assertEquals(5, dto.getNextAdd());
assertEquals(9, dto.getFill());
assertEquals(10, dto.getBuffer().length);
assertEquals(0, dto.getNumberOfDiskFiles());
assertEquals(0, dto.getNumberOfDiskChunks());
assertNull(dto.getDataDir());

// then convert the DTO back into a buffer and do a next() as well
try (BlockingSeekableRingBuffer back = BlockingSeekableRingBuffer.fromPersistence(dto)) {
assertEquals(next, back.next());
}

// and finally ensure the state is the same
assertFalse(localBuffer.full());
assertFalse(localBuffer.empty());
assertEquals(8, localBuffer.size());
assertEquals(9, localBuffer.fill());
}

Stream stream = new Stream();
stream.setUrl("url1");
stream.setStreamType(Stream.StreamType.live);

// get the persistence
final BufferPersistenceDTO dto = localBuffer.toPersistence(stream, false, false);
assertNotNull(dto);
assertEquals("url1", dto.getStream().getUrl());

// check what next returns
final Chunk next = localBuffer.next();
assertEquals(new Chunk(new byte[] {6}, "", 0), next);

// check the local buffer
assertFalse(localBuffer.full());
assertFalse(localBuffer.empty());
assertEquals(8, localBuffer.size());
assertEquals(9, localBuffer.fill());

// check the DTO
assertEquals(6, dto.getNextGet());
assertEquals(5, dto.getNextAdd());
assertEquals(9, dto.getFill());
assertEquals(10, dto.getBuffer().length);
assertEquals(0, dto.getNumberOfDiskFiles());
assertEquals(0, dto.getNumberOfDiskChunks());
assertNull(dto.getDataDir());

// then convert the DTO back into a buffer and do a next() as well
BlockingSeekableRingBuffer back = BlockingSeekableRingBuffer.fromPersistence(dto);
assertEquals(next, back.next());

// and finally ensure the state is the same
assertFalse(localBuffer.full());
assertFalse(localBuffer.empty());
assertEquals(8, localBuffer.size());
assertEquals(9, localBuffer.fill());
}

@Test
public void testToStringBuffer() {
BlockingSeekableRingBuffer localBuffer = new BlockingSeekableRingBuffer(10);

assertTrue(localBuffer.empty());
assertFalse(localBuffer.full());
assertTrue("Had: " + localBuffer,
localBuffer.toString().contains("empty=true"));
assertTrue("Had: " + localBuffer,
localBuffer.toString().contains("full=false"));

for(byte i = 0;i < 15;i++) {
localBuffer.add(new Chunk(new byte[] { i }, "", 0));
try (BlockingSeekableRingBuffer localBuffer = new BlockingSeekableRingBuffer(10)) {
assertTrue(localBuffer.empty());
assertFalse(localBuffer.full());
assertTrue("Had: " + localBuffer,
localBuffer.toString().contains("empty=true"));
assertTrue("Had: " + localBuffer,
localBuffer.toString().contains("full=false"));

for (byte i = 0; i < 15; i++) {
localBuffer.add(new Chunk(new byte[]{i}, "", 0));
}

assertFalse(localBuffer.empty());
assertTrue(localBuffer.full());
assertTrue("Had: " + localBuffer,
localBuffer.toString().contains("empty=false"));
assertTrue("Had: " + localBuffer,
localBuffer.toString().contains("full=true"));
}
}

assertFalse(localBuffer.empty());
assertTrue(localBuffer.full());
assertTrue("Had: " + localBuffer,
localBuffer.toString().contains("empty=false"));
assertTrue("Had: " + localBuffer,
localBuffer.toString().contains("full=true"));
@Test
public void testFailedToRead() {
BufferPersistenceDTO dto = new BufferPersistenceDTO(
0, null, false, false);
//noinspection resource
assertThrows(IOException.class,
() -> BlockingSeekableRingBuffer.fromPersistence(dto));
}
}

0 comments on commit fcd2fd5

Please sign in to comment.