Search in sources :

Example 1 with Wait

use of org.apache.activemq.util.Wait in project activemq-artemis by apache.

the class AbstractCachedLDAPAuthorizationMapLegacyTest method testRestart.

public void testRestart(final boolean sync) throws Exception {
    if (sync) {
        // ldap connection can be slow to close
        map.setRefreshInterval(1000);
    }
    map.query();
    Set<?> failedACLs = map.getReadACLs(new ActiveMQQueue("FAILED"));
    assertEquals("set size: " + failedACLs, 0, failedACLs.size());
    failedACLs = map.getReadACLs(new ActiveMQQueue("TEST.FOO"));
    assertEquals("set size: " + failedACLs, 2, failedACLs.size());
    getLdapServer().stop();
    // wait for the context to be closed
    // as we can't rely on ldar server isStarted()
    Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            if (sync) {
                return !map.isContextAlive();
            } else {
                return map.context == null;
            }
        }
    });
    failedACLs = map.getReadACLs(new ActiveMQQueue("TEST.FOO"));
    assertEquals("set size: " + failedACLs, 2, failedACLs.size());
    getLdapServer().start();
    Thread.sleep(2000);
    connection = getLdapConnection();
    LdifReader reader = new LdifReader(getAddLdif());
    for (LdifEntry entry : reader) {
        connection.add(entry.getEntry());
    }
    reader.close();
    assertTrue("did not get expected size. ", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return map.getReadACLs(new ActiveMQQueue("FAILED")).size() == 2;
        }
    }));
}
Also used : LdifReader(org.apache.directory.shared.ldap.model.ldif.LdifReader) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Wait(org.apache.activemq.util.Wait) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) LdifEntry(org.apache.directory.shared.ldap.model.ldif.LdifEntry)

Example 2 with Wait

use of org.apache.activemq.util.Wait in project activemq-artemis by apache.

the class TwoBrokerQueueClientsReconnectTest method testDuplicateSendWithNoAuditEnqueueCountStat.

public void testDuplicateSendWithNoAuditEnqueueCountStat() throws Exception {
    broker1 = "BrokerA";
    broker2 = "BrokerB";
    NetworkConnector networkConnector = bridgeBrokers(broker1, broker2);
    final AtomicBoolean first = new AtomicBoolean();
    final CountDownLatch gotMessageLatch = new CountDownLatch(1);
    BrokerService brokerService = brokers.get(broker2).broker;
    brokerService.setPersistent(true);
    brokerService.setDeleteAllMessagesOnStartup(true);
    // disable concurrent dispatch otherwise store duplicate suppression will be skipped b/c cursor audit is already
    // disabled so verification of stats will fail - ie: duplicate will be dispatched
    ((KahaDBPersistenceAdapter) brokerService.getPersistenceAdapter()).setConcurrentStoreAndDispatchQueues(false);
    brokerService.setPlugins(new BrokerPlugin[] { new BrokerPluginSupport() {

        @Override
        public void send(final ProducerBrokerExchange producerExchange, org.apache.activemq.command.Message messageSend) throws Exception {
            super.send(producerExchange, messageSend);
            if (first.compareAndSet(false, true)) {
                producerExchange.getConnectionContext().setDontSendReponse(true);
                new Thread() {

                    @Override
                    public void run() {
                        try {
                            LOG.info("Waiting for recepit");
                            assertTrue("message received on time", gotMessageLatch.await(60, TimeUnit.SECONDS));
                            LOG.info("Stopping connection post send and receive and multiple producers");
                            producerExchange.getConnectionContext().getConnection().stop();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        }
    } });
    // Create queue
    ActiveMQDestination dest = createDestination("TEST.FOO", false);
    // statically include our destination
    networkConnector.addStaticallyIncludedDestination(dest);
    // Run brokers
    startAllBrokers();
    waitForBridgeFormation();
    sendMessages("BrokerA", dest, 1);
    // wait for broker2 to get the initial forward
    Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return brokers.get(broker2).broker.getAdminView().getTotalMessageCount() == 1;
        }
    });
    // message still pending on broker1
    assertEquals("messages message still there", 1, brokers.get(broker1).broker.getAdminView().getTotalMessageCount());
    // allow the bridge to be shutdown and restarted
    gotMessageLatch.countDown();
    // verify message is forwarded after restart
    assertTrue("no messages enqueued on origin", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return 0 == brokers.get(broker1).broker.getAdminView().getTotalMessageCount();
        }
    }));
    assertEquals("one messages pending", 1, brokers.get(broker2).broker.getAdminView().getTotalMessageCount());
    assertEquals("one messages enqueued", 1, brokers.get(broker2).broker.getDestination(dest).getDestinationStatistics().getEnqueues().getCount());
}
Also used : ProducerBrokerExchange(org.apache.activemq.broker.ProducerBrokerExchange) BrokerPluginSupport(org.apache.activemq.broker.BrokerPluginSupport) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NetworkConnector(org.apache.activemq.network.NetworkConnector) Wait(org.apache.activemq.util.Wait) BrokerService(org.apache.activemq.broker.BrokerService) KahaDBPersistenceAdapter(org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter)

Example 3 with Wait

use of org.apache.activemq.util.Wait in project activemq-artemis by apache.

the class BrokerQueueNetworkWithDisconnectTest method testNoStuckConnectionsWithTransportDisconnect.

public void testNoStuckConnectionsWithTransportDisconnect() throws Exception {
    inactiveDuration = 60000L;
    useDuplexNetworkBridge = true;
    bridgeBrokers(SPOKE, HUB);
    final BrokerItem hub = brokers.get(HUB);
    hub.broker.setPlugins(new BrokerPlugin[] { new BrokerPluginSupport() {

        int sleepCount = 2;

        @Override
        public void removeConnection(ConnectionContext context, ConnectionInfo info, Throwable error) throws Exception {
            try {
                while (--sleepCount >= 0) {
                    LOG.info("sleeping for a bit in close impl to simulate load where reconnect fails due to a pending close");
                    TimeUnit.SECONDS.sleep(2);
                }
            } catch (Exception ignored) {
            }
            super.removeConnection(context, info, error);
        }
    } });
    startAllBrokers();
    waitForBridgeFormation();
    // remote side will need to spot duplicate network and stop/kill the original
    for (int i = 0; i < 3; i++) {
        socketProxy.halfClose();
        sleep(10000);
    }
    // wait for full reformation of bridge
    // verify no extra connections
    boolean allGood = Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            long numConnections = hub.broker.getTransportConnectors().get(0).getConnections().size();
            LOG.info("Num connetions:" + numConnections);
            return numConnections == 1;
        }
    });
    if (!allGood) {
        dumpAllThreads("ExtraHubConnection");
    }
    assertTrue("should be only one transport connection for the single duplex network connector", allGood);
    allGood = Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            long numVmConnections = VMTransportFactory.SERVERS.get(HUB).getConnectionCount();
            LOG.info("Num VM connetions:" + numVmConnections);
            return numVmConnections == 2;
        }
    });
    if (!allGood) {
        dumpAllThreads("ExtraHubVMConnection");
    }
    assertTrue("should be only 2 vm connections for the single network duplex network connector", allGood);
}
Also used : BrokerPluginSupport(org.apache.activemq.broker.BrokerPluginSupport) ConnectionContext(org.apache.activemq.broker.ConnectionContext) ConnectionInfo(org.apache.activemq.command.ConnectionInfo) Wait(org.apache.activemq.util.Wait)

Example 4 with Wait

use of org.apache.activemq.util.Wait in project activemq-artemis by apache.

the class DurableSubDelayedUnsubscribeTest method testProcess.

@Test
public void testProcess() throws Exception {
    server.start();
    clientManager.start();
    houseKeeper.start();
    // Sleep to
    Thread.sleep(RUNTIME);
    // inform message producer to stop
    server.stopped = true;
    // add one Subscriber to the topic that will not be unsubscribed.
    // should not have any pending messages in the kahadb store
    Client lastClient = new Client(32000, ClientType.A);
    lastClient.process(1000);
    // stop client manager from creating any more clients
    clientManager.stopped = true;
    final BrokerService brokerService = this.broker;
    // Wait for all client subscription to be unsubscribed or swept away.
    // Ensure we sleep longer than the housekeeper's sweep delay otherwise we can
    // miss the fact that all durables that were abandoned do finally get cleaned up.
    // Wait for all clients to stop
    Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return clientManager.getClientCount() == 0;
        }
    }, Client.lifetime + TimeUnit.SECONDS.toMillis(10));
    assertTrue("should have only one inactiveSubscriber subscribed but was: " + brokerService.getAdminView().getInactiveDurableTopicSubscribers().length, Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return brokerService.getAdminView().getInactiveDurableTopicSubscribers().length == 1;
        }
    }, houseKeeper.SWEEP_DELAY * 2));
    assertTrue("should be no subscribers subscribed but was: " + brokerService.getAdminView().getDurableTopicSubscribers().length, Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return brokerService.getAdminView().getDurableTopicSubscribers().length == 0;
        }
    }, TimeUnit.MINUTES.toMillis(3)));
    processLock.writeLock().lock();
    // check outcome.
    ObjectName[] subscribers = broker.getAdminView().getDurableTopicSubscribers();
    ObjectName[] inactiveSubscribers = broker.getAdminView().getInactiveDurableTopicSubscribers();
    final KahaDBPersistenceAdapter persistenceAdapter = (KahaDBPersistenceAdapter) broker.getPersistenceAdapter();
    printDebugClientInfo(subscribers, inactiveSubscribers, persistenceAdapter);
    assertEquals("should have only one inactiveSubscriber subscribed", 1, broker.getAdminView().getInactiveDurableTopicSubscribers().length);
    assertEquals("should be no subscribers subscribed", 0, broker.getAdminView().getDurableTopicSubscribers().length);
    final KahaDBPersistenceAdapter pa = (KahaDBPersistenceAdapter) broker.getPersistenceAdapter();
    assertTrue("should be less than 3 journal file left but was: " + persistenceAdapter.getStore().getJournal().getFileMap().size(), Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return pa.getStore().getJournal().getFileMap().size() <= 3;
        }
    }, TimeUnit.MINUTES.toMillis(3)));
    // Be good and cleanup our mess a bit.
    this.houseKeeper.shutdown();
    assertTrue("no exceptions: " + exceptions, exceptions.isEmpty());
    LOG.info("DONE.");
}
Also used : Wait(org.apache.activemq.util.Wait) BrokerService(org.apache.activemq.broker.BrokerService) IOException(java.io.IOException) JMSException(javax.jms.JMSException) ObjectName(javax.management.ObjectName) KahaDBPersistenceAdapter(org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter) Test(org.junit.Test)

Example 5 with Wait

use of org.apache.activemq.util.Wait in project activemq-artemis by apache.

the class ExpiredMessagesTest method testExpiredMessages.

public void testExpiredMessages() throws Exception {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUri);
    connection = factory.createConnection();
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    producer = session.createProducer(destination);
    producer.setTimeToLive(100);
    consumer = session.createConsumer(destination);
    connection.start();
    final AtomicLong received = new AtomicLong();
    Thread consumerThread = new Thread("Consumer Thread") {

        @Override
        public void run() {
            long start = System.currentTimeMillis();
            try {
                long end = System.currentTimeMillis();
                while (end - start < 3000) {
                    if (consumer.receive(1000) != null) {
                        received.incrementAndGet();
                    }
                    Thread.sleep(100);
                    end = System.currentTimeMillis();
                }
                consumer.close();
            } catch (Throwable ex) {
                ex.printStackTrace();
            }
        }
    };
    consumerThread.start();
    final int numMessagesToSend = 10000;
    Thread producingThread = new Thread("Producing Thread") {

        @Override
        public void run() {
            try {
                int i = 0;
                while (i++ < numMessagesToSend) {
                    producer.send(session.createTextMessage("test"));
                }
                producer.close();
            } catch (Throwable ex) {
                ex.printStackTrace();
            }
        }
    };
    producingThread.start();
    consumerThread.join();
    producingThread.join();
    session.close();
    final DestinationStatistics view = getDestinationStatistics(broker, destination);
    // wait for all to inflight to expire
    assertTrue("all inflight messages expired ", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return view.getInflight().getCount() == 0;
        }
    }));
    assertEquals("Wrong inFlightCount: ", 0, view.getInflight().getCount());
    LOG.info("Stats: received: " + received.get() + ", enqueues: " + view.getEnqueues().getCount() + ", dequeues: " + view.getDequeues().getCount() + ", dispatched: " + view.getDispatched().getCount() + ", inflight: " + view.getInflight().getCount() + ", expiries: " + view.getExpired().getCount());
    // wait for all sent to get delivered and expire
    assertTrue("all sent messages expired ", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            long oldEnqueues = view.getEnqueues().getCount();
            Thread.sleep(200);
            LOG.info("Stats: received: " + received.get() + ", size= " + view.getMessages().getCount() + ", enqueues: " + view.getDequeues().getCount() + ", dequeues: " + view.getDequeues().getCount() + ", dispatched: " + view.getDispatched().getCount() + ", inflight: " + view.getInflight().getCount() + ", expiries: " + view.getExpired().getCount());
            return oldEnqueues == view.getEnqueues().getCount();
        }
    }, 60 * 1000));
    LOG.info("Stats: received: " + received.get() + ", size= " + view.getMessages().getCount() + ", enqueues: " + view.getEnqueues().getCount() + ", dequeues: " + view.getDequeues().getCount() + ", dispatched: " + view.getDispatched().getCount() + ", inflight: " + view.getInflight().getCount() + ", expiries: " + view.getExpired().getCount());
    assertTrue("got at least what did not expire", received.get() >= view.getDequeues().getCount() - view.getExpired().getCount());
    assertTrue("all messages expired - queue size gone to zero " + view.getMessages().getCount(), Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            LOG.info("Stats: received: " + received.get() + ", size= " + view.getMessages().getCount() + ", enqueues: " + view.getEnqueues().getCount() + ", dequeues: " + view.getDequeues().getCount() + ", dispatched: " + view.getDispatched().getCount() + ", inflight: " + view.getInflight().getCount() + ", expiries: " + view.getExpired().getCount());
            return view.getMessages().getCount() == 0;
        }
    }));
    final long expiredBeforeEnqueue = numMessagesToSend - view.getEnqueues().getCount();
    final long totalExpiredCount = view.getExpired().getCount() + expiredBeforeEnqueue;
    final DestinationStatistics dlqView = getDestinationStatistics(broker, dlqDestination);
    LOG.info("DLQ stats: size= " + dlqView.getMessages().getCount() + ", enqueues: " + dlqView.getDequeues().getCount() + ", dequeues: " + dlqView.getDequeues().getCount() + ", dispatched: " + dlqView.getDispatched().getCount() + ", inflight: " + dlqView.getInflight().getCount() + ", expiries: " + dlqView.getExpired().getCount());
    Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return totalExpiredCount == dlqView.getMessages().getCount();
        }
    });
    assertEquals("dlq contains all expired", totalExpiredCount, dlqView.getMessages().getCount());
    // memory check
    assertEquals("memory usage is back to duck egg", 0, getDestination(broker, destination).getMemoryUsage().getPercentUsage());
    assertTrue("memory usage is increased ", 0 < getDestination(broker, dlqDestination).getMemoryUsage().getUsage());
    // verify DLQ
    MessageConsumer dlqConsumer = createDlqConsumer(connection);
    final DLQListener dlqListener = new DLQListener();
    dlqConsumer.setMessageListener(dlqListener);
    Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return totalExpiredCount == dlqListener.count;
        }
    }, 60 * 1000);
    assertEquals("dlq returned all expired", dlqListener.count, totalExpiredCount);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TestSupport.getDestinationStatistics(org.apache.activemq.TestSupport.getDestinationStatistics) DestinationStatistics(org.apache.activemq.broker.region.DestinationStatistics) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) Wait(org.apache.activemq.util.Wait)

Aggregations

Wait (org.apache.activemq.util.Wait)8 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)4 Connection (javax.jms.Connection)3 Session (javax.jms.Session)3 ProducerThread (org.apache.activemq.util.ProducerThread)3 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Destination (javax.jms.Destination)2 JMSException (javax.jms.JMSException)2 Message (javax.jms.Message)2 MessageConsumer (javax.jms.MessageConsumer)2 ScheduledMessage (org.apache.activemq.ScheduledMessage)2 BrokerPluginSupport (org.apache.activemq.broker.BrokerPluginSupport)2 BrokerService (org.apache.activemq.broker.BrokerService)2 KahaDBPersistenceAdapter (org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter)2 Test (org.junit.Test)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 MessageListener (javax.jms.MessageListener)1