<commit_before>/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.activemq.network.jms;

import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Iterator;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.util.Wait;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class QueueBridgeStandaloneReconnectTest {

    private SimpleJmsQueueConnector jmsQueueConnector;

    private BrokerService localBroker;
    private BrokerService foreignBroker;

    private ActiveMQConnectionFactory localConnectionFactory;
    private ActiveMQConnectionFactory foreignConnectionFactory;

    private Destination outbound;
    private Destination inbound;

    private final ArrayList<Connection> connections = new ArrayList<Connection>();

    @Test
    public void testSendAndReceiveOverConnectedBridges() throws Exception {

        startLocalBroker();
        startForeignBroker();

        jmsQueueConnector.start();

        sendMessageToForeignBroker("to.foreign.broker");
        sendMessageToLocalBroker("to.local.broker");

        final MessageConsumer local = createConsumerForLocalBroker();

        assertTrue("Should have received a Message.", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                Message message = local.receive(100);
                if (message != null && ((TextMessage) message).getText().equals("to.local.broker")) {
                    return true;
                }
                return false;
            }
        }));

        final MessageConsumer foreign = createConsumerForForeignBroker();

        assertTrue("Should have received a Message.", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                Message message = foreign.receive(100);
                if (message != null && ((TextMessage) message).getText().equals("to.foreign.broker")) {
                    return true;
                }
                return false;
            }
        }));
    }

    @Test
    public void testSendAndReceiveOverBridgeWhenStartedBeforeBrokers() throws Exception {

        jmsQueueConnector.start();

        startLocalBroker();
        startForeignBroker();

        assertTrue("Should have Connected.", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                return jmsQueueConnector.isConnected();
            }
        }));

        sendMessageToForeignBroker("to.foreign.broker");
        sendMessageToLocalBroker("to.local.broker");

        final MessageConsumer local = createConsumerForLocalBroker();

        assertTrue("Should have received a Message.", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                Message message = local.receive(100);
                if (message != null && ((TextMessage) message).getText().equals("to.local.broker")) {
                    return true;
                }
                return false;
            }
        }));

        final MessageConsumer foreign = createConsumerForForeignBroker();

        assertTrue("Should have received a Message.", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                Message message = foreign.receive(100);
                if (message != null && ((TextMessage) message).getText().equals("to.foreign.broker")) {
                    return true;
                }
                return false;
            }
        }));
    }

    @Test
    public void testSendAndReceiveOverBridgeWithRestart() throws Exception {

        startLocalBroker();
        startForeignBroker();

        jmsQueueConnector.start();

        assertTrue("Should have Connected.", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                return jmsQueueConnector.isConnected();
            }
        }));

        stopLocalBroker();
        stopForeignBroker();

        assertTrue("Should have detected connection drop.", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                return !jmsQueueConnector.isConnected();
            }
        }));

        startLocalBroker();
        startForeignBroker();<commit_msg>Added timeouts because of CI hangs<commit_after>/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.activemq.network.jms;

import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Iterator;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.util.Wait;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class QueueBridgeStandaloneReconnectTest {

    private SimpleJmsQueueConnector jmsQueueConnector;

    private BrokerService localBroker;
    private BrokerService foreignBroker;

    private ActiveMQConnectionFactory localConnectionFactory;
    private ActiveMQConnectionFactory foreignConnectionFactory;

    private Destination outbound;
    private Destination inbound;

    private final ArrayList<Connection> connections = new ArrayList<Connection>();

    @Test(timeout = 60 * 1000)
    public void testSendAndReceiveOverConnectedBridges() throws Exception {

        startLocalBroker();
        startForeignBroker();

        jmsQueueConnector.start();

        sendMessageToForeignBroker("to.foreign.broker");
        sendMessageToLocalBroker("to.local.broker");

        final MessageConsumer local = createConsumerForLocalBroker();

        assertTrue("Should have received a Message.", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                Message message = local.receive(100);
                if (message != null && ((TextMessage) message).getText().equals("to.local.broker")) {
                    return true;
                }
                return false;
            }
        }));

        final MessageConsumer foreign = createConsumerForForeignBroker();

        assertTrue("Should have received a Message.", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                Message message = foreign.receive(100);
                if (message != null && ((TextMessage) message).getText().equals("to.foreign.broker")) {
                    return true;
                }
                return false;
            }
        }));
    }

    @Test(timeout = 60 * 1000)
    public void testSendAndReceiveOverBridgeWhenStartedBeforeBrokers() throws Exception {

        jmsQueueConnector.start();

        startLocalBroker();
        startForeignBroker();

        assertTrue("Should have Connected.", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                return jmsQueueConnector.isConnected();
            }
        }));

        sendMessageToForeignBroker("to.foreign.broker");
        sendMessageToLocalBroker("to.local.broker");

        final MessageConsumer local = createConsumerForLocalBroker();

        assertTrue("Should have received a Message.", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                Message message = local.receive(100);
                if (message != null && ((TextMessage) message).getText().equals("to.local.broker")) {
                    return true;
                }
                return false;
            }
        }));

        final MessageConsumer foreign = createConsumerForForeignBroker();

        assertTrue("Should have received a Message.", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                Message message = foreign.receive(100);
                if (message != null && ((TextMessage) message).getText().equals("to.foreign.broker")) {
                    return true;
                }
                return false;
            }
        }));
    }

    @Test(timeout = 60 * 1000)
    public void testSendAndReceiveOverBridgeWithRestart() throws Exception {

        startLocalBroker();
        startForeignBroker();

        jmsQueueConnector.start();

        assertTrue("Should have Connected.", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                return jmsQueueConnector.isConnected();
            }
        }));

        stopLocalBroker();
        stopForeignBroker();

        assertTrue("Should have detected connection drop.", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                return !jmsQueueConnector.isConnected();
            }
        }));

        startLocalBroker();
        startForeignBroker();
{'fraction_non_alphanumeric': 0.0889978976874562, 'fraction_numerical': 0.004379817799579537, 'mean_word_length': 2.5434512725015517, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5533384', 'n_tokens_mistral': 2842, 'n_tokens_neox': 2711, 'n_words': 758}