org.apache.activemq.artemis.utils.ReusableLatch

Here are the examples of the java api class org.apache.activemq.artemis.utils.ReusableLatch taken from open source projects.

1. ReusableLatchTest#testLatchOnSingleThread()

Project: activemq-artemis
File: ReusableLatchTest.java
@Test
public void testLatchOnSingleThread() throws Exception {
    ReusableLatch latch = new ReusableLatch();
    for (int i = 1; i <= 100; i++) {
        latch.countUp();
        Assert.assertEquals(i, latch.getCount());
    }
    for (int i = 100; i > 0; i--) {
        Assert.assertEquals(i, latch.getCount());
        latch.countDown();
        Assert.assertEquals(i - 1, latch.getCount());
    }
    latch.await();
}

2. ReusableLatchTest#testTimeout()

Project: activemq-artemis
File: ReusableLatchTest.java
@Test
public void testTimeout() throws Exception {
    ReusableLatch latch = new ReusableLatch();
    latch.countUp();
    long start = System.currentTimeMillis();
    Assert.assertFalse(latch.await(1000));
    long end = System.currentTimeMillis();
    Assert.assertTrue("Timeout didn't work correctly", end - start >= 1000 && end - start < 2000);
}

3. MixupCompactorTestBase#setUp()

Project: activemq-artemis
File: MixupCompactorTestBase.java
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    tCompact = null;
    startedCompactingLatch = new ReusableLatch(1);
    releaseCompactingLatch = new ReusableLatch(1);
    setup(2, 60 * 1024, false);
}

4. ReusableLatchTest#testReuseLatch()

Project: activemq-artemis
File: ReusableLatchTest.java
@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());
}

5. ReusableLatchTest#testLatchOnMultiThread()

Project: activemq-artemis
File: ReusableLatchTest.java
/**
    * 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);
    }
}

6. ReusableLatchTest#testLatchWithParameterizedDown()

Project: activemq-artemis
File: ReusableLatchTest.java
@Test
public void testLatchWithParameterizedDown() throws Exception {
    ReusableLatch latch = new ReusableLatch(1000);
    latch.countDown(5000);
    assertTrue(latch.await(1000));
    assertEquals(0, latch.getCount());
}

7. ConsumerTest#testIndividualACKMessageConsumer()

Project: activemq-artemis
File: ConsumerTest.java
@Test
public void testIndividualACKMessageConsumer() throws Exception {
    Connection conn = cf.createConnection();
    Session session = conn.createSession(false, ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE);
    jBossQueue = ActiveMQJMSClient.createQueue(ConsumerTest.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(ActiveMQDestination.JMS_QUEUE_ADDRESS_PREFIX + ConsumerTest.Q_NAME);
    Assert.assertEquals(0, ((Queue) server.getPostOffice().getBinding(queueName).getBindable()).getDeliveringCount());
    Assert.assertEquals(0, getMessageCount((Queue) server.getPostOffice().getBinding(queueName).getBindable()));
    conn.close();
}

8. ClusteredResetMockTest#testMultipleSenders()

Project: activemq-artemis
File: ClusteredResetMockTest.java
@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();
        }
    }
}