<commit_before>#region License

/*
 * Copyright  2002-2007 the original author or authors.
 *
 * Licensed 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.
 */

#endregion

#region Imports

using System.Messaging;
using System.Threading;
using NUnit.Framework;
using Spring.Messaging.Core;
using Spring.Testing.NUnit;
using System;

#endregion

namespace Spring.Messaging.Listener
{
    /// <summary>
    /// This class contains tests for DistributedTxMessageListenerContainer
    /// </summary>
    /// <author><NAME></author>
    [TestFixture]
    public class DistributedTxMessageListenerContainerTests : AbstractDependencyInjectionSpringContextTests
    {


        private int waitInMillis = 20000;
        private DistributedTxMessageListenerContainer distributedTxMessageListenerContainer;
        private SimpleHandler listener;

        [SetUp]
        public override void SetUp()
        {
            MessageQueueUtils.RecreateMessageQueue(@".\Private$\testtxqueue", true);
            MessageQueueUtils.RecreateMessageQueue(@".\Private$\testtxretryqueue", true);
            MessageQueueUtils.RecreateMessageQueue(@".\Private$\testtxresponsequeue", true);


            if (listener != null)
                listener.MessageCount = 0; //reset the property between tests b/c the object lifecycle is singleton!

            base.SetUp();
        }


        public DistributedTxMessageListenerContainer DistributedTxMessageListenerContainer
        {
            set { distributedTxMessageListenerContainer = value; }
        }


        public SimpleHandler Listener
        {
            set { listener = value; }
        }

        [Test]
        public void SendAndAsyncReceiveWithExceptionHandling()
        {
            MessageQueueTemplate q = applicationContext["queueTemplate"] as MessageQueueTemplate;
            Assert.IsNotNull(q);

            MessageQueueTemplate retryQ = applicationContext["retryQueueTemplate"] as MessageQueueTemplate;
            Assert.IsNotNull(retryQ);

            q.ConvertAndSend("Goodbye World 1");

            Assert.AreEqual(0, listener.MessageCount, "PRECONDITION FAILURE: Unable to send the message!");

            distributedTxMessageListenerContainer.Start();

            Thread.Sleep(waitInMillis);

            distributedTxMessageListenerContainer.Stop();
            distributedTxMessageListenerContainer.Shutdown();
            Thread.Sleep(2500);

            object msg = retryQ.ReceiveAndConvert();
            Assert.IsNotNull(msg);
            string textMsg = msg as string;
            Assert.IsNotNull(textMsg);
            Assert.AreEqual("Goodbye World 1", textMsg);
        }

        [Test]
        public void SendAndAsyncReceive()
        {
            const int MESSAGE_COUNT = 5;

            //must match the retry count in the object registration for test to pass!
            const int EXCEPTION_QUEUE_RETRY_COUNT = 2;

            int expectedMessageCount = MESSAGE_COUNT; // +(MESSAGE_COUNT * EXCEPTION_QUEUE_RETRY_COUNT);

            MessageQueueTemplate q = applicationContext["queueTemplate"] as MessageQueueTemplate;
            Assert.IsNotNull(q);

            for (int i = 0; i < MESSAGE_COUNT; i++)
            {
                q.ConvertAndSend(String.Format("Hello World {0}", (i + 1)));
            }

            Assert.AreEqual(0, listener.MessageCount);

            distributedTxMessageListenerContainer.Start();

            Thread.Sleep(10000);
            /*
            System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            timer.Start();
            
            while (listener.MessageCount < expectedMessageCount)
            {
                if (timer.ElapsedMilliseconds > 10000)
                {<commit_msg>some misc test code cleanup<commit_after>#region License

/*
 * Copyright  2002-2007 the original author or authors.
 *
 * Licensed 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.
 */

#endregion

#region Imports

using System.Messaging;
using System.Threading;
using NUnit.Framework;
using Spring.Messaging.Core;
using Spring.Testing.NUnit;
using System;

#endregion

namespace Spring.Messaging.Listener
{
    /// <summary>
    /// This class contains tests for DistributedTxMessageListenerContainer
    /// </summary>
    /// <author><NAME></author>
    [TestFixture]
    public class DistributedTxMessageListenerContainerTests : AbstractDependencyInjectionSpringContextTests
    {


        private int waitInMillis = 20000;
        private DistributedTxMessageListenerContainer distributedTxMessageListenerContainer;
        private SimpleHandler listener;

        [SetUp]
        public override void SetUp()
        {
            MessageQueueUtils.RecreateMessageQueue(@".\Private$\testtxqueue", true);
            MessageQueueUtils.RecreateMessageQueue(@".\Private$\testtxretryqueue", true);
            MessageQueueUtils.RecreateMessageQueue(@".\Private$\testtxresponsequeue", true);


            if (listener != null)
                listener.MessageCount = 0; //reset the property between tests b/c the object lifecycle is singleton!

            base.SetUp();
        }


        public DistributedTxMessageListenerContainer DistributedTxMessageListenerContainer
        {
            set { distributedTxMessageListenerContainer = value; }
        }


        public SimpleHandler Listener
        {
            set { listener = value; }
        }

        [Test]
        public void SendAndAsyncReceiveWithExceptionHandling()
        {
            MessageQueueTemplate q = applicationContext["queueTemplate"] as MessageQueueTemplate;
            Assert.IsNotNull(q);

            MessageQueueTemplate retryQ = applicationContext["retryQueueTemplate"] as MessageQueueTemplate;
            Assert.IsNotNull(retryQ);

            q.ConvertAndSend("Goodbye World 1");

            Assert.AreEqual(0, listener.MessageCount, "PRECONDITION FAILURE: Unable to send the message!");

            distributedTxMessageListenerContainer.Start();

            Thread.Sleep(waitInMillis);

            distributedTxMessageListenerContainer.Stop();
            distributedTxMessageListenerContainer.Shutdown();
            Thread.Sleep(2500);

            object msg = retryQ.ReceiveAndConvert();
            Assert.IsNotNull(msg);
            string textMsg = msg as string;
            Assert.IsNotNull(textMsg);
            Assert.AreEqual("Goodbye World 1", textMsg);
        }

        [Test]
        public void SendAndAsyncReceive()
        {
            const int MESSAGE_COUNT = 5;

            //must match the retry count in the object registration for test to pass!
            //const int EXCEPTION_QUEUE_RETRY_COUNT = 2;

            int expectedMessageCount = MESSAGE_COUNT; // +(MESSAGE_COUNT * EXCEPTION_QUEUE_RETRY_COUNT);

            MessageQueueTemplate q = applicationContext["queueTemplate"] as MessageQueueTemplate;
            Assert.IsNotNull(q);

            for (int i = 0; i < MESSAGE_COUNT; i++)
            {
                q.ConvertAndSend(String.Format("Hello World {0}", (i + 1)));
            }

            Assert.AreEqual(0, listener.MessageCount);

            distributedTxMessageListenerContainer.Start();

            Thread.Sleep(10000);
            /*
            System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            timer.Start();
            
            while (listener.MessageCount < expectedMessageCount)
            {
                if (timer.ElapsedMilliseconds > 10000)
                {
{'fraction_non_alphanumeric': 0.08149365899483325, 'fraction_numerical': 0.009628933771723814, 'mean_word_length': 2.4664224664224665, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '1469744', 'n_tokens_mistral': 2143, 'n_tokens_neox': 2007, 'n_words': 562}