You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by db...@apache.org on 2016/08/24 03:48:01 UTC

cassandra git commit: close files/resources in tests

Repository: cassandra
Updated Branches:
  refs/heads/trunk 5a249362e -> 9484783a8


close files/resources in tests


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/9484783a
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/9484783a
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/9484783a

Branch: refs/heads/trunk
Commit: 9484783a868279439e8d5a658b27a7c63a0482b7
Parents: 5a24936
Author: Dave Brosius <db...@mebigfatguy.com>
Authored: Tue Aug 23 23:47:24 2016 -0400
Committer: Dave Brosius <db...@mebigfatguy.com>
Committed: Tue Aug 23 23:47:24 2016 -0400

----------------------------------------------------------------------
 .../org/apache/cassandra/db/VerifyTest.java     |  25 +++--
 .../io/util/BufferedRandomAccessFileTest.java   |  89 ++++++++--------
 .../io/util/FileSegmentInputStreamTest.java     |  40 +++++---
 .../apache/cassandra/io/util/FileUtilsTest.java |   3 +-
 .../service/ProtocolBetaVersionTest.java        |   3 +-
 .../apache/cassandra/utils/KeyGenerator.java    | 101 +++++++++++++------
 6 files changed, 156 insertions(+), 105 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/9484783a/test/unit/org/apache/cassandra/db/VerifyTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/db/VerifyTest.java b/test/unit/org/apache/cassandra/db/VerifyTest.java
index 53bf655..4e55a60 100644
--- a/test/unit/org/apache/cassandra/db/VerifyTest.java
+++ b/test/unit/org/apache/cassandra/db/VerifyTest.java
@@ -275,11 +275,12 @@ public class VerifyTest
         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))
         {
@@ -373,13 +374,15 @@ public class VerifyTest
 
     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)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/9484783a/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java b/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java
index 44be53d..2386160 100644
--- a/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java
+++ b/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java
@@ -144,25 +144,26 @@ public class BufferedRandomAccessFileTest
     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();
+            }
         }
     }
 
@@ -170,32 +171,34 @@ public class BufferedRandomAccessFileTest
     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());
+            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/cassandra/blob/9484783a/test/unit/org/apache/cassandra/io/util/FileSegmentInputStreamTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/io/util/FileSegmentInputStreamTest.java b/test/unit/org/apache/cassandra/io/util/FileSegmentInputStreamTest.java
index fcee9b7..b040d27 100644
--- a/test/unit/org/apache/cassandra/io/util/FileSegmentInputStreamTest.java
+++ b/test/unit/org/apache/cassandra/io/util/FileSegmentInputStreamTest.java
@@ -88,44 +88,56 @@ public class FileSegmentInputStreamTest
     @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);
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/9484783a/test/unit/org/apache/cassandra/io/util/FileUtilsTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/io/util/FileUtilsTest.java b/test/unit/org/apache/cassandra/io/util/FileUtilsTest.java
index 09612c8..0e7d8c8 100644
--- a/test/unit/org/apache/cassandra/io/util/FileUtilsTest.java
+++ b/test/unit/org/apache/cassandra/io/util/FileUtilsTest.java
@@ -97,9 +97,8 @@ public class FileUtilsTest
 
     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)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/9484783a/test/unit/org/apache/cassandra/service/ProtocolBetaVersionTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/service/ProtocolBetaVersionTest.java b/test/unit/org/apache/cassandra/service/ProtocolBetaVersionTest.java
index 7eae7bb..238c2f9 100644
--- a/test/unit/org/apache/cassandra/service/ProtocolBetaVersionTest.java
+++ b/test/unit/org/apache/cassandra/service/ProtocolBetaVersionTest.java
@@ -69,9 +69,8 @@ public class ProtocolBetaVersionTest extends CQLTester
     @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");
         }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/9484783a/test/unit/org/apache/cassandra/utils/KeyGenerator.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/utils/KeyGenerator.java b/test/unit/org/apache/cassandra/utils/KeyGenerator.java
index 8a9d8b8..df95867 100644
--- a/test/unit/org/apache/cassandra/utils/KeyGenerator.java
+++ b/test/unit/org/apache/cassandra/utils/KeyGenerator.java
@@ -24,7 +24,8 @@ import java.util.Random;
 
 public class KeyGenerator
 {
-    private static ByteBuffer randomKey(Random r) {
+    private static ByteBuffer randomKey(Random r) 
+    {
         byte[] bytes = new byte[48];
         r.nextBytes(bytes);
         return ByteBuffer.wrap(bytes);
@@ -35,31 +36,37 @@ public class KeyGenerator
         int i, n, seed;
         Random random;
 
-        RandomStringGenerator(int seed, int n) {
+        RandomStringGenerator(int seed, int n) 
+        {
             i = 0;
             this.seed = seed;
             this.n = n;
             reset();
         }
 
-        public int size() {
+        public int size() 
+        {
             return n;
         }
 
-        public void reset() {
+        public void reset() 
+        {
             random = new Random(seed);
         }
 
-        public boolean hasNext() {
+        public boolean hasNext() 
+        {
             return i < n;
         }
 
-        public ByteBuffer next() {
+        public ByteBuffer next() 
+        {
             i++;
             return randomKey(random);
         }
 
-        public void remove() {
+        public void remove() 
+        {
             throw new UnsupportedOperationException();
         }
     }
@@ -68,33 +75,40 @@ public class KeyGenerator
     {
         private int i, start, n;
 
-        IntGenerator(int n) {
+        IntGenerator(int n) 
+        {
             this(0, n);
         }
 
-        IntGenerator(int start, int n) {
+        IntGenerator(int start, int n) 
+        {
             this.start = start;
             this.n = n;
             reset();
         }
 
-        public int size() {
+        public int size() 
+        {
             return n - start;
         }
 
-        public void reset() {
+        public void reset() 
+        {
             i = start;
         }
 
-        public boolean hasNext() {
+        public boolean hasNext() 
+        {
             return i < n;
         }
 
-        public ByteBuffer next() {
+        public ByteBuffer next() 
+        {
             return ByteBufferUtil.bytes(Integer.toString(i++));
         }
 
-        public void remove() {
+        public void remove() 
+        {
             throw new UnsupportedOperationException();
         }
     }
@@ -103,14 +117,18 @@ public class KeyGenerator
     {
         static int WORDS;
 
-        static {
-            try {
-                BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("/usr/share/dict/words")));
-                while (br.ready()) {
+        static 
+        {
+            try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("/usr/share/dict/words")))) 
+            {
+                while (br.ready()) 
+                {
                     br.readLine();
                     WORDS++;
                 }
-            } catch (IOException e) {
+            } 
+            catch (IOException e) 
+            {
                 WORDS = 0;
             }
         }
@@ -120,50 +138,67 @@ public class KeyGenerator
         private int skip;
         byte[] next;
 
-        WordGenerator(int skip, int modulo) {
+        WordGenerator(int skip, int modulo) 
+        {
             this.skip = skip;
             this.modulo = modulo;
             reset();
         }
 
-        public int size() {
+        public int size() 
+        {
             return (1 + WORDS - skip) / modulo;
         }
 
-        public void reset() {
-            try {
+        public void reset() 
+        {
+            try 
+            {
                 reader = new BufferedReader(new InputStreamReader(new FileInputStream("/usr/share/dict/words")));
-            } catch (FileNotFoundException e) {
+            } 
+            catch (FileNotFoundException e) 
+            {
                 throw new RuntimeException(e);
             }
-            for (int i = 0; i < skip; i++) {
-                try {
+            for (int i = 0; i < skip; i++) 
+            {
+                try 
+                {
                     reader.readLine();
-                } catch (IOException e) {
+                } 
+                catch (IOException e) 
+                {
                     throw new RuntimeException(e);
                 }
             }
             next();
         }
 
-        public boolean hasNext() {
+        public boolean hasNext() 
+        {
             return next != null;
         }
 
-        public ByteBuffer next() {
-            try {
+        public ByteBuffer next() 
+        {
+            try 
+            {
                 byte[] s = next;
-                for (int i = 0; i < modulo; i++) {
+                for (int i = 0; i < modulo; i++) 
+                {
                     String line = reader.readLine();
                     next = line == null ? null : line.getBytes();
                 }
                 return s == null ? null : ByteBuffer.wrap(s);
-            } catch (IOException e) {
+            } 
+            catch (IOException e) 
+            {
                 throw new RuntimeException(e);
             }
         }
 
-        public void remove() {
+        public void remove() 
+        {
             throw new UnsupportedOperationException();
         }
     }