Esempio n. 1
0
        public async void ServiceConnectionShouldIgnoreFirstHandshakeResponse()
        {
            var factory    = new TestClientConnectionFactory();
            var connection = MockServiceConnection(null, factory);

            // create a connection with migration header.
            await connection.OnClientConnectedAsyncForTest(new OpenConnectionMessage("foo", new Claim[0])
            {
                Headers = new Dictionary <string, StringValues> {
                    { Constants.AsrsMigrateIn, "another-server" }
                }
            });

            Assert.Equal(1, factory.Connections.Count);
            var context = factory.Connections[0];

            Assert.True(context.IsMigrated);

            var message = new AspNetCore.SignalR.Protocol.HandshakeResponseMessage("");

            HandshakeProtocol.WriteResponseMessage(message, context.Transport.Output);
            await context.Transport.Output.FlushAsync();

            var task = context.Transport.Input.ReadAsync();
            await Task.Delay(100);

            // nothing should be written into the transport
            Assert.False(task.IsCompleted);
            // but the `migrated` status should remain False (readonly)
            Assert.True(context.IsMigrated);
        }
        public async Task TestServiceConnectionForMigratedIn()
        {
            var factory    = new TestClientConnectionFactory();
            var connection = MockServiceConnection(null, factory);

            // create a connection with migration header.
            await connection.OnClientConnectedAsyncForTest(new OpenConnectionMessage("foo", new Claim[0])
            {
                Headers = new Dictionary <string, StringValues> {
                    { Constants.AsrsMigrateFrom, "another-server" }
                }
            });

            Assert.Equal(1, factory.Connections.Count);
            var context = factory.Connections[0];

            Assert.True(context.IsMigrated);

            var message = new AspNetCore.SignalR.Protocol.HandshakeResponseMessage("");

            HandshakeProtocol.WriteResponseMessage(message, context.Transport.Output);
            await context.Transport.Output.FlushAsync();

            var task = context.Transport.Input.ReadAsync();
            await Task.Delay(100);

            // nothing should be written into the transport
            Assert.False(task.IsCompleted);
            Assert.True(context.IsMigrated);

            var feature = context.Features.Get <IConnectionMigrationFeature>();

            Assert.NotNull(feature);
            Assert.Equal("another-server", feature.MigrateFrom);
        }
Esempio n. 3
0
        public async Task TestServiceConnectionForMigratedOut()
        {
            var factory = new TestClientConnectionFactory();

            CancellationTokenSource cts = new();
            var connection = CreateServiceConnection(clientConnectionFactory: factory, mode: GracefulShutdownMode.MigrateClients);

            // create a connection with migration header.
            await connection.OnClientConnectedAsyncForTest(new OpenConnectionMessage("foo", new Claim[0]));

            var context = factory.Connections[0];

            var closeMessage = new CloseConnectionMessage(context.ConnectionId);

            closeMessage.Headers.Add(Constants.AsrsMigrateTo, "another-server");

            var disconnect = connection.OnClientDisconnectedAsyncForTest(closeMessage);

            var feature = context.Features.Get <IConnectionMigrationFeature>();

            Assert.NotNull(feature);
            Assert.Equal("another-server", feature.MigrateTo);

            cts.Cancel();
            await disconnect.OrTimeout();
        }
Esempio n. 4
0
        public async Task TestCloseConnectionMessageWithMigrateOut()
        {
            var clientConnectionFactory = new TestClientConnectionFactory();

            var connection = CreateServiceConnection(clientConnectionFactory: clientConnectionFactory, handler: new TestConnectionHandler(3000, "foobar"));

            _ = connection.StartAsync();
            await connection.ConnectionInitializedTask.OrTimeout(1000);

            var openConnectionMessage = new OpenConnectionMessage("foo", Array.Empty <Claim>());

            _ = connection.WriteFromServiceAsync(openConnectionMessage);
            await connection.ClientConnectedTask;

            Assert.Equal(1, clientConnectionFactory.Connections.Count);
            var clientConnection = clientConnectionFactory.Connections[0];

            Assert.False(clientConnection.IsMigrated);

            // write a signalr handshake response
            var message = new SignalRProtocol.HandshakeResponseMessage("");

            SignalRProtocol.HandshakeProtocol.WriteResponseMessage(message, clientConnection.Transport.Output);
            await clientConnection.Transport.Output.FlushAsync();

            // write a close connection message with migration header
            var closeMessage = new CloseConnectionMessage(clientConnection.ConnectionId);

            closeMessage.Headers.Add(Constants.AsrsMigrateTo, "another-server");
            await connection.WriteFromServiceAsync(closeMessage);

            // wait until app task completed.
            await Assert.ThrowsAsync <TimeoutException>(async() => await clientConnection.LifetimeTask.OrTimeout(1000));

            await clientConnection.LifetimeTask.OrTimeout(3000);

            // expect a handshake response message.
            await connection.ExpectSignalRMessage(SignalRProtocol.HandshakeResponseMessage.Empty).OrTimeout(3000);

            // signalr close message should be skipped.
            await Assert.ThrowsAsync <TimeoutException>(async() => await connection.ExpectSignalRMessage(SignalRProtocol.CloseMessage.Empty).OrTimeout(1000));

            var feature = clientConnection.Features.Get <IConnectionMigrationFeature>();

            Assert.NotNull(feature);
            Assert.Equal("another-server", feature.MigrateTo);

            await connection.StopAsync();
        }
        public async Task TestServiceConnectionForMigratedOut()
        {
            var factory = new TestClientConnectionFactory();

            var connection = MockServiceConnection(null, factory);

            // create a connection with migration header.
            await connection.OnClientConnectedAsyncForTest(new OpenConnectionMessage("foo", new Claim[0]));

            var context = factory.Connections[0];

            var closeMessage = new CloseConnectionMessage(context.ConnectionId);

            closeMessage.Headers.Add(Constants.AsrsMigrateTo, "another-server");

            await connection.OnClientDisconnectedAsyncForTest(closeMessage);

            var feature = context.Features.Get <IConnectionMigrationFeature>();

            Assert.NotNull(feature);
            Assert.Equal("another-server", feature.MigrateTo);
        }
Esempio n. 6
0
        public async Task TestCloseConnectionMessage()
        {
            var clientConnectionFactory = new TestClientConnectionFactory();

            var connection = CreateServiceConnection(clientConnectionFactory: clientConnectionFactory, handler: new TestConnectionHandler(3000, "foobar"));

            _ = connection.StartAsync();
            await connection.ConnectionInitializedTask.OrTimeout(1000);

            var openConnectionMessage = new OpenConnectionMessage("foo", Array.Empty <Claim>());

            _ = connection.WriteFromServiceAsync(openConnectionMessage);
            await connection.ClientConnectedTask;

            Assert.Equal(1, clientConnectionFactory.Connections.Count);
            var clientConnection = clientConnectionFactory.Connections[0];

            // write a signalr handshake response
            var message = new SignalRProtocol.HandshakeResponseMessage("");

            SignalRProtocol.HandshakeProtocol.WriteResponseMessage(message, clientConnection.Transport.Output);

            // write close connection message
            await connection.WriteFromServiceAsync(new CloseConnectionMessage(clientConnection.ConnectionId));

            // wait until app task completed.
            await Assert.ThrowsAsync <TimeoutException>(async() => await clientConnection.LifetimeTask.OrTimeout(1000));

            await clientConnection.LifetimeTask;

            await connection.ExpectSignalRMessage(SignalRProtocol.HandshakeResponseMessage.Empty).OrTimeout(1000);

            await connection.ExpectStringMessage("foobar").OrTimeout(1000);

            await connection.ExpectSignalRMessage(SignalRProtocol.CloseMessage.Empty).OrTimeout(1000);

            await connection.StopAsync();
        }
Esempio n. 7
0
        public async Task TestOpenConnectionMessageWithMigrateIn()
        {
            var clientConnectionFactory = new TestClientConnectionFactory();
            var connection = CreateServiceConnection(clientConnectionFactory: clientConnectionFactory);

            _ = connection.StartAsync();
            await connection.ConnectionInitializedTask.OrTimeout(1000);

            var openConnectionMessage = new OpenConnectionMessage("foo", Array.Empty <Claim>());

            openConnectionMessage.Headers.Add(Constants.AsrsMigrateFrom, "another-server");
            _ = connection.WriteFromServiceAsync(openConnectionMessage);
            await connection.ClientConnectedTask;

            Assert.Equal(1, clientConnectionFactory.Connections.Count);
            var clientConnection = clientConnectionFactory.Connections[0];

            Assert.True(clientConnection.IsMigrated);

            // write a handshake response
            var message = new SignalRProtocol.HandshakeResponseMessage("");

            SignalRProtocol.HandshakeProtocol.WriteResponseMessage(message, clientConnection.Transport.Output);
            await clientConnection.Transport.Output.FlushAsync();

            // signalr handshake response should be skipped.
            await Assert.ThrowsAsync <TimeoutException>(async() => await connection.ExpectSignalRMessage(SignalRProtocol.HandshakeResponseMessage.Empty).OrTimeout(1000));

            Assert.True(clientConnection.IsMigrated);

            var feature = clientConnection.Features.Get <IConnectionMigrationFeature>();

            Assert.NotNull(feature);
            Assert.Equal("another-server", feature.MigrateFrom);

            await connection.StopAsync();
        }