Search in sources :

Example 1 with SynchronousChannel

use of EDU.oswego.cs.dl.util.concurrent.SynchronousChannel in project jackrabbit by apache.

the class GarbageCollectorTest method testConcurrentGC.

public void testConcurrentGC() throws Exception {
    Node root = testRootNode;
    Session session = root.getSession();
    final SynchronousChannel sync = new SynchronousChannel();
    final Node node = root.addNode("slowBlob");
    final int blobLength = 1000;
    final ValueFactory vf = session.getValueFactory();
    new Thread() {

        public void run() {
            try {
                node.setProperty("slowBlob", vf.createBinary(new InputStream() {

                    int pos;

                    public int read() throws IOException {
                        pos++;
                        if (pos < blobLength) {
                            return pos % 80 == 0 ? '\n' : '.';
                        } else if (pos == blobLength) {
                            try {
                                sync.put("x");
                                // deleted
                                sync.take();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            return 'x';
                        }
                        return -1;
                    }
                }));
                node.getSession().save();
                sync.put("saved");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.start();
    assertEquals("x", sync.take());
    DataStoreGarbageCollector gc = ((SessionImpl) session).createDataStoreGarbageCollector();
    gc.setPersistenceManagerScan(false);
    gc.mark();
    gc.sweep();
    sync.put("deleted");
    assertEquals("saved", sync.take());
    InputStream in = node.getProperty("slowBlob").getBinary().getStream();
    for (int pos = 1; pos < blobLength; pos++) {
        int expected = pos % 80 == 0 ? '\n' : '.';
        assertEquals(expected, in.read());
    }
    assertEquals('x', in.read());
    in.close();
    gc.close();
}
Also used : InputStream(java.io.InputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) IOException(java.io.IOException) SessionImpl(org.apache.jackrabbit.core.SessionImpl) SynchronousChannel(EDU.oswego.cs.dl.util.concurrent.SynchronousChannel) IOException(java.io.IOException) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session) DataStoreGarbageCollector(org.apache.jackrabbit.api.management.DataStoreGarbageCollector)

Aggregations

SynchronousChannel (EDU.oswego.cs.dl.util.concurrent.SynchronousChannel)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Node (javax.jcr.Node)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1 ValueFactory (javax.jcr.ValueFactory)1 DataStoreGarbageCollector (org.apache.jackrabbit.api.management.DataStoreGarbageCollector)1 SessionImpl (org.apache.jackrabbit.core.SessionImpl)1