Added unit test for proxy

pull/1/head
unknown 4 years ago
parent 14b41cc4ff
commit 59e855e31e

@ -2,6 +2,7 @@
using System.Threading; using System.Threading;
using Xunit; using Xunit;
using ZeroLevel.Services.Applications; using ZeroLevel.Services.Applications;
using ZeroLevel.Services.Network.Proxies;
namespace ZeroLevel.Network namespace ZeroLevel.Network
{ {
@ -63,6 +64,42 @@ namespace ZeroLevel.Network
} }
} }
[Fact]
public void ProxyTest()
{
bool got_message_no_request = false;
bool got_message_with_request = false;
using (var proxy = new Proxy(NetUtils.CreateIPEndPoint("127.0.0.1:92")))
{
proxy.AppendServer(NetUtils.CreateIPEndPoint("127.0.0.1:93"));
proxy.Run();
var server = Exchange.UseHost(NetUtils.CreateIPEndPoint("127.0.0.1:93"));
server.RegisterInbox("empty", (_) =>
{
got_message_no_request = true;
});
server.RegisterInbox<bool>((_, ___) =>
{
got_message_with_request = true;
});
var client = Exchange.GetConnection(NetUtils.CreateIPEndPoint("127.0.0.1:92"));
int repeat = 10;
Assert.True(client.Send("empty"));
Assert.True(client.Send<bool>(true));
while (!got_message_no_request || !got_message_with_request)
{
Thread.Sleep(200);
repeat--;
if (repeat == 0) break;
}
}
Assert.True(got_message_no_request, "No signal for no request default inbox");
Assert.True(got_message_with_request, "No signal for default inbox");
}
protected override void StartAction() protected override void StartAction()
{ {
throw new NotImplementedException(); throw new NotImplementedException();

@ -1,10 +1,12 @@
using System.Net; using System;
using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ZeroLevel.Services.Network.Proxies namespace ZeroLevel.Services.Network.Proxies
{ {
public class Proxy public class Proxy
: IDisposable
{ {
private readonly ProxyBalancer _balancer = new ProxyBalancer(); private readonly ProxyBalancer _balancer = new ProxyBalancer();
@ -17,29 +19,59 @@ namespace ZeroLevel.Services.Network.Proxies
_incomingSocket = new Socket(listenEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); _incomingSocket = new Socket(listenEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
_incomingSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true); _incomingSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);
_incomingSocket.Bind(listenEndpoint); _incomingSocket.Bind(listenEndpoint);
_incomingSocket.Listen(100);
} }
public async Task Run() public void Run()
{ {
while (true) Task.Factory.StartNew(async () =>
{
try
{
_incomingSocket.Listen(100);
while (true)
{
var socket = await _incomingSocket.AcceptAsync();
// no await!
Task.Run(async () =>
{
await CreateProxyConnection(socket);
});
}
}
catch (Exception ex)
{
Log.Error(ex, "[Proxy.Run]");
}
});
}
private async Task CreateProxyConnection(Socket connection)
{
try
{
var endpoint = _balancer.GetServerProxy();
var server = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);
server.Connect(endpoint);
using (var bind = new ProxyBinding(connection, server))
{
await bind.Bind();
}
}
catch (Exception ex)
{ {
var socket = await _incomingSocket.AcceptAsync();
// no await!
CreateProxyConnection(socket);
} }
} }
public async Task CreateProxyConnection(Socket connection) public void Dispose()
{ {
var endpoint = _balancer.GetServerProxy(); try
var server = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);
server.Bind(endpoint);
using (var bind = new ProxyBinding(connection, server))
{ {
await bind.Bind(); _incomingSocket.Shutdown(SocketShutdown.Both);
_incomingSocket.Dispose();
} }
catch (Exception ex)
{ }
} }
} }
} }

@ -6,16 +6,16 @@
</Description> </Description>
<Authors>ogoun</Authors> <Authors>ogoun</Authors>
<Company>ogoun</Company> <Company>ogoun</Company>
<AssemblyVersion>3.3.5.1</AssemblyVersion> <AssemblyVersion>3.3.5.2</AssemblyVersion>
<PackageReleaseNotes>Light Fix in MemoryStreamWriter, MemoryStreamReader</PackageReleaseNotes> <PackageReleaseNotes>Added tcp proxy</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/ogoun/Zero/wiki</PackageProjectUrl> <PackageProjectUrl>https://github.com/ogoun/Zero/wiki</PackageProjectUrl>
<Copyright>Copyright Ogoun 2020</Copyright> <Copyright>Copyright Ogoun 2021</Copyright>
<PackageLicenseUrl></PackageLicenseUrl> <PackageLicenseUrl></PackageLicenseUrl>
<PackageIconUrl></PackageIconUrl> <PackageIconUrl></PackageIconUrl>
<RepositoryUrl>https://github.com/ogoun/Zero</RepositoryUrl> <RepositoryUrl>https://github.com/ogoun/Zero</RepositoryUrl>
<RepositoryType>GitHub</RepositoryType> <RepositoryType>GitHub</RepositoryType>
<Version>3.3.5.1</Version> <Version>3.3.5.2</Version>
<FileVersion>3.3.5.1</FileVersion> <FileVersion>3.3.5.2</FileVersion>
<Platforms>AnyCPU;x64;x86</Platforms> <Platforms>AnyCPU;x64;x86</Platforms>
<PackageIcon>zero.png</PackageIcon> <PackageIcon>zero.png</PackageIcon>
</PropertyGroup> </PropertyGroup>

Loading…
Cancel
Save

Powered by TurnKey Linux.