Skip to content

Commit

Permalink
Add failing regression test for #100.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshRosen committed May 14, 2015
1 parent 70ff167 commit 1285253
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/xerial/snappy/SnappyOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class SnappyOutputStream extends OutputStream {
protected final byte[] outputBuffer;
private int inputCursor = 0;
private int outputCursor = 0;
private boolean closed;

public SnappyOutputStream(OutputStream out) {
this(out, DEFAULT_BLOCK_SIZE);
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/org/xerial/snappy/SnappyOutputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.io.ByteArrayOutputStream;

import org.junit.Test;
import org.xerial.snappy.buffer.BufferAllocatorFactory;
import org.xerial.snappy.buffer.CachedBufferAllocator;
import org.xerial.util.FileResource;
import org.xerial.util.log.Logger;

Expand Down Expand Up @@ -157,6 +159,38 @@ public void batchingOfWritesShouldNotAffectCompressedDataSize() throws Exception
}
}

@Test
public void closeShouldBeIdempotent() throws Exception {
// Regression test for issue #107, a bug where close() was non-idempotent and would release
// its buffers to the allocator multiple times, which could cause scenarios where two open
// SnappyOutputStreams could share the same buffers, leading to stream corruption issues.
final BufferAllocatorFactory bufferAllocatorFactory = CachedBufferAllocator.factory;
final int BLOCK_SIZE = 4096;
// Create a stream, use it, then close it once:
ByteArrayOutputStream ba1 = new ByteArrayOutputStream();
SnappyOutputStream os1 = new SnappyOutputStream(ba1, BLOCK_SIZE, bufferAllocatorFactory);
os1.write(42);
os1.close();
// Create a new output stream, which should end up re-using the first stream's freed buffers
ByteArrayOutputStream ba2 = new ByteArrayOutputStream();
SnappyOutputStream os2 = new SnappyOutputStream(ba2, BLOCK_SIZE, bufferAllocatorFactory);
// Close the first stream a second time, which is supposed to be safe due to idempotency:
os1.close();
// Allocate a third output stream, which is supposed to get its own fresh set of buffers:
ByteArrayOutputStream ba3 = new ByteArrayOutputStream();
SnappyOutputStream os3 = new SnappyOutputStream(ba3, BLOCK_SIZE, bufferAllocatorFactory);
// Since the second and third streams should have distinct sets of buffers, writes to these
// streams should not interfere with one another:
os2.write(2);
os3.write(3);
os2.close();
os3.close();
SnappyInputStream in2 = new SnappyInputStream(new ByteArrayInputStream(ba2.toByteArray()));
assertEquals(2, in2.read());
SnappyInputStream in3 = new SnappyInputStream(new ByteArrayInputStream(ba3.toByteArray()));
assertEquals(3, in3.read());
}

@Test
public void longArrayCompress() throws Exception {
long[] l = new long[10];
Expand Down

0 comments on commit 1285253

Please sign in to comment.