Skip to content

Commit

Permalink
fix 183: implement Flushable in WritableStreamingData (#184)
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Petrov <anthony@swirldslabs.com>
  • Loading branch information
anthony-swirldslabs authored Jan 25, 2024
1 parent d7d0add commit 5d0fcc0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pbj-core/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Version number
version=0.7.14-SNAPSHOT
version=0.7.15-SNAPSHOT

# Need increased heap for running Gradle itself, or SonarQube will run the JVM out of metaspace
org.gradle.jvmargs=-Xmx2048m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.hedera.pbj.runtime.io.WritableSequentialData;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.BufferOverflowException;
Expand All @@ -14,7 +15,7 @@
* <p>A {@code WritableSequentialData} backed by an output stream. If the instance is closed,
* the underlying {@link OutputStream} is closed too.
*/
public class WritableStreamingData implements WritableSequentialData, Closeable {
public class WritableStreamingData implements WritableSequentialData, Closeable, Flushable {

/** The underlying output stream */
private final OutputStream out;
Expand Down Expand Up @@ -219,4 +220,15 @@ public void writeBytes(@NonNull final ByteBuffer src) {
throw new DataAccessException(e);
}
}

// ================================================================================================================
// Flushable Methods

/**
* {@inheritDoc}
*/
@Override
public void flush() throws IOException {
out.flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

public class WritableStreamingDataTest extends WritableTestBase {

Expand Down Expand Up @@ -100,4 +104,18 @@ void closed() throws IOException {
// When we try to write some bytes, then we get an exception because the stream throws IOException
assertThatThrownBy(() -> seq.writeBytes(src, 5)).isInstanceOf(DataAccessException.class);
}

@Test
@DisplayName("Calling flush() is delegated to the OutputStream")
void testFlushable() throws IOException {
final OutputStream out = mock(OutputStream.class);
doNothing().when(out).flush();

final WritableStreamingData seq = new WritableStreamingData(out);

seq.flush();

verify(out, times(1)).flush();
verifyNoMoreInteractions(out);
}
}

0 comments on commit 5d0fcc0

Please sign in to comment.