Search in sources :

Example 6 with BMUnitConfig

use of org.jboss.byteman.contrib.bmunit.BMUnitConfig in project partyline by Commonjava.

the class ConcurrentReadRuntimeErrorsClearLocksTest method run.

/**
 * Test that locks for mutiple reads clear correctly. This will setup an script of events for
 * a single file, where:
 * <ol>
 *     <li>Multiple reads happen simultaneously, read the content, and close</li>
 *     <li>A single write at the end ensures the other locks are clear</li>
 * </ol>
 * @throws Exception
 */
/*@formatter:off*/
@BMRules(rules = { // When we try to init a new JoinableFile for INPUT, simulate an IOException from somewhere deeper in the stack.
@BMRule(name = "new JoinableFile error", targetClass = "RandomAccessJF", targetMethod = "<init>", targetLocation = "ENTRY", condition = "$4 == false", action = "debug(\"Throwing test exception.\"); " + "throw new IllegalStateException(\"Test exception\")") })
/*@formatter:on*/
@BMUnitConfig(debug = true)
@Test
public void run() throws Exception {
    final ExecutorService execs = Executors.newFixedThreadPool(5);
    final File f = temp.newFile("child.txt");
    FileUtils.write(f, "test data");
    final CountDownLatch latch = new CountDownLatch(3);
    final CountDownLatch readBeginLatch = new CountDownLatch(3);
    final CountDownLatch readEndLatch = new CountDownLatch(3);
    final Partyline manager = getPartylineInstance();
    manager.startReporting(5000, 5000);
    final long start = System.currentTimeMillis();
    execs.execute(writer(manager, f, latch, readEndLatch));
    for (int i = 0; i < 3; i++) {
        final int k = i;
        execs.execute(reader(k, manager, f, latch, readBeginLatch, readEndLatch, null));
    }
    latch.await();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) BMUnitConfig(org.jboss.byteman.contrib.bmunit.BMUnitConfig) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 7 with BMUnitConfig

use of org.jboss.byteman.contrib.bmunit.BMUnitConfig in project partyline by Commonjava.

the class LockFileOpenOutputStreamWaitsForUnlockTest method run.

/**
 * Simulate the condition after releasing the write-lock on a file, another writing on the file is able to be proceeded.
 * this setup an script of events for one single file, where:
 * <ol>
 *     <li>Lock and then Unlock on a specific file</li>
 *     <li>Then proceed the writing on this file</li>
 * </ol>
 * @throws Exception
 */
@BMRules(rules = { // wait for lockUnlock call to exit
@BMRule(name = "openOutputStream", targetClass = "Partyline", targetMethod = "openOutputStream(File,long)", targetLocation = "ENTRY", // condition = "$2==-1",
action = "debug(\">>>wait for service enter lockUnlock.\");" + "waitFor(\"lockUnlock\");" + "debug(\"<<<proceed with openOutputStream.\")"), // setup the trigger to signal openOutputStream when the lockUnlock exits
@BMRule(name = "lockUnlock", targetClass = "Partyline", targetMethod = "unlock", targetLocation = "EXIT", // condition = "$2.equals(\"test\")",
action = "debug(\"<<<signalling openOutputStream.\"); " + "signalWake(\"lockUnlock\", true);" + "debug(\"<<<signalled openOutputStream.\")") })
@Test
@BMUnitConfig(debug = true)
public void run() throws Exception {
    final Partyline manager = getPartylineInstance();
    final File f = temp.newFile("test.txt");
    final String lockUnlock = "lock-clearLocks";
    final String output = "output";
    Map<String, Runnable> executions = new LinkedHashMap<>();
    executions.put(output, () -> {
        Thread.currentThread().setName(output);
        try {
            OutputStream o = manager.openOutputStream(f, 60000);
            o.close();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Failed to open outputStream: " + e.getMessage());
        }
    });
    executions.put(lockUnlock, () -> {
        Thread.currentThread().setName(lockUnlock);
        try {
            final boolean locked = manager.lock(f, 100, LockLevel.write, "test");
            assertThat(locked, equalTo(true));
            assertThat(manager.unlock(f), equalTo(true));
        } catch (final InterruptedException e) {
            fail("Interrupted!");
        } catch (IOException e) {
            e.printStackTrace();
            fail("Failed to unlock: " + e.getMessage());
        }
    });
    assertThat(raceExecutions(executions), equalTo(lockUnlock));
}
Also used : OutputStream(java.io.OutputStream) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) BMUnitConfig(org.jboss.byteman.contrib.bmunit.BMUnitConfig) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 8 with BMUnitConfig

use of org.jboss.byteman.contrib.bmunit.BMUnitConfig in project partyline by Commonjava.

the class OpenInputStreamConcurrentReadersGetSameResultTest method run.

/**
 * Test that verifies concurrent reading tasks, including timeout one, on the same file are allowable, this setup an script of events for
 * one single file, where:
 * <ol>
 *     <li>Multiple reads happen simultaneously, read the content</li>
 *     <li>Reading processes on the same file have no interaction between each other</li>
 * </ol>
 * @throws Exception
 */
@BMRules(rules = { // wait for first openInputStream call to exit
@BMRule(name = "second openInputStream", targetClass = "Partyline", targetMethod = "openInputStream", targetLocation = "ENTRY", condition = "$2==100", action = "debug(\">>>wait for service enter first openInputStream.\");" + "waitFor(\"first openInputStream\");" + "debug(\"<<<proceed with second openInputStream.\")"), // setup the trigger to signal second openInputStream when the first openInputStream exits
@BMRule(name = "first openInputStream", targetClass = "Partyline", targetMethod = "openInputStream", targetLocation = "ENTRY", condition = "$2==-1", action = "debug(\"<<<signalling second openInputStream.\"); " + "signalWake(\"first openInputStream\", true);" + "debug(\"<<<signalled second openInputStream.\")") })
@Test
@BMUnitConfig(debug = true)
public void run() throws Exception {
    final ExecutorService execs = Executors.newFixedThreadPool(2);
    final CountDownLatch latch = new CountDownLatch(2);
    final Partyline manager = getPartylineInstance();
    final File f = temp.newFile();
    String str = "This is a test";
    FileUtils.write(f, str);
    List<String> returning = new ArrayList<String>();
    for (int i = 0; i < 2; i++) {
        final int k = i;
        execs.execute(() -> {
            Thread.currentThread().setName("openInputStream-" + k);
            InputStream s = null;
            try {
                switch(k) {
                    case 0:
                        s = manager.openInputStream(f, -1);
                        break;
                    case 1:
                        // note reporting timeout handle error
                        s = manager.openInputStream(f, 100);
                        break;
                }
                returning.add(IOUtils.toString(s));
                s.close();
            } catch (final Exception e) {
                e.printStackTrace();
                fail("Failed to open inputStream: " + e.getMessage());
            } finally {
                latch.countDown();
            }
        });
    }
    latch.await();
    assertThat(returning.size(), equalTo(2));
    assertThat(returning.get(0), equalTo(str));
    assertThat(returning.get(1), equalTo(str));
}
Also used : InputStream(java.io.InputStream) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) BMUnitConfig(org.jboss.byteman.contrib.bmunit.BMUnitConfig) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 9 with BMUnitConfig

use of org.jboss.byteman.contrib.bmunit.BMUnitConfig 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)

Example 10 with BMUnitConfig

use of org.jboss.byteman.contrib.bmunit.BMUnitConfig in project partyline by Commonjava.

the class OpenOutputStreamSecondWaitsUntilFirstCloseTest method run.

/**
 * Test aligns two concurrent writing tasks as second starts until first close, operated on the same file
 * verified to be available, this setup an script of events for one single file, where:
 * <ol>
 *     <li>Multiple writes happened as a specific sequence</li>
 *     <li>Has no simultaneous Writing lock on the same file</li>
 * </ol>
 * @throws Exception
 */
@BMRules(rules = { // wait for first openOutputStream call to exit
@BMRule(name = "second openOutputStream", targetClass = "Partyline", targetMethod = "openOutputStream(File,long)", targetLocation = "ENTRY", condition = "$2==100", action = "debug(\">>>wait for service enter first openOutputStream.\");" + "waitFor(\"first openOutputStream\");" + "java.lang.Thread.sleep(100);" + "debug(\"<<<proceed with second openOutputStream.\")"), // setup the trigger to signal second openOutputStream when the first openOutputStream exits
@BMRule(name = "first openOutputStream", targetClass = "Partyline", targetMethod = "openOutputStream(File,long)", targetLocation = "EXIT", condition = "$2==-1", action = "debug(\"<<<signalling second openOutputStream.\"); " + "signalWake(\"first openOutputStream\", true);" + "debug(\"<<<signalled second openOutputStream.\")") })
@Test
@BMUnitConfig(debug = true)
public void run() throws Exception {
    final Partyline manager = getPartylineInstance();
    final File f = temp.newFile();
    final String first = "first";
    final String second = "second";
    Map<String, Runnable> executions = new LinkedHashMap<>();
    for (int i = 0; i < 2; i++) {
        final int k = i;
        String tname = k < 1 ? first : second;
        executions.put(tname, () -> {
            Thread.currentThread().setName(tname);
            OutputStream o = null;
            try {
                switch(k) {
                    case 0:
                        o = manager.openOutputStream(f, -1);
                        break;
                    case 1:
                        o = manager.openOutputStream(f, 100);
                }
                o.write("Test data".getBytes());
                o.close();
            } catch (Exception e) {
                e.printStackTrace();
                fail("Failed to open outputStream: " + e.getMessage());
            }
        });
    }
    assertThat(raceExecutions(executions), equalTo(first));
}
Also used : OutputStream(java.io.OutputStream) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) BMUnitConfig(org.jboss.byteman.contrib.bmunit.BMUnitConfig) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Aggregations

File (java.io.File)15 BMUnitConfig (org.jboss.byteman.contrib.bmunit.BMUnitConfig)15 Test (org.junit.Test)15 BMRules (org.jboss.byteman.contrib.bmunit.BMRules)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 ExecutorService (java.util.concurrent.ExecutorService)12 JoinableFile (org.commonjava.util.partyline.spi.JoinableFile)5 SignallingLock (org.commonjava.cdi.util.weft.SignallingLock)4 TimedFileWriter (org.commonjava.util.partyline.fixture.TimedFileWriter)4 RandomAccessJFS (org.commonjava.util.partyline.impl.local.RandomAccessJFS)4 LocalLockOwner (org.commonjava.util.partyline.lock.local.LocalLockOwner)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 LinkedHashMap (java.util.LinkedHashMap)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Closeable (java.io.Closeable)1 WeakReference (java.lang.ref.WeakReference)1 Map (java.util.Map)1