Skip to content

Commit

Permalink
Single-call server component doesn't see the current remote user's se…
Browse files Browse the repository at this point in the history
…ssion in its constructor, see #87.
  • Loading branch information
yallie committed Dec 4, 2024
1 parent c39b901 commit e8667e1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
6 changes: 5 additions & 1 deletion CoreRemoting.Tests/ServerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ public ServerFixture()
container.RegisterService<IEnumTestService, EnumTestService>(
lifetime: ServiceLifetime.Singleton);

// Service for session tests
// Failing constructor service
container.RegisterService<IFailingService, FailingService>(
lifetime: ServiceLifetime.SingleCall);

// Service for session tests
container.RegisterService<ISessionAwareService, SessionAwareService>(
lifetime: ServiceLifetime.SingleCall);
}
};
}
Expand Down
37 changes: 29 additions & 8 deletions CoreRemoting.Tests/SessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -94,15 +94,15 @@ public void Client_Connect_should_throw_exception_on_invalid_auth_credentials()
AuthenticateFake = credentials => credentials[1].Value == "secret"
}
};

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

try
{
var clientAction = new Action<string, bool>((password, shouldThrow) =>
{
using var client =
using var client =
new RemotingClient(new ClientConfig()
{
ConnectionTimeout = 0,
Expand All @@ -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<SecurityException>(() => client.Connect());
else
Expand All @@ -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();
Expand Down Expand Up @@ -161,16 +161,37 @@ public void RemotingSession_Dispose_should_disconnect_client()
{
waitForDisconnect.Set();
};

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

proxy.TestMethod(null);

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<ISessionAwareService>();

// RemotingSession should be the same as in the constructor
Assert.True(proxy.HasSameSessionInstance);
}
}
}
7 changes: 7 additions & 0 deletions CoreRemoting.Tests/Tools/ISessionAwareService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CoreRemoting.Tests.Tools
{
public interface ISessionAwareService
{
bool HasSameSessionInstance { get; }
}
}
19 changes: 19 additions & 0 deletions CoreRemoting.Tests/Tools/SessionAwareService.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit e8667e1

Please sign in to comment.