aspnetcore/test/Microsoft.AspNetCore.Socket.../ConnectionManagerTests.cs

86 lines
3.1 KiB
C#

// 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;
using System.Collections.Generic;
using System.IO.Pipelines;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.AspNetCore.Sockets.Tests
{
public class ConnectionManagerTests
{
[Fact]
public void ReservedConnectionsHaveConnectionId()
{
var connectionManager = new ConnectionManager();
var state = connectionManager.ReserveConnection();
Assert.NotNull(state.Connection);
Assert.NotNull(state.Connection.ConnectionId);
Assert.True(state.Active);
Assert.Null(state.Close);
Assert.Null(state.Connection.Channel);
}
[Fact]
public void ReservedConnectionsCanBeRetrieved()
{
var connectionManager = new ConnectionManager();
var state = connectionManager.ReserveConnection();
Assert.NotNull(state.Connection);
Assert.NotNull(state.Connection.ConnectionId);
ConnectionState newState;
Assert.True(connectionManager.TryGetConnection(state.Connection.ConnectionId, out newState));
Assert.Same(newState, state);
}
[Fact]
public void AddNewConnection()
{
using (var factory = new PipelineFactory())
using (var channel = new HttpConnection(factory))
{
var connectionManager = new ConnectionManager();
var state = connectionManager.AddNewConnection(channel);
Assert.NotNull(state.Connection);
Assert.NotNull(state.Connection.ConnectionId);
Assert.NotNull(state.Connection.Channel);
ConnectionState newState;
Assert.True(connectionManager.TryGetConnection(state.Connection.ConnectionId, out newState));
Assert.Same(newState, state);
Assert.Same(channel, newState.Connection.Channel);
}
}
[Fact]
public void RemoveConnection()
{
using (var factory = new PipelineFactory())
using (var channel = new HttpConnection(factory))
{
var connectionManager = new ConnectionManager();
var state = connectionManager.AddNewConnection(channel);
Assert.NotNull(state.Connection);
Assert.NotNull(state.Connection.ConnectionId);
Assert.NotNull(state.Connection.Channel);
ConnectionState newState;
Assert.True(connectionManager.TryGetConnection(state.Connection.ConnectionId, out newState));
Assert.Same(newState, state);
Assert.Same(channel, newState.Connection.Channel);
connectionManager.RemoveConnection(state.Connection.ConnectionId);
Assert.False(connectionManager.TryGetConnection(state.Connection.ConnectionId, out newState));
}
}
}
}