From ded63631fd984fea52ebdf411415c1fd4044db8a Mon Sep 17 00:00:00 2001 From: "Hiram R. Chirino" Date: Thu, 24 Sep 2009 13:46:30 +0000 Subject: [PATCH] AMQ-2405: Better tests for the kahadb corruption handling. git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@818484 13f79535-47bb-0310-9956-ffa450edef68 --- .../activemq/store/kahadb/KahaDBTest.java | 73 ++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/activemq-core/src/test/java/org/apache/activemq/store/kahadb/KahaDBTest.java b/activemq-core/src/test/java/org/apache/activemq/store/kahadb/KahaDBTest.java index 3cc0233c10..049f01e7b0 100644 --- a/activemq-core/src/test/java/org/apache/activemq/store/kahadb/KahaDBTest.java +++ b/activemq-core/src/test/java/org/apache/activemq/store/kahadb/KahaDBTest.java @@ -24,6 +24,8 @@ import org.apache.activemq.command.ActiveMQQueue; import javax.jms.*; import java.io.File; import java.io.IOException; +import java.io.RandomAccessFile; +import java.io.FileNotFoundException; /** * @author chirino @@ -68,7 +70,7 @@ public class KahaDBTest extends TestCase { broker = createBroker(kaha); fail("expected IOException"); } catch (IOException e) { - assertTrue( e.getMessage().startsWith("Detected missing journal files") ); + assertTrue( e.getMessage().startsWith("Detected missing/corrupt journal files") ); } } @@ -99,6 +101,75 @@ public class KahaDBTest extends TestCase { broker.stop(); } + + public void testCheckCorruptionNotIgnored() throws Exception { + KahaDBStore kaha = createStore(true); + assertFalse(kaha.isChecksumJournalFiles()); + assertFalse(kaha.isCheckForCorruptJournalFiles()); + + kaha.setJournalMaxFileLength(1024*100); + kaha.setChecksumJournalFiles(true); + BrokerService broker = createBroker(kaha); + sendMessages(1000); + broker.stop(); + + // Modify/Corrupt some journal files.. + assertExistsAndCorrupt(new File(kaha.getDirectory(), "db-4.log")); + assertExistsAndCorrupt(new File(kaha.getDirectory(), "db-8.log")); + + kaha = createStore(false); + kaha.setJournalMaxFileLength(1024*100); + kaha.setChecksumJournalFiles(true); + kaha.setCheckForCorruptJournalFiles(true); + assertFalse(kaha.isIgnoreMissingJournalfiles()); + try { + broker = createBroker(kaha); + fail("expected IOException"); + } catch (IOException e) { + assertTrue( e.getMessage().startsWith("Detected missing/corrupt journal files") ); + } + + } + + private void assertExistsAndCorrupt(File file) throws IOException { + assertTrue(file.exists()); + RandomAccessFile f = new RandomAccessFile(file, "rw"); + try { + f.seek(1024*5+134); + f.write("... corruption string ...".getBytes()); + } finally { + f.close(); + } + } + + + public void testCheckCorruptionIgnored() throws Exception { + KahaDBStore kaha = createStore(true); + kaha.setJournalMaxFileLength(1024*100); + kaha.setChecksumJournalFiles(true); + BrokerService broker = createBroker(kaha); + sendMessages(1000); + broker.stop(); + + // Delete some journal files.. + assertExistsAndCorrupt(new File(kaha.getDirectory(), "db-4.log")); + assertExistsAndCorrupt(new File(kaha.getDirectory(), "db-8.log")); + + kaha = createStore(false); + kaha.setIgnoreMissingJournalfiles(true); + kaha.setJournalMaxFileLength(1024*100); + kaha.setChecksumJournalFiles(true); + kaha.setCheckForCorruptJournalFiles(true); + broker = createBroker(kaha); + + // We know we won't get all the messages but we should get most of them. + int count = receiveMessages(); + assertTrue("Expected to received a min # of messages.. Got: "+count, count > 990 ); + assertTrue( count < 1000 ); + + broker.stop(); + } + private void assertExistsAndDelete(File file) { assertTrue(file.exists()); file.delete();