diff --git a/ZeroLevel.UnitTests/ExchangeTests.cs b/ZeroLevel.UnitTests/ExchangeTests.cs new file mode 100644 index 0000000..0bd9044 --- /dev/null +++ b/ZeroLevel.UnitTests/ExchangeTests.cs @@ -0,0 +1,99 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +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 + { + Endpoint = "192.168.1.11:7755", + 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(server.Endpoint.Address.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 + { + Endpoint = "192.168.1.11:7755", + ServiceGroup = "MyServiceGroup", + ServiceKey = "MyServiceKey", + ServiceType = "MyServiceType", + Version = "1.1.1.1" + }; + var info2 = new ExServiceInfo + { + Endpoint = "192.168.41.11:4564", + 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(server.Endpoint.Address.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(); + } + } +} diff --git a/ZeroLevel.UnitTests/ZeroLevel.UnitTests.csproj b/ZeroLevel.UnitTests/ZeroLevel.UnitTests.csproj index c4739c4..8e255e4 100644 --- a/ZeroLevel.UnitTests/ZeroLevel.UnitTests.csproj +++ b/ZeroLevel.UnitTests/ZeroLevel.UnitTests.csproj @@ -51,6 +51,7 @@ + diff --git a/ZeroLevel/Services/Network/Services/DiscoveryClient.cs b/ZeroLevel/Services/Network/Services/DiscoveryClient.cs index 0562cc7..dc6833a 100644 --- a/ZeroLevel/Services/Network/Services/DiscoveryClient.cs +++ b/ZeroLevel/Services/Network/Services/DiscoveryClient.cs @@ -70,7 +70,7 @@ namespace ZeroLevel.Network public IEnumerable GetServiceEndpoints(string serviceKey) { - var key = serviceKey.Trim().ToLowerInvariant(); + var key = serviceKey.Trim().ToUpperInvariant(); _lock.EnterReadLock(); try { @@ -88,7 +88,7 @@ namespace ZeroLevel.Network public IEnumerable GetServiceEndpointsByGroup(string serviceGroup) { - var group = serviceGroup.Trim().ToLowerInvariant(); + var group = serviceGroup.Trim().ToUpperInvariant(); _lock.EnterReadLock(); try { @@ -106,7 +106,7 @@ namespace ZeroLevel.Network public IEnumerable GetServiceEndpointsByType(string serviceType) { - var type = serviceType.Trim().ToLowerInvariant(); + var type = serviceType.Trim().ToUpperInvariant(); _lock.EnterReadLock(); try { diff --git a/ZeroLevel/Services/Serialization/PrimitiveTypeSerializer.cs b/ZeroLevel/Services/Serialization/PrimitiveTypeSerializer.cs index 94f2c07..ae2f4ae 100644 --- a/ZeroLevel/Services/Serialization/PrimitiveTypeSerializer.cs +++ b/ZeroLevel/Services/Serialization/PrimitiveTypeSerializer.cs @@ -282,17 +282,25 @@ namespace ZeroLevel.Services.Serialization } else if (TypeHelpers.IsAssignableToGenericType(type, typeof(IEnumerable<>))) { - var typeArg = type.GetGenericArguments().First(); - if (_enumTypesCachee.ContainsKey(typeArg)) + Type elementType; + if (TypeHelpers.IsArray(type)) { - _concrete_type_cachee[type] = _cachee[_enumTypesCachee[typeArg]]; + elementType = type.GetElementType(); } - else if (typeof(IBinarySerializable).IsAssignableFrom(typeArg)) + else + { + elementType = type.GetGenericArguments().First(); + } + if (_enumTypesCachee.ContainsKey(elementType)) + { + _concrete_type_cachee[type] = _cachee[_enumTypesCachee[elementType]]; + } + else if (typeof(IBinarySerializable).IsAssignableFrom(elementType)) { var wrapper = new Wrapper { Invoker = InvokeWrapper.Create() }; - wrapper.ReadId = wrapper.Invoker.ConfigureGeneric(typeof(MemoryStreamReader), typeArg, "ReadCollection").First(); - wrapper.WriteId = wrapper.Invoker.ConfigureGeneric(typeof(MemoryStreamWriter), typeArg, + wrapper.ReadId = wrapper.Invoker.ConfigureGeneric(typeof(MemoryStreamReader), elementType, "ReadCollection").First(); + wrapper.WriteId = wrapper.Invoker.ConfigureGeneric(typeof(MemoryStreamWriter), elementType, mi => mi.Name.Equals("WriteCollection") && mi.IsGenericMethod).First(); _concrete_type_cachee[type] = wrapper; }