Author: jgomes
Date: Wed Aug 6 12:05:08 2008
New Revision: 683376
URL: http://svn.apache.org/viewvc?rev=683376&view=rev
Log:
Refactored durable consumer tests.
Fixes [AMQNET-92]. (See https://issues.apache.org/activemq/browse/AMQNET-92)
Modified:
activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/ConsumerTest.cs
activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/DurableTest.cs
activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/NMSTestSupport.cs
activemq/activemq-dotnet/Apache.NMS/trunk/vs2008-nms.csproj
Modified:
activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/ConsumerTest.cs
URL:
http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/ConsumerTest.cs?rev=683376&r1=683375&r2=683376&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/ConsumerTest.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/ConsumerTest.cs
Wed Aug 6 12:05:08 2008
@@ -17,78 +17,80 @@
using System;
using System.Threading;
using NUnit.Framework;
+using Apache.NMS.Util;
namespace Apache.NMS.Test
{
[TestFixture]
public abstract class ConsumerTest : NMSTestSupport
{
- public int prefetch;
- public bool durableConsumer;
-
- [SetUp]
- public override void SetUp()
- {
- clientId = "Apache.NMS.Test.ConsumerTest";
- base.SetUp();
- }
-
- [TearDown]
- public override void TearDown()
- {
- base.TearDown();
- }
-
+ private static string TEST_CLIENT_ID = "ConsumerTestClientId";
+ private static string TOPIC = "TestTopicConsumerTest";
+ private static string CONSUMER_ID = "ConsumerTestConsumerId";
[Test]
public void TestDurableConsumerSelectorChangePersistent()
{
- destinationType = DestinationType.Topic;
- persistent = true;
- doTestDurableConsumerSelectorChange();
+ doTestDurableConsumerSelectorChange(true);
}
[Test]
public void TestDurableConsumerSelectorChangeNonPersistent()
{
- destinationType = DestinationType.Topic;
- persistent = false;
- doTestDurableConsumerSelectorChange();
+ doTestDurableConsumerSelectorChange(false);
}
- public void doTestDurableConsumerSelectorChange()
+ public void doTestDurableConsumerSelectorChange(bool persistent)
{
- IMessageProducer producer = Session.CreateProducer(Destination);
- producer.Persistent = persistent;
- IMessageConsumer consumer =
- Session.CreateDurableConsumer((ITopic) Destination, "test",
"color='red'", false);
-
- // Send the messages
- ITextMessage message = Session.CreateTextMessage("1st");
- message.Properties["color"] = "red";
- producer.Send(message);
-
- IMessage m = consumer.Receive(receiveTimeout);
- Assert.IsNotNull(m);
- Assert.AreEqual("1st", ((ITextMessage) m).Text);
-
- // Change the subscription.
- consumer.Dispose();
- consumer = Session.CreateDurableConsumer((ITopic) Destination,
"test", "color='blue'", false);
-
- message = Session.CreateTextMessage("2nd");
- message.Properties["color"] = "red";
- producer.Send(message);
- message = Session.CreateTextMessage("3rd");
- message.Properties["color"] = "blue";
- producer.Send(message);
-
- // Selector should skip the 2nd message.
- m = consumer.Receive(TimeSpan.FromMilliseconds(1000));
- Assert.IsNotNull(m);
- Assert.AreEqual("3rd", ((ITextMessage) m).Text);
-
- Assert.IsNull(consumer.ReceiveNoWait());
+ try
+ {
+ using(IConnection connection =
CreateConnection(TEST_CLIENT_ID))
+ {
+ connection.Start();
+ using(ISession session =
connection.CreateSession(AcknowledgementMode.AutoAcknowledge))
+ {
+ ITopic topic =
SessionUtil.GetTopic(session, TOPIC);
+ IMessageProducer producer =
session.CreateProducer(topic);
+ IMessageConsumer consumer =
session.CreateDurableConsumer(topic, CONSUMER_ID, "color='red'", false);
+
+ producer.Persistent =
persistent;
+
+ // Send the messages
+ ITextMessage sendMessage =
session.CreateTextMessage("1st");
+ sendMessage.Properties["color"]
= "red";
+ producer.Send(sendMessage);
+
+ ITextMessage receiveMsg =
consumer.Receive(receiveTimeout) as ITextMessage;
+ Assert.IsNotNull(receiveMsg,
"Failed to retrieve 1st durable message.");
+ Assert.AreEqual("1st",
receiveMsg.Text);
+
Assert.IsTrue(receiveMsg.NMSPersistent == persistent, "message does not match
persistent setting.");
+
+ // Change the subscription.
+ consumer.Dispose();
+ consumer =
session.CreateDurableConsumer(topic, CONSUMER_ID, "color='blue'", false);
+
+ sendMessage =
session.CreateTextMessage("2nd");
+ sendMessage.Properties["color"]
= "red";
+ producer.Send(sendMessage);
+ sendMessage =
session.CreateTextMessage("3rd");
+ sendMessage.Properties["color"]
= "blue";
+ producer.Send(sendMessage);
+
+ // Selector should skip the 2nd
message.
+ receiveMsg =
consumer.Receive(receiveTimeout) as ITextMessage;
+ Assert.IsNotNull(receiveMsg,
"Failed to retrieve durable message.");
+ Assert.AreEqual("3rd",
receiveMsg.Text, "Retrieved the wrong durable message.");
+
Assert.IsTrue(receiveMsg.NMSPersistent == persistent, "message does not match
persistent setting.");
+
+ // Make sure there are no
pending messages.
+
Assert.IsNull(consumer.ReceiveNoWait(), "Wrong number of messages in durable
subscription.");
+ }
+ }
+ }
+ finally
+ {
+ UnregisterDurableConsumer(TEST_CLIENT_ID,
CONSUMER_ID);
+ }
}
[Test]
@@ -97,26 +99,33 @@
destinationType = DestinationType.Queue;
// Launch a thread to perform
IMessageConsumer.Receive().
// If it doesn't fail in less than three seconds, no
exception was thrown.
- Thread receiveThread = new Thread(new
ThreadStart(doTestNoTimeoutConsumer));
-
- using(timeoutConsumer =
Session.CreateConsumer(Destination))
+ Thread receiveThread = new Thread(new
ThreadStart(TimeoutConsumerThreadProc));
+ using(IConnection connection =
CreateConnection(TEST_CLIENT_ID))
{
- receiveThread.Start();
- if(receiveThread.Join(3000))
- {
- Assert.Fail("IMessageConsumer.Receive()
returned without blocking. Test failed.");
- }
- else
+ connection.Start();
+ using(ISession session =
connection.CreateSession(AcknowledgementMode.AutoAcknowledge))
{
- // Kill the thread - otherwise it'll
sit in Receive() until a message arrives.
- receiveThread.Interrupt();
+ ITemporaryQueue queue =
session.CreateTemporaryQueue();
+ using(this.timeoutConsumer =
session.CreateConsumer(queue))
+ {
+ receiveThread.Start();
+ if(receiveThread.Join(3000))
+ {
+
Assert.Fail("IMessageConsumer.Receive() returned without blocking. Test
failed.");
+ }
+ else
+ {
+ // Kill the thread -
otherwise it'll sit in Receive() until a message arrives.
+
receiveThread.Interrupt();
+ }
+ }
}
}
}
protected IMessageConsumer timeoutConsumer;
- public void doTestNoTimeoutConsumer()
+ protected void TimeoutConsumerThreadProc()
{
try
{
@@ -125,7 +134,7 @@
catch(ArgumentOutOfRangeException e)
{
// The test failed. We will know because the
timeout will expire inside TestNoTimeoutConsumer().
- Console.WriteLine("Test failed with exception:
" + e.Message);
+ Assert.Fail("Test failed with exception: " +
e.Message);
}
catch(ThreadInterruptedException)
{
@@ -134,7 +143,7 @@
catch(Exception e)
{
// Some other exception occurred.
- Console.WriteLine("Test failed with exception:
" + e.Message);
+ Assert.Fail("Test failed with exception: " +
e.Message);
}
}
}
Modified:
activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/DurableTest.cs
URL:
http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/DurableTest.cs?rev=683376&r1=683375&r2=683376&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/DurableTest.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/DurableTest.cs
Wed Aug 6 12:05:08 2008
@@ -16,6 +16,7 @@
*/
using System;
using NUnit.Framework;
+using Apache.NMS.Util;
namespace Apache.NMS.Test
{
@@ -28,33 +29,6 @@
private static string CONSUMER_ID = "DurableTestConsumerId";
private static string DURABLE_SELECTOR = "2 > 1";
- protected void RegisterDurableConsumer()
- {
- using(IConnection connection =
CreateConnection(TEST_CLIENT_ID))
- {
- connection.Start();
- using(ISession session =
connection.CreateSession(AcknowledgementMode.DupsOkAcknowledge))
- {
- ITopic topic = session.GetTopic(TOPIC);
- using(IMessageConsumer consumer =
session.CreateDurableConsumer(topic, CONSUMER_ID, DURABLE_SELECTOR, false))
- {
- }
- }
- }
- }
-
- protected void UnregisterDurableConsumer()
- {
- using(IConnection connection =
CreateConnection(TEST_CLIENT_ID))
- {
- connection.Start();
- using(ISession session =
connection.CreateSession(AcknowledgementMode.DupsOkAcknowledge))
- {
-
session.DeleteDurableConsumer(CONSUMER_ID);
- }
- }
- }
-
protected void SendPersistentMessage()
{
using(IConnection connection =
CreateConnection(SEND_CLIENT_ID))
@@ -62,11 +36,13 @@
connection.Start();
using(ISession session =
connection.CreateSession(AcknowledgementMode.DupsOkAcknowledge))
{
- ITopic topic = session.GetTopic(TOPIC);
+ ITopic topic =
SessionUtil.GetTopic(session, TOPIC);
ITextMessage message =
session.CreateTextMessage("Persistent Hello");
- using(IMessageProducer producer =
session.CreateProducer())
+ using(IMessageProducer producer =
session.CreateProducer(topic))
{
- producer.Send(topic, message,
true, message.NMSPriority, receiveTimeout);
+ producer.Persistent = true;
+ producer.RequestTimeout =
TimeSpan.FromSeconds(5);
+ producer.Send(message);
}
}
}
@@ -75,17 +51,17 @@
[Test]
public void TestDurableConsumer()
{
- RegisterDurableConsumer();
- SendPersistentMessage();
-
try
{
+ RegisterDurableConsumer(TEST_CLIENT_ID, TOPIC,
CONSUMER_ID, DURABLE_SELECTOR, false);
+ SendPersistentMessage();
+
using(IConnection connection =
CreateConnection(TEST_CLIENT_ID))
{
connection.Start();
using(ISession session =
connection.CreateSession(AcknowledgementMode.DupsOkAcknowledge))
{
- ITopic topic =
session.GetTopic(TOPIC);
+ ITopic topic =
SessionUtil.GetTopic(session, TOPIC);
using(IMessageConsumer consumer
= session.CreateDurableConsumer(topic, CONSUMER_ID, DURABLE_SELECTOR, false))
{
IMessage msg =
consumer.Receive(receiveTimeout);
@@ -100,22 +76,22 @@
}
finally
{
- UnregisterDurableConsumer();
+ UnregisterDurableConsumer(TEST_CLIENT_ID,
CONSUMER_ID);
}
}
[Test]
public void TestDurableConsumerTransactional()
{
- RegisterDurableConsumer();
try
{
+ RegisterDurableConsumer(TEST_CLIENT_ID, TOPIC,
CONSUMER_ID, DURABLE_SELECTOR, false);
RunTestDurableConsumerTransactional();
RunTestDurableConsumerTransactional();
}
finally
{
- UnregisterDurableConsumer();
+ UnregisterDurableConsumer(TEST_CLIENT_ID,
CONSUMER_ID);
}
}
@@ -128,7 +104,7 @@
connection.Start();
using(ISession session =
connection.CreateSession(AcknowledgementMode.Transactional))
{
- ITopic topic = session.GetTopic(TOPIC);
+ ITopic topic =
SessionUtil.GetTopic(session, TOPIC);
using(IMessageConsumer consumer =
session.CreateDurableConsumer(topic, CONSUMER_ID, DURABLE_SELECTOR, false))
{
IMessage msg =
consumer.Receive(receiveTimeout);
Modified:
activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/NMSTestSupport.cs
URL:
http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/NMSTestSupport.cs?rev=683376&r1=683375&r2=683376&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/NMSTestSupport.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/NMSTestSupport.cs
Wed Aug 6 12:05:08 2008
@@ -15,6 +15,7 @@
* limitations under the License.
*/
using Apache.NMS;
+using Apache.NMS.Util;
using NUnit.Framework;
using System;
using System.IO;
@@ -48,6 +49,11 @@
protected DestinationType destinationType =
DestinationType.Queue;
protected AcknowledgementMode acknowledgementMode =
AcknowledgementMode.ClientAcknowledge;
+ public NMSTestSupport()
+ {
+ clientId = "NMSUnitTests";
+ }
+
[SetUp]
public virtual void SetUp()
{
@@ -296,6 +302,41 @@
return newConnection;
}
+ /// <summary>
+ /// Register a durable consumer
+ /// </summary>
+ /// <param name="destination">Destination name to register.
Supports embedded prefix names.</param>
+ /// <param name="consumerID">Name of the durable
consumer.</param>
+ /// <param name="selector">Selector parameters for
consumer.</param>
+ /// <param name="noLocal"></param>
+ protected void RegisterDurableConsumer(string connectionID,
string destination, string consumerID, string selector, bool noLocal)
+ {
+ using(IConnection connection =
CreateConnection(connectionID))
+ {
+ connection.Start();
+ using(ISession session =
connection.CreateSession(AcknowledgementMode.DupsOkAcknowledge))
+ {
+ ITopic destinationTopic =
SessionUtil.GetTopic(session, destination);
+ Assert.IsNotNull(destinationTopic,
"Could not get destination topic.");
+ using(IMessageConsumer consumer =
session.CreateDurableConsumer(destinationTopic, consumerID, selector, noLocal,
receiveTimeout))
+ {
+ }
+ }
+ }
+ }
+
+ protected void UnregisterDurableConsumer(string connectionID,
string consumerID)
+ {
+ using(IConnection connection =
CreateConnection(connectionID))
+ {
+ connection.Start();
+ using(ISession session =
connection.CreateSession(AcknowledgementMode.DupsOkAcknowledge))
+ {
+
session.DeleteDurableConsumer(consumerID, receiveTimeout);
+ }
+ }
+ }
+
protected virtual IMessageProducer CreateProducer()
{
return Session.CreateProducer(Destination);
Modified: activemq/activemq-dotnet/Apache.NMS/trunk/vs2008-nms.csproj
URL:
http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/vs2008-nms.csproj?rev=683376&r1=683375&r2=683376&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/vs2008-nms.csproj (original)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/vs2008-nms.csproj Wed Aug 6
12:05:08 2008
@@ -72,7 +72,10 @@
<Compile Include="src\main\csharp\Util\Convert.cs" />
<Compile Include="src\main\csharp\Util\CountDownLatch.cs" />
<Compile Include="src\main\csharp\Util\DateUtils.cs" />
- <Compile Include="src\main\csharp\Util\URISupport.cs" />
+ <Compile Include="src\main\csharp\Util\SessionUtils.cs" />
+ <Compile Include="src\main\csharp\Util\URISupport.cs">
+ <SubType>Code</SubType>
+ </Compile>
</ItemGroup>
<ItemGroup>
<None Include="activemq-dotnet.snk" />