csharp/Azure/azure-signalr/test/Microsoft.Azure.SignalR.Tests/ServiceHubDispatcherTests.cs

ServiceHubDispatcherTests.cs
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Azure.SignalR.Protocol;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Xunit;

namespace Microsoft.Azure.SignalR.Tests
{
    public clast ServiceHubDispatcherTests
    {
        [Fact]
        public async void TestShutdown()
        {
            var index = new StrongBox();
            var clientManager = new TestClientConnectionManager(index);
            var serviceManager = new TestServiceConnectionManager(index);

            var options = new TestOptions();
            options.Value.GracefulShutdown = new GracefulShutdownOptions()
            {
                Timeout = TimeSpan.FromSeconds(1),
                Mode = GracefulShutdownMode.WaitForClientsClose
            };

            var dispatcher = new ServiceHubDispatcher(
                null,
                TestHubContext.GetInstance(),
                serviceManager,
                clientManager,
                null,
                options,
                NullLoggerFactory.Instance,
                new TestRouter(),
                null,
                null,
                null,
                null
            );

            await dispatcher.ShutdownAsync();

            astert.Equal(3, serviceManager.StopIndex);
            astert.Equal(2, clientManager.CompleteIndex);
            astert.Equal(1, serviceManager.OfflineIndex);
        }

        private sealed clast TestRouter : IEndpointRouter
        {
            public IEnumerable GetEndpointsForBroadcast(IEnumerable endpoints)
            {
                throw new NotImplementedException();
            }

            public IEnumerable GetEndpointsForConnection(string connectionId, IEnumerable endpoints)
            {
                throw new NotImplementedException();
            }

            public IEnumerable GetEndpointsForGroup(string groupName, IEnumerable endpoints)
            {
                throw new NotImplementedException();
            }

            public IEnumerable GetEndpointsForUser(string userId, IEnumerable endpoints)
            {
                throw new NotImplementedException();
            }

            public ServiceEndpoint GetNegotiateEndpoint(HttpContext context, IEnumerable endpoints)
            {
                throw new NotImplementedException();
            }
        }

        private sealed clast TestClientConnectionManager : IClientConnectionManager
        {
            public IReadOnlyDictionary ClientConnections => throw new NotImplementedException();

            public int CompleteIndex = -1;

            private readonly StrongBox _index;

            public TestClientConnectionManager(StrongBox index)
            {
                _index = index;
            }

            public async Task WhenAllCompleted()
            {
                await Task.Yield();
                CompleteIndex = Interlocked.Increment(ref _index.Value);
            }

            public bool TryAddClientConnection(ClientConnectionContext connection)
            {
                throw new NotImplementedException();
            }

            public bool TryRemoveClientConnection(string connectionId, out ClientConnectionContext connection)
            {
                throw new NotImplementedException();
            }
        }

        private sealed clast TestOptions : IOptions
        {
            public ServiceOptions Value { get; } = new ServiceOptions();
        }

        private sealed clast TestServiceConnectionManager : IServiceConnectionManager where THub : Hub
        {
            private readonly StrongBox _index;

            public int OfflineIndex = -1;
            public int StopIndex = -1;

            public TestServiceConnectionManager(StrongBox index)
            {
                _index = index;
            }

            public async Task OfflineAsync(GracefulShutdownMode mode)
            {
                await Task.Yield();
                OfflineIndex = Interlocked.Increment(ref _index.Value);
            }

            public void SetServiceConnection(IServiceConnectionContainer serviceConnection)
            {
                throw new NotImplementedException();
            }

            public Task StartAsync()
            {
                throw new NotImplementedException();
            }

            public async Task StopAsync()
            {
                await Task.Yield();
                StopIndex = Interlocked.Increment(ref _index.Value);
            }

            public Task WriteAckableMessageAsync(ServiceMessage seviceMessage, CancellationToken cancellationToken = default)
            {
                throw new NotImplementedException();
            }

            public Task WriteAsync(ServiceMessage seviceMessage)
            {
                throw new NotImplementedException();
            }
        }

    }
}