Search in sources :

Example 11 with Latch

use of org.mule.runtime.api.util.concurrent.Latch in project mule by mulesoft.

the class AbstractTransactionQueueManagerTestCase method testTakePut.

@Test
public void testTakePut() throws Exception {
    final QueueManager mgr = createQueueManager();
    mgr.start();
    final Latch latch = new Latch();
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                latch.countDown();
                Thread.sleep(200);
                QueueSession s = mgr.getQueueSession();
                Queue q = s.getQueue("queue1");
                assertEquals("Queue size", 0, q.size());
                q.put("String1");
            } catch (Exception e) {
            // ignore, let test fail
            }
        }
    };
    t.start();
    latch.await();
    long t0 = System.currentTimeMillis();
    QueueSession s = mgr.getQueueSession();
    Queue q = s.getQueue("queue1");
    assertEquals("Queue size", 0, q.size());
    Object o = q.take();
    long t1 = System.currentTimeMillis();
    t.join();
    assertNotNull(o);
    assertEquals("Queue content", "String1", o);
    assertEquals("Queue size", 0, q.size());
    assertTrue(t1 - t0 > 100);
    mgr.stop();
}
Also used : Latch(org.mule.runtime.api.util.concurrent.Latch) Queue(org.mule.runtime.core.api.util.queue.Queue) QueueSession(org.mule.runtime.core.api.util.queue.QueueSession) QueueManager(org.mule.runtime.core.api.util.queue.QueueManager) AbstractQueueManager(org.mule.runtime.core.internal.util.queue.AbstractQueueManager) Test(org.junit.Test)

Example 12 with Latch

use of org.mule.runtime.api.util.concurrent.Latch in project mule by mulesoft.

the class AbstractTransactionQueueManagerTestCase method testTakePutOverCapacity.

@Test
public void testTakePutOverCapacity() throws Exception {
    final QueueManager mgr = createQueueManager();
    mgr.start();
    mgr.setDefaultQueueConfiguration(new DefaultQueueConfiguration(2, false));
    final Latch latch = new Latch();
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                latch.await();
                Thread.sleep(200);
                QueueSession s = mgr.getQueueSession();
                Queue q = s.getQueue("queue1");
                Object o = q.take();
                assertEquals("Queue content", "String1", o);
            } catch (Exception e) {
            // ignore, let test fail
            }
        }
    };
    t.start();
    QueueSession s = mgr.getQueueSession();
    Queue q = s.getQueue("queue1");
    assertEquals("Queue size", 0, q.size());
    q.put("String1");
    q.put("String2");
    latch.countDown();
    long t0 = System.currentTimeMillis();
    q.put("String3");
    long t1 = System.currentTimeMillis();
    t.join();
    assertEquals("Queue size", 2, q.size());
    assertTrue(t1 - t0 > 100);
    mgr.stop();
}
Also used : DefaultQueueConfiguration(org.mule.runtime.core.api.util.queue.DefaultQueueConfiguration) Latch(org.mule.runtime.api.util.concurrent.Latch) Queue(org.mule.runtime.core.api.util.queue.Queue) QueueSession(org.mule.runtime.core.api.util.queue.QueueSession) QueueManager(org.mule.runtime.core.api.util.queue.QueueManager) AbstractQueueManager(org.mule.runtime.core.internal.util.queue.AbstractQueueManager) Test(org.junit.Test)

Example 13 with Latch

use of org.mule.runtime.api.util.concurrent.Latch in project mule by mulesoft.

the class AbstractTransactionQueueManagerTestCase method testOffer.

@Test
public void testOffer() throws Exception {
    final QueueManager mgr = createQueueManager();
    mgr.setDefaultQueueConfiguration(new DefaultQueueConfiguration(1, false));
    try {
        mgr.start();
        QueueSession s = mgr.getQueueSession();
        Queue q = s.getQueue("queue1");
        assertThat("Queue size", q.size(), is(0));
        assertThat(q.offer("String1", 0L), is(true));
        assertThat("Queue size", q.size(), is(1));
        assertThat(q.offer("String2", 1000), is(false));
        assertThat("Queue size", q.size(), is(1));
        final Latch takeExecutionLatch = new Latch();
        final Thread takeExecutionThread = new Thread(() -> {
            try {
                takeExecutionLatch.release();
                QueueSession s1 = mgr.getQueueSession();
                Queue q1 = s1.getQueue("queue1");
                assertThat("Queue content", q1.take(), is("String1"));
            } catch (Exception e) {
                // unlikely to happen. But if it does lets show it in the test logs.
                logger.warn("Error using queue session", e);
            }
        });
        takeExecutionThread.start();
        if (!takeExecutionLatch.await(THREAD_EXECUTION_TIMEOUT, TimeUnit.MILLISECONDS)) {
            fail("Thread executing put over queue was not executed");
        }
        assertThat(q.offer("String2", 1000), is(true));
        takeExecutionThread.join(THREAD_EXECUTION_TIMEOUT);
        assertThat("Queue size", q.size(), is(1));
    } finally {
        mgr.stop();
    }
}
Also used : DefaultQueueConfiguration(org.mule.runtime.core.api.util.queue.DefaultQueueConfiguration) Latch(org.mule.runtime.api.util.concurrent.Latch) Queue(org.mule.runtime.core.api.util.queue.Queue) QueueSession(org.mule.runtime.core.api.util.queue.QueueSession) QueueManager(org.mule.runtime.core.api.util.queue.QueueManager) AbstractQueueManager(org.mule.runtime.core.internal.util.queue.AbstractQueueManager) Test(org.junit.Test)

Example 14 with Latch

use of org.mule.runtime.api.util.concurrent.Latch in project mule by mulesoft.

the class AbstractTransactionQueueManagerTestCase method testPoll.

@Test
public void testPoll() throws Exception {
    final QueueManager mgr = createQueueManager();
    try {
        mgr.start();
        QueueSession s = mgr.getQueueSession();
        Queue q = s.getQueue("queue1");
        assertEquals("Queue size", 0, q.size());
        Object o = q.poll(0);
        assertEquals("Queue size", 0, q.size());
        assertNull(o);
        o = q.poll(1000);
        assertEquals("Queue size", 0, q.size());
        assertNull(o);
        q.put("String1");
        assertEquals("Queue size", 1, q.size());
        o = q.poll(0);
        assertEquals("Queue size", 0, q.size());
        assertEquals("Queue content", "String1", o);
        final Latch putExecutionLatch = new Latch();
        Thread putExecutionThread = new Thread(() -> {
            try {
                QueueSession s1 = mgr.getQueueSession();
                Queue q1 = s1.getQueue("queue1");
                putExecutionLatch.release();
                q1.put("String1");
            } catch (Exception e) {
                // unlikely to happen. But if it does lets show it in the test logs.
                logger.warn("Error using queue session", e);
            }
        });
        putExecutionThread.start();
        if (!putExecutionLatch.await(THREAD_EXECUTION_TIMEOUT, TimeUnit.MILLISECONDS)) {
            fail("Thread executing put over queue was not executed");
        }
        o = q.poll(RECEIVE_TIMEOUT);
        putExecutionThread.join(THREAD_EXECUTION_TIMEOUT);
        assertEquals("Queue size", q.size(), 0);
        assertEquals("Queue content", "String1", o);
    } finally {
        mgr.stop();
    }
}
Also used : Latch(org.mule.runtime.api.util.concurrent.Latch) Queue(org.mule.runtime.core.api.util.queue.Queue) QueueSession(org.mule.runtime.core.api.util.queue.QueueSession) QueueManager(org.mule.runtime.core.api.util.queue.QueueManager) AbstractQueueManager(org.mule.runtime.core.internal.util.queue.AbstractQueueManager) Test(org.junit.Test)

Example 15 with Latch

use of org.mule.runtime.api.util.concurrent.Latch in project mule by mulesoft.

the class OnceTestCase method concurrentConsume.

@Test
public void concurrentConsume() {
    Latch controlLath = new Latch();
    Latch testLath = new Latch();
    CountingConsumer consumer = new CountingConsumer();
    ConsumeOnce<String> once = Once.of(consumer);
    new Thread(() -> {
        await(controlLath);
        once.consumeOnce("s");
    }).start();
    new Thread(() -> {
        controlLath.release();
        once.consumeOnce("s");
        testLath.release();
    }).start();
    await(testLath);
    assertThat(consumer.getInvokationCount(), is(1));
}
Also used : Latch(org.mule.runtime.api.util.concurrent.Latch) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Aggregations

Latch (org.mule.runtime.api.util.concurrent.Latch)24 Test (org.junit.Test)17 Queue (org.mule.runtime.core.api.util.queue.Queue)7 QueueManager (org.mule.runtime.core.api.util.queue.QueueManager)7 QueueSession (org.mule.runtime.core.api.util.queue.QueueSession)7 AbstractQueueManager (org.mule.runtime.core.internal.util.queue.AbstractQueueManager)7 Description (io.qameta.allure.Description)3 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Consumer (java.util.function.Consumer)3 Serializable (java.io.Serializable)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 TimeoutException (java.util.concurrent.TimeoutException)2 Assert.assertThat (org.junit.Assert.assertThat)2 ExpectedException (org.junit.rules.ExpectedException)2 MuleException (org.mule.runtime.api.exception.MuleException)2 Reference (org.mule.runtime.api.util.Reference)2 DefaultQueueConfiguration (org.mule.runtime.core.api.util.queue.DefaultQueueConfiguration)2