Search in sources :

Example 16 with JoinableFile

use of org.commonjava.util.partyline.spi.JoinableFile in project partyline by Commonjava.

the class FileTreeTest method addChildAndRenderTree.

@Test
public void addChildAndRenderTree() throws IOException, InterruptedException {
    FileTree root = new FileTree(new RandomAccessJFS());
    File child = createStructure("child.txt", true);
    JoinableFile jf = root.setOrJoinFile(child, false, -1, TimeUnit.MILLISECONDS, (result) -> result);
    // JoinableFile jf = new JoinableFile( child, false );
    // root.add( jf );
    System.out.println("File tree rendered as:\n" + renderTree(root.getUnmodifiableEntryMap()));
}
Also used : RandomAccessJFS(org.commonjava.util.partyline.impl.local.RandomAccessJFS) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) File(java.io.File) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) Test(org.junit.Test)

Example 17 with JoinableFile

use of org.commonjava.util.partyline.spi.JoinableFile in project partyline by Commonjava.

the class FileTreeTest method lockDirThenLockTwoFiles.

@Test
public void lockDirThenLockTwoFiles() throws IOException, InterruptedException {
    FileTree root = new FileTree(new RandomAccessJFS());
    String dirPath = "directory";
    File child = createStructure(Paths.get(dirPath, "child.txt").toString(), true);
    File child2 = createStructure(Paths.get(dirPath, "child2.txt").toString(), true);
    File dir = child.getParentFile();
    boolean dirLocked = root.tryLock(dir, "lock directory", LockLevel.write, 2000, TimeUnit.MILLISECONDS);
    assertThat(dirLocked, equalTo(true));
    try (JoinableFile jf = root.setOrJoinFile(child, true, 2000, TimeUnit.MILLISECONDS, (result) -> result);
        JoinableFile jf2 = root.setOrJoinFile(child2, true, 2000, TimeUnit.MILLISECONDS, (result) -> result);
        OutputStream out = jf.getOutputStream();
        OutputStream out2 = jf2.getOutputStream()) {
        IOUtils.write("This is a test", out2);
        out2.close();
        IOUtils.write("This is a test", out);
    }
}
Also used : RandomAccessJFS(org.commonjava.util.partyline.impl.local.RandomAccessJFS) OutputStream(java.io.OutputStream) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) File(java.io.File) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) Test(org.junit.Test)

Example 18 with JoinableFile

use of org.commonjava.util.partyline.spi.JoinableFile in project partyline by Commonjava.

the class FileTreeTest method lockDirThenLockFile.

@Test
public void lockDirThenLockFile() throws IOException, InterruptedException {
    FileTree root = new FileTree(new RandomAccessJFS());
    String dirPath = "directory";
    File child = createStructure(Paths.get(dirPath, "child.txt").toString(), true);
    File dir = child.getParentFile();
    boolean dirLocked = root.tryLock(dir, "lock directory", LockLevel.write, 2000, TimeUnit.MILLISECONDS);
    assertThat(dirLocked, equalTo(true));
    try (JoinableFile jf = root.setOrJoinFile(child, true, 2000, TimeUnit.MILLISECONDS, (result) -> result);
        OutputStream out = jf.getOutputStream()) {
        IOUtils.write("This is a test", out);
    }
}
Also used : RandomAccessJFS(org.commonjava.util.partyline.impl.local.RandomAccessJFS) OutputStream(java.io.OutputStream) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) File(java.io.File) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) Test(org.junit.Test)

Example 19 with JoinableFile

use of org.commonjava.util.partyline.spi.JoinableFile in project partyline by Commonjava.

the class JoinFileWriteJustBeforeFinishedTest method run.

/**
 * Test verifies JoinableFile read could be done before its write stream close
 * with read init delay, this setup an script of events for one single file, where:
 * <ol>
 *     <li>Simulate JoinableFile write process </li>
 *     <li>Read should be proceeded before write stream close</li>
 * </ol>
 * @throws Exception
 */
@BMRules(rules = { // wait for read call to exit
@BMRule(name = "write close", targetClass = "RandomAccessJF", targetMethod = "close", targetLocation = "ENTRY", condition = "incrementCounter($0)==1", action = "debug(\">>>wait for service enter read.\");" + "waitFor(\"read\");" + "debug(\"<<<proceed with write close.\")"), // setup the trigger to signal write close when the read exits
@BMRule(name = "read", targetClass = "RandomAccessJF", targetMethod = "joinStream", targetLocation = "EXIT", action = "debug(\"<<<signalling write close.\"); " + "signalWake(\"read\", true);" + "debug(\"<<<signalled write close.\")") })
@Test
@BMUnitConfig(debug = true)
public void run() throws Exception {
    final ExecutorService execs = Executors.newFixedThreadPool(2);
    final CountDownLatch latch = new CountDownLatch(2);
    final File file = temp.newFile();
    String threadName = "writer" + writers++;
    final JoinableFile stream = new RandomAccessJFS().getFile(file, new LocalLockOwner(file.getAbsolutePath(), name.getMethodName(), LockLevel.write), null, true, new SignallingLock());
    execs.execute(() -> {
        Thread.currentThread().setName(threadName);
        new TimedFileWriter(stream, 1, latch).run();
    });
    execs.execute(() -> {
        Thread.currentThread().setName("reader" + readers++);
        new AsyncFileReader(1000, -1, -1, stream, latch).run();
    });
    System.out.println("Waiting for " + name.getMethodName() + " threads to complete.");
    latch.await();
}
Also used : SignallingLock(org.commonjava.cdi.util.weft.SignallingLock) RandomAccessJFS(org.commonjava.util.partyline.impl.local.RandomAccessJFS) TimedFileWriter(org.commonjava.util.partyline.fixture.TimedFileWriter) ExecutorService(java.util.concurrent.ExecutorService) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) CountDownLatch(java.util.concurrent.CountDownLatch) LocalLockOwner(org.commonjava.util.partyline.lock.local.LocalLockOwner) File(java.io.File) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) BMUnitConfig(org.jboss.byteman.contrib.bmunit.BMUnitConfig) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Aggregations

File (java.io.File)19 RandomAccessJFS (org.commonjava.util.partyline.impl.local.RandomAccessJFS)19 JoinableFile (org.commonjava.util.partyline.spi.JoinableFile)19 Test (org.junit.Test)18 SignallingLock (org.commonjava.cdi.util.weft.SignallingLock)16 OutputStream (java.io.OutputStream)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 ExecutorService (java.util.concurrent.ExecutorService)6 TimedFileWriter (org.commonjava.util.partyline.fixture.TimedFileWriter)6 LocalLockOwner (org.commonjava.util.partyline.lock.local.LocalLockOwner)6 FileOutputStream (java.io.FileOutputStream)4 InputStream (java.io.InputStream)4 BMRules (org.jboss.byteman.contrib.bmunit.BMRules)4 BMUnitConfig (org.jboss.byteman.contrib.bmunit.BMUnitConfig)4 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)1 Logger (org.slf4j.Logger)1