/Main/src/Tests/RockBus.Samples.5DatabaseTransport.Dapper.SqlServer/Sample5DatabaseTransportTests.cs
C# | 259 lines | 207 code | 42 blank | 10 comment | 50 complexity | 321ce24a0d9d5b6e8bb4d0c78abedee0 MD5 | raw file
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using RockBus.Clients.Interfaces;
- using RockBus.Clients.LoanProcessor;
- using RockBus.Composition;
- using RockBus.Composition.Interfaces;
- using RockBus.Initialization.Interfaces;
- using RockBus.Logging.Trace;
- using RockBus.Routing.Interfaces;
- using RockBus.ServiceBus;
- using RockBus.ServiceModel;
- using RockBus.TestUtils;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.ServiceModel;
- using System.Threading;
- using System.Threading.Tasks;
-
- namespace RockBus.Samples._5DatabaseTransport.Dapper.SqlServer
- {
- [TestClass]
- public class Sample5DatabaseTransportTests
- {
- private static ServiceHost _cbrServiceHost;
- private static ServiceHost _loanProcessorServiceHost;
- private static ServiceBusEnvironment _sbe;
-
- static Sample5DatabaseTransportTests()
- {
- // Make sure dependencies are copied to test location
- var loggerType = typeof(TraceLogger);
- var dmType = typeof(DependencyManagerBase);
- }
-
- [TestInitialize()]
- public void TestInitialize()
- {
- var initializationManager = DependencyManager.Instance.Resolve<IInitializationManager>();
- initializationManager.Initialize();
-
- _cbrServiceHost = ServiceHostUtils.OpenRockBusServiceHost(new TraceLogger());
- _loanProcessorServiceHost = ServiceHostUtils.OpenHost<LoanProcessorService>(new Uri[] { new Uri("net.pipe://localhost/LoanProcessorService/"), new Uri("http://localhost:9997/LoanProcessorService/") });
- _sbe = new ServiceBusEnvironment(new TraceLogger());
- _sbe.ConfigureSubscribersAsync().Wait();
- }
-
- [TestCleanup()]
- public void TestCleanup()
- {
- _sbe.UnconfigureSubscribersAsync().Wait();
-
- List<ServiceHost> hosts = new List<ServiceHost>(2)
- {
- _cbrServiceHost,
- _loanProcessorServiceHost,
- };
-
- var closingTasks = new List<TaskCompletionSource<bool>>();
- foreach (ServiceHost s in hosts)
- {
- var closingTask = new TaskCompletionSource<bool>();
- closingTasks.Add(closingTask);
- s.Closed += delegate(Object o, System.EventArgs n)
- {
- closingTask.TrySetResult(true);
- };
- s.Close();
- }
- Task[] taskArray = closingTasks.Select((tcs) => tcs.Task).ToArray();
- Task.WaitAll(taskArray, 2000);
- }
-
- [TestMethod]
- [TestCategory("Samples")]
- [DeploymentItem("RockBus.Composition.dll")]
- [DeploymentItem("RockBus.Logging.Trace.dll")]
- public void TestNewInterestRateEvent()
- {
- IPublisherManager pm = DependencyManager.Instance.Resolve<IPublisherManager>();
- pm.EnableAll();
-
- // We have to wait here in order for the subscriptions to become active
- // See <internalConfiguration reinitializationIntervalMs="2000" reinitLockTimeoutMs="5000" />
- Thread.Sleep(3000);
-
- LoanProcessorService.Reset();
-
- string endpointName = "CBR_BasicHttpEndpointAsync";
- ClientBaseArgs args = new ClientBaseArgs(endpointName);
- ILoanProcessorAsync client = new LoanProcessorClientAsync(args);
-
- int id = 9996;
- int i = 0;
- NewInterestRateEvent newInterestRateEvent0 = new NewInterestRateEvent() { ID = 9996, SomeValue = "NewInterestRateEvent" + id + "_" + i };
- IAsyncResult asyncResult = client.BeginProcessNewInterestRateEvent(newInterestRateEvent0, ProcessNewInterestRateEventCallback, client);
- bool callbackCalled = asyncResult.AsyncWaitHandle.WaitOne(60000);
- Assert.IsTrue(callbackCalled);
-
- for (i = 0; i < 300; i++)
- {
- if (LoanProcessorService.CallStatsSet.Count >= 2)
- {
- break;
- }
-
- Thread.Sleep(100);
- }
-
- Assert.AreEqual<long>(2, LoanProcessorService.CallStatsSet.Count);
- Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9996 && cs.Count == 1 && cs.Address.StartsWith("net.pipe://")));
- Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9996 && cs.Count == 1 && cs.Address.StartsWith("http://")));
- }
-
- [TestMethod]
- [TestCategory("Samples")]
- [DeploymentItem("RockBus.Composition.dll")]
- [DeploymentItem("RockBus.Logging.Trace.dll")]
- public void TestLoanApplicationEvent()
- {
- var pm = DependencyManager.Instance.Resolve<IPublisherManager>();
- pm.EnableAll();
-
- // We have to wait here in order for the subscriptions to become active
- // See <internalConfiguration reinitializationIntervalMs="2000" reinitLockTimeoutMs="5000" />
- Thread.Sleep(3000);
-
- LoanProcessorService.Reset();
-
- string endpointName = "CBR_BasicHttpEndpointAsync";
- ClientBaseArgs args = new ClientBaseArgs(endpointName);
- ILoanProcessorAsync client = new LoanProcessorClientAsync(args);
-
- int id = 9996;
- int i = 0;
- LoanApplicationEvent loanApplicationEvent = new LoanApplicationEvent() { ID = id, SomeValue = "LoanApplicationEvent" + id + "_" + i };
- IAsyncResult asyncResult = client.BeginProcessLoanApplicationEvent(loanApplicationEvent, ProcessLoanApplicationEventCallback, client);
- bool callbackCalled = asyncResult.AsyncWaitHandle.WaitOne(60000);
- Assert.IsTrue(callbackCalled);
-
- for (i = 0; i < 600; i++)
- {
- if (LoanProcessorService.CallStatsSet.Count >= 1)
- {
- break;
- }
-
- Thread.Sleep(100);
- }
-
- Assert.AreEqual<long>(1, LoanProcessorService.CallStatsSet.Count);
- Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9996 && cs.Count == 1 && cs.Address.StartsWith("net.pipe://")));
- Assert.AreEqual<int>(0, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9997 && cs.Count == 1 && cs.Address.StartsWith("http://")));
-
- id = 9997;
- i = 0;
- loanApplicationEvent = new LoanApplicationEvent() { ID = id, SomeValue = "LoanApplicationEvent" + id + "_" + i };
- asyncResult = client.BeginProcessLoanApplicationEvent(loanApplicationEvent, ProcessLoanApplicationEventCallback, client);
- callbackCalled = asyncResult.AsyncWaitHandle.WaitOne(5000);
- Assert.IsTrue(callbackCalled);
-
- for (i = 0; i < 600; i++)
- {
- if (LoanProcessorService.CallStatsSet.Count >= 2)
- {
- break;
- }
-
- Thread.Sleep(100);
- }
-
- Assert.AreEqual<long>(2, LoanProcessorService.CallStatsSet.Count);
- Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9996 && cs.Count == 1 && cs.Address.StartsWith("net.pipe://")));
- Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9997 && cs.Count == 1 && cs.Address.StartsWith("http://")));
- }
-
- [TestMethod]
- [TestCategory("Samples")]
- [DeploymentItem("RockBus.Composition.dll")]
- [DeploymentItem("RockBus.Logging.Trace.dll")]
- public void TestLoanApplicationStatusRequest()
- {
- var pm = DependencyManager.Instance.Resolve<IPublisherManager>();
- pm.EnableAll();
-
- // We have to wait here in order for the subscriptions to become active
- // See <internalConfiguration reinitializationIntervalMs="2000" reinitLockTimeoutMs="5000" />
- Thread.Sleep(3000);
-
- LoanProcessorService.Reset();
-
- string endpointName = "CBR_BasicHttpEndpointAsync";
- ClientBaseArgs args = new ClientBaseArgs(endpointName);
- ILoanProcessorAsync client = new LoanProcessorClientAsync(args);
-
- int id = 9996;
- int i = 0;
- LoanApplicationStatusRequest request = new LoanApplicationStatusRequest() { ID = id, SomeValue = "LoanApplicationStatusRequest" + id + "_" + i };
- IAsyncResult asyncResult = client.BeginProcessLoanApplicationStatusRequest(request, ProcessLoanApplicationStatusRequestCallback, client);
- bool callbackCalled = asyncResult.AsyncWaitHandle.WaitOne(60000);
- Assert.IsTrue(callbackCalled);
-
- for (i = 0; i < 600; i++)
- {
- if (LoanProcessorService.CallStatsSet.Count >= 1)
- {
- break;
- }
-
- Thread.Sleep(100);
- }
-
- // Due to the way the RoundRobinIndex works, the 2nd one in the list is the first one to receive the message.
- Assert.AreEqual<long>(1, LoanProcessorService.CallStatsSet.Count);
- Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9996 && cs.Count == 1 && cs.Address.StartsWith("net.pipe://")));
- Assert.AreEqual<int>(0, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9997 && cs.Count == 1 && cs.Address.StartsWith("net.pipe://")));
- //Assert.AreEqual<int>(0, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9997 && cs.Count == 1 && cs.Address.StartsWith("http://")));
-
- id = 9997;
- i = 0;
- request = new LoanApplicationStatusRequest() { ID = id, SomeValue = "LoanApplicationStatusRequest" + id + "_" + i };
- asyncResult = client.BeginProcessLoanApplicationStatusRequest(request, ProcessLoanApplicationStatusRequestCallback, client);
- callbackCalled = asyncResult.AsyncWaitHandle.WaitOne(60000);
- Assert.IsTrue(callbackCalled);
-
- for (i = 0; i < 600; i++)
- {
- if (LoanProcessorService.CallStatsSet.Count >= 2)
- {
- break;
- }
-
- Thread.Sleep(100);
- }
-
- Assert.AreEqual<long>(2, LoanProcessorService.CallStatsSet.Count);
- Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9996 && cs.Count == 1 && cs.Address.StartsWith("net.pipe://")));
- Assert.AreEqual<int>(1, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9997 && cs.Count == 1 && cs.Address.StartsWith("net.pipe://")));
- //Assert.AreEqual<int>(0, LoanProcessorService.CallStatsSet.Count(cs => cs.ID == 9997 && cs.Count == 1 && cs.Address.StartsWith("http://")));
- }
-
- private static void ProcessNewInterestRateEventCallback(IAsyncResult asyncResult)
- {
- ILoanProcessorAsync client = asyncResult.AsyncState as ILoanProcessorAsync;
- client.EndProcessNewInterestRateEvent(asyncResult);
- }
-
- private static void ProcessLoanApplicationEventCallback(IAsyncResult asyncResult)
- {
- ILoanProcessorAsync client = asyncResult.AsyncState as ILoanProcessorAsync;
- client.EndProcessLoanApplicationEvent(asyncResult);
- }
-
- private static void ProcessLoanApplicationStatusRequestCallback(IAsyncResult asyncResult)
- {
- ILoanProcessorAsync client = asyncResult.AsyncState as ILoanProcessorAsync;
- LoanApplicationStatusResponse loanApplicationStatusResponse = client.EndProcessLoanApplicationStatusRequest(asyncResult);
- }
- }
- }