Search in sources :

Example 1 with Queue

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

the class AbstractTransactionQueueManagerTestCase method testRecoverColdRestart.

@Test
public void testRecoverColdRestart() throws Exception {
    QueueManager mgr = createQueueManager();
    QueueSession s = mgr.getQueueSession();
    Queue q = s.getQueue("warmRecoverQueue");
    mgr.start();
    int toPopulate = 50;
    // Populate queue
    Random rnd = new Random();
    for (int j = 0; j < toPopulate; j++) {
        byte[] o = new byte[2048];
        rnd.nextBytes(o);
        q.put(o);
    }
    assertEquals(toPopulate, q.size());
    // Stop and recreate TransactionalQueueManager simulating a cold restart
    mgr.stop();
    mgr = createQueueManager();
    s = mgr.getQueueSession();
    q = s.getQueue("warmRecoverQueue");
    mgr.start();
    if (isPersistent()) {
        assertEquals(toPopulate, q.size());
    } else {
        assertEquals(0, q.size());
    }
}
Also used : Random(java.util.Random) 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 2 with Queue

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

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

the class AbstractTransactionQueueManagerTestCase method testPutTake.

@Test
public void testPutTake() throws Exception {
    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());
    Object o = q.take();
    assertNotNull(o);
    assertEquals("Queue content", "String1", o);
    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 4 with Queue

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

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

the class AbstractTransactionQueueManagerTestCase method testPeek.

@Test
public void testPeek() throws Exception {
    QueueManager mgr = createQueueManager();
    try {
        mgr.start();
        QueueSession s = mgr.getQueueSession();
        Queue q = s.getQueue("queue1");
        assertEquals("Queue size", 0, q.size());
        Object o = q.peek();
        assertEquals("Queue size", 0, q.size());
        assertNull(o);
        q.put("String1");
        assertEquals("Queue size", 1, q.size());
        o = q.peek();
        assertEquals("Queue size", 1, q.size());
        assertEquals("Queue content", "String1", o);
        o = q.poll(1000);
        assertEquals("Queue size", 0, q.size());
        assertEquals("Queue content", "String1", o);
    } finally {
        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)

Aggregations

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