#region Copyright
//-------------------------------------------------
// Author: Paolo Salvatori
// Email: paolos@microsoft.com
// History: 2009-09-20 Created
//-------------------------------------------------
#endregion
#region Using Directives
using System;
using System.IO;
using System.Diagnostics;
using System.Text;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endregion
namespace Microsoft.BizTalk.CAT.Samples.HandleXLANGMessages.WCFLoadTest
{
/// <summary>
/// Summary description for UnitTest1
/// </summary>
[TestClass]
public class WCFLoadTest
{
#region Constants
private const int MaxBufferSize = 2097152;
private const string Source = "WCF Load Test";
private const string Star = "*";
private const string TestMessageFolderParameter = "testMessageFolder";
private const string TestMessageFolderDefault = @"C:\Projects\HandleXLANGMessages\WCFLoadTest\TestMessages";
private const string TestMessageFolderFormat = @"Test Message Folder = {0}";
private const string NetTcpCalculatorServiceReceiveLocationEndpoint = "netTcpCalculatorServiceReceiveLocationEndpoint";
private const string BasicHttpCalculatorServiceReceiveLocationEndpoint = "basicHttpCalculatorServiceReceiveLocationEndpoint";
private const string SmallCustomBtxMessageOrchestration = "SmallCustomBtxMessageOrchestration.xml";
private const string SmallMessageClassesOrchestration = "SmallMessageClassesOrchestration.xml";
private const string SmallStreamOrchestration = "SmallStreamOrchestration.xml";
private const string SmallXmlDocumentOrchestration = "SmallXmlDocumentOrchestration.xml";
private const string MediumCustomBtxMessageOrchestration = "MediumCustomBtxMessageOrchestration.xml";
private const string MediumMessageClassesOrchestration = "MediumMessageClassesOrchestration.xml";
private const string MediumStreamOrchestration = "MediumStreamOrchestration.xml";
private const string MediumXmlDocumentOrchestration = "MediumXmlDocumentOrchestration.xml";
private const string LargeCustomBtxMessageOrchestration = "LargeCustomBtxMessageOrchestration.xml";
private const string LargeMessageClassesOrchestration = "LargeMessageClassesOrchestration.xml";
private const string LargeStreamOrchestration = "LargeStreamOrchestration.xml";
private const string LargeXmlDocumentOrchestration = "LargeXmlDocumentOrchestration.xml";
#endregion
#region Private Instance Fields
private TestContext testContextInstance;
#endregion
#region Private Static Fields
private static string testMessageFolder = null;
#endregion
#region Public Instance Constructor
public WCFLoadTest()
{
}
#endregion
#region Public Static Constructor
static WCFLoadTest()
{
try
{
testMessageFolder = ConfigurationManager.AppSettings[TestMessageFolderParameter];
if (string.IsNullOrEmpty(testMessageFolder))
{
testMessageFolder = TestMessageFolderDefault;
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
EventLog.WriteEntry(Source, ex.Message, EventLogEntryType.Error);
}
}
#endregion
#region Public Properties
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#endregion
#region Test Methods
[TestMethod]
public void BasicHttpSmallCustomBtxMessageOrchestration()
{
InvokeTwoWayWCFReceiveLocation(BasicHttpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
SmallCustomBtxMessageOrchestration,
MessageVersion.Soap11,
SessionMode.Allowed);
}
[TestMethod]
public void BasicHttpSmallMessageClassesOrchestration()
{
InvokeTwoWayWCFReceiveLocation(BasicHttpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
SmallMessageClassesOrchestration,
MessageVersion.Soap11,
SessionMode.Allowed);
}
[TestMethod]
public void BasicHttpSmallStreamOrchestration()
{
InvokeTwoWayWCFReceiveLocation(BasicHttpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
SmallStreamOrchestration,
MessageVersion.Soap11,
SessionMode.Allowed);
}
[TestMethod]
public void BasicHttpSmallXmlDocumentOrchestration()
{
InvokeTwoWayWCFReceiveLocation(BasicHttpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
SmallXmlDocumentOrchestration,
MessageVersion.Soap11,
SessionMode.Allowed);
}
[TestMethod]
public void BasicHttpMediumCustomBtxMessageOrchestration()
{
InvokeTwoWayWCFReceiveLocation(BasicHttpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
MediumCustomBtxMessageOrchestration,
MessageVersion.Soap11,
SessionMode.Allowed);
}
[TestMethod]
public void BasicHttpMediumMessageClassesOrchestration()
{
InvokeTwoWayWCFReceiveLocation(BasicHttpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
MediumMessageClassesOrchestration,
MessageVersion.Soap11,
SessionMode.Allowed);
}
[TestMethod]
public void BasicHttpMediumStreamOrchestration()
{
InvokeTwoWayWCFReceiveLocation(BasicHttpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
MediumStreamOrchestration,
MessageVersion.Soap11,
SessionMode.Allowed);
}
[TestMethod]
public void BasicHttpMediumXmlDocumentOrchestration()
{
InvokeTwoWayWCFReceiveLocation(BasicHttpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
MediumXmlDocumentOrchestration,
MessageVersion.Soap11,
SessionMode.Allowed);
}
[TestMethod]
public void BasicHttpLargeCustomBtxMessageOrchestration()
{
InvokeTwoWayWCFReceiveLocation(BasicHttpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
LargeCustomBtxMessageOrchestration,
MessageVersion.Soap11,
SessionMode.Allowed);
}
[TestMethod]
public void BasicHttpLargeMessageClassesOrchestration()
{
InvokeTwoWayWCFReceiveLocation(BasicHttpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
LargeMessageClassesOrchestration,
MessageVersion.Soap11,
SessionMode.Allowed);
}
[TestMethod]
public void BasicHttpLargeStreamOrchestration()
{
InvokeTwoWayWCFReceiveLocation(BasicHttpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
LargeStreamOrchestration,
MessageVersion.Soap11,
SessionMode.Allowed);
}
[TestMethod]
public void BasicHttpLargeXmlDocumentOrchestration()
{
InvokeTwoWayWCFReceiveLocation(BasicHttpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
LargeXmlDocumentOrchestration,
MessageVersion.Soap11,
SessionMode.Allowed);
}
[TestMethod]
public void NetTcpSmallCustomBtxMessageOrchestration()
{
InvokeTwoWayWCFReceiveLocation(NetTcpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
SmallCustomBtxMessageOrchestration,
MessageVersion.Default,
SessionMode.Allowed);
}
[TestMethod]
public void NetTcpSmallMessageClassesOrchestration()
{
InvokeTwoWayWCFReceiveLocation(NetTcpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
SmallMessageClassesOrchestration,
MessageVersion.Default,
SessionMode.Allowed);
}
[TestMethod]
public void NetTcpSmallStreamOrchestration()
{
InvokeTwoWayWCFReceiveLocation(NetTcpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
SmallStreamOrchestration,
MessageVersion.Default,
SessionMode.Allowed);
}
[TestMethod]
public void NetTcpSmallXmlDocumentOrchestration()
{
InvokeTwoWayWCFReceiveLocation(NetTcpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
SmallXmlDocumentOrchestration,
MessageVersion.Default,
SessionMode.Allowed);
}
[TestMethod]
public void NetTcpMediumCustomBtxMessageOrchestration()
{
InvokeTwoWayWCFReceiveLocation(NetTcpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
MediumCustomBtxMessageOrchestration,
MessageVersion.Default,
SessionMode.Allowed);
}
[TestMethod]
public void NetTcpMediumMessageClassesOrchestration()
{
InvokeTwoWayWCFReceiveLocation(NetTcpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
MediumMessageClassesOrchestration,
MessageVersion.Default,
SessionMode.Allowed);
}
[TestMethod]
public void NetTcpMediumStreamOrchestration()
{
InvokeTwoWayWCFReceiveLocation(NetTcpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
MediumStreamOrchestration,
MessageVersion.Default,
SessionMode.Allowed);
}
[TestMethod]
public void NetTcpMediumXmlDocumentOrchestration()
{
InvokeTwoWayWCFReceiveLocation(NetTcpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
MediumXmlDocumentOrchestration,
MessageVersion.Default,
SessionMode.Allowed);
}
[TestMethod]
public void NetTcpLargeCustomBtxMessageOrchestration()
{
InvokeTwoWayWCFReceiveLocation(NetTcpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
LargeCustomBtxMessageOrchestration,
MessageVersion.Default,
SessionMode.Allowed);
}
[TestMethod]
public void NetTcpLargeMessageClassesOrchestration()
{
InvokeTwoWayWCFReceiveLocation(NetTcpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
LargeMessageClassesOrchestration,
MessageVersion.Default,
SessionMode.Allowed);
}
[TestMethod]
public void NetTcpLargeStreamOrchestration()
{
InvokeTwoWayWCFReceiveLocation(NetTcpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
LargeStreamOrchestration,
MessageVersion.Default,
SessionMode.Allowed);
}
[TestMethod]
public void NetTcpLargeXmlDocumentOrchestration()
{
InvokeTwoWayWCFReceiveLocation(NetTcpCalculatorServiceReceiveLocationEndpoint,
testMessageFolder,
LargeXmlDocumentOrchestration,
MessageVersion.Default,
SessionMode.Allowed);
}
#endregion
#region Helper Methods
public void InvokeTwoWayWCFReceiveLocation(string endpointConfigurationName,
string requestMessageFolder,
string requestMessageName,
MessageVersion messageVersion,
SessionMode sessionMode)
{
ChannelFactory<IRequestChannel> channelFactory = null;
IRequestChannel channel = null;
XmlTextReader xmlTextReader = null;
Message requestMessage = null;
Message responseMessage = null;
try
{
channelFactory = new ChannelFactory<IRequestChannel>(endpointConfigurationName);
channelFactory.Endpoint.Contract.SessionMode = sessionMode;
channel = channelFactory.CreateChannel();
string path = Path.Combine(requestMessageFolder, requestMessageName);
xmlTextReader = new XmlTextReader(path);
requestMessage = Message.CreateMessage(messageVersion, Star, xmlTextReader);
TestContext.BeginTimer(requestMessageName);
responseMessage = channel.Request(requestMessage);
channel.Close();
channelFactory.Close();
}
catch (FaultException ex)
{
HandleException(ref channelFactory,
ref channel,
ex);
throw;
}
catch (CommunicationException ex)
{
HandleException(ref channelFactory,
ref channel,
ex);
throw;
}
catch (TimeoutException ex)
{
HandleException(ref channelFactory,
ref channel,
ex);
throw;
}
catch (Exception ex)
{
HandleException(ref channelFactory,
ref channel,
ex);
throw;
}
finally
{
TestContext.EndTimer(requestMessageName);
CloseObjects(channelFactory,
channel,
xmlTextReader,
requestMessage,
responseMessage);
}
}
private void HandleException(ref ChannelFactory<IRequestChannel> channelFactory,
ref IRequestChannel channel,
Exception ex)
{
try
{
if (channelFactory != null)
{
channelFactory.Abort();
channelFactory = null;
}
if (channel != null)
{
channel.Abort();
channel = null;
}
Trace.WriteLine(ex.Message);
EventLog.WriteEntry(Source, ex.Message, EventLogEntryType.Error);
}
catch (Exception)
{
}
}
private void CloseObjects(ChannelFactory<IRequestChannel> channelFactory,
IRequestChannel channel,
XmlTextReader xmlTextReader,
Message requestMessage,
Message responseMessage)
{
try
{
if (channelFactory != null)
{
channelFactory.Close();
}
if (channel != null)
{
channel.Close();
}
if (xmlTextReader != null)
{
xmlTextReader.Close();
}
if (requestMessage != null)
{
requestMessage.Close();
}
if (responseMessage != null)
{
responseMessage.Close();
}
}
catch (Exception)
{
}
}
#endregion
}
}
|