Skip to content

Commit

Permalink
Add DataPipe.available()
Browse files Browse the repository at this point in the history
This is needed to see how much data is currently buffered
and thus might need to be included in some calculations
  • Loading branch information
centic9 committed Sep 10, 2024
1 parent 1ddc9b3 commit 2d807be
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/main/java/org/dstadler/audio/util/DataPipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ public void waitAllConsumed() throws IOException, InterruptedException {
}
}

/**
* Returns the number of bytes that can be read from this input
* stream without blocking.
*
* @return the number of bytes that can be read from this input stream
* without blocking, or {@code 0} if this input stream has been
* closed by invoking its {@link #close()} method.
*
* @throws IOException if an I/O error occurs.
*/
public int available() throws IOException {
if (pipedIn == null) {
return 0;
}

return pipedIn.available();
}

/**
* Close both sides of the pipe.
*
Expand Down
16 changes: 13 additions & 3 deletions src/test/java/org/dstadler/audio/util/DataPipeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ public void testDataPipeNotStarted() throws IOException, InterruptedException {
assertFalse(pipe.clearBuffer());
pipe.waitAllConsumed();
TestHelpers.ToStringTest(pipe);
assertEquals(0, pipe.available());

pipe.close();
TestHelpers.ToStringTest(pipe);
assertEquals(0, pipe.available());
}

@Test
Expand All @@ -53,21 +55,26 @@ public void testDataPipeCreated() throws IOException, InterruptedException {
assertNotNull(pipe.getInput());
assertTrue(pipe.clearBuffer());
pipe.waitAllConsumed();
assertEquals(0, pipe.available());

pipe.write(new byte[] {0});
assertEquals(1, pipe.available());

pipe.close();
assertEquals(0, pipe.available());

assertFalse(pipe.isRunning());
assertNull(pipe.getInput());
assertThrows(NullPointerException.class, () -> pipe.write(new byte[0]));
assertFalse(pipe.clearBuffer());
pipe.waitAllConsumed();
assertEquals(0, pipe.available());

TestHelpers.ToStringTest(pipe);
}

private static final int NUMBER_OF_THREADS = 10;
private static final int NUMBER_OF_TESTS = 2000;
private static final int NUMBER_OF_THREADS = 20;
private static final int NUMBER_OF_TESTS = 3000;

@Test
public void testMultipleThreads() throws Throwable {
Expand All @@ -79,7 +86,7 @@ public void testMultipleThreads() throws Throwable {
new ThreadTestHelper(NUMBER_OF_THREADS, NUMBER_OF_TESTS);

helper.executeTest((threadNum, itNum) -> {
int rnd = RNG.nextInt(0, 6);
int rnd = RNG.nextInt(0, 7);

switch (rnd) {
case 0:
Expand All @@ -102,6 +109,9 @@ public void testMultipleThreads() throws Throwable {
TestHelpers.ToStringTest(pipe);
}
break;
case 6:
pipe.available();
break;
// close is not called to always be "running"
// however createPipe will re-create the pipe frequently
default:
Expand Down

0 comments on commit 2d807be

Please sign in to comment.