Skip to content

Commit

Permalink
- Implemented TCP channel pair using WatsonTCP
Browse files Browse the repository at this point in the history
- Reusing server for multiple unit tests via class fixture
  • Loading branch information
theRainbird committed Jan 22, 2023
1 parent 946f23b commit 9d71a25
Show file tree
Hide file tree
Showing 27 changed files with 666 additions and 391 deletions.
60 changes: 25 additions & 35 deletions CoreRemoting.Tests/AsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,55 @@
using System.Text;
using System.Threading.Tasks;
using CoreRemoting.DependencyInjection;
using CoreRemoting.Tests.Tools;
using Xunit;

namespace CoreRemoting.Tests
{
public class AsyncTests
public class AsyncTests : IClassFixture<ServerFixture>
{
#region Service with async method
private ServerFixture _serverFixture;

public interface IAsyncService
public AsyncTests(ServerFixture serverFixture)
{
Task<string> ConvertToBase64Async(string text);
_serverFixture = serverFixture;
}

public class AsyncService : IAsyncService

[Fact]
public async void AsyncMethods_should_work()
{
public async Task<string> ConvertToBase64Async(string text)
using var client = new RemotingClient(new ClientConfig()
{
var convertFunc = new Func<string>(() =>
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(text));
return Convert.ToBase64String(stream.ToArray());
});
ConnectionTimeout = 0,
InvocationTimeout = 0,
ServerPort = _serverFixture.Server.Config.NetworkPort
});

client.Connect();
var proxy = client.CreateProxy<IAsyncService>();

var base64String = await Task.Run(convertFunc);
var base64String = await proxy.ConvertToBase64Async("Yay");

return base64String;
}
Assert.Equal("WWF5", base64String);
}

#endregion

[Fact]
public async void AsyncMethods_should_work()
/// <summary>
/// Awaiting for ordinary non-generic task method should not hangs.
/// </summary>
[Fact(Timeout = 15000)]
public async void AwaitingNonGenericTask_should_not_hang_forever()
{
var serverConfig =
new ServerConfig()
{
NetworkPort = 9196,
RegisterServicesAction = container =>
container.RegisterService<IAsyncService, AsyncService>(
lifetime: ServiceLifetime.Singleton)
};

using var server = new RemotingServer(serverConfig);
server.Start();

using var client = new RemotingClient(new ClientConfig()
{
ConnectionTimeout = 0,
InvocationTimeout = 0,
ServerPort = 9196
ServerPort = _serverFixture.Server.Config.NetworkPort
});

client.Connect();
var proxy = client.CreateProxy<IAsyncService>();

var base64String = await proxy.ConvertToBase64Async("Yay");

Assert.Equal("WWF5", base64String);
await proxy.NonGenericTask();
}
}
}
42 changes: 15 additions & 27 deletions CoreRemoting.Tests/CallContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,24 @@

namespace CoreRemoting.Tests
{
public class CallContextTests
public class CallContextTests : IClassFixture<ServerFixture>
{
private ServerFixture _serverFixture;

public CallContextTests(ServerFixture serverFixture)
{
_serverFixture = serverFixture;
}

[Fact]
public void CallContext_should_flow_from_client_to_server_and_back()
{
var testService =
new TestService
{
TestMethodFake = _ =>
{
CallContext.SetData("test", "Changed");
return CallContext.GetData("test");
}
};

var serverConfig =
new ServerConfig()
{
NetworkPort = 9093,
RegisterServicesAction = container =>
container.RegisterService<ITestService>(
factoryDelegate: () => testService,
lifetime: ServiceLifetime.Singleton)
};

using var server = new RemotingServer(serverConfig);
server.Start();

_serverFixture.TestService.TestMethodFake = _ =>
{
CallContext.SetData("test", "Changed");
return CallContext.GetData("test");
};

var clientThread =
new Thread(() =>
{
Expand All @@ -41,7 +31,7 @@ public void CallContext_should_flow_from_client_to_server_and_back()
var client =
new RemotingClient(new ClientConfig()
{
ServerPort = 9093,
ServerPort = _serverFixture.Server.Config.NetworkPort,
ConnectionTimeout = 0
});

Expand All @@ -63,8 +53,6 @@ public void CallContext_should_flow_from_client_to_server_and_back()

clientThread.Start();
clientThread.Join();

server.Dispose();
}
}
}
49 changes: 6 additions & 43 deletions CoreRemoting.Tests/LinqExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,27 @@
using System.Linq;
using System.Linq.Expressions;
using CoreRemoting.DependencyInjection;
using CoreRemoting.Tests.Tools;
using Xunit;

namespace CoreRemoting.Tests
{
public class LinqExpressionTests
public class LinqExpressionTests : IClassFixture<ServerFixture>
{
#region Service with method using expressions
private ServerFixture _serverFixture;

public class Hobbit
public LinqExpressionTests(ServerFixture serverFixture)
{
public string FirstName { get; set; }
public string LastName { get; set; }
_serverFixture = serverFixture;
}

public interface IHobbitService
{
Hobbit QueryHobbits(Expression<Func<Hobbit, bool>> criteriaExpression);
}

public class HobbitService : IHobbitService
{
private List<Hobbit> _hobbits =
new List<Hobbit>
{
new Hobbit {FirstName = "Bilbo", LastName = "Baggins"},
new Hobbit {FirstName = "Frodo", LastName = "Baggins"},
new Hobbit {FirstName = "Peregrin", LastName = "Tuck"}
};

public Hobbit QueryHobbits(Expression<Func<Hobbit, bool>> criteriaExpression)
{
var criteria = criteriaExpression.Compile();

return _hobbits.FirstOrDefault(criteria);
}
}

#endregion

[Fact]
public void LinqExpression_should_be_serialized_and_deserialized()
{
var serverConfig =
new ServerConfig()
{
NetworkPort = 9198,
RegisterServicesAction = container =>
container.RegisterService<IHobbitService, HobbitService>(
lifetime: ServiceLifetime.Singleton)
};

using var server = new RemotingServer(serverConfig);
server.Start();

using var client = new RemotingClient(new ClientConfig()
{
ConnectionTimeout = 0,
ServerPort = 9198
ServerPort = _serverFixture.Server.Config.NetworkPort
});

client.Connect();
Expand Down
24 changes: 12 additions & 12 deletions CoreRemoting.Tests/RemotingConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@

namespace CoreRemoting.Tests
{
public class RemotingConfigurationTests
public class RemotingConfigurationTests : IClassFixture<ServerFixture>
{
private ServerFixture _serverFixture;

public RemotingConfigurationTests(ServerFixture serverFixture)
{
_serverFixture = serverFixture;
}

[Fact]
public void RegisterWellKnownServiceType_should_register_type_resolved_at_runtime()
{
using var server = new RemotingServer(new ServerConfig { UniqueServerInstanceName = "Server1" });

RemotingConfiguration.RegisterWellKnownServiceType(
interfaceType: typeof(ITestService),
implementationType: typeof(TestService),
lifetime: ServiceLifetime.Singleton,
serviceName: "Service1",
uniqueServerInstanceName: "Server1");

var service = server.ServiceRegistry.GetService("Service1");
var service = _serverFixture.Server.ServiceRegistry.GetService("Service1");

Assert.NotNull(service);
Assert.Equal(typeof(TestService), service.GetType());
Expand All @@ -36,16 +41,11 @@ public void RegisterWellKnownServiceType_should_register_type_resolved_at_runtim
[Fact]
public void RemotingServer_should_register_on_construction_AND_unregister_on_Dispose()
{
using var server = new RemotingServer(new ServerConfig()
{
UniqueServerInstanceName = "TestServer"
});

Assert.NotNull(RemotingConfiguration.GetRegisteredServer("TestServer"));
Assert.NotNull(RemotingConfiguration.GetRegisteredServer("Server1"));

server.Dispose();
_serverFixture.Server.Dispose();

Assert.Null(RemotingConfiguration.GetRegisteredServer("TestServer"));
Assert.Null(RemotingConfiguration.GetRegisteredServer("Server1"));
}

[Fact]
Expand Down
31 changes: 7 additions & 24 deletions CoreRemoting.Tests/ReturnAsProxyTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Xunit;
using CoreRemoting;
using System;
using System.Threading;
using CoreRemoting.ClassicRemotingApi;
Expand All @@ -7,47 +9,28 @@

namespace CoreRemoting.Tests
{
using Xunit;
using CoreRemoting;

public class ReturnAsProxyTests
public class ReturnAsProxyTests : IClassFixture<ServerFixture>
{
private ServerFixture _serverFixture;
private readonly ITestOutputHelper _testOutputHelper;

public ReturnAsProxyTests(ITestOutputHelper testOutputHelper)
public ReturnAsProxyTests(ServerFixture serverFixture, ITestOutputHelper testOutputHelper)
{
_serverFixture = serverFixture;
_testOutputHelper = testOutputHelper;
}

[Fact]
public void Call_on_Proxy_should_be_invoked_on_remote_service()
{
var factoryService = new FactoryService();

var serverConfig =
new ServerConfig()
{
NetworkPort = 9084,
RegisterServicesAction = container =>
{
container.RegisterService<IFactoryService>(
factoryDelegate: () => factoryService,
lifetime: ServiceLifetime.Singleton);
},
IsDefault = true
};

using var server = new RemotingServer(serverConfig);
server.Start();

void ClientAction()
{
try
{
using var client = new RemotingClient(new ClientConfig()
{
ConnectionTimeout = 0,
ServerPort = 9084
ServerPort = _serverFixture.Server.Config.NetworkPort
});

client.Connect();
Expand Down
Loading

0 comments on commit 9d71a25

Please sign in to comment.