Search in sources :

Example 6 with ReusableLatch

use of org.apache.activemq.artemis.utils.ReusableLatch in project activemq-artemis by apache.

the class ReloadManagerTest method internalTest.

private void internalTest(ReloadManagerImpl manager, File file) throws IOException, InterruptedException {
    file.createNewFile();
    final ReusableLatch latch = new ReusableLatch(1);
    manager.addCallback(file.toURL(), new ReloadCallback() {

        @Override
        public void reload(URL uri) {
            latch.countDown();
        }
    });
    Assert.assertFalse(latch.await(1, TimeUnit.SECONDS));
    file.setLastModified(System.currentTimeMillis());
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : ReusableLatch(org.apache.activemq.artemis.utils.ReusableLatch) ReloadCallback(org.apache.activemq.artemis.core.server.reload.ReloadCallback) URL(java.net.URL)

Example 7 with ReusableLatch

use of org.apache.activemq.artemis.utils.ReusableLatch in project activemq-artemis by apache.

the class JmsConsumerTest method testIndividualACKMessageConsumer.

@Test
public void testIndividualACKMessageConsumer() throws Exception {
    Connection conn = cf.createConnection();
    Session session = conn.createSession(false, ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE);
    jBossQueue = ActiveMQJMSClient.createQueue(JmsConsumerTest.Q_NAME);
    MessageProducer producer = session.createProducer(jBossQueue);
    MessageConsumer consumer = session.createConsumer(jBossQueue);
    int noOfMessages = 100;
    for (int i = 0; i < noOfMessages; i++) {
        producer.setPriority(2);
        producer.send(session.createTextMessage("m" + i));
    }
    conn.start();
    final AtomicInteger errors = new AtomicInteger(0);
    final ReusableLatch latch = new ReusableLatch();
    latch.setCount(noOfMessages);
    class MessageAckEven implements MessageListener {

        int count = 0;

        @Override
        public void onMessage(Message msg) {
            try {
                TextMessage txtmsg = (TextMessage) msg;
                if (!txtmsg.getText().equals("m" + count)) {
                    errors.incrementAndGet();
                }
                if (count % 2 == 0) {
                    msg.acknowledge();
                }
                count++;
            } catch (Exception e) {
                errors.incrementAndGet();
            } finally {
                latch.countDown();
            }
        }
    }
    consumer.setMessageListener(new MessageAckEven());
    Assert.assertTrue(latch.await(5000));
    session.close();
    session = conn.createSession(false, ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE);
    consumer = session.createConsumer(jBossQueue);
    // Consume odd numbers first
    for (int i = 0; i < noOfMessages; i++) {
        if (i % 2 == 0) {
            continue;
        }
        TextMessage m = (TextMessage) consumer.receive(1000);
        Assert.assertNotNull(m);
        m.acknowledge();
        Assert.assertEquals("m" + i, m.getText());
    }
    SimpleString queueName = new SimpleString(JmsConsumerTest.Q_NAME);
    conn.close();
    Queue queue = server.locateQueue(queueName);
    Wait.assertEquals(0, queue::getDeliveringCount);
    Wait.assertEquals(0, queue::getMessageCount);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JMSException(javax.jms.JMSException) ReusableLatch(org.apache.activemq.artemis.utils.ReusableLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageProducer(javax.jms.MessageProducer) Queue(org.apache.activemq.artemis.core.server.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 8 with ReusableLatch

use of org.apache.activemq.artemis.utils.ReusableLatch in project activemq-artemis by apache.

the class ClusteredResetMockTest method testMultipleSenders.

@Test
public void testMultipleSenders() throws Throwable {
    int NUMBER_OF_SENDERS = 100;
    ReusableLatch latchSends = new ReusableLatch(NUMBER_OF_SENDERS);
    FakeManagement fake = new FakeManagement(latchSends);
    RemoteGroupingHandler handler = new RemoteGroupingHandler(fake, SimpleString.toSimpleString("tst1"), SimpleString.toSimpleString("tst2"), 50000, 499);
    handler.start();
    Sender[] sn = new Sender[NUMBER_OF_SENDERS];
    for (int i = 0; i < sn.length; i++) {
        sn[i] = new Sender("grp" + i, handler);
        sn[i].start();
    }
    try {
        // Waiting two requests to arrive
        Assert.assertTrue(latchSends.await(1, TimeUnit.MINUTES));
        // we will ask a resend.. need to add 2 back
        for (int i = 0; i < NUMBER_OF_SENDERS; i++) {
            // There is no countUp(NUMBER_OF_SENDERS); adding two back on the reusable latch
            latchSends.countUp();
        }
        fake.pendingNotifications.clear();
        handler.resendPending();
        assertTrue(latchSends.await(10, TimeUnit.SECONDS));
        HashSet<SimpleString> codesAsked = new HashSet<>();
        for (Notification notification : fake.pendingNotifications) {
            codesAsked.add(notification.getProperties().getSimpleStringProperty(ManagementHelper.HDR_PROPOSAL_GROUP_ID));
        }
        for (Sender snItem : sn) {
            assertTrue(codesAsked.contains(snItem.code));
        }
        for (int i = NUMBER_OF_SENDERS - 1; i >= 0; i--) {
            // Sending back the response as Notifications would be doing
            Response response = new Response(sn[i].code, ANYCLUSTER);
            handler.proposed(response);
        }
        for (Sender sni : sn) {
            sni.join();
            if (sni.ex != null) {
                throw sni.ex;
            }
        }
    } finally {
        for (Sender sni : sn) {
            sni.interrupt();
        }
    }
}
Also used : ReusableLatch(org.apache.activemq.artemis.utils.ReusableLatch) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Notification(org.apache.activemq.artemis.core.server.management.Notification) HashSet(java.util.HashSet) ConcurrentHashSet(org.apache.activemq.artemis.utils.collections.ConcurrentHashSet) Test(org.junit.Test)

Example 9 with ReusableLatch

use of org.apache.activemq.artemis.utils.ReusableLatch in project activemq-artemis by apache.

the class ReusableLatchTest method testLatchOnMultiThread.

/**
 * This test will open numberOfThreads threads, and add numberOfAdds on the
 * VariableLatch After those addthreads are finished, the latch count should
 * be numberOfThreads * numberOfAdds Then it will open numberOfThreads
 * threads again releasing numberOfAdds on the VariableLatch After those
 * releaseThreads are finished, the latch count should be 0 And all the
 * waiting threads should be finished also
 *
 * @throws Exception
 */
@Test
public void testLatchOnMultiThread() throws Exception {
    final ReusableLatch latch = new ReusableLatch();
    // We hold at least one, so ThreadWaits won't go away
    latch.countUp();
    final int numberOfThreads = 100;
    final int numberOfAdds = 100;
    class ThreadWait extends Thread {

        private volatile boolean waiting = true;

        @Override
        public void run() {
            try {
                if (!latch.await(5000)) {
                    UnitTestLogger.LOGGER.error("Latch timed out");
                }
            } catch (Exception e) {
                UnitTestLogger.LOGGER.error(e);
            }
            waiting = false;
        }
    }
    class ThreadAdd extends Thread {

        private final CountDownLatch latchReady;

        private final CountDownLatch latchStart;

        ThreadAdd(final CountDownLatch latchReady, final CountDownLatch latchStart) {
            this.latchReady = latchReady;
            this.latchStart = latchStart;
        }

        @Override
        public void run() {
            try {
                latchReady.countDown();
                // Everybody should start at the same time, to worse concurrency
                // effects
                latchStart.await();
                for (int i = 0; i < numberOfAdds; i++) {
                    latch.countUp();
                }
            } catch (Exception e) {
                UnitTestLogger.LOGGER.error(e.getMessage(), e);
            }
        }
    }
    CountDownLatch latchReady = new CountDownLatch(numberOfThreads);
    CountDownLatch latchStart = new CountDownLatch(1);
    ThreadAdd[] threadAdds = new ThreadAdd[numberOfThreads];
    ThreadWait[] waits = new ThreadWait[numberOfThreads];
    for (int i = 0; i < numberOfThreads; i++) {
        threadAdds[i] = new ThreadAdd(latchReady, latchStart);
        threadAdds[i].start();
        waits[i] = new ThreadWait();
        waits[i].start();
    }
    latchReady.await();
    latchStart.countDown();
    for (int i = 0; i < numberOfThreads; i++) {
        threadAdds[i].join();
    }
    for (int i = 0; i < numberOfThreads; i++) {
        Assert.assertTrue(waits[i].waiting);
    }
    Assert.assertEquals(numberOfThreads * numberOfAdds + 1, latch.getCount());
    class ThreadDown extends Thread {

        private final CountDownLatch latchReady;

        private final CountDownLatch latchStart;

        ThreadDown(final CountDownLatch latchReady, final CountDownLatch latchStart) {
            this.latchReady = latchReady;
            this.latchStart = latchStart;
        }

        @Override
        public void run() {
            try {
                latchReady.countDown();
                // Everybody should start at the same time, to worse concurrency
                // effects
                latchStart.await();
                for (int i = 0; i < numberOfAdds; i++) {
                    latch.countDown();
                }
            } catch (Exception e) {
                UnitTestLogger.LOGGER.error(e.getMessage(), e);
            }
        }
    }
    latchReady = new CountDownLatch(numberOfThreads);
    latchStart = new CountDownLatch(1);
    ThreadDown[] down = new ThreadDown[numberOfThreads];
    for (int i = 0; i < numberOfThreads; i++) {
        down[i] = new ThreadDown(latchReady, latchStart);
        down[i].start();
    }
    latchReady.await();
    latchStart.countDown();
    for (int i = 0; i < numberOfThreads; i++) {
        down[i].join();
    }
    Assert.assertEquals(1, latch.getCount());
    for (int i = 0; i < numberOfThreads; i++) {
        Assert.assertTrue(waits[i].waiting);
    }
    latch.countDown();
    for (int i = 0; i < numberOfThreads; i++) {
        waits[i].join();
    }
    Assert.assertEquals(0, latch.getCount());
    for (int i = 0; i < numberOfThreads; i++) {
        Assert.assertFalse(waits[i].waiting);
    }
}
Also used : ReusableLatch(org.apache.activemq.artemis.utils.ReusableLatch) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 10 with ReusableLatch

use of org.apache.activemq.artemis.utils.ReusableLatch in project activemq-artemis by apache.

the class ReusableLatchTest method testReuseLatch.

@Test
public void testReuseLatch() throws Exception {
    final ReusableLatch latch = new ReusableLatch(5);
    for (int i = 0; i < 5; i++) {
        latch.countDown();
    }
    latch.countUp();
    class ThreadWait extends Thread {

        private volatile boolean waiting = false;

        private volatile Exception e;

        private final CountDownLatch readyLatch = new CountDownLatch(1);

        @Override
        public void run() {
            waiting = true;
            readyLatch.countDown();
            try {
                if (!latch.await(1000)) {
                    UnitTestLogger.LOGGER.error("Latch timed out!", new Exception("trace"));
                }
            } catch (Exception e) {
                UnitTestLogger.LOGGER.error(e);
                this.e = e;
            }
            waiting = false;
        }
    }
    ThreadWait t = new ThreadWait();
    t.start();
    t.readyLatch.await();
    Assert.assertEquals(true, t.waiting);
    latch.countDown();
    t.join();
    Assert.assertEquals(false, t.waiting);
    Assert.assertNull(t.e);
    latch.countUp();
    t = new ThreadWait();
    t.start();
    t.readyLatch.await();
    Assert.assertEquals(true, t.waiting);
    latch.countDown();
    t.join();
    Assert.assertEquals(false, t.waiting);
    Assert.assertNull(t.e);
    Assert.assertTrue(latch.await(1000));
    Assert.assertEquals(0, latch.getCount());
    latch.countDown();
    Assert.assertEquals(0, latch.getCount());
}
Also used : ReusableLatch(org.apache.activemq.artemis.utils.ReusableLatch) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ReusableLatch (org.apache.activemq.artemis.utils.ReusableLatch)15 Test (org.junit.Test)12 URL (java.net.URL)4 Path (java.nio.file.Path)3 EmbeddedJMS (org.apache.activemq.artemis.jms.server.embedded.EmbeddedJMS)3 ByteBuffer (java.nio.ByteBuffer)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Connection (javax.jms.Connection)2 MessageConsumer (javax.jms.MessageConsumer)2 MessageProducer (javax.jms.MessageProducer)2 Session (javax.jms.Session)2 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 IOCallback (org.apache.activemq.artemis.core.io.IOCallback)2 IOException (java.io.IOException)1 FileStore (java.nio.file.FileStore)1 DecimalFormat (java.text.DecimalFormat)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1