Search in sources :

Example 11 with SignallingLock

use of org.commonjava.cdi.util.weft.SignallingLock in project partyline by Commonjava.

the class JoinableFileTest method lockDirectoryJoinFails.

@Test(expected = IOException.class)
public void lockDirectoryJoinFails() throws IOException, InterruptedException {
    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));
    jf.joinStream();
}
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 12 with SignallingLock

use of org.commonjava.cdi.util.weft.SignallingLock in project partyline by Commonjava.

the class JoinableFileTest method assertReadOfExistingFileOfSize.

private void assertReadOfExistingFileOfSize(String s) throws IOException, InterruptedException {
    File f = temp.newFile("read-target.txt");
    int sz = (int) FileSize.valueOf("11mb").getSize();
    byte[] src = new byte[sz];
    Random rand = new Random();
    rand.nextBytes(src);
    try (OutputStream out = new FileOutputStream(f)) {
        IOUtils.write(src, out);
    }
    final JoinableFile jf = new RandomAccessJFS().getFile(f, newLockOwner(f.getAbsolutePath(), LockLevel.read), null, false, new SignallingLock());
    try (InputStream stream = jf.joinStream()) {
        byte[] result = IOUtils.toByteArray(stream);
        assertThat(result, equalTo(src));
    }
}
Also used : SignallingLock(org.commonjava.cdi.util.weft.SignallingLock) RandomAccessJFS(org.commonjava.util.partyline.impl.local.RandomAccessJFS) Random(java.util.Random) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) File(java.io.File)

Example 13 with SignallingLock

use of org.commonjava.cdi.util.weft.SignallingLock in project partyline by Commonjava.

the class JoinableFileTest method lockDirectory.

@Test
public void lockDirectory() 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));
    jf.close();
}
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 14 with SignallingLock

use of org.commonjava.cdi.util.weft.SignallingLock in project partyline by Commonjava.

the class JoinableFileTest method overwriteFile_SmallerReplacementTruncates.

@Test
public void overwriteFile_SmallerReplacementTruncates() throws Exception {
    File f = temp.newFile();
    JoinableFile jf = new RandomAccessJFS().getFile(f, newLockOwner(f.getAbsolutePath(), LockLevel.write), null, true, new SignallingLock());
    OutputStream stream = jf.getOutputStream();
    String longer = "This is a really really really long string";
    stream.write(longer.getBytes());
    stream.close();
    jf = new RandomAccessJFS().getFile(f, newLockOwner(f.getAbsolutePath(), LockLevel.write), null, true, new SignallingLock());
    stream = jf.getOutputStream();
    String shorter = "This is a short string";
    stream.write(shorter.getBytes());
    stream.close();
    final File file = new File(jf.getPath());
    System.out.println("File length: " + file.length());
    assertThat(file.length(), equalTo((long) shorter.getBytes().length));
    String content = FileUtils.readFileToString(f);
    assertThat(content, equalTo(shorter));
}
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 15 with SignallingLock

use of org.commonjava.cdi.util.weft.SignallingLock in project partyline by Commonjava.

the class BinaryFileTest method shouldReadBinaryFile.

@Test
public void shouldReadBinaryFile() throws IOException, InterruptedException {
    List<String> failures = new ArrayList<>();
    File binaryFile = temp.newFile("binary-file.bin");
    ReentrantLock lock = new ReentrantLock();
    JoinableFile jf = new RandomAccessJFS().getFile(binaryFile, new LocalLockOwner(binaryFile.getAbsolutePath(), name.getMethodName(), LockLevel.write), null, true, new SignallingLock());
    OutputStream jos = jf.getOutputStream();
    InputStream actual = jf.joinStream();
    ByteArrayOutputStream written = new ByteArrayOutputStream();
    writeBinaryFile(jos, written);
    int pos = 0;
    int exp, act;
    ByteArrayInputStream expected = new ByteArrayInputStream(written.toByteArray());
    while ((exp = expected.read()) != -1) {
        act = actual.read();
        pos++;
        if (act != exp) {
            failures.add(String.format("Failure at position %d. Expected %d, got %d", pos, exp, act));
        }
    }
    actual.close();
    if (!failures.isEmpty()) {
        fail("Failures: " + failures);
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) SignallingLock(org.commonjava.cdi.util.weft.SignallingLock) RandomAccessJFS(org.commonjava.util.partyline.impl.local.RandomAccessJFS) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ArrayList(java.util.ArrayList) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) LocalLockOwner(org.commonjava.util.partyline.lock.local.LocalLockOwner) File(java.io.File) JoinableFile(org.commonjava.util.partyline.spi.JoinableFile) Test(org.junit.Test)

Aggregations

File (java.io.File)16 SignallingLock (org.commonjava.cdi.util.weft.SignallingLock)16 RandomAccessJFS (org.commonjava.util.partyline.impl.local.RandomAccessJFS)16 JoinableFile (org.commonjava.util.partyline.spi.JoinableFile)16 Test (org.junit.Test)15 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 OutputStream (java.io.OutputStream)5 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