Serialization fixed

pull/1/head
Ogoun 6 years ago
parent 2f2703bdc9
commit 75a6a7b2a8

@ -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<ExServiceInfo>("register", (i, _, __) =>
{
received = i;
locker.Set();
});
// Act
var client = ExchangeTransportFactory.GetClient(server.Endpoint.Address.ToString() + ":6666");
var ir = client.Send<ExServiceInfo>("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<ExServiceInfo> received = null;
server.RegisterInbox<IEnumerable<ExServiceInfo>>("services", (_, __) => new[] { info1, info2 });
// Act
var client = ExchangeTransportFactory.GetClient(server.Endpoint.Address.ToString() + ":6666");
var ir = client.Request<IEnumerable<ExServiceInfo>>("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();
}
}
}

@ -51,6 +51,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="ArrayToolsTest.cs" /> <Compile Include="ArrayToolsTest.cs" />
<Compile Include="CollectionsTests.cs" /> <Compile Include="CollectionsTests.cs" />
<Compile Include="ExchangeTests.cs" />
<Compile Include="InvokingTest.cs" /> <Compile Include="InvokingTest.cs" />
<Compile Include="MappingTest.cs" /> <Compile Include="MappingTest.cs" />
<Compile Include="Models\BaseClass.cs" /> <Compile Include="Models\BaseClass.cs" />

@ -70,7 +70,7 @@ namespace ZeroLevel.Network
public IEnumerable<ServiceEndpointInfo> GetServiceEndpoints(string serviceKey) public IEnumerable<ServiceEndpointInfo> GetServiceEndpoints(string serviceKey)
{ {
var key = serviceKey.Trim().ToLowerInvariant(); var key = serviceKey.Trim().ToUpperInvariant();
_lock.EnterReadLock(); _lock.EnterReadLock();
try try
{ {
@ -88,7 +88,7 @@ namespace ZeroLevel.Network
public IEnumerable<ServiceEndpointInfo> GetServiceEndpointsByGroup(string serviceGroup) public IEnumerable<ServiceEndpointInfo> GetServiceEndpointsByGroup(string serviceGroup)
{ {
var group = serviceGroup.Trim().ToLowerInvariant(); var group = serviceGroup.Trim().ToUpperInvariant();
_lock.EnterReadLock(); _lock.EnterReadLock();
try try
{ {
@ -106,7 +106,7 @@ namespace ZeroLevel.Network
public IEnumerable<ServiceEndpointInfo> GetServiceEndpointsByType(string serviceType) public IEnumerable<ServiceEndpointInfo> GetServiceEndpointsByType(string serviceType)
{ {
var type = serviceType.Trim().ToLowerInvariant(); var type = serviceType.Trim().ToUpperInvariant();
_lock.EnterReadLock(); _lock.EnterReadLock();
try try
{ {

@ -282,17 +282,25 @@ namespace ZeroLevel.Services.Serialization
} }
else if (TypeHelpers.IsAssignableToGenericType(type, typeof(IEnumerable<>))) else if (TypeHelpers.IsAssignableToGenericType(type, typeof(IEnumerable<>)))
{ {
var typeArg = type.GetGenericArguments().First(); Type elementType;
if (_enumTypesCachee.ContainsKey(typeArg)) 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() }; var wrapper = new Wrapper { Invoker = InvokeWrapper.Create() };
wrapper.ReadId = wrapper.Invoker.ConfigureGeneric(typeof(MemoryStreamReader), typeArg, "ReadCollection").First(); wrapper.ReadId = wrapper.Invoker.ConfigureGeneric(typeof(MemoryStreamReader), elementType, "ReadCollection").First();
wrapper.WriteId = wrapper.Invoker.ConfigureGeneric(typeof(MemoryStreamWriter), typeArg, wrapper.WriteId = wrapper.Invoker.ConfigureGeneric(typeof(MemoryStreamWriter), elementType,
mi => mi.Name.Equals("WriteCollection") && mi.IsGenericMethod).First(); mi => mi.Name.Equals("WriteCollection") && mi.IsGenericMethod).First();
_concrete_type_cachee[type] = wrapper; _concrete_type_cachee[type] = wrapper;
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.