From 4ef59000ca71e68eaf8ff87adb2d76c3dca0efa3 Mon Sep 17 00:00:00 2001 From: nenoNaninu Date: Wed, 23 Aug 2023 02:45:38 +0900 Subject: [PATCH] use interface property --- .../Hubs/UnaryHub3.cs | 21 +++++++ .../Program.cs | 1 + .../IUnaryHub3.cs | 6 ++ tests/TypedSignalR.Client.Tests/UnaryTest3.cs | 61 +++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 tests/TypedSignalR.Client.Tests.Server/Hubs/UnaryHub3.cs create mode 100644 tests/TypedSignalR.Client.Tests.Shared/IUnaryHub3.cs create mode 100644 tests/TypedSignalR.Client.Tests/UnaryTest3.cs diff --git a/tests/TypedSignalR.Client.Tests.Server/Hubs/UnaryHub3.cs b/tests/TypedSignalR.Client.Tests.Server/Hubs/UnaryHub3.cs new file mode 100644 index 0000000..5afe91a --- /dev/null +++ b/tests/TypedSignalR.Client.Tests.Server/Hubs/UnaryHub3.cs @@ -0,0 +1,21 @@ +using Microsoft.AspNetCore.SignalR; +using TypedSignalR.Client.Tests.Shared; + +namespace TypedSignalR.Client.Tests.Server.Hubs; + +public class UnaryHub3 : Hub, IUnaryHub3 +{ + private readonly ILogger _logger; + + public UnaryHub3(ILogger logger) + { + _logger = logger; + } + + public Task Add(int x, int y) + { + _logger.Log(LogLevel.Information, "UnaryHub.Add"); + + return Task.FromResult(x + y); + } +} diff --git a/tests/TypedSignalR.Client.Tests.Server/Program.cs b/tests/TypedSignalR.Client.Tests.Server/Program.cs index 49192c7..7555458 100644 --- a/tests/TypedSignalR.Client.Tests.Server/Program.cs +++ b/tests/TypedSignalR.Client.Tests.Server/Program.cs @@ -21,6 +21,7 @@ app.MapControllers(); app.MapHub("/Hubs/UnaryHub"); +app.MapHub("/Hubs/UnaryHub3"); app.MapHub("/Hubs/SideEffectHub"); app.MapHub("/Hubs/ReceiverTestHub"); app.MapHub("/Hubs/StreamingHub"); diff --git a/tests/TypedSignalR.Client.Tests.Shared/IUnaryHub3.cs b/tests/TypedSignalR.Client.Tests.Shared/IUnaryHub3.cs new file mode 100644 index 0000000..7bec573 --- /dev/null +++ b/tests/TypedSignalR.Client.Tests.Shared/IUnaryHub3.cs @@ -0,0 +1,6 @@ +namespace TypedSignalR.Client.Tests.Shared; + +public interface IUnaryHub3 +{ + Task Add(int x, int y); +} diff --git a/tests/TypedSignalR.Client.Tests/UnaryTest3.cs b/tests/TypedSignalR.Client.Tests/UnaryTest3.cs new file mode 100644 index 0000000..0c0653b --- /dev/null +++ b/tests/TypedSignalR.Client.Tests/UnaryTest3.cs @@ -0,0 +1,61 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR.Client; +using TypedSignalR.Client.Tests.Shared; +using Xunit; + +namespace TypedSignalR.Client.Tests; + +// Lunch TypedSignalR.Client.Tests.Server.csproj before test! +public class UnaryTest3 +{ + [Fact] + public async Task Add() + { + // case 1. holder's type is ConnectionHolder + //var holder = new ConnectionHolder(); + + //case 2. holder's type is IConnectionHolder + var holder = ConnectionHolder.Create(); + + // --------- + + // case 1. use var + //var connection = holder.HubConnection; + //var hubProxy = connection.CreateHubProxy(); + + // case 2. use property directly + var hubProxy = holder.HubConnection.CreateHubProxy(); + + await holder.HubConnection.StartAsync(); + + var x = Random.Shared.Next(); + var y = Random.Shared.Next(); + + var added = await hubProxy.Add(x, y); + + Assert.Equal(added, x + y); + + await holder.HubConnection.StopAsync(); + await holder.HubConnection.DisposeAsync(); + } +} + +interface IConnectionHolder +{ + HubConnection HubConnection { get; } +} + +class ConnectionHolder : IConnectionHolder +{ + public HubConnection HubConnection { get; } + + public ConnectionHolder() + { + this.HubConnection = new HubConnectionBuilder() + .WithUrl("http://localhost:5105/Hubs/UnaryHub3") + .Build(); + } + + public static IConnectionHolder Create() => new ConnectionHolder(); +}