jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1325300 - in /jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs: AbstractBlobStoreTest.java DbBlobStoreTest.java FileBlobStoreTest.java MemoryBlobStoreTest.java MongoBlobStoreTest.java
Date Thu, 12 Apr 2012 15:01:52 GMT
Author: jukka
Date: Thu Apr 12 15:01:51 2012
New Revision: 1325300

URL: http://svn.apache.org/viewvc?rev=1325300&view=rev
Log:
OAK-58: connection leak in h2 persistence

Move generic test code to Db- to a new AbstractBlobStoreTest class.

Added:
    jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/AbstractBlobStoreTest.java
      - copied, changed from r1325296, jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/DbBlobStoreTest.java
Modified:
    jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/DbBlobStoreTest.java
    jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/FileBlobStoreTest.java
    jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/MemoryBlobStoreTest.java
    jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/MongoBlobStoreTest.java

Copied: jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/AbstractBlobStoreTest.java
(from r1325296, jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/DbBlobStoreTest.java)
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/AbstractBlobStoreTest.java?p2=jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/AbstractBlobStoreTest.java&p1=jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/DbBlobStoreTest.java&r1=1325296&r2=1325300&rev=1325300&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/DbBlobStoreTest.java
(original)
+++ jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/AbstractBlobStoreTest.java
Thu Apr 12 15:01:51 2012
@@ -21,7 +21,6 @@ import org.apache.jackrabbit.mk.api.Micr
 import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.json.JsopTokenizer;
 import org.apache.jackrabbit.mk.util.IOUtilsTest;
-import org.h2.jdbcx.JdbcConnectionPool;
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
@@ -30,7 +29,6 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.sql.Connection;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -38,34 +36,19 @@ import java.util.Random;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 /**
- * Tests the DbBlobStore implementation.
+ * Tests a BlobStore implementation.
  */
-public class DbBlobStoreTest extends TestCase {
+abstract class AbstractBlobStoreTest extends TestCase {
 
     protected AbstractBlobStore store;
-    private Connection sentinel;
-    private JdbcConnectionPool cp;
 
-    public void setUp() throws Exception {
-        Class.forName("org.h2.Driver");
-        cp = JdbcConnectionPool.create("jdbc:h2:mem:", "", "");
-        sentinel = cp.getConnection();
-        DbBlobStore blobStore = new DbBlobStore();
-        blobStore.setConnectionPool(cp);
-        blobStore.setBlockSize(128);
-        blobStore.setBlockSizeMin(32);
-        this.store = blobStore;
-    }
+    /**
+     * Should be overridden by subclasses to set the {@link #store} variable.
+     */
+    public abstract void setUp() throws Exception;
 
     public void tearDown() throws Exception {
-        if (sentinel != null) {
-            sentinel.close();
-        }
         store.close();
-
-        if (cp != null) {
-            cp.dispose();
-        }
     }
 
     public void testAddFile() throws Exception {

Modified: jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/DbBlobStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/DbBlobStoreTest.java?rev=1325300&r1=1325299&r2=1325300&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/DbBlobStoreTest.java
(original)
+++ jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/DbBlobStoreTest.java
Thu Apr 12 15:01:51 2012
@@ -16,33 +16,15 @@
  */
 package org.apache.jackrabbit.mk.blobs;
 
-import junit.framework.TestCase;
-import org.apache.jackrabbit.mk.api.MicroKernelException;
-import org.apache.jackrabbit.mk.json.JsopBuilder;
-import org.apache.jackrabbit.mk.json.JsopTokenizer;
-import org.apache.jackrabbit.mk.util.IOUtilsTest;
-import org.h2.jdbcx.JdbcConnectionPool;
-
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
 import java.sql.Connection;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Random;
-import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.h2.jdbcx.JdbcConnectionPool;
 
 /**
  * Tests the DbBlobStore implementation.
  */
-public class DbBlobStoreTest extends TestCase {
+public class DbBlobStoreTest extends AbstractBlobStoreTest {
 
-    protected AbstractBlobStore store;
     private Connection sentinel;
     private JdbcConnectionPool cp;
 
@@ -58,300 +40,11 @@ public class DbBlobStoreTest extends Tes
     }
 
     public void tearDown() throws Exception {
+        super.tearDown();
         if (sentinel != null) {
             sentinel.close();
         }
-        store.close();
-
-        if (cp != null) {
-            cp.dispose();
-        }
-    }
-
-    public void testAddFile() throws Exception {
-        store.setBlockSize(1024 * 1024);
-        byte[] data = new byte[4 * 1024 * 1024];
-        Random r = new Random(0);
-        r.nextBytes(data);
-        String tempFileName = "target/temp/test";
-        File tempFile = new File(tempFileName);
-        tempFile.getParentFile().mkdirs();
-        OutputStream out = new FileOutputStream(tempFile, false);
-        out.write(data);
-        out.close();
-        String s = store.addBlob(tempFileName);
-        assertEquals(data.length, store.getBlobLength(s));
-        byte[] buff = new byte[1];
-        for (int i = 0; i < data.length; i += 1024) {
-            store.readBlob(s, i, buff, 0, 1);
-            assertEquals(data[i], buff[0]);
-        }
-        try {
-            store.addBlob(tempFileName + "_wrong");
-            fail();
-        } catch (Exception e) {
-            // expected
-        }
-    }
-
-    public void testCombinedIdentifier() throws Exception {
-        String id = store.writeBlob(new ByteArrayInputStream(new byte[2]));
-        assertEquals(2, store.getBlobLength(id));
-        String combinedId = id + id;
-        assertEquals(4, store.getBlobLength(combinedId));
-        doTestRead(new byte[4], 4, combinedId);
-    }
-
-    public void testEmptyIdentifier() throws Exception {
-        byte[] data = new byte[1];
-        assertEquals(-1, store.readBlob("", 0, data, 0, 1));
-        assertEquals(0, store.getBlobLength(""));
-    }
-
-    public void testCloseStream() throws Exception {
-        final AtomicBoolean closed = new AtomicBoolean();
-        InputStream in = new InputStream() {
-            public void close() {
-                closed.set(true);
-            }
-            public int read() throws IOException {
-                return -1;
-            }
-        };
-        store.writeBlob(in);
-        assertTrue(closed.get());
-    }
-
-    public void testExceptionWhileReading() throws Exception {
-        final AtomicBoolean closed = new AtomicBoolean();
-        InputStream in = new InputStream() {
-            public void close() {
-                closed.set(true);
-            }
-            public int read() throws IOException {
-                throw new RuntimeException("abc");
-            }
-        };
-        try {
-            store.writeBlob(in);
-        } catch (Exception e) {
-            String msg = e.getMessage();
-            assertTrue(msg, msg.indexOf("abc") >= 0);
-        }
-        assertTrue(closed.get());
-    }
-
-
-    public void testIllegalIdentifier() throws Exception {
-        byte[] data = new byte[1];
-        try {
-            store.readBlob("ff", 0, data, 0, 1);
-            fail();
-        } catch (MicroKernelException e) {
-            // expected
-        }
-        try {
-            store.getBlobLength("ff");
-            fail();
-        } catch (MicroKernelException e) {
-            // expected
-        }
-        try {
-            store.mark("ff");
-            fail();
-        } catch (Exception e) {
-            // expected
-        }
-    }
-
-    public void testSmall() throws Exception {
-        doTest(10, 300);
-    }
-
-    public void testMedium() throws Exception {
-        doTest(100, 100);
-    }
-
-    public void testLarge() throws Exception {
-        doTest(1000, 10);
-    }
-
-    public void testGarbageCollection() throws Exception {
-        HashMap<String, byte[]> map = new HashMap<String, byte[]>();
-        ArrayList<String> mem = new ArrayList<String>();
-        int count;
-        for (int i = 1; i < 10000; i += (i + 1) * 10) {
-            byte[] data = new byte[i];
-            String id;
-            id = store.writeBlob(new ByteArrayInputStream(data));
-            // copy the id so the string is not in the weak hash map
-            map.put(new String(id), data);
-            mem.add(id);
-            data = new byte[i];
-            Arrays.fill(data, (byte) 1);
-            id = store.writeBlob(new ByteArrayInputStream(data));
-            // copy the id so the string is not in the weak hash map
-            map.put(new String(id), data);
-            mem.add(id);
-        }
-        store.startMark();
-        store.sweep();
-        for (String id : map.keySet()) {
-            byte[] test = readFully(id);
-            assertTrue(Arrays.equals(map.get(id), test));
-        }
-
-        mem.clear();
-
-        store.clearInUse();
-        store.startMark();
-        for (String id : map.keySet()) {
-            byte[] d = map.get(id);
-            if (d[0] != 0) {
-                continue;
-            }
-            store.mark(id);
-        }
-        store.sweep();
-
-        store.clearInUse();
-        store.clearCache();
-
-        // https://issues.apache.org/jira/browse/OAK-60
-        // endure there is at least one old entry (with age 1 ms)
-        try {
-            Thread.sleep(1);
-        } catch (InterruptedException e) {
-            // ignore
-        }
-
-        store.startMark();
-        count = store.sweep();
-        assertTrue("count: " + count, count > 0);
-        int failedCount = 0;
-        for (String id : map.keySet()) {
-            long length = store.getBlobLength(id);
-            try {
-                readFully(id);
-            } catch (Exception e) {
-                assertTrue(id + ":" + length, length > store.getBlockSizeMin());
-                failedCount++;
-            }
-        }
-        assertTrue("failedCount: " + failedCount, failedCount > 0);
-    }
-
-    private void doTest(int maxLength, int count) throws Exception {
-        String[] s = new String[count * 2];
-        Random r = new Random(0);
-        for (int i = 0; i < s.length;) {
-            byte[] data = new byte[r.nextInt(maxLength)];
-            r.nextBytes(data);
-            s[i++] = store.writeBlob(new ByteArrayInputStream(data));
-            s[i++] = store.writeBlob(new ByteArrayInputStream(data));
-        }
-        r.setSeed(0);
-        for (int i = 0; i < s.length;) {
-            int expectedLen = r.nextInt(maxLength);
-            byte[] expectedData = new byte[expectedLen];
-            r.nextBytes(expectedData);
-            assertEquals(expectedLen, store.getBlobLength(s[i++]));
-
-            String id = s[i++];
-            doTestRead(expectedData, expectedLen, id);
-        }
-    }
-
-    private void doTestRead(byte[] expectedData, int expectedLen, String id) throws Exception
{
-        byte[] got = readFully(id);
-        assertEquals(expectedLen, got.length);
-        IOUtilsTest.assertEquals(expectedData, got);
-    }
-
-    public byte[] readFully(String id) throws Exception {
-        int len = (int) store.getBlobLength(id);
-        byte[] data;
-        if (len < 100) {
-            data = new byte[len];
-            for (int i = 0; i < len; i++) {
-                store.readBlob(id, i, data, i, 1);
-            }
-        } else {
-            data = BlobStoreInputStream.readFully(store, id);
-        }
-        assertEquals(len, data.length);
-        return data;
-    }
-
-    public static void main(String... args) throws Exception {
-        FileBlobStore store = new FileBlobStore("target/temp");
-
-//        DbBlobStore store = new DbBlobStore();
-//        store.setConnectionPool(JdbcConnectionPool.create("jdbc:h2:target/test;log=0;undo_log=0",
"", ""));
-
-        String id = addFiles(store, "~/temp/ds");
-        extractFiles(store, id, "target/test");
-
-        store.close();
-    }
-
-    public static void extractFiles(AbstractBlobStore store, String listingId, String target)
throws IOException {
-        String listing = new String(BlobStoreInputStream.readFully(store, listingId), "UTF-8");
-        JsopTokenizer t = new JsopTokenizer(listing);
-        File targetDir = new File(target);
-        targetDir.mkdirs();
-        t.read('{');
-        if (!t.matches('}')) {
-            do {
-                String file = t.readString();
-                t.read(':');
-                String id = t.readString();
-                byte[] data = BlobStoreInputStream.readFully(store, id);
-                File outFile = new File(targetDir, file);
-                outFile.getParentFile().mkdirs();
-                FileOutputStream out = new FileOutputStream(outFile);
-                try {
-                    out.write(data);
-                } finally {
-                    out.close();
-                }
-            } while (t.matches(','));
-        }
-        t.read('}');
-    }
-
-    public static String addFiles(AbstractBlobStore store, String dir) throws Exception {
-        ArrayList<String> list = new ArrayList<String>();
-        String root = new File(dir).getAbsolutePath();
-        String parent = new File(dir).getParentFile().getAbsolutePath();
-        addFiles(list, new File(root));
-        JsopBuilder listing = new JsopBuilder();
-        listing.object();
-        for (String f : list) {
-            FileInputStream in = new FileInputStream(f);
-            String id = store.writeBlob(in);
-            in.close();
-            String name = f.substring(parent.length());
-            listing.key(name).value(id);
-            listing.newline();
-        }
-        listing.endObject();
-        String l = listing.toString();
-        String id = store.writeBlob(new ByteArrayInputStream(l.getBytes("UTF-8")));
-        return id;
-    }
-
-    private static void addFiles(ArrayList<String> list, File file) {
-        if (file.isDirectory()) {
-            for (File f : file.listFiles()) {
-                addFiles(list, f);
-            }
-            return;
-        }
-        if (!file.isFile()) {
-            return;
-        }
-        list.add(file.getAbsolutePath());
+        cp.dispose();
     }
 
 }

Modified: jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/FileBlobStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/FileBlobStoreTest.java?rev=1325300&r1=1325299&r2=1325300&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/FileBlobStoreTest.java
(original)
+++ jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/FileBlobStoreTest.java
Thu Apr 12 15:01:51 2012
@@ -19,7 +19,7 @@ package org.apache.jackrabbit.mk.blobs;
 /**
  * Tests the FileBlobStore implementation.
  */
-public class FileBlobStoreTest extends DbBlobStoreTest {
+public class FileBlobStoreTest extends AbstractBlobStoreTest {
 
     public void setUp() throws Exception {
         FileBlobStore store = new FileBlobStore("target/temp");

Modified: jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/MemoryBlobStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/MemoryBlobStoreTest.java?rev=1325300&r1=1325299&r2=1325300&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/MemoryBlobStoreTest.java
(original)
+++ jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/MemoryBlobStoreTest.java
Thu Apr 12 15:01:51 2012
@@ -19,7 +19,7 @@ package org.apache.jackrabbit.mk.blobs;
 /**
  * Tests the MemoryBlobStore implementation.
  */
-public class MemoryBlobStoreTest extends DbBlobStoreTest {
+public class MemoryBlobStoreTest extends AbstractBlobStoreTest {
 
     public void setUp() throws Exception {
         store = new MemoryBlobStore();

Modified: jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/MongoBlobStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/MongoBlobStoreTest.java?rev=1325300&r1=1325299&r2=1325300&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/MongoBlobStoreTest.java
(original)
+++ jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/blobs/MongoBlobStoreTest.java
Thu Apr 12 15:01:51 2012
@@ -19,7 +19,7 @@ package org.apache.jackrabbit.mk.blobs;
 /**
  * Tests the MongoBlobStore implementation.
  */
-public class MongoBlobStoreTest extends DbBlobStoreTest {
+public class MongoBlobStoreTest extends AbstractBlobStoreTest {
 
     public void setUp() throws Exception {
         store = new MemoryBlobStore();



Mime
View raw message