/Main/src/Tests/RockBus.Samples.3DynamicPublishSubscribe/Sample3DynamicPublishSubscribeTests.cs
C# | 255 lines | 200 code | 41 blank | 14 comment | 50 complexity | af2d637404a268656355cd76f038011a MD5 | raw file
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using RockBus.Clients.Interfaces;
- using RockBus.Clients.LoanProcessor;
- using RockBus.Composition.Interfaces;
- using RockBus.Initialization.Interfaces;
- using RockBus.Logging.Trace;
- using RockBus.ServiceBus;
- using RockBus.ServiceModel;
- using RockBus.SubscriptionRepository.Db;
- 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._3DynamicPublishSubscribe
- {
- [TestClass]
- public class Sample3DynamicPublishSubscribeTests
- {
- private static ServiceHost _cbrServiceHost;
- private static ServiceHost _loanProcessorServiceHost;
- private static ServiceBusEnvironment _sbe;
- private static RockBus.SubscriptionRepository.Db.SubscriptionRepository SubscriptionRepository;
-
- [TestInitialize()]
- public void TestInitialize()
- {
- var initializationManager = DependencyManager.Instance.Resolve<IInitializationManager>();
- initializationManager.Initialize();
-
- var logger = new TraceLogger();
-
- // Start RockBus and subscriber services
- _cbrServiceHost = ServiceHostUtils.OpenRockBusServiceHost(new TraceLogger());
- _loanProcessorServiceHost = ServiceHostUtils.OpenHost<LoanProcessorService>(new Uri[] { new Uri("net.pipe://localhost/LoanProcessorService/"), new Uri("http://localhost:9997/LoanProcessorService/") });
- DbSubscriptionRepositoryConfiguration config = new DbSubscriptionRepositoryConfiguration();
- SubscriptionRepository = new RockBus.SubscriptionRepository.Db.SubscriptionRepository(logger, config);
-
- // Clear all Subscriptions
- SubscriptionRepository.ClearAllClients();
-
- // Set up subscriptions
- _sbe = new ServiceBusEnvironment(logger);
- _sbe.ConfigureSubscribersAsync().Wait();
- }
-
- [TestCleanup()]
- public void TestCleanup()
- {
- // Unsubscribe
- _sbe.UnconfigureSubscribersAsync().Wait();
-
- // Close all services
- 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, 5000);
- }
-
- [TestMethod]
- [TestCategory("Samples")]
- [DeploymentItem("RockBus.Composition.dll")]
- [DeploymentItem("RockBus.Logging.Trace.dll")]
- public void TestNewInterestRateEvent()
- {
- // 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 < 100; 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()
- {
- // 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()
- {
- // 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);
- }
- }
- }