Skip to content

Commit

Permalink
close files/resources in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mebigfatguy committed Aug 24, 2016
1 parent 5a24936 commit 9484783
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 105 deletions.
25 changes: 14 additions & 11 deletions test/unit/org/apache/cassandra/db/VerifyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,12 @@ public void testVerifyIncorrectDigest() throws IOException, WriteTimeoutExceptio
SSTableReader sstable = cfs.getLiveSSTables().iterator().next();


RandomAccessFile file = new RandomAccessFile(sstable.descriptor.filenameFor(sstable.descriptor.digestComponent), "rw");
Long correctChecksum = Long.parseLong(file.readLine());
file.close();

writeChecksum(++correctChecksum, sstable.descriptor.filenameFor(sstable.descriptor.digestComponent));
try (RandomAccessFile file = new RandomAccessFile(sstable.descriptor.filenameFor(sstable.descriptor.digestComponent), "rw"))
{
Long correctChecksum = Long.valueOf(file.readLine());

writeChecksum(++correctChecksum, sstable.descriptor.filenameFor(sstable.descriptor.digestComponent));
}

try (Verifier verifier = new Verifier(cfs, sstable, false))
{
Expand Down Expand Up @@ -373,13 +374,15 @@ protected void fillCounterCF(ColumnFamilyStore cfs, int partitionsPerSSTable) th

protected long simpleFullChecksum(String filename) throws IOException
{
FileInputStream inputStream = new FileInputStream(filename);
CRC32 checksum = new CRC32();
CheckedInputStream cinStream = new CheckedInputStream(inputStream, checksum);
byte[] b = new byte[128];
while (cinStream.read(b) >= 0) {
try (FileInputStream inputStream = new FileInputStream(filename))
{
CRC32 checksum = new CRC32();
CheckedInputStream cinStream = new CheckedInputStream(inputStream, checksum);
byte[] b = new byte[128];
while (cinStream.read(b) >= 0) {
}
return cinStream.getChecksum().getValue();
}
return cinStream.getChecksum().getValue();
}

protected void writeChecksum(long checksum, String filePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,58 +144,61 @@ public void testReadAndWrite() throws Exception
public void testReadAndWriteOnCapacity() throws IOException
{
File tmpFile = File.createTempFile("readtest", "bin");
SequentialWriter w = new SequentialWriter(tmpFile);

// Fully write the file and sync..
byte[] in = generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE);
w.write(in);

try (FileHandle.Builder builder = new FileHandle.Builder(w.getPath());
FileHandle fh = builder.complete();
RandomAccessReader r = fh.createReader())
try (SequentialWriter w = new SequentialWriter(tmpFile))
{
// Read it into a same size array.
byte[] out = new byte[RandomAccessReader.DEFAULT_BUFFER_SIZE];
r.read(out);

// Cannot read any more.
int negone = r.read();
assert negone == -1 : "We read past the end of the file, should have gotten EOF -1. Instead, " + negone;

w.finish();
// Fully write the file and sync..
byte[] in = generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE);
w.write(in);

try (FileHandle.Builder builder = new FileHandle.Builder(w.getPath());
FileHandle fh = builder.complete();
RandomAccessReader r = fh.createReader())
{
// Read it into a same size array.
byte[] out = new byte[RandomAccessReader.DEFAULT_BUFFER_SIZE];
r.read(out);

// Cannot read any more.
int negone = r.read();
assert negone == -1 : "We read past the end of the file, should have gotten EOF -1. Instead, " + negone;

w.finish();
}
}
}

@Test
public void testLength() throws IOException
{
File tmpFile = File.createTempFile("lengthtest", "bin");
SequentialWriter w = new SequentialWriter(tmpFile);
assertEquals(0, w.length());

// write a chunk smaller then our buffer, so will not be flushed
// to disk
byte[] lessThenBuffer = generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE / 2);
w.write(lessThenBuffer);
assertEquals(lessThenBuffer.length, w.length());

// sync the data and check length
w.sync();
assertEquals(lessThenBuffer.length, w.length());

// write more then the buffer can hold and check length
byte[] biggerThenBuffer = generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE * 2);
w.write(biggerThenBuffer);
assertEquals(biggerThenBuffer.length + lessThenBuffer.length, w.length());

w.finish();

// will use cachedlength
try (FileHandle.Builder builder = new FileHandle.Builder(tmpFile.getPath());
FileHandle fh = builder.complete();
RandomAccessReader r = fh.createReader())
try (SequentialWriter w = new SequentialWriter(tmpFile))
{
assertEquals(lessThenBuffer.length + biggerThenBuffer.length, r.length());
assertEquals(0, w.length());

// write a chunk smaller then our buffer, so will not be flushed
// to disk
byte[] lessThenBuffer = generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE / 2);
w.write(lessThenBuffer);
assertEquals(lessThenBuffer.length, w.length());

// sync the data and check length
w.sync();
assertEquals(lessThenBuffer.length, w.length());

// write more then the buffer can hold and check length
byte[] biggerThenBuffer = generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE * 2);
w.write(biggerThenBuffer);
assertEquals(biggerThenBuffer.length + lessThenBuffer.length, w.length());

w.finish();

// will use cachedlength
try (FileHandle.Builder builder = new FileHandle.Builder(tmpFile.getPath());
FileHandle fh = builder.complete();
RandomAccessReader r = fh.createReader())
{
assertEquals(lessThenBuffer.length + biggerThenBuffer.length, r.length());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,44 +88,56 @@ private void testRead(int offset, int size, int checkInterval) throws IOExceptio
@Test(expected = UnsupportedOperationException.class)
public void testMarkNotSupported() throws Exception
{
FileSegmentInputStream reader = new FileSegmentInputStream(allocateBuffer(1024), "", 0);
assertFalse(reader.markSupported());
assertEquals(0, reader.bytesPastMark(null));
reader.mark();
try (FileSegmentInputStream reader = new FileSegmentInputStream(allocateBuffer(1024), "", 0))
{
assertFalse(reader.markSupported());
assertEquals(0, reader.bytesPastMark(null));
reader.mark();
}
}

@Test(expected = UnsupportedOperationException.class)
public void testResetNotSupported() throws Exception
{
FileSegmentInputStream reader = new FileSegmentInputStream(allocateBuffer(1024), "", 0);
reader.reset(null);
try (FileSegmentInputStream reader = new FileSegmentInputStream(allocateBuffer(1024), "", 0))
{
reader.reset(null);
}
}

@Test(expected = IllegalArgumentException.class)
public void testSeekNegative() throws Exception
{
FileSegmentInputStream reader = new FileSegmentInputStream(allocateBuffer(1024), "", 0);
reader.seek(-1);
try (FileSegmentInputStream reader = new FileSegmentInputStream(allocateBuffer(1024), "", 0))
{
reader.seek(-1);
}
}

@Test(expected = IllegalArgumentException.class)
public void testSeekBeforeOffset() throws Exception
{
FileSegmentInputStream reader = new FileSegmentInputStream(allocateBuffer(1024), "", 1024);
reader.seek(1023);
try (FileSegmentInputStream reader = new FileSegmentInputStream(allocateBuffer(1024), "", 1024))
{
reader.seek(1023);
}
}

@Test(expected = IllegalArgumentException.class)
public void testSeekPastLength() throws Exception
{
FileSegmentInputStream reader = new FileSegmentInputStream(allocateBuffer(1024), "", 1024);
reader.seek(2049);
try (FileSegmentInputStream reader = new FileSegmentInputStream(allocateBuffer(1024), "", 1024))
{
reader.seek(2049);
}
}

@Test(expected = EOFException.class)
public void testReadBytesTooMany() throws Exception
{
FileSegmentInputStream reader = new FileSegmentInputStream(allocateBuffer(1024), "", 1024);
ByteBufferUtil.read(reader, 2049);
try (FileSegmentInputStream reader = new FileSegmentInputStream(allocateBuffer(1024), "", 1024))
{
ByteBufferUtil.read(reader, 2049);
}
}
}
3 changes: 1 addition & 2 deletions test/unit/org/apache/cassandra/io/util/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ private File createFolder(Path path)

private File createFile(File file, long size)
{
try
try (RandomAccessFile f = new RandomAccessFile(file, "rw"))
{
RandomAccessFile f = new RandomAccessFile(file, "rw");
f.setLength(size);
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ public void testProtocolBetaVersion() throws Exception
@Test
public void unforcedProtocolVersionTest() throws Exception
{
try
try (SimpleClient client = new SimpleClient(nativeAddr.getHostAddress(), nativePort, Server.BETA_VERSION, false, new EncryptionOptions.ClientEncryptionOptions()))
{
SimpleClient client = new SimpleClient(nativeAddr.getHostAddress(), nativePort, Server.BETA_VERSION, false, new EncryptionOptions.ClientEncryptionOptions());
client.connect(false);
fail("Exception should have been thrown");
}
Expand Down
Loading

0 comments on commit 9484783

Please sign in to comment.