using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; using ZeroLevel.Network; namespace ZeroLevel.NetworkUnitTests { [TestClass] public class ExchangeTests { [TestMethod] public void HandleMessageTest() { // Arrange var info = new ExServiceInfo { ServiceGroup = "MyServiceGroup", ServiceKey = "MyServiceKey", ServiceType = "MyServiceType", Version = "1.1.1.1" }; var locker = new ManualResetEvent(false); var server = ExchangeTransportFactory.GetServer(6666); ExServiceInfo received = null; server.RegisterInbox("register", (i, _, __) => { received = i; locker.Set(); }); // Act var client = ExchangeTransportFactory.GetClient(IPAddress.Loopback.ToString() + ":6666"); var ir = client.Send("register", info); locker.WaitOne(1000); // Assert Assert.IsTrue(ir.Success); Assert.IsTrue(info.Equals(received)); // Dispose locker.Dispose(); client.Dispose(); server.Dispose(); } [TestMethod] public void RequestMessageTest() { // Arrange var info1 = new ExServiceInfo { ServiceGroup = "MyServiceGroup", ServiceKey = "MyServiceKey", ServiceType = "MyServiceType", Version = "1.1.1.1" }; var info2 = new ExServiceInfo { ServiceGroup = "MyServiceGroup", ServiceKey = "MyServiceKey2", ServiceType = "MyServiceType", Version = "1.1.0.1" }; var locker = new ManualResetEvent(false); var server = ExchangeTransportFactory.GetServer(6666); IEnumerable received = null; server.RegisterInbox>("services", (_, __) => new[] { info1, info2 }); // Act var client = ExchangeTransportFactory.GetClient(IPAddress.Loopback.ToString() + ":6666"); var ir = client.Request>("services", response => { received = response; locker.Set(); }); locker.WaitOne(1000); // Assert Assert.IsTrue(ir.Success); Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(new[] { info1, info2 }, received, (a, b) => a.Equals(b))); // Dispose locker.Dispose(); client.Dispose(); server.Dispose(); } } }