Repository: cassandra
Updated Branches:
  refs/heads/cassandra-3.X 316e1cd7b -> e930ffa8a
  refs/heads/trunk 253801323 -> 29c5fe69c


CompressedRandomAccessReaderTest.testDataCorruptionDetection fails sporadically

Patch by Blake Eggleston; reviewed by Caleb Rackliffe for CASSANDRA-12552


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

Branch: refs/heads/cassandra-3.X
Commit: e9ea5e0a28b998e0c2f318309b66f0aea9561c38
Parents: 695065e
Author: mck <m...@apache.org>
Authored: Sat Oct 8 22:51:32 2016 +1100
Committer: mck <m...@apache.org>
Committed: Sat Oct 8 22:51:32 2016 +1100

----------------------------------------------------------------------
 .../CompressedRandomAccessReaderTest.java       | 82 +++++++++-----------
 1 file changed, 38 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/e9ea5e0a/test/unit/org/apache/cassandra/io/compress/CompressedRandomAccessReaderTest.java
----------------------------------------------------------------------
diff --git 
a/test/unit/org/apache/cassandra/io/compress/CompressedRandomAccessReaderTest.java
 
b/test/unit/org/apache/cassandra/io/compress/CompressedRandomAccessReaderTest.java
index 802d9c8..0c96327 100644
--- 
a/test/unit/org/apache/cassandra/io/compress/CompressedRandomAccessReaderTest.java
+++ 
b/test/unit/org/apache/cassandra/io/compress/CompressedRandomAccessReaderTest.java
@@ -21,6 +21,7 @@ package org.apache.cassandra.io.compress;
 import java.io.File;
 import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.util.Arrays;
 import java.util.Random;
 
 import org.junit.Test;
@@ -176,6 +177,9 @@ public class CompressedRandomAccessReaderTest
         }
     }
 
+    /**
+     * If the data read out doesn't match the checksum, an exception should be 
thrown
+     */
     @Test
     public void testDataCorruptionDetection() throws IOException
     {
@@ -206,59 +210,49 @@ public class CompressedRandomAccessReaderTest
             try(RandomAccessReader reader = new 
CompressedRandomAccessReader.Builder(channel, meta).build())
             {// read and verify compressed data
                 assertEquals(CONTENT, reader.readLine());
+            }
+
+            Random random = new Random();
+            try(RandomAccessFile checksumModifier = new RandomAccessFile(file, 
"rw"))
+            {
+                byte[] checksum = new byte[4];
 
-                Random random = new Random();
-                RandomAccessFile checksumModifier = null;
+                // seek to the end of the compressed chunk
+                checksumModifier.seek(chunk.length);
+                // read checksum bytes
+                checksumModifier.read(checksum);
 
-                try
+                byte[] corruptChecksum = new byte[4];
+                do
                 {
-                    checksumModifier = new RandomAccessFile(file, "rw");
-                    byte[] checksum = new byte[4];
-
-                    // seek to the end of the compressed chunk
-                    checksumModifier.seek(chunk.length);
-                    // read checksum bytes
-                    checksumModifier.read(checksum);
-                    // seek back to the chunk end
-                    checksumModifier.seek(chunk.length);
-
-                    // lets modify one byte of the checksum on each iteration
-                    for (int i = 0; i < checksum.length; i++)
-                    {
-                        checksumModifier.write(random.nextInt());
-                        SyncUtil.sync(checksumModifier); // making sure that 
change was synced with disk
-
-                        try (final RandomAccessReader r = new 
CompressedRandomAccessReader.Builder(channel, meta).build())
-                        {
-                            Throwable exception = null;
-                            try
-                            {
-                                r.readLine();
-                            }
-                            catch (Throwable t)
-                            {
-                                exception = t;
-                            }
-                            assertNotNull(exception);
-                            assertSame(exception.getClass(), 
CorruptSSTableException.class);
-                            assertSame(exception.getCause().getClass(), 
CorruptBlockException.class);
-                        }
-                    }
+                    random.nextBytes(corruptChecksum);
+                } while (Arrays.equals(corruptChecksum, checksum));
 
-                    // lets write original checksum and check if we can read 
data
-                    updateChecksum(checksumModifier, chunk.length, checksum);
+                updateChecksum(checksumModifier, chunk.length, 
corruptChecksum);
 
-                    try (RandomAccessReader cr = new 
CompressedRandomAccessReader.Builder(channel, meta).build())
+                try (final RandomAccessReader r = new 
CompressedRandomAccessReader.Builder(channel, meta).build())
+                {
+                    Throwable exception = null;
+                    try
+                    {
+                        r.readLine();
+                    }
+                    catch (Throwable t)
                     {
-                        // read and verify compressed data
-                        assertEquals(CONTENT, cr.readLine());
-                        // close reader
+                        exception = t;
                     }
+                    assertNotNull(exception);
+                    assertSame(exception.getClass(), 
CorruptSSTableException.class);
+                    assertSame(exception.getCause().getClass(), 
CorruptBlockException.class);
                 }
-                finally
+
+                // lets write original checksum and check if we can read data
+                updateChecksum(checksumModifier, chunk.length, checksum);
+
+                // read and verify compressed data
+                try (RandomAccessReader cr = new 
CompressedRandomAccessReader.Builder(channel, meta).build())
                 {
-                    if (checksumModifier != null)
-                        checksumModifier.close();
+                    assertEquals(CONTENT, cr.readLine());
                 }
             }
         }

Reply via email to