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));
}
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);
}
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();
}
}
}
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);
}
}
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());
}
Aggregations