Added test for network

pull/1/head
a.bozhenov 6 years ago
parent 63eb20dcef
commit b38dd5508e

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Xunit;
using ZeroLevel.Network;
namespace ZeroLevel.UnitTests
{
public class NetworkTest
{
[Fact]
public void ClientServerTest()
{
// Arrange
var server = ExchangeTransportFactory.GetServer(8181);
var client = ExchangeTransportFactory.GetClient("127.0.0.1:8181");
bool got_message_no_request = false;
bool got_message_with_request = false;
bool got_response_message_no_request = false;
bool got_response_message_with_request = false;
using (var signal = new ManualResetEvent(false))
{
server.RegisterInbox("empty", (_, __) => { signal.Set(); got_message_no_request = true; });
server.RegisterInbox<string>((_, __, ___) => { signal.Set(); got_message_with_request = true; });
server.RegisterInbox<string>("get_response", (_, __) => { return "Hello"; });
server.RegisterInbox<int, string>("convert", (num, _, __) => { return num.ToString(); });
// Act
signal.Reset();
client.Send("empty");
signal.WaitOne(1000);
signal.Reset();
client.Send<string>("hello");
signal.WaitOne(100);
signal.Reset();
client.Request<string>("get_response", (s) => { signal.Set(); got_response_message_no_request = s.Equals("Hello", StringComparison.Ordinal); });
signal.WaitOne(1000);
signal.Reset();
client.Request<int, string>("convert", 10, (s) => { signal.Set(); got_response_message_with_request = s.Equals("10", StringComparison.Ordinal); });
signal.WaitOne(1000);
}
// Assert
Assert.True(got_message_no_request, "No signal for no request default inbox");
Assert.True(got_message_with_request, "No signal for default inbox");
Assert.True(got_response_message_no_request, "No response without request");
Assert.True(got_response_message_with_request, "No response with request");
}
}
}

@ -15,6 +15,10 @@ namespace ZeroLevel.Network
IPEndPoint Endpoint { get; } IPEndPoint Endpoint { get; }
InvokeResult Send();
InvokeResult Send(string inbox);
InvokeResult Send<T>(T obj); InvokeResult Send<T>(T obj);
InvokeResult Send<T>(string inbox, T obj); InvokeResult Send<T>(string inbox, T obj);

@ -12,6 +12,8 @@ namespace ZeroLevel.Network
void RegisterInbox<T>(string inbox, Action<T, long, IZBackward> handler); void RegisterInbox<T>(string inbox, Action<T, long, IZBackward> handler);
void RegisterInbox(string inbox, Action<long, IZBackward> handler);
void RegisterInbox<Treq, Tresp>(string inbox, Func<Treq, long, IZBackward, Tresp> handler); void RegisterInbox<Treq, Tresp>(string inbox, Func<Treq, long, IZBackward, Tresp> handler);
/// <summary> /// <summary>
@ -19,9 +21,10 @@ namespace ZeroLevel.Network
/// </summary> /// </summary>
void RegisterInbox<Tresp>(string inbox, Func<long, IZBackward, Tresp> handler); void RegisterInbox<Tresp>(string inbox, Func<long, IZBackward, Tresp> handler);
/* /*
DEFAULT INBOXES DEFAULT INBOXES
*/ */
void RegisterInbox(Action<long, IZBackward> handler);
void RegisterInbox<T>(Action<T, long, IZBackward> handler); void RegisterInbox<T>(Action<T, long, IZBackward> handler);
void RegisterInbox<Treq, Tresp>(Func<Treq, long, IZBackward, Tresp> handler); void RegisterInbox<Treq, Tresp>(Func<Treq, long, IZBackward, Tresp> handler);
void RegisterInbox<Tresp>(Func<long, IZBackward, Tresp> handler); void RegisterInbox<Tresp>(Func<long, IZBackward, Tresp> handler);

@ -110,5 +110,15 @@ namespace ZeroLevel.Network
{ {
return Send(inbox, obj); return Send(inbox, obj);
} }
public InvokeResult Send()
{
return _fe.Send(DEFAULT_MESSAGE_INBOX);
}
public InvokeResult Send(string inbox)
{
return _fe.Send(inbox);
}
} }
} }

@ -45,8 +45,21 @@ namespace ZeroLevel.Network
private Invoker _invoker; private Invoker _invoker;
private Type _typeReq; private Type _typeReq;
private Type _typeResp; private Type _typeResp;
private bool _noArguments = false;
public static MRInvoker Create<T>(string inbox, Action<T, long, IZBackward> handler) public static MRInvoker Create(Action<long, IZBackward> handler)
{
return new MRInvoker
{
_noArguments = true,
_typeReq = null,
_typeResp = null,
_instance = handler.Target,
_invoker = CreateCompiledExpression(handler)
};
}
public static MRInvoker Create<T>(Action<T, long, IZBackward> handler)
{ {
return new MRInvoker return new MRInvoker
{ {
@ -57,7 +70,7 @@ namespace ZeroLevel.Network
}; };
} }
public static MRInvoker Create<Treq, Tresp>(string inbox, Func<Treq, long, IZBackward, Tresp> handler) public static MRInvoker Create<Treq, Tresp>(Func<Treq, long, IZBackward, Tresp> handler)
{ {
return new MRInvoker return new MRInvoker
{ {
@ -68,7 +81,7 @@ namespace ZeroLevel.Network
}; };
} }
public static MRInvoker Create<Tresp>(string inbox, Func<long, IZBackward, Tresp> handler) public static MRInvoker Create<Tresp>(Func<long, IZBackward, Tresp> handler)
{ {
return new MRInvoker return new MRInvoker
{ {
@ -84,8 +97,15 @@ namespace ZeroLevel.Network
if (_typeResp == null) if (_typeResp == null)
{ {
var incoming = MessageSerializer.DeserializeCompatible(_typeReq, frame.Payload); var incoming = MessageSerializer.DeserializeCompatible(_typeReq, frame.Payload);
if (_noArguments)
{
this._invoker.Invoke(this._instance, new object[] { frame.FrameId, client });
}
else
{
this._invoker.Invoke(this._instance, new object[] { incoming, frame.FrameId, client }); this._invoker.Invoke(this._instance, new object[] { incoming, frame.FrameId, client });
} }
}
else if (_typeReq == null) else if (_typeReq == null)
{ {
return this._invoker.Invoke(this._instance, new object[] { frame.FrameId, client }); return this._invoker.Invoke(this._instance, new object[] { frame.FrameId, client });
@ -109,20 +129,29 @@ namespace ZeroLevel.Network
#region Registration #region Registration
public void RegisterInbox(string inbox, Action<long, IZBackward> handler)
{
if (false == _handlers.ContainsKey(inbox))
{
_handlers.Add(inbox, new List<MRInvoker>());
}
_handlers[inbox].Add(MRInvoker.Create(handler));
}
public void RegisterInbox<T>(string inbox, Action<T, long, IZBackward> handler) public void RegisterInbox<T>(string inbox, Action<T, long, IZBackward> handler)
{ {
if (false == _handlers.ContainsKey(inbox)) if (false == _handlers.ContainsKey(inbox))
{ {
_handlers.Add(inbox, new List<MRInvoker>()); _handlers.Add(inbox, new List<MRInvoker>());
} }
_handlers[inbox].Add(MRInvoker.Create<T>(inbox, handler)); _handlers[inbox].Add(MRInvoker.Create<T>(handler));
} }
public void RegisterInbox<Treq, Tresp>(string inbox, Func<Treq, long, IZBackward, Tresp> hanlder) public void RegisterInbox<Treq, Tresp>(string inbox, Func<Treq, long, IZBackward, Tresp> hanlder)
{ {
if (false == _requestors.ContainsKey(inbox)) if (false == _requestors.ContainsKey(inbox))
{ {
_requestors.Add(inbox, MRInvoker.Create<Treq, Tresp>(inbox, hanlder)); _requestors.Add(inbox, MRInvoker.Create<Treq, Tresp>(hanlder));
} }
else else
{ {
@ -134,7 +163,7 @@ namespace ZeroLevel.Network
{ {
if (false == _requestors.ContainsKey(inbox)) if (false == _requestors.ContainsKey(inbox))
{ {
_requestors.Add(inbox, MRInvoker.Create<Tresp>(inbox, hanlder)); _requestors.Add(inbox, MRInvoker.Create<Tresp>(hanlder));
} }
else else
{ {

@ -96,6 +96,16 @@ namespace ZeroLevel.Network
_router.RegisterInbox<Tresp>(DEFAULT_REQUEST_INBOX, handler); _router.RegisterInbox<Tresp>(DEFAULT_REQUEST_INBOX, handler);
} }
public void RegisterInbox(string inbox, Action<long, IZBackward> handler)
{
_router.RegisterInbox(inbox, handler);
}
public void RegisterInbox(Action<long, IZBackward> handler)
{
_router.RegisterInbox(DEFAULT_REQUEST_INBOX, handler);
}
public override void Dispose() public override void Dispose()
{ {
_server.Dispose(); _server.Dispose();

@ -31,6 +31,21 @@ namespace ZeroLevel.Network
} }
} }
public InvokeResult Send(string inbox)
{
try
{
var frame = FrameBuilder.BuildFrame(inbox);
_current.Send(frame);
return InvokeResult.Succeeding();
}
catch (Exception ex)
{
Log.SystemError(ex, "[FrameExchange] Fault send frame");
return InvokeResult.Fault(ex.Message);
}
}
public InvokeResult Send(Frame frame) public InvokeResult Send(Frame frame)
{ {
try try

Loading…
Cancel
Save

Powered by TurnKey Linux.