diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/AddSignalRTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/AddSignalRTests.cs new file mode 100644 index 0000000000..6d8360d745 --- /dev/null +++ b/test/Microsoft.AspNetCore.SignalR.Tests/AddSignalRTests.cs @@ -0,0 +1,188 @@ +// Copyright(c) .NET Foundation.All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR.Protocol; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace Microsoft.AspNetCore.SignalR.Tests +{ + public class AddSignalRTests + { + [Fact] + public void ServicesAddedBeforeAddSignalRAreUsed() + { + var serviceCollection = new ServiceCollection(); + + serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(typeof(HubLifetimeManager<>), typeof(CustomHubLifetimeManager<>)); + serviceCollection.AddSingleton(); + serviceCollection.AddScoped(typeof(IHubActivator<>), typeof(CustomHubActivator<>)); + serviceCollection.AddSingleton(typeof(IHubContext<>), typeof(CustomHubContext<>)); + serviceCollection.AddSingleton(typeof(IHubContext<,>), typeof(CustomHubContext<,>)); + serviceCollection.AddSignalR(); + + var serviceProvider = serviceCollection.BuildServiceProvider(); + Assert.IsType(serviceProvider.GetRequiredService()); + Assert.IsType>(serviceProvider.GetRequiredService>()); + Assert.IsType(serviceProvider.GetRequiredService()); + Assert.IsType>(serviceProvider.GetRequiredService>()); + Assert.IsType>(serviceProvider.GetRequiredService>()); + Assert.IsType>(serviceProvider.GetRequiredService>()); + Assert.IsType>(serviceProvider.GetRequiredService>()); + } + + [Fact] + public void ServicesAddedAfterAddSignalRAreUsed() + { + var serviceCollection = new ServiceCollection(); + + serviceCollection.AddSignalR(); + serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(typeof(HubLifetimeManager<>), typeof(CustomHubLifetimeManager<>)); + serviceCollection.AddSingleton(); + serviceCollection.AddScoped(typeof(IHubActivator<>), typeof(CustomHubActivator<>)); + serviceCollection.AddSingleton(typeof(IHubContext<>), typeof(CustomHubContext<>)); + serviceCollection.AddSingleton(typeof(IHubContext<,>), typeof(CustomHubContext<,>)); + + var serviceProvider = serviceCollection.BuildServiceProvider(); + Assert.IsType(serviceProvider.GetRequiredService()); + Assert.IsType>(serviceProvider.GetRequiredService>()); + Assert.IsType(serviceProvider.GetRequiredService()); + Assert.IsType>(serviceProvider.GetRequiredService>()); + Assert.IsType>(serviceProvider.GetRequiredService>()); + Assert.IsType>(serviceProvider.GetRequiredService>()); + Assert.IsType>(serviceProvider.GetRequiredService>()); + } + } + + public class CustomHub : Hub + { + } + + public class CustomTHub : Hub + { + } + + public class CustomDynamicHub : DynamicHub + { + } + + public class CustomIdProvider : IUserIdProvider + { + public string GetUserId(HubConnectionContext connection) + { + throw new System.NotImplementedException(); + } + } + + public class CustomHubProtocolResolver : IHubProtocolResolver + { + public IReadOnlyList AllProtocols => throw new System.NotImplementedException(); + + public IHubProtocol GetProtocol(string protocolName, IReadOnlyList supportedProtocols) + { + throw new System.NotImplementedException(); + } + } + + public class CustomHubActivator : IHubActivator where THub : Hub + { + public THub Create() + { + throw new System.NotImplementedException(); + } + + public void Release(THub hub) + { + throw new System.NotImplementedException(); + } + } + + public class CustomHubContext : IHubContext where THub : Hub + { + public IHubClients Clients => throw new System.NotImplementedException(); + + public IGroupManager Groups => throw new System.NotImplementedException(); + } + + public class CustomHubContext : IHubContext + where THub : Hub + where T : class + { + public IHubClients Clients => throw new System.NotImplementedException(); + + public IGroupManager Groups => throw new System.NotImplementedException(); + } + + public class CustomHubLifetimeManager : HubLifetimeManager where THub : Hub + { + public override Task AddToGroupAsync(string connectionId, string groupName, CancellationToken cancellationToken = default) + { + throw new System.NotImplementedException(); + } + + public override Task OnConnectedAsync(HubConnectionContext connection) + { + throw new System.NotImplementedException(); + } + + public override Task OnDisconnectedAsync(HubConnectionContext connection) + { + throw new System.NotImplementedException(); + } + + public override Task RemoveFromGroupAsync(string connectionId, string groupName, CancellationToken cancellationToken = default) + { + throw new System.NotImplementedException(); + } + + public override Task SendAllAsync(string methodName, object[] args, CancellationToken cancellationToken = default) + { + throw new System.NotImplementedException(); + } + + public override Task SendAllExceptAsync(string methodName, object[] args, IReadOnlyList excludedConnectionIds, CancellationToken cancellationToken = default) + { + throw new System.NotImplementedException(); + } + + public override Task SendConnectionAsync(string connectionId, string methodName, object[] args, CancellationToken cancellationToken = default) + { + throw new System.NotImplementedException(); + } + + public override Task SendConnectionsAsync(IReadOnlyList connectionIds, string methodName, object[] args, CancellationToken cancellationToken = default) + { + throw new System.NotImplementedException(); + } + + public override Task SendGroupAsync(string groupName, string methodName, object[] args, CancellationToken cancellationToken = default) + { + throw new System.NotImplementedException(); + } + + public override Task SendGroupExceptAsync(string groupName, string methodName, object[] args, IReadOnlyList excludedConnectionIds, CancellationToken cancellationToken = default) + { + throw new System.NotImplementedException(); + } + + public override Task SendGroupsAsync(IReadOnlyList groupNames, string methodName, object[] args, CancellationToken cancellationToken = default) + { + throw new System.NotImplementedException(); + } + + public override Task SendUserAsync(string userId, string methodName, object[] args, CancellationToken cancellationToken = default) + { + throw new System.NotImplementedException(); + } + + public override Task SendUsersAsync(IReadOnlyList userIds, string methodName, object[] args, CancellationToken cancellationToken = default) + { + throw new System.NotImplementedException(); + } + } +}