Skip to content

Commit

Permalink
[core] AsyncPositionOutputStream should not flush after close. (#3967)
Browse files Browse the repository at this point in the history
  • Loading branch information
leaves12138 authored Aug 15, 2024
1 parent 0af4feb commit 9f3389e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class AsyncPositionOutputStream extends PositionOutputStream {

private int totalBuffers;
private long position;
private boolean closed = false;

public AsyncPositionOutputStream(PositionOutputStream out) {
this.out = out;
Expand Down Expand Up @@ -172,6 +173,9 @@ public void write(byte[] b, int off, int len) throws IOException {

@Override
public void flush() throws IOException {
if (closed) {
throw new IOException("Already closed");
}
checkException();
flushBuffer();
FlushEvent event = new FlushEvent();
Expand All @@ -193,6 +197,9 @@ public void flush() throws IOException {

@Override
public void close() throws IOException {
if (closed) {
return;
}
checkException();
flushBuffer();
sendEndEvent();
Expand All @@ -204,6 +211,7 @@ public void close() throws IOException {
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
this.closed = true;
}

private void sendEndEvent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.concurrent.ThreadLocalRandom;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class AsyncPositionOutputStreamTest {
Expand Down Expand Up @@ -173,6 +174,14 @@ public void write(byte[] b, int off, int len) {
assertThat(out.closed).isTrue();
}

@Test
public void testCloseFlushThrowsException() throws Exception {
AsyncPositionOutputStream asyncPositionOutputStream =
new AsyncPositionOutputStream(new ByteArrayPositionOutputStream());
asyncPositionOutputStream.close();
assertThatCode(asyncPositionOutputStream::flush).hasMessage("Already closed");
}

private static class ByteArrayPositionOutputStream extends PositionOutputStream {

private final ByteArrayOutputStream out;
Expand Down

0 comments on commit 9f3389e

Please sign in to comment.