Skip to content

Commit

Permalink
fix: no beforecall for incomming callbacks
Browse files Browse the repository at this point in the history
- write test
- tweak debug names
  • Loading branch information
eduard-dumitru committed Aug 13, 2024
1 parent e93e8ed commit b5ce48c
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/UiPath.CoreIpc/Client/ServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private async Task<Network> Connect(CancellationToken ct)
protected override ConnectionFactory? ConnectionFactory => _client.ConnectionFactory;
protected override BeforeCallHandler? BeforeCall => _client.BeforeCall;
protected override ILogger? Log => _client.Logger;
protected override string DebugName => "Some ServiceClient"; // TODO: get the DebugName from the client
protected override string DebugName => _client.ToString();
protected override ISerializer? Serializer => _client.Serializer;
}

Expand All @@ -301,7 +301,7 @@ public ServiceClientForCallback(Connection connection, Listener listener, Type i
protected override ConnectionFactory? ConnectionFactory => null;
protected override BeforeCallHandler? BeforeCall => null;
protected override ILogger? Log => null;
protected override string DebugName => "Some Callback ServiceClient"; // TODO: get the DebugName from the listener or somewhere else
protected override string DebugName => $"ReverseClient for {_listener}";
protected override ISerializer? Serializer => null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/UiPath.CoreIpc/Extensibility/ClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal override RouterConfig CreateCallbackRouterConfig()
Callbacks.OrDefault(),
endpoint => endpoint with
{
BeforeCall = endpoint.BeforeCall ?? BeforeCall,
BeforeCall = null, // callbacks don't support BeforeCall
Scheduler = endpoint.Scheduler ?? Scheduler
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/UiPath.CoreIpc/Helpers/Router.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public static RouterConfig From(EndpointCollection endpoints, Func<EndpointSetti

foreach (var endpoint in endpoints)
{
var newValue = transform(endpoint);
var newEndpoint = transform(endpoint);
foreach (var iface in endpoint.Service.Type.GetInterfaces().Prepend(endpoint.Service.Type))
{
nameToEndpoint[iface.Name] = newValue;
nameToEndpoint[iface.Name] = newEndpoint;
}
}

Expand All @@ -32,7 +32,7 @@ public Router(RouterConfig config, IServiceProvider? serviceProvider)

public bool TryResolve(string endpoint, out Route route)
{
if (_config is not { } config) /// in case <see cref="Router"/> was allocated as default, bypassing the constructor
if (_config is not { } config) /// in case <see cref="Router"/> was allocated as <c>default(Router)</c>, bypassing the constructor
{
throw new InvalidOperationException();
}
Expand Down
2 changes: 1 addition & 1 deletion src/UiPath.CoreIpc/Server/EndpointSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public record EndpointSettings
public IServiceProvider? ServiceProvider => Service.MaybeGetServiceProvider();
internal ServiceFactory Service { get; }

public EndpointSettings(Type contractType, object? serviceInstance) : this(
public EndpointSettings(Type contractType, object? serviceInstance = null) : this(
serviceInstance is not null
? new ServiceFactory.Instance()
{
Expand Down
2 changes: 2 additions & 0 deletions src/UiPath.CoreIpc/Server/Listener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,6 @@ private async Task AcceptConnection(CancellationToken ct)
}
}
}

public override string ToString() => Config.ToString();
}
2 changes: 1 addition & 1 deletion src/UiPath.CoreIpc/Server/ServerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public async Task Listen(Stream network, CancellationToken cancellationToken)
{
var stream = await AuthenticateAsServer(); // TODO: should we decommission this?
var serializer = Listener.Server.ServiceProvider.GetService<ISerializer>();
Connection = new Connection(stream, serializer, Listener.Logger, Listener.Config.DebugName, Listener.Config.MaxMessageSize);
Connection = new Connection(stream, serializer, Listener.Logger, debugName: Listener.ToString(), maxMessageSize: Listener.Config.MaxMessageSize);
Server = new Server(
new Router(
Listener.Config.CreateRouterConfig(Listener.Server),
Expand Down
2 changes: 2 additions & 0 deletions src/UiPath.CoreIpc/Transport/NamedPipe/NamedPipeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public sealed record NamedPipeClient : ClientBase, IClient<NamedPipeClientState,
public required string PipeName { get; init; }
public string ServerName { get; init; } = ".";
public bool AllowImpersonation { get; init; } = false;

public override string ToString() => $"ClientPipe={PipeName}";
}

internal sealed class NamedPipeClientState : IClientState<NamedPipeClient, NamedPipeClientState>
Expand Down
2 changes: 2 additions & 0 deletions src/UiPath.CoreIpc/Transport/NamedPipe/NamedPipeListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ IEnumerable<string> INamedPipeListenerConfig.Validate()
{
if (PipeName is null or "") { yield return "PipeName is required"; }
}

public override string ToString() => $"ServerPipe={PipeName}";
}

internal sealed class NamedPipeServerConnectionState
Expand Down
2 changes: 2 additions & 0 deletions src/UiPath.CoreIpc/Transport/Tcp/TcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace UiPath.Ipc.Transport.Tcp;
public sealed record TcpClient : ClientBase, IClient<TcpClientState, TcpClient>
{
public required IPEndPoint EndPoint { get; init; }

public override string ToString() => $"TcpClient={EndPoint}";
}

internal sealed class TcpClientState : IClientState<TcpClient, TcpClientState>
Expand Down
2 changes: 2 additions & 0 deletions src/UiPath.CoreIpc/Transport/Tcp/TcpListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ IEnumerable<string> ITcpListenerConfig.Validate()
yield return "EndPoint is required";
}
}

public override string ToString() => $"TcpServer={EndPoint}";
}

internal sealed class TcpListenerState : IAsyncDisposable
Expand Down
1 change: 1 addition & 0 deletions src/UiPath.CoreIpc/Transport/WebSocket/WebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace UiPath.Ipc.Transport.WebSocket;
public sealed record WebSocketClient : ClientBase, IClient<WebSocketClientState, WebSocketClient>
{
public required Uri Uri { get; init; }
public override string ToString() => $"WebSocketClient={Uri}";
}

internal sealed class WebSocketClientState : IClientState<WebSocketClient, WebSocketClientState>
Expand Down
2 changes: 2 additions & 0 deletions src/UiPath.CoreIpc/Transport/WebSocket/WebSocketListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ IEnumerable<string> IWebSocketListenerConfig.Validate()
{
if (Accept is null) { yield return "Accept is required"; }
}

public override string ToString() => "WebSocketServer";
}

internal sealed class WebSocketListenerState : IAsyncDisposable
Expand Down
22 changes: 19 additions & 3 deletions src/UiPath.Ipc.Tests/ComputingTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xunit.Abstractions;
using System.Collections.Concurrent;
using Xunit.Abstractions;

namespace UiPath.Ipc.Tests;

Expand All @@ -16,6 +17,8 @@ public abstract class ComputingTests : TestBase
protected sealed override IpcProxy IpcProxy => Proxy as IpcProxy ?? throw new InvalidOperationException($"Proxy was expected to be a {nameof(IpcProxy)} but was not.");
protected sealed override Type ContractType => typeof(IComputingService);

protected readonly ConcurrentBag<CallInfo> _clientBeforeCalls = new();

protected ComputingTests(ITestOutputHelper outputHelper) : base(outputHelper)
{
ServiceProvider.InjectLazy(out _service);
Expand All @@ -27,7 +30,7 @@ protected override ListenerConfig ConfigTransportAgnostic(ListenerConfig listene
{
ConcurrentAccepts = 10,
RequestTimeout = Timeouts.DefaultRequest,
MaxReceivedMessageSizeInMegabytes = 1,
MaxReceivedMessageSizeInMegabytes = 1,
};
protected override ClientBase ConfigTransportAgnostic(ClientBase client)
=> client with
Expand All @@ -37,7 +40,8 @@ protected override ClientBase ConfigTransportAgnostic(ClientBase client)
Callbacks = new()
{
{ typeof(IComputingCallback), _computingCallback }
}
},
BeforeCall = async (callInfo, _) => _clientBeforeCalls.Add(callInfo),
};
#endregion

Expand Down Expand Up @@ -109,4 +113,16 @@ public async Task CallbacksWithParams_ShouldWork()
public async Task ConcurrentCallbacksWithParams_ShouldWork()
=> await Task.WhenAll(
Enumerable.Range(1, 50).Select(_ => CallbacksWithParams_ShouldWork()));

[Fact]
public async Task BeforeCall_ShouldApplyToCallsButNotToToCallbacks()
{
await Proxy.GetCallbackThreadName(TimeSpan.Zero).ShouldBeAsync(Names.GuiThreadName);

_clientBeforeCalls.ShouldContain(x => x.Method.Name == nameof(IComputingService.GetCallbackThreadName));
_clientBeforeCalls.ShouldNotContain(x => x.Method.Name == nameof(IComputingCallback.GetThreadName));

_serverBeforeCalls.ShouldContain(x => x.Method.Name == nameof(IComputingService.GetCallbackThreadName));
_serverBeforeCalls.ShouldNotContain(x => x.Method.Name == nameof(IComputingCallback.GetThreadName));
}
}
12 changes: 10 additions & 2 deletions src/UiPath.Ipc.Tests/TestBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Nito.AsyncEx;
using System.Collections.Concurrent;
using System.Runtime.InteropServices;
using Xunit.Abstractions;

Expand All @@ -20,6 +21,8 @@ public abstract class TestBase : IAsyncLifetime
protected abstract IpcProxy IpcProxy { get; }
protected abstract Type ContractType { get; }

protected readonly ConcurrentBag<CallInfo> _serverBeforeCalls = new();

public TestBase(ITestOutputHelper outputHelper)
{
_outputHelper = outputHelper;
Expand All @@ -42,7 +45,12 @@ public TestBase(ITestOutputHelper outputHelper)

_ipcServer = new(() => new()
{
Endpoints = new() { ContractType },
Endpoints = new() {
new EndpointSettings(ContractType)
{
BeforeCall = async (callInfo, _) => _serverBeforeCalls.Add(callInfo)
}
},
Listeners = [CreateListenerAndConfigure()],
ServiceProvider = _serviceProvider,
Scheduler = GuiScheduler
Expand Down Expand Up @@ -71,7 +79,7 @@ private ListenerConfig CreateListenerAndConfigure()
_outputHelper.WriteLine(" - Creating transport specific listener...");
var listener = CreateListener();
_outputHelper.WriteLine($" Result:\r\n\t\t{listener}");
_outputHelper.WriteLine( " - Applying transport agnostic configuration...");
_outputHelper.WriteLine(" - Applying transport agnostic configuration...");
listener = ConfigTransportAgnostic(listener);
_outputHelper.WriteLine($" Result:\r\n\t\t{listener}");
if (_overrideConfig is null)
Expand Down

0 comments on commit b5ce48c

Please sign in to comment.