Caching of NMS sessions/message producers

Add IsTrue utility method to AssertUtils
This commit is contained in:
markpollack
2008-07-18 15:30:44 +00:00
parent e51c7e9bc7
commit 6084d8f068
25 changed files with 1859 additions and 97 deletions

View File

@@ -35,6 +35,32 @@ namespace Spring.Util
[TestFixture]
public sealed class AssertUtilsTests
{
[Test]
[ExpectedException(typeof(ArgumentException),ExpectedMessage = "foo")]
public void IsTrueWithMesssage()
{
AssertUtils.IsTrue(false,"foo");
}
[Test]
public void IsTrueWithMessageValidExpression()
{
AssertUtils.IsTrue(true, "foo");
}
[Test]
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "[Assertion failed] - this expression must be true")]
public void IsTrue()
{
AssertUtils.IsTrue(false);
}
[Test]
public void IsTrueValidExpression()
{
AssertUtils.IsTrue(true);
}
[Test]
[ExpectedException(typeof(InvalidOperationException))]
public void StateTrue()

View File

@@ -0,0 +1,221 @@
#region License
/*
* Copyright <20> 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 Apache.NMS;
using NUnit.Framework;
using Rhino.Mocks;
#endregion
namespace Spring.Messaging.Nms.Connection
{
/// <summary>
/// This class contains tests for
/// </summary>
/// <author>Mark Pollack</author>
/// <version>$Id:$</version>
[TestFixture]
public class CachingConnectionFactoryTests
{
private MockRepository mocks;
[SetUp]
public void Setup()
{
mocks = new MockRepository();
}
[Test]
public void CachedSession()
{
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 session1 = con1.CreateSession(AcknowledgementMode.Transactional);
TestSession testSession = GetTestSession(session1);
Assert.AreEqual(1, testSession.CreatedCount);
Assert.AreEqual(0, testSession.CloseCount);
session1.Close(); // won't close, will put in session cache.
Assert.AreEqual(0, testSession.CloseCount);
ISession session2 = con1.CreateSession(AcknowledgementMode.Transactional);
TestSession testSession2 = GetTestSession(session2);
Assert.AreSame(testSession, testSession2);
Assert.AreEqual(1, testSession.CreatedCount);
Assert.AreEqual(0, testSession.CloseCount);
mocks.VerifyAll();
//don't explicitly call close on
}
private static TestSession GetTestSession(ISession session1)
{
CachedSession cachedSession = session1 as CachedSession;
Assert.IsNotNull(cachedSession);
TestSession testSession = cachedSession.TargetSession as TestSession;
Assert.IsNotNull(testSession);
return testSession;
}
[Test]
public void CachedSessionTwoRequests()
{
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 session1 = con1.CreateSession(AcknowledgementMode.Transactional);
TestSession testSession1 = GetTestSession(session1);
Assert.AreEqual(1, testSession1.CreatedCount);
Assert.AreEqual(0, testSession1.CloseCount);
//will create a new one, not in the cache.
ISession session2 = con1.CreateSession(AcknowledgementMode.Transactional);
TestSession testSession2 = GetTestSession(session2);
Assert.AreEqual(1, testSession2.CreatedCount);
Assert.AreEqual(0, testSession2.CloseCount);
Assert.AreNotSame(testSession1, testSession2);
Assert.AreNotSame(session1, session2);
session1.Close(); // will be put in the cache
ISession session3 = con1.CreateSession(AcknowledgementMode.Transactional);
TestSession testSession3 = GetTestSession(session3);
Assert.AreSame(testSession1, testSession3);
Assert.AreSame(session1, session3);
Assert.AreEqual(1, testSession1.CreatedCount);
Assert.AreEqual(0, testSession1.CloseCount);
mocks.VerifyAll();
}
/// <summary>
/// Tests that the same underlying instance of the message producer is returned after
/// creating a session, creating the producer (A), closing the session, and creating another
/// producer (B). Assert that (A)=(B).
/// </summary>
[Test]
public void CachedMessageProducer()
{
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);
IMessageProducer producerA = sessionA.CreateProducer();
TestMessageProducer tmpA = GetTestMessageProducer(producerA);
sessionA.Close();
ISession sessionB = con1.CreateSession(AcknowledgementMode.Transactional);
IMessageProducer producerB = sessionB.CreateProducer();
TestMessageProducer tmpB = GetTestMessageProducer(producerB);
Assert.AreSame(tmpA, tmpB);
mocks.VerifyAll();
}
[Test]
public void CachedMessageProducerTwoRequests()
{
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);
IMessageProducer producerA = sessionA.CreateProducer();
TestMessageProducer tmpA = GetTestMessageProducer(producerA);
ISession sessionB = con1.CreateSession(AcknowledgementMode.Transactional);
IMessageProducer producerB = sessionB.CreateProducer();
TestMessageProducer tmpB = GetTestMessageProducer(producerB);
Assert.AreNotSame(tmpA, tmpB);
sessionA.Close();
ISession sessionC = con1.CreateSession(AcknowledgementMode.Transactional);
IMessageProducer producerC = sessionC.CreateProducer();
TestMessageProducer tmpC = GetTestMessageProducer(producerC);
Assert.AreSame(tmpA, tmpC);
mocks.VerifyAll();
}
private static TestMessageProducer GetTestMessageProducer(IMessageProducer producer1)
{
CachedMessageProducer cmp1 = producer1 as CachedMessageProducer;
Assert.IsNotNull(cmp1);
TestMessageProducer tmp1 = cmp1.Target as TestMessageProducer;
Assert.IsNotNull(tmp1);
return tmp1;
}
}
}

View File

@@ -24,13 +24,13 @@ using System;
using Apache.NMS;
using NUnit.Framework;
using Rhino.Mocks;
using Spring.Messaging.Nms.IConnections;
using Spring.Messaging.Nms.Connection;
using Spring.Transaction;
using Spring.Transaction.Support;
#endregion
namespace Spring.Messaging.Nms.Connections
namespace Spring.Messaging.Nms.Connection
{
/// <summary>
/// This class contains tests for

View File

@@ -0,0 +1,235 @@
#region License
/*
* Copyright <20> 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;
using Apache.NMS;
using NUnit.Framework;
using Rhino.Mocks;
using Spring.Messaging.Nms.Connection;
#endregion
namespace Spring.Messaging.Nms.Connection
{
/// <summary>
/// This class contains tests for the SingleConnectionFactory
/// </summary>
/// <author>Mark Pollack</author>
[TestFixture]
public class SingleConnectionFactoryTests
{
private MockRepository mocks;
[SetUp]
public void Setup()
{
mocks = new MockRepository();
}
[Test]
public void UsingConnection()
{
IConnection connection = (IConnection)mocks.CreateMock(typeof(IConnection));
connection.Start();
LastCall.On(connection).Repeat.Twice();
connection.Stop();
LastCall.On(connection).Repeat.Once();
connection.Close();
LastCall.On(connection).Repeat.Once();
mocks.ReplayAll();
SingleConnectionFactory scf = new SingleConnectionFactory(connection);
IConnection con1 = scf.CreateConnection();
con1.Start();
con1.Stop(); // should be ignored
con1.Close(); // should be ignored
IConnection con2 = scf.CreateConnection();
con2.Start();
con2.Stop(); // should be ignored
con2.Close(); // should be ignored.
scf.Dispose();
mocks.VerifyAll();
}
[Test]
public void UsingConnectionFactory()
{
IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
IConnection connection = (IConnection)mocks.CreateMock(typeof(IConnection));
Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();
connection.Start();
LastCall.On(connection).Repeat.Twice();
connection.Stop();
LastCall.On(connection).Repeat.Once();
connection.Close();
LastCall.On(connection).Repeat.Once();
mocks.ReplayAll();
SingleConnectionFactory scf = new SingleConnectionFactory(connectionFactory);
IConnection con1 = scf.CreateConnection();
con1.Start();
con1.Close(); // should be ignored
IConnection con2 = scf.CreateConnection();
con2.Start();
con2.Close(); //should be ignored
scf.Dispose(); //should trigger actual close
mocks.VerifyAll();
}
[Test]
public void UsingConnectionFactoryAndClientId()
{
IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
IConnection connection = (IConnection)mocks.CreateMock(typeof(IConnection));
Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();
connection.ClientId = "MyId";
LastCall.On(connection).Repeat.Once();
connection.Start();
LastCall.On(connection).Repeat.Twice();
connection.Stop();
LastCall.On(connection).Repeat.Once();
connection.Close();
LastCall.On(connection).Repeat.Once();
mocks.ReplayAll();
SingleConnectionFactory scf = new SingleConnectionFactory(connectionFactory);
scf.ClientId = "MyId";
IConnection con1 = scf.CreateConnection();
con1.Start();
con1.Close(); // should be ignored
IConnection con2 = scf.CreateConnection();
con2.Start();
con2.Close(); // should be ignored
scf.Dispose(); // should trigger actual close
mocks.VerifyAll();
}
[Test]
public void UsingConnectionFactoryAndExceptionListener()
{
IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
IConnection connection = (IConnection)mocks.CreateMock(typeof(IConnection));
IExceptionListener listener = new ChainedExceptionListener();
Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();
connection.ExceptionListener += listener.OnException;
LastCall.On(connection).IgnoreArguments();
connection.Start();
LastCall.On(connection).Repeat.Twice();
connection.Stop();
LastCall.On(connection).Repeat.Once();
connection.Close();
LastCall.On(connection).Repeat.Once();
mocks.ReplayAll();
SingleConnectionFactory scf = new SingleConnectionFactory(connectionFactory);
scf.ExceptionListener = listener;
IConnection con1 = scf.CreateConnection();
//can't look at invocation list on event ...grrr.
con1.Start();
con1.Stop(); // should be ignored
con1.Close(); // should be ignored
IConnection con2 = scf.CreateConnection();
con2.Start();
con2.Stop();
con2.Close();
scf.Dispose();
mocks.VerifyAll();
}
[Test]
public void UsingConnectionFactoryAndReconnectOnException()
{
IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
TestConnection con = new TestConnection();
Expect.Call(connectionFactory.CreateConnection()).Return(con).Repeat.Twice();
mocks.ReplayAll();
SingleConnectionFactory scf = new SingleConnectionFactory(connectionFactory);
scf.ReconnectOnException = true;
IConnection con1 = scf.CreateConnection();
con1.Start();
con.FireExcpetionEvent(new NMSException(""));
IConnection con2 = scf.CreateConnection();
con2.Start();
scf.Dispose();
mocks.VerifyAll();
Assert.AreEqual(2, con.StartCount);
Assert.AreEqual(2, con.CloseCount);
}
[Test]
public void UsingConnectionFactoryAndExceptionListenerAndReconnectOnException()
{
IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
TestConnection con = new TestConnection();
TestExceptionListener listener = new TestExceptionListener();
Expect.Call(connectionFactory.CreateConnection()).Return(con).Repeat.Twice();
mocks.ReplayAll();
SingleConnectionFactory scf = new SingleConnectionFactory(connectionFactory);
scf.ExceptionListener = listener;
scf.ReconnectOnException = true;
IConnection con1 = scf.CreateConnection();
//Assert.AreSame(listener, );
con1.Start();
con.FireExcpetionEvent(new NMSException(""));
IConnection con2 = scf.CreateConnection();
con2.Start();
scf.Dispose();
mocks.VerifyAll();
Assert.AreEqual(2, con.StartCount);
Assert.AreEqual(2, con.CloseCount);
Assert.AreEqual(1, listener.Count);
}
}
}

View File

@@ -0,0 +1,82 @@
using System;
using Apache.NMS;
namespace Spring.Messaging.Nms.Connection
{
public class TestConnection : IConnection
{
private int startCount;
private int closeCount;
private int createSessionCount;
private int closeSessionCount;
public int StartCount
{
get { return startCount; }
}
public int CloseCount
{
get { return closeCount; }
}
public event ExceptionListener ExceptionListener;
public ISession CreateSession()
{
createSessionCount++;
return new TestSession();
}
public ISession CreateSession(AcknowledgementMode acknowledgementMode)
{
createSessionCount++;
return new TestSession();
}
public void Close()
{
closeCount++;
}
public AcknowledgementMode AcknowledgementMode
{
get { return AcknowledgementMode.ClientAcknowledge; }
set { }
}
public string ClientId
{
get { return null; }
set { }
}
public void Dispose()
{
}
public void Start()
{
startCount++;
}
public bool IsStarted
{
get
{
if (startCount > 0) return true;
return false;
}
}
public void Stop()
{
}
public void FireExcpetionEvent(Exception e)
{
ExceptionListener(e);
}
}
}

View File

@@ -0,0 +1,40 @@
#region License
/*
* Copyright <20> 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
using System;
namespace Spring.Messaging.Nms.Connection
{
public class TestExceptionListener : IExceptionListener
{
private int count = 0;
public void OnException(Exception exception)
{
count++;
}
public int Count
{
get { return count; }
}
}
}

View File

@@ -0,0 +1,119 @@
#region License
/*
* Copyright 2002-2008 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
using System;
using Apache.NMS;
namespace Spring.Messaging.Nms.Connection
{
public class TestMessageProducer : IMessageProducer
{
public void Send(IMessage message)
{
throw new NotImplementedException();
}
public void Send(IMessage message, bool persistent, byte priority, TimeSpan timeToLive)
{
throw new NotImplementedException();
}
public void Send(IDestination destination, IMessage message)
{
throw new NotImplementedException();
}
public void Send(IDestination destination, IMessage message, bool persistent, byte priority, TimeSpan timeToLive)
{
throw new NotImplementedException();
}
public IMessage CreateMessage()
{
throw new NotImplementedException();
}
public ITextMessage CreateTextMessage()
{
throw new NotImplementedException();
}
public ITextMessage CreateTextMessage(string text)
{
throw new NotImplementedException();
}
public IMapMessage CreateMapMessage()
{
throw new NotImplementedException();
}
public IObjectMessage CreateObjectMessage(object body)
{
throw new NotImplementedException();
}
public IBytesMessage CreateBytesMessage()
{
throw new NotImplementedException();
}
public IBytesMessage CreateBytesMessage(byte[] body)
{
throw new NotImplementedException();
}
public bool Persistent
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public TimeSpan TimeToLive
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public byte Priority
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public bool DisableMessageID
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public bool DisableMessageTimestamp
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public void Dispose()
{
throw new NotImplementedException();
}
}
}

View File

@@ -0,0 +1,164 @@
#region License
/*
* Copyright 2002-2008 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
using System;
using Apache.NMS;
namespace Spring.Messaging.Nms.Connection
{
public class TestSession : ISession
{
private int closeCount;
private int createdCount;
public TestSession()
{
createdCount++;
}
public int CloseCount
{
get { return closeCount; }
}
public int CreatedCount
{
get { return createdCount; }
}
public IMessageProducer CreateProducer()
{
return new TestMessageProducer();
}
public IMessageProducer CreateProducer(IDestination destination)
{
return new TestMessageProducer();
}
public IMessageConsumer CreateConsumer(IDestination destination)
{
throw new NotImplementedException();
}
public IMessageConsumer CreateConsumer(IDestination destination, string selector)
{
throw new NotImplementedException();
}
public IMessageConsumer CreateConsumer(IDestination destination, string selector, bool noLocal)
{
throw new NotImplementedException();
}
public IMessageConsumer CreateDurableConsumer(ITopic destination, string name, string selector, bool noLocal)
{
throw new NotImplementedException();
}
public IQueue GetQueue(string name)
{
throw new NotImplementedException();
}
public ITopic GetTopic(string name)
{
throw new NotImplementedException();
}
public ITemporaryQueue CreateTemporaryQueue()
{
throw new NotImplementedException();
}
public ITemporaryTopic CreateTemporaryTopic()
{
throw new NotImplementedException();
}
public IMessage CreateMessage()
{
throw new NotImplementedException();
}
public ITextMessage CreateTextMessage()
{
throw new NotImplementedException();
}
public ITextMessage CreateTextMessage(string text)
{
throw new NotImplementedException();
}
public IMapMessage CreateMapMessage()
{
throw new NotImplementedException();
}
public IObjectMessage CreateObjectMessage(object body)
{
throw new NotImplementedException();
}
public IBytesMessage CreateBytesMessage()
{
throw new NotImplementedException();
}
public IBytesMessage CreateBytesMessage(byte[] body)
{
throw new NotImplementedException();
}
public void Close()
{
closeCount++;
}
public void Commit()
{
throw new NotImplementedException();
}
public void Rollback()
{
throw new NotImplementedException();
}
public bool Transacted
{
get { throw new NotImplementedException(); }
}
public AcknowledgementMode AcknowledgementMode
{
get { throw new NotImplementedException(); }
}
public void Dispose()
{
throw new NotImplementedException();
}
}
}

View File

@@ -74,9 +74,19 @@
<Project>{AEB1578C-9018-4D49-B440-789F38DD2F29}</Project>
<Name>Spring.Messaging.Nms.2005</Name>
</ProjectReference>
<ProjectReference Include="..\Spring.Core.Tests\Spring.Core.Tests.2005.csproj">
<Project>{44B16BAA-6DF8-447C-9D7F-3AD3D854D904}</Project>
<Name>Spring.Core.Tests.2005</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="Messaging\Nms\Connections\CachingConnectionFactoryTests.cs" />
<Compile Include="Messaging\Nms\Connections\NmsTransactionManagerTests.cs" />
<Compile Include="Messaging\Nms\Connections\SingleConnectionFactoryTests.cs" />
<Compile Include="Messaging\Nms\Connections\TestConnection.cs" />
<Compile Include="Messaging\Nms\Connections\TestExceptionListener.cs" />
<Compile Include="Messaging\Nms\Connections\TestMessageProducer.cs" />
<Compile Include="Messaging\Nms\Connections\TestSession.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Spring.Messaging.Nms.Tests.dll.config">