From dff26cbcde9e03483584f39bdf2521ddb3d441ef Mon Sep 17 00:00:00 2001 From: markpollack Date: Thu, 4 Sep 2008 18:29:40 +0000 Subject: [PATCH] SPRNET-1020 - Consumer caching was not taking place for queues. --- .../Nms/Connections/CachedMessageConsumer .cs | 10 ++++ .../Nms/Connections/CachedSession.cs | 2 +- .../CachingConnectionFactoryTests.cs | 48 +++++++++++++++++++ .../Nms/Connections/TestMessageConsumer.cs | 38 +++++++++++++++ .../Messaging/Nms/Connections/TestSession.cs | 19 ++++---- .../Spring.Messaging.Nms.Tests.2005.csproj | 1 + 6 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Connections/TestMessageConsumer.cs diff --git a/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Connections/CachedMessageConsumer .cs b/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Connections/CachedMessageConsumer .cs index d61c598f..4ba9ad88 100644 --- a/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Connections/CachedMessageConsumer .cs +++ b/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Connections/CachedMessageConsumer .cs @@ -42,6 +42,16 @@ namespace Spring.Messaging.Nms.Connections this.target = target; } + + /// + /// Gets the target MessageConsumer, the consumer we are 'wrapping' + /// + /// The target MessageConsumer. + public IMessageConsumer Target + { + get { return target; } + } + /// /// Register for message events. /// diff --git a/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Connections/CachedSession.cs b/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Connections/CachedSession.cs index a7f5fa0f..52744545 100644 --- a/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Connections/CachedSession.cs +++ b/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Connections/CachedSession.cs @@ -444,7 +444,7 @@ namespace Spring.Messaging.Nms.Connections } else { - return target.CreateConsumer(destination, selector); + consumer = target.CreateConsumer(destination, selector); } if (LOG.IsDebugEnabled) { diff --git a/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Connections/CachingConnectionFactoryTests.cs b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Connections/CachingConnectionFactoryTests.cs index 7c87ae87..a3e0b4e1 100644 --- a/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Connections/CachingConnectionFactoryTests.cs +++ b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Connections/CachingConnectionFactoryTests.cs @@ -21,6 +21,7 @@ #region Imports using Apache.NMS; +using Apache.NMS.ActiveMQ.Commands; using NUnit.Framework; using Rhino.Mocks; @@ -208,6 +209,53 @@ namespace Spring.Messaging.Nms.Connections mocks.VerifyAll(); } + + /// + /// Tests that the same underlying instance of the message consumer is returned after + /// creating a session, creating the consumer (A), closing the session, and creating another + /// consumer (B). Assert that (A)=(B). + /// + [Test] + public void CachedMessageConsumer() + { + IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory)); + IConnection connection = new TestConnection(); + + + Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once(); + + mocks.ReplayAll(); + + + CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(); + cachingConnectionFactory.TargetConnectionFactory = connectionFactory; + IConnection con1 = cachingConnectionFactory.CreateConnection(); + + ISession sessionA = con1.CreateSession(AcknowledgementMode.Transactional); + IDestination destination = new ActiveMQQueue("test.dest"); + IMessageConsumer consumerA = sessionA.CreateConsumer(destination); + TestMessageConsumer tmpA = GetTestMessageConsumer(consumerA); + + sessionA.Close(); + + ISession sessionB = con1.CreateSession(AcknowledgementMode.Transactional); + IMessageConsumer consumerB = sessionB.CreateConsumer(destination); + TestMessageConsumer tmpB = GetTestMessageConsumer(consumerB); + + Assert.AreSame(tmpA, tmpB); + + mocks.VerifyAll(); + } + + private static TestMessageConsumer GetTestMessageConsumer(IMessageConsumer consumer) + { + CachedMessageConsumer cmp1 = consumer as CachedMessageConsumer; + Assert.IsNotNull(cmp1); + TestMessageConsumer tmp1 = cmp1.Target as TestMessageConsumer; + Assert.IsNotNull(tmp1); + return tmp1; + } + private static TestMessageProducer GetTestMessageProducer(IMessageProducer producer1) { CachedMessageProducer cmp1 = producer1 as CachedMessageProducer; diff --git a/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Connections/TestMessageConsumer.cs b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Connections/TestMessageConsumer.cs new file mode 100644 index 00000000..7ea57b88 --- /dev/null +++ b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Connections/TestMessageConsumer.cs @@ -0,0 +1,38 @@ + + +using System; +using Apache.NMS; + +namespace Spring.Messaging.Nms.Connections +{ + public class TestMessageConsumer : IMessageConsumer + { + + public event MessageListener Listener; + + public IMessage Receive() + { + throw new NotImplementedException(); + } + + public IMessage Receive(TimeSpan timeout) + { + throw new NotImplementedException(); + } + + public IMessage ReceiveNoWait() + { + throw new NotImplementedException(); + } + + public void Close() + { + throw new NotImplementedException(); + } + + public void Dispose() + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Connections/TestSession.cs b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Connections/TestSession.cs index 06775263..a0ef8c37 100644 --- a/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Connections/TestSession.cs +++ b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Connections/TestSession.cs @@ -66,46 +66,47 @@ namespace Spring.Messaging.Nms.Connections public IMessageConsumer CreateConsumer(IDestination destination) { - throw new NotImplementedException(); + return new TestMessageConsumer(); } public IMessageConsumer CreateConsumer(IDestination destination, TimeSpan requestTimeout) { - throw new NotImplementedException(); + return new TestMessageConsumer(); } public IMessageConsumer CreateConsumer(IDestination destination, string selector) { + return new TestMessageConsumer(); //IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory)); - IMessageConsumer msgConsumer = (IMessageConsumer) mocks.CreateMock(typeof (IMessageConsumer)); - return msgConsumer; + //IMessageConsumer msgConsumer = (IMessageConsumer) mocks.CreateMock(typeof (IMessageConsumer)); + //return msgConsumer; } public IMessageConsumer CreateConsumer(IDestination destination, string selector, TimeSpan requestTimeout) { - throw new NotImplementedException(); + return new TestMessageConsumer(); } public IMessageConsumer CreateConsumer(IDestination destination, string selector, bool noLocal) { - throw new NotImplementedException(); + return new TestMessageConsumer(); } public IMessageConsumer CreateConsumer(IDestination destination, string selector, bool noLocal, TimeSpan requestTimeout) { - throw new NotImplementedException(); + return new TestMessageConsumer(); } public IMessageConsumer CreateDurableConsumer(ITopic destination, string name, string selector, bool noLocal) { - throw new NotImplementedException(); + return new TestMessageConsumer(); } public IMessageConsumer CreateDurableConsumer(ITopic destination, string name, string selector, bool noLocal, TimeSpan requestTimeout) { - throw new NotImplementedException(); + return new TestMessageConsumer(); } public void DeleteDurableConsumer(string name) diff --git a/test/Spring/Spring.Messaging.Nms.Tests/Spring.Messaging.Nms.Tests.2005.csproj b/test/Spring/Spring.Messaging.Nms.Tests/Spring.Messaging.Nms.Tests.2005.csproj index fcecb4bb..279ac3b6 100644 --- a/test/Spring/Spring.Messaging.Nms.Tests/Spring.Messaging.Nms.Tests.2005.csproj +++ b/test/Spring/Spring.Messaging.Nms.Tests/Spring.Messaging.Nms.Tests.2005.csproj @@ -94,6 +94,7 @@ +