Search in sources :

Example 21 with Latch

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

the class RoutersExecutionTestCase method concurrentRouterExecution.

/**
 * Executes the same flow concurrently to check that no race condition exists because
 * two different instances of Chain are being used
 */
@Test
public void concurrentRouterExecution() throws Exception {
    executor = newFixedThreadPool(2);
    final Latch beginLatch = new Latch();
    final CountDownLatch assertLatch = new CountDownLatch(2);
    final Consumer<Reference<CoreEvent>> runner = reference -> {
        try {
            beginLatch.await(10000, MILLISECONDS);
            reference.set(flowRunner("singleRouteRouter").withPayload("CustomPayload").run());
            assertLatch.countDown();
        } catch (Exception e) {
            fail(e.getMessage());
        }
    };
    final Reference<CoreEvent> first = new Reference<>();
    final Reference<CoreEvent> second = new Reference<>();
    executor.submit(() -> runner.accept(first));
    executor.submit(() -> runner.accept(second));
    beginLatch.release();
    assertLatch.await(10000, MILLISECONDS);
    CoreEvent firstResult = first.get();
    assertThat(firstResult, is(notNullValue()));
    CoreEvent secondResult = second.get();
    assertThat(secondResult, is(notNullValue()));
    assertThat(secondResult, is(not(sameInstance(firstResult))));
    assertThat(firstResult.getMessage().getPayload().getValue(), is("CustomPayload"));
    assertThat(secondResult.getMessage().getPayload().getValue(), is("CustomPayload"));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) AbstractExtensionFunctionalTestCase(org.mule.test.module.extension.AbstractExtensionFunctionalTestCase) Ricin(org.mule.test.heisenberg.extension.model.Ricin) Event(org.mule.runtime.api.event.Event) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) After(org.junit.After) Assert.fail(org.junit.Assert.fail) ExpectedException(org.junit.rules.ExpectedException) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) ExecutorService(java.util.concurrent.ExecutorService) CoreMatchers.sameInstance(org.hamcrest.CoreMatchers.sameInstance) IsNot.not(org.hamcrest.core.IsNot.not) SystemProperty(org.mule.tck.junit4.rule.SystemProperty) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Executors.newFixedThreadPool(java.util.concurrent.Executors.newFixedThreadPool) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Latch(org.mule.runtime.api.util.concurrent.Latch) List(java.util.List) ConnectionException(org.mule.runtime.api.connection.ConnectionException) Rule(org.junit.Rule) Reference(org.mule.runtime.api.util.Reference) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Reference(org.mule.runtime.api.util.Reference) CountDownLatch(java.util.concurrent.CountDownLatch) Latch(org.mule.runtime.api.util.concurrent.Latch) CountDownLatch(java.util.concurrent.CountDownLatch) ExpectedException(org.junit.rules.ExpectedException) ConnectionException(org.mule.runtime.api.connection.ConnectionException) Test(org.junit.Test)

Example 22 with Latch

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

the class AbstractTransactionQueueManagerTestCase method testTakePutRollbackPut.

@Test
public void testTakePutRollbackPut() 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());
                s.begin();
                q.put("String1");
                s.rollback();
                s.begin();
                q.put("String2");
                s.commit();
            } 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", "String2", 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 23 with Latch

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

the class AbstractTransactionQueueManagerTestCase method testPutTakeUntakeRollbackUntake.

@Test
public void testPutTakeUntakeRollbackUntake() throws Exception {
    final QueueManager mgr = createQueueManager();
    mgr.start();
    final Latch latch = new Latch();
    final Serializable object1 = "string1";
    final Serializable object2 = "string2";
    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());
                s.begin();
                q.untake(object1);
                s.commit();
                s.begin();
                q.untake(object2);
                s.rollback();
            } 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", object1, o);
    assertEquals("Queue size", 0, q.size());
    assertTrue(t1 - t0 > 100);
    mgr.stop();
}
Also used : Serializable(java.io.Serializable) 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 24 with Latch

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

the class AbstractTransactionQueueManagerTestCase method testPutTakeUntake.

@Test
public void testPutTakeUntake() 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");
                q.put("String2");
            } 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());
    Serializable o = q.take();
    long t1 = System.currentTimeMillis();
    t.join();
    assertNotNull(o);
    assertEquals("Queue content", "String1", o);
    assertEquals("Queue size", 1, q.size());
    assertTrue(t1 - t0 > 100);
    // Same as put/take until now, but now we do an untake
    q.untake(o);
    // Ensure queue size is now 2
    assertEquals("Queue size", 2, q.size());
    // Take to ensure order is correct
    Object o2 = q.take();
    assertEquals("Queue content", "String1", o2);
    assertEquals("Queue size", 1, q.size());
    mgr.stop();
}
Also used : Serializable(java.io.Serializable) 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)

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