From e8667e1c8b9d4142981524f67beb821490fb12a4 Mon Sep 17 00:00:00 2001 From: yallie Date: Wed, 4 Dec 2024 04:19:32 +0300 Subject: [PATCH] Single-call server component doesn't see the current remote user's session in its constructor, see #87. --- CoreRemoting.Tests/ServerFixture.cs | 6 ++- CoreRemoting.Tests/SessionTests.cs | 37 +++++++++++++++---- .../Tools/ISessionAwareService.cs | 7 ++++ .../Tools/SessionAwareService.cs | 19 ++++++++++ 4 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 CoreRemoting.Tests/Tools/ISessionAwareService.cs create mode 100644 CoreRemoting.Tests/Tools/SessionAwareService.cs diff --git a/CoreRemoting.Tests/ServerFixture.cs b/CoreRemoting.Tests/ServerFixture.cs index abcb3d0..fb39bcb 100644 --- a/CoreRemoting.Tests/ServerFixture.cs +++ b/CoreRemoting.Tests/ServerFixture.cs @@ -49,9 +49,13 @@ public ServerFixture() container.RegisterService( lifetime: ServiceLifetime.Singleton); - // Service for session tests + // Failing constructor service container.RegisterService( lifetime: ServiceLifetime.SingleCall); + + // Service for session tests + container.RegisterService( + lifetime: ServiceLifetime.SingleCall); } }; } diff --git a/CoreRemoting.Tests/SessionTests.cs b/CoreRemoting.Tests/SessionTests.cs index c9aa9e0..1854ca1 100644 --- a/CoreRemoting.Tests/SessionTests.cs +++ b/CoreRemoting.Tests/SessionTests.cs @@ -21,7 +21,7 @@ public SessionTests(ServerFixture serverFixture) _serverFixture = serverFixture; _serverFixture.Start(); } - + [Fact] public void Client_Connect_should_create_new_session_AND_Disconnect_should_close_session() { @@ -54,7 +54,7 @@ public void Client_Connect_should_create_new_session_AND_Disconnect_should_close client.Dispose(); }); - + var clientThread1 = new Thread(() => clientAction(0)); var clientThread2 = new Thread(() => clientAction(1)); @@ -94,7 +94,7 @@ public void Client_Connect_should_throw_exception_on_invalid_auth_credentials() AuthenticateFake = credentials => credentials[1].Value == "secret" } }; - + var server = new RemotingServer(serverConfig); server.Start(); @@ -102,7 +102,7 @@ public void Client_Connect_should_throw_exception_on_invalid_auth_credentials() { var clientAction = new Action((password, shouldThrow) => { - using var client = + using var client = new RemotingClient(new ClientConfig() { ConnectionTimeout = 0, @@ -114,7 +114,7 @@ public void Client_Connect_should_throw_exception_on_invalid_auth_credentials() new Credential() {Name = "Password", Value = password } } }); - + if (shouldThrow) Assert.Throws(() => client.Connect()); else @@ -124,7 +124,7 @@ public void Client_Connect_should_throw_exception_on_invalid_auth_credentials() var clientThread1 = new Thread(() => clientAction("wrong", true)); clientThread1.Start(); clientThread1.Join(); - + var clientThread2 = new Thread(() => clientAction("secret", false)); clientThread2.Start(); clientThread2.Join(); @@ -161,7 +161,7 @@ public void RemotingSession_Dispose_should_disconnect_client() { waitForDisconnect.Set(); }; - + client.Connect(); var proxy = client.CreateProxy(); @@ -169,8 +169,29 @@ public void RemotingSession_Dispose_should_disconnect_client() waitForDisconnect.Wait(); Assert.False(client.IsConnected); - + client.Dispose(); } + + [Fact] + public void RemotingSession_should_be_accessible_to_the_component_constructor() + { + using var client = new RemotingClient(new ClientConfig() + { + ConnectionTimeout = 0, + InvocationTimeout = 0, + SendTimeout = 0, + MessageEncryption = false, + ServerPort = _serverFixture.Server.Config.NetworkPort, + }); + + client.Connect(); + + // RemotingSession.Current should be accessible to the component constructor + var proxy = client.CreateProxy(); + + // RemotingSession should be the same as in the constructor + Assert.True(proxy.HasSameSessionInstance); + } } } \ No newline at end of file diff --git a/CoreRemoting.Tests/Tools/ISessionAwareService.cs b/CoreRemoting.Tests/Tools/ISessionAwareService.cs new file mode 100644 index 0000000..e2b1e60 --- /dev/null +++ b/CoreRemoting.Tests/Tools/ISessionAwareService.cs @@ -0,0 +1,7 @@ +namespace CoreRemoting.Tests.Tools +{ + public interface ISessionAwareService + { + bool HasSameSessionInstance { get; } + } +} diff --git a/CoreRemoting.Tests/Tools/SessionAwareService.cs b/CoreRemoting.Tests/Tools/SessionAwareService.cs new file mode 100644 index 0000000..0513803 --- /dev/null +++ b/CoreRemoting.Tests/Tools/SessionAwareService.cs @@ -0,0 +1,19 @@ +using System; + +namespace CoreRemoting.Tests.Tools +{ + internal class SessionAwareService : ISessionAwareService + { + public SessionAwareService() + { + CurrentSession = RemotingSession.Current; + if (CurrentSession == null) + throw new ArgumentNullException(nameof(CurrentSession)); + } + + public RemotingSession CurrentSession { get; } + + public bool HasSameSessionInstance => + ReferenceEquals(CurrentSession, RemotingSession.Current); + } +}