Search in sources :

Example 1 with RandomAccessJFS

use of org.commonjava.util.partyline.impl.local.RandomAccessJFS in project partyline by Commonjava.

the class JoinFileWriteAndCloseBeforeFinishedTest method run.

/**
 * Test verifies JoinableFile read could be done before its write stream close
 * with read close 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(0, -1, 6000, 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)

Example 2 with RandomAccessJFS

use of org.commonjava.util.partyline.impl.local.RandomAccessJFS in project partyline by Commonjava.

the class JoinableFileTest method readExistingFile.

@Test
public void readExistingFile() throws Exception {
    File f = temp.newFile("read-target.txt");
    String src = "This is a test";
    FileUtils.write(f, src);
    final JoinableFile stream = new RandomAccessJFS().getFile(f, newLockOwner(f.getAbsolutePath(), LockLevel.read), null, false, new SignallingLock());
    System.out.println("File length: " + f.length());
    String result = FileUtils.readFileToString(f);
    assertThat(result, equalTo(src));
}
Also used : SignallingLock(org.commonjava.cdi.util.weft.SignallingLock) RandomAccessJFS(org.commonjava.util.partyline.impl.local.RandomAccessJFS) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) File(java.io.File) Test(org.junit.Test)

Example 3 with RandomAccessJFS

use of org.commonjava.util.partyline.impl.local.RandomAccessJFS in project partyline by Commonjava.

the class JoinableFileTest method lockDirectoryCannotBeWritten.

@Test
public void lockDirectoryCannotBeWritten() throws IOException {
    File dir = temp.newFolder();
    dir.mkdirs();
    final JoinableFile jf = new RandomAccessJFS().getFile(dir, newLockOwner(dir.getAbsolutePath(), LockLevel.read), null, false, new SignallingLock());
    assertThat(jf.isWriteLocked(), equalTo(true));
    OutputStream out = jf.getOutputStream();
    assertThat(out, nullValue());
    jf.close();
}
Also used : SignallingLock(org.commonjava.cdi.util.weft.SignallingLock) RandomAccessJFS(org.commonjava.util.partyline.impl.local.RandomAccessJFS) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) File(java.io.File) Test(org.junit.Test)

Example 4 with RandomAccessJFS

use of org.commonjava.util.partyline.impl.local.RandomAccessJFS in project partyline by Commonjava.

the class JoinableFileTest method joinFileRead.

@Test
public void joinFileRead() throws Exception {
    final File file = temp.newFile();
    final File f = temp.newFile();
    String str = "This is a test";
    FileUtils.write(f, str);
    JoinableFile jf = null;
    InputStream s1 = null;
    InputStream s2 = null;
    Logger logger = LoggerFactory.getLogger(getClass());
    try {
        jf = new RandomAccessJFS().getFile(f, newLockOwner(f.getAbsolutePath(), LockLevel.read), null, false, new SignallingLock());
        s1 = jf.joinStream();
        s2 = jf.joinStream();
        logger.info("READ first thread");
        String out1 = IOUtils.toString(s1);
        logger.info("READ second thread");
        String out2 = IOUtils.toString(s2);
        assertThat("first reader returned wrong data", out1, equalTo(str));
        assertThat("second reader returned wrong data", out2, equalTo(str));
    } finally {
        logger.info("CLOSE first thread");
        IOUtils.closeQuietly(s1);
        logger.info("CLOSE second thread");
        IOUtils.closeQuietly(s2);
    }
    assertThat(jf, notNullValue());
    assertThat(jf.isOpen(), equalTo(false));
}
Also used : SignallingLock(org.commonjava.cdi.util.weft.SignallingLock) RandomAccessJFS(org.commonjava.util.partyline.impl.local.RandomAccessJFS) InputStream(java.io.InputStream) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) Logger(org.slf4j.Logger) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) File(java.io.File) Test(org.junit.Test)

Example 5 with RandomAccessJFS

use of org.commonjava.util.partyline.impl.local.RandomAccessJFS in project partyline by Commonjava.

the class JoinFileWriteTwiceWithDelayTest method run.

/**
 * Test verifies JoinableFile simultaneous reads could be done before its write stream close,
 * this setup an script of events for one single file, where:
 * <ol>
 *     <li>Simulate JoinableFile write process </li>
 *     <li>Two simultaneous reads with init delay should be proceeded before write stream close</li>
 * </ol>
 * @throws Exception
 */
@BMRules(rules = { // setup the rendezvous for all threads, which will mean suspending everything until all threads are started.
@BMRule(name = "init rendezvous", targetClass = "RandomAccessJF", targetMethod = "<init>", targetLocation = "ENTRY", action = "createRendezvous(\"begin\", 3);" + "debug(\"<<<init rendezvous for begin.\")"), // setup the rendezvous to wait for all threads to be ready before proceeding.
@BMRule(name = "write close", targetClass = "RandomAccessJF", targetMethod = "close", targetLocation = "ENTRY", condition = "incrementCounter($0)==1", action = "debug(\">>>Waiting for ALL to start.\");" + "rendezvous(\"begin\");" + "debug(\"<<<\"+Thread.currentThread().getName() + \": write thread proceeding.\" )"), @BMRule(name = "read", targetClass = "RandomAccessJF", targetMethod = "joinStream", targetLocation = "EXIT", action = "debug(\">>>Waiting for ALL to start.\");" + "rendezvous(\"begin\");" + "debug(\"<<<\"+Thread.currentThread().getName() + \": read thread proceeding.\" )") })
@Test
@BMUnitConfig(debug = true)
public void run() throws Exception {
    final ExecutorService execs = Executors.newFixedThreadPool(3);
    final CountDownLatch latch = new CountDownLatch(3);
    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(0, -1, -1, stream, latch).run();
    });
    execs.execute(() -> {
        Thread.currentThread().setName("reader" + readers++);
        new AsyncFileReader(500, -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