Search in sources :

Example 6 with QueueSession

use of org.mule.runtime.core.api.util.queue.QueueSession 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 7 with QueueSession

use of org.mule.runtime.core.api.util.queue.QueueSession in project mule by mulesoft.

the class AbstractTransactionQueueManagerTestCase method testTransactionsOnMultipleQueues.

@Test
public void testTransactionsOnMultipleQueues() throws Exception {
    AbstractQueueManager mgr = createQueueManager();
    try {
        mgr.start();
        QueueSession s1 = mgr.getQueueSession();
        QueueSession s2 = mgr.getQueueSession();
        Queue q1s1 = s1.getQueue("queue1");
        Queue q1s2 = s2.getQueue("queue1");
        Queue q2s1 = s1.getQueue("queue2");
        Queue q2s2 = s2.getQueue("queue2");
        q1s1.put("String1");
        assertEquals("Queue size", 1, q1s1.size());
        assertEquals("Queue size", 1, q1s2.size());
        s1.begin();
        Object o = q1s1.take();
        assertNotNull(o);
        assertEquals("String1", o);
        assertEquals("Queue size", 0, q1s1.size());
        assertEquals("Queue size", 0, q1s2.size());
        q2s1.put("String2");
        assertEquals("Queue size", 1, q2s1.size());
        assertEquals("Queue size", 0, q2s2.size());
        s1.commit();
        assertEquals("Queue size", 0, q1s1.size());
        assertEquals("Queue size", 0, q1s2.size());
        assertEquals("Queue size", 1, q2s1.size());
        assertEquals("Queue size", 1, q2s2.size());
        s1.begin();
        o = q2s1.take();
        assertNotNull(o);
        assertEquals("String2", o);
        assertEquals("Queue size", 0, q1s1.size());
        assertEquals("Queue size", 0, q1s2.size());
        assertEquals("Queue size", 0, q2s1.size());
        assertEquals("Queue size", 0, q2s2.size());
        q1s1.put("String1");
        assertEquals("Queue size", 1, q1s1.size());
        assertEquals("Queue size", 0, q1s2.size());
        assertEquals("Queue size", 0, q2s1.size());
        assertEquals("Queue size", 0, q2s2.size());
        s1.rollback();
        assertEquals("Queue size", 0, q1s1.size());
        assertEquals("Queue size", 0, q1s2.size());
        assertEquals("Queue size", 1, q2s1.size());
        assertEquals("Queue size", 1, q2s2.size());
    } finally {
        mgr.stop();
        mgr.dispose();
    }
}
Also used : Queue(org.mule.runtime.core.api.util.queue.Queue) QueueSession(org.mule.runtime.core.api.util.queue.QueueSession) AbstractQueueManager(org.mule.runtime.core.internal.util.queue.AbstractQueueManager) Test(org.junit.Test)

Example 8 with QueueSession

use of org.mule.runtime.core.api.util.queue.QueueSession in project mule by mulesoft.

the class AbstractTransactionQueueManagerTestCase method testClearWithoutTransaction.

@Test
public void testClearWithoutTransaction() throws Exception {
    final QueueManager mgr = createQueueManager();
    mgr.start();
    QueueSession s = mgr.getQueueSession();
    Queue q = s.getQueue("queue1");
    assertEquals("Queue size", 0, q.size());
    q.put("String1");
    assertEquals("Queue size", 1, q.size());
    q.clear();
    assertEquals("Queue size", 0, q.size());
    mgr.stop();
}
Also used : 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 9 with QueueSession

use of org.mule.runtime.core.api.util.queue.QueueSession 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 10 with QueueSession

use of org.mule.runtime.core.api.util.queue.QueueSession in project mule by mulesoft.

the class AbstractTransactionQueueManagerTestCase method testTransactedPutRollbackWithPersistence.

@Test
public void testTransactedPutRollbackWithPersistence() throws Exception {
    if (isPersistent()) {
        AbstractQueueManager mgr = createQueueManager();
        try {
            mgr.start();
            QueueSession s = mgr.getQueueSession();
            Queue q = s.getQueue("queue1");
            s.begin();
            q.put("String1");
            assertEquals("Queue size", 1, q.size());
            s.rollback();
            assertEquals("Queue size", 0, q.size());
            s = mgr.getQueueSession();
            q = s.getQueue("queue1");
            assertEquals("Queue size", 0, q.size());
            mgr.stop();
            mgr = createQueueManager();
            mgr.start();
            s = mgr.getQueueSession();
            q = s.getQueue("queue1");
            assertEquals("Queue size", 0, q.size());
        } finally {
            mgr.stop();
            mgr.dispose();
        }
    } else {
        logger.info("Ignoring test because queue manager is not persistent");
    }
}
Also used : Queue(org.mule.runtime.core.api.util.queue.Queue) QueueSession(org.mule.runtime.core.api.util.queue.QueueSession) AbstractQueueManager(org.mule.runtime.core.internal.util.queue.AbstractQueueManager) Test(org.junit.Test)

Aggregations

QueueSession (org.mule.runtime.core.api.util.queue.QueueSession)22 Test (org.junit.Test)20 Queue (org.mule.runtime.core.api.util.queue.Queue)20 AbstractQueueManager (org.mule.runtime.core.internal.util.queue.AbstractQueueManager)18 QueueManager (org.mule.runtime.core.api.util.queue.QueueManager)16 Latch (org.mule.runtime.api.util.concurrent.Latch)7 DefaultQueueConfiguration (org.mule.runtime.core.api.util.queue.DefaultQueueConfiguration)5 Serializable (java.io.Serializable)2 Random (java.util.Random)2