Skip to content

Commit

Permalink
Added a unit test demonstrating component lifetime.
Browse files Browse the repository at this point in the history
# Conflicts:
#	CoreRemoting.Tests/RpcTests.cs
#	CoreRemoting.Tests/ServerFixture.cs
  • Loading branch information
yallie committed Feb 11, 2025
1 parent 9547cf5 commit f2a5b0a
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
81 changes: 81 additions & 0 deletions CoreRemoting.Tests/RpcTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,87 @@ void ClientAction()

[Fact]
public void Events_should_work_remotely()
{
bool longRunnigCalled = false;
void BeforeCall(object sender, ServerRpcContext e)
{
if (e.MethodCallMessage.MethodName == "LongRunnigTestMethod")
longRunnigCalled = true;
}
try
{
_serverFixture.Server.BeforeCall += BeforeCall;
using var client = new RemotingClient(
new ClientConfig()
{
ConnectionTimeout = 0,
Channel = ClientChannel,
MessageEncryption = false,
ServerPort = _serverFixture.Server.Config.NetworkPort,
});

client.Connect();

var proxy = client.CreateProxy<ITestService>();
_ = Task.Run(() => proxy.LongRunnigTestMethod(5000));
await Task.Run(() =>
{
while (!longRunnigCalled)
Task.Delay(100);
proxy.TestMethod("x");
}).Timeout(1, "RPC timed out");
}
catch (Exception e)
{
_testOutputHelper.WriteLine(e.ToString());
throw;
}
finally
{
_serverFixture.Server.BeforeCall += BeforeCall;
}
}

[Theory]
[InlineData("TestService_Singleton_Service")]
[InlineData("TestService_Singleton_Factory")]

Check failure on line 282 in CoreRemoting.Tests/RpcTests.cs

View workflow job for this annotation

GitHub Actions / build

Test method 'Events_should_work_remotely' on test class 'CoreRemoting.Tests.RpcTests' has the same name as another method declared on class 'CoreRemoting.Tests.RpcTests'. Rename method(s) so that there are no overloaded names. (https://xunit.net/xunit.analyzers/rules/xUnit1024)

Check failure on line 282 in CoreRemoting.Tests/RpcTests.cs

View workflow job for this annotation

GitHub Actions / build

Test method 'Events_should_work_remotely' on test class 'CoreRemoting.Tests.RpcTests' has the same name as another method declared on class 'CoreRemoting.Tests.RpcTests'. Rename method(s) so that there are no overloaded names. (https://xunit.net/xunit.analyzers/rules/xUnit1024)
[InlineData("TestService_SingleCall_Service")]
[InlineData("TestService_SingleCall_Factory")]
[InlineData("TestService_Scoped_Service")]
[InlineData("TestService_Scoped_Factory")]
public void Component_lifetime_matches_the_expectation(string serviceName)
{
using var ctx = ValidationSyncContext.Install();
using var client = new RemotingClient(
new ClientConfig()
{
ConnectionTimeout = 0,
SendTimeout = 0,
Channel = ClientChannel,
MessageEncryption = false,
ServerPort = _serverFixture.Server.Config.NetworkPort,
});

client.Connect();

var proxy = client.CreateProxy<ITestService>(serviceName);

// check if service lifetime matches the expectations
proxy.SaveLastInstance();

Check failure on line 306 in CoreRemoting.Tests/RpcTests.cs

View workflow job for this annotation

GitHub Actions / build

The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

Check failure on line 306 in CoreRemoting.Tests/RpcTests.cs

View workflow job for this annotation

GitHub Actions / build

The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
// singleton component should have the same instance
var sameInstance = serviceName.Contains("Singleton");
Assert.Equal(sameInstance, proxy.CheckLastSavedInstance());
}

[Theory]
[InlineData("TestService_Singleton_Service")]
[InlineData("TestService_Singleton_Factory")]
[InlineData("TestService_SingleCall_Service")]
[InlineData("TestService_SingleCall_Factory")]
[InlineData("TestService_Scoped_Service")]
[InlineData("TestService_Scoped_Factory")]
public void Events_should_work_remotely(string serviceName)
{
using var ctx = ValidationSyncContext.Install();

Expand Down
23 changes: 23 additions & 0 deletions CoreRemoting.Tests/ServerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ public ServerFixture()
factoryDelegate: () => TestService,
lifetime: ServiceLifetime.Singleton);

// Services for event tests
container.RegisterService<ITestService, TestService>(
lifetime: ServiceLifetime.Singleton,
serviceName: "TestService_Singleton_Service");
container.RegisterService<ITestService, TestService>(
lifetime: ServiceLifetime.SingleCall,
serviceName: "TestService_SingleCall_Service");
container.RegisterService<ITestService, TestService>(
lifetime: ServiceLifetime.Scoped,
serviceName: "TestService_Scoped_Service");
container.RegisterService<ITestService>(
factoryDelegate: () => new TestService(),
lifetime: ServiceLifetime.Singleton,
serviceName: "TestService_Singleton_Factory");
container.RegisterService<ITestService>(
factoryDelegate: () => new TestService(),
lifetime: ServiceLifetime.SingleCall,
serviceName: "TestService_SingleCall_Factory");
container.RegisterService<ITestService>(
factoryDelegate: () => new TestService(),
lifetime: ServiceLifetime.Scoped,
serviceName: "TestService_Scoped_Factory");

// Service for async tests
container.RegisterService<IAsyncService, AsyncService>(
lifetime: ServiceLifetime.Singleton);
Expand Down
6 changes: 5 additions & 1 deletion CoreRemoting.Tests/Tools/ITestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface ITestService : IBaseService
event Action ServiceEvent;

event ServerEventHandler CustomDelegateEvent;

object TestMethod(object arg);

void TestMethodWithDelegateArg(Action<string> callback);
Expand Down Expand Up @@ -42,4 +42,8 @@ public interface ITestService : IBaseService
DataTable TestDt(DataTable dt, long num);

(T duplicate, int size) Duplicate<T>(T sample) where T : class;

void SaveLastInstance();

bool CheckLastSavedInstance();
}
10 changes: 9 additions & 1 deletion CoreRemoting.Tests/Tools/TestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ namespace CoreRemoting.Tests.Tools;

public class TestService : ITestService
{

private int _counter;

public Func<object, object> TestMethodFake { get; set; }

public Action OneWayMethodFake { get; set; }
Expand Down Expand Up @@ -124,4 +125,11 @@ TItem[] Dup<TItem>(TItem[] arr)
return arr;
}
}

private static TestService LastInstance { get; set; }

public void SaveLastInstance() => LastInstance = this;

public bool CheckLastSavedInstance() => LastInstance == this;

}

0 comments on commit f2a5b0a

Please sign in to comment.