Search in sources :

Example 1 with RunOnce

use of org.mule.runtime.core.api.util.func.Once.RunOnce in project mule by mulesoft.

the class OnceTestCase method concurrentRun.

@Test
public void concurrentRun() {
    Latch controlLath = new Latch();
    Latch testLath = new Latch();
    CountingRunnable runnable = new CountingRunnable();
    RunOnce once = Once.of(runnable);
    new Thread(() -> {
        await(controlLath);
        once.runOnce();
    }).start();
    new Thread(() -> {
        controlLath.release();
        once.runOnce();
        testLath.release();
    }).start();
    await(testLath);
    assertThat(runnable.getInvokationCount(), is(1));
}
Also used : RunOnce(org.mule.runtime.core.api.util.func.Once.RunOnce) Latch(org.mule.runtime.api.util.concurrent.Latch) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 2 with RunOnce

use of org.mule.runtime.core.api.util.func.Once.RunOnce in project mule by mulesoft.

the class OnceTestCase method runUntilSuccessful.

@Test
public void runUntilSuccessful() {
    CountingRunnable runnable = new CountingRunnable();
    RunOnce once = Once.of(() -> {
        runnable.runChecked();
        int count = runnable.getInvokationCount();
        if (count < 3) {
            throw new RuntimeException();
        }
    });
    for (int i = 0; i < 5; i++) {
        try {
            once.runOnce();
            break;
        } catch (Exception e) {
        }
    }
    assertThat(runnable.getInvokationCount(), is(3));
}
Also used : RunOnce(org.mule.runtime.core.api.util.func.Once.RunOnce) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 3 with RunOnce

use of org.mule.runtime.core.api.util.func.Once.RunOnce in project mule by mulesoft.

the class OnceTestCase method runOnlyOnce.

@Test
public void runOnlyOnce() {
    CountingRunnable runnable = new CountingRunnable();
    RunOnce once = Once.of(runnable);
    once.runOnce();
    once.runOnce();
    assertThat(runnable.getInvokationCount(), is(1));
}
Also used : RunOnce(org.mule.runtime.core.api.util.func.Once.RunOnce) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 RunOnce (org.mule.runtime.core.api.util.func.Once.RunOnce)3 SmallTest (org.mule.tck.size.SmallTest)3 Latch (org.mule.runtime.api.util.concurrent.Latch)1