// 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.Internal; using Microsoft.AspNetCore.SignalR.Protocol; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Xunit; namespace Microsoft.AspNetCore.SignalR.Tests { public class AddSignalRTests { [Fact] public void ServicesAddedBeforeAddSignalRAreUsed() { var serviceCollection = new ServiceCollection(); var markerService = new SignalRCoreMarkerService(); serviceCollection.AddSingleton(markerService); 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 hubOptions = new HubOptionsSetup(new List()); serviceCollection.AddSingleton>(hubOptions); 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>()); Assert.Equal(hubOptions, serviceProvider.GetRequiredService>()); Assert.Equal(markerService, 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(); } } }