Skip to content

Commit

Permalink
Add some comments and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
centic9 committed Oct 5, 2024
1 parent 714ac8d commit 324a373
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public class BlockingSeekableRingBuffer implements SeekableRingBuffer<Chunk>, Pe
*/
private int nextAdd = 0;

/**
* indicates how many elements in the buffer
* are populated.
*
* This will increase when items are added until
* it has reached "numberOfChunks - 1" where
* it stays
*/
private int fill = 0;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,27 @@ public void addAndGet2() {
public void buffered() {
assertEquals(0, buffer.bufferedForward());
assertEquals(0, buffer.bufferedBackward());
assertEquals(0, buffer.fill());

buffer.add(CHUNK_1);
assertEquals(1, buffer.bufferedForward());
assertEquals(0, buffer.bufferedBackward());
assertEquals(1, buffer.fill());

buffer.add(CHUNK_2);
assertEquals(2, buffer.bufferedForward());
assertEquals(0, buffer.bufferedBackward());
assertEquals(2, buffer.fill());

assertArrayEquals(CHUNK_1.getData(), buffer.next().getData());
assertEquals(1, buffer.bufferedForward());
assertEquals(1, buffer.bufferedBackward());
assertEquals(2, buffer.fill());

assertArrayEquals(CHUNK_2.getData(), buffer.next().getData());
assertEquals(0, buffer.bufferedForward());
assertEquals(2, buffer.bufferedBackward());
assertEquals(2, buffer.fill());

buffer.add(CHUNK_1);
buffer.add(CHUNK_1);
Expand All @@ -92,26 +97,32 @@ public void buffered() {
buffer.add(CHUNK_1);
assertEquals(7, buffer.bufferedForward());
assertEquals(2, buffer.bufferedBackward());
assertEquals(9, buffer.fill());

buffer.add(CHUNK_1);
assertEquals(8, buffer.bufferedForward());
assertEquals(1, buffer.bufferedBackward());
assertEquals(9, buffer.fill());

buffer.add(CHUNK_1);
assertEquals(9, buffer.bufferedForward());
assertEquals(0, buffer.bufferedBackward());
assertEquals(9, buffer.fill());

buffer.add(CHUNK_1);
assertEquals(9, buffer.bufferedForward());
assertEquals(0, buffer.bufferedBackward());
assertEquals(9, buffer.fill());

assertNotNull(buffer.next());
assertEquals(8, buffer.bufferedForward());
assertEquals(1, buffer.bufferedBackward());
assertEquals(9, buffer.fill());

assertNotNull(buffer.next());
assertEquals(7, buffer.bufferedForward());
assertEquals(2, buffer.bufferedBackward());
assertEquals(9, buffer.fill());

assertNotNull(buffer.next());
assertNotNull(buffer.next());
Expand All @@ -121,10 +132,12 @@ public void buffered() {
assertNotNull(buffer.next());
assertEquals(1, buffer.bufferedForward());
assertEquals(8, buffer.bufferedBackward());
assertEquals(9, buffer.fill());

assertNotNull(buffer.next());
assertEquals(0, buffer.bufferedForward());
assertEquals(9, buffer.bufferedBackward());
assertEquals(9, buffer.fill());
}

@Test
Expand Down Expand Up @@ -250,10 +263,10 @@ public void run(int threadNum, int iter) {
buffer.reset();
break;
case 4:
buffer.seek(RandomUtils.nextInt(0, 20));
buffer.seek(RandomUtils.insecure().randomInt(0, 20));
break;
case 5:
buffer.seek(RandomUtils.nextInt(0, 20)* (-1));
buffer.seek(RandomUtils.insecure().randomInt(0, 20) * (-1));
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.dstadler.commons.testing.TestHelpers;
import org.junit.Test;

import java.io.File;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand All @@ -29,6 +31,10 @@ public void testLive() {
assertTrue(dto.isPlaying());
assertTrue(dto.isDownloadWhilePaused());

assertEquals(0, dto.getNumberOfDiskChunks());
assertEquals(0, dto.getNumberOfDiskFiles());
assertNull(dto.getDataDir());

TestHelpers.ToStringTest(dto);
}

Expand All @@ -52,6 +58,25 @@ public void testDownload() {
TestHelpers.ToStringTest(dto);
}

@Test
public void testToString() {
Stream stream = new Stream();
BufferPersistenceDTO dto = new BufferPersistenceDTO(3, stream, false, false);
TestHelpers.ToStringTest(dto);

dto = new BufferPersistenceDTO(0, 0, null,
0, 0, 0, stream, false, false);
TestHelpers.ToStringTest(dto);

dto = new BufferPersistenceDTO(1, 1, new File("."),
1, 1, 1, stream, false, false);
TestHelpers.ToStringTest(dto);

assertEquals(1, dto.getNumberOfDiskChunks());
assertEquals(1, dto.getNumberOfDiskFiles());
assertEquals(new File("."), dto.getDataDir());
}

// helper method to get coverage of the unused constructor
@Test
public void testPrivateConstructor() throws Exception {
Expand Down
76 changes: 41 additions & 35 deletions src/test/java/org/dstadler/audio/buffer/BufferPersistenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,56 +117,56 @@ public void testMicroBenchmark() throws IOException {
File file = File.createTempFile("BufferPersistence", ".bson");
assertTrue(file.delete());
try {
BlockingSeekableRingBuffer buffer = new BlockingSeekableRingBuffer(5000);

log.info("Creating data structure");
for(int i = 0;i < 5000;i++) {
byte[] data = new byte[CHUNK_SIZE];
for(int d = 0;d < CHUNK_SIZE;d++) {
data[d] = (byte)d;
try (BlockingSeekableRingBuffer buffer = new BlockingSeekableRingBuffer(5000)) {

log.info("Creating data structure");
for (int i = 0; i < 5000; i++) {
byte[] data = new byte[CHUNK_SIZE];
for (int d = 0; d < CHUNK_SIZE; d++) {
data[d] = (byte) d;
}
buffer.add(new Chunk(data, "meta data", System.currentTimeMillis()));
}
buffer.add(new Chunk(data, "meta data", System.currentTimeMillis()));
}
assertEquals(4999, buffer.fill());
assertEquals(4999, buffer.fill());

for(int i = 0;i < 5;i++) {
log.info("Start run " + i);
for (int i = 0; i < 5; i++) {
log.info("Start run " + i);

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

long start = System.currentTimeMillis();
BufferPersistence.writeBufferToDisk(file, buffer.toPersistence(stream, false, false));
log.info("Writing for run " + i + " took " + (System.currentTimeMillis() - start) + "ms");
long start = System.currentTimeMillis();
BufferPersistence.writeBufferToDisk(file, buffer.toPersistence(stream, false, false));
log.info("Writing for run " + i + " took " + (System.currentTimeMillis() - start) + "ms");

assertTrue(BufferPersistence.hasBufferOnDisk(file));
assertTrue(BufferPersistence.hasBufferOnDisk(file));

start = System.currentTimeMillis();
final BufferPersistenceDTO dto = BufferPersistence.readBufferFromDisk(file);
assertEquals("url1", dto.getStream().getUrl());
assertEquals(Stream.StreamType.live, dto.getStream().getStreamType());
assertFalse(dto.isPlaying());
assertFalse(dto.isDownloadWhilePaused());
start = System.currentTimeMillis();
final BufferPersistenceDTO dto = BufferPersistence.readBufferFromDisk(file);
assertEquals("url1", dto.getStream().getUrl());
assertEquals(Stream.StreamType.live, dto.getStream().getStreamType());
assertFalse(dto.isPlaying());
assertFalse(dto.isDownloadWhilePaused());

BlockingSeekableRingBuffer back = BlockingSeekableRingBuffer.fromPersistence(dto);
log.info("Reading for run " + i + " took " + (System.currentTimeMillis() - start) + "ms");
try (BlockingSeekableRingBuffer back = BlockingSeekableRingBuffer.fromPersistence(dto)) {
log.info("Reading for run " + i + " took " + (System.currentTimeMillis() - start) + "ms");

assertTrue(BufferPersistence.hasBufferOnDisk(file));
assertEquals(4999, back.fill());
assertEquals(4999, back.size());
}
assertTrue(BufferPersistence.hasBufferOnDisk(file));
assertEquals(4999, back.fill());
assertEquals(4999, back.size());
}
}

assertTrue(file.exists());
assertTrue(file.delete());
assertTrue(file.exists());
assertTrue(file.delete());
}
} finally {
// make sure to clean up even on exception
assertTrue(!file.exists() || file.delete());
}
}



@Test
public void testInvalidStartPosition() throws IOException {
File tempPersist = File.createTempFile("RangeDownloadingBuffer", ".bin");
Expand Down Expand Up @@ -203,4 +203,10 @@ public void testInvalidStartPosition() throws IOException {
assertTrue(!tempPersist.exists() || tempPersist.delete());
}
}

// helper method to get coverage of the unused constructor
@Test
public void testPrivateConstructor() throws Exception {
org.dstadler.commons.testing.PrivateConstructorCoverage.executePrivateConstructor(BufferPersistence.class);
}
}

0 comments on commit 324a373

Please sign in to comment.