Skip to content

Commit

Permalink
connection reroute test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
terencefan committed Feb 10, 2025
1 parent 3cbbbcb commit e2093a3
Show file tree
Hide file tree
Showing 31 changed files with 764 additions and 1,532 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_anonymous_types = true

# Namespace settings
csharp_style_namespace_declarations = file_scoped:silent
csharp_style_namespace_declarations = file_scoped:error

# Brace settings
csharp_prefer_braces = true:silent# Prefer curly braces even for one line of code
Expand Down Expand Up @@ -102,6 +102,7 @@ csharp_prefer_simple_default_expression = true:suggestion
csharp_style_prefer_local_over_anonymous_function = true:suggestion
csharp_style_prefer_index_operator = true:suggestion
csharp_style_prefer_range_operator = true:suggestion
csharp_space_around_binary_operators = before_and_after

[*.{xml,config,*proj,nuspec,props,resx,targets,yml,tasks}]
indent_size = 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
Expand All @@ -10,6 +10,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Connections;
using Microsoft.Azure.SignalR.Protocol;
using Microsoft.Extensions.Logging;
Expand All @@ -21,13 +22,13 @@ internal partial class ServiceConnection : ServiceConnectionBase
{
private const string ReconnectMessage = "asrs:reconnect";

private static readonly Dictionary<string, string> CustomHeader = new Dictionary<string, string>
{{Constants.AsrsUserAgent, ProductInfo.GetProductInfo()}};
private static readonly Dictionary<string, string> CustomHeader = new() {
{Constants.AsrsUserAgent, ProductInfo.GetProductInfo()}
};

private static readonly TimeSpan CloseApplicationTimeout = TimeSpan.FromSeconds(5);

private readonly ConcurrentDictionary<string, ClientConnectionContext> _clientConnections =
new ConcurrentDictionary<string, ClientConnectionContext>(StringComparer.Ordinal);
private readonly ConcurrentDictionary<string, ClientConnectionContext> _clientConnections = new(StringComparer.Ordinal);

private readonly IConnectionFactory _connectionFactory;

Expand Down Expand Up @@ -75,6 +76,11 @@ public override bool TryRemoveClientConnection(string connectionId, out IClientC
return r;
}

public override Task CloseClientConnections(CancellationToken token)
{
throw new NotSupportedException();
}

protected override Task<ConnectionContext> CreateConnection(string target = null)
{
return _connectionFactory.ConnectAsync(HubEndpoint, TransferFormat.Binary, ConnectionId, target,
Expand All @@ -97,11 +103,6 @@ protected override Task CleanupClientConnections(string fromInstanceId = null)
return Task.CompletedTask;
}

public override Task CloseClientConnections(CancellationToken token)
{
throw new NotSupportedException();
}

protected override Task OnClientConnectedAsync(OpenConnectionMessage openConnectionMessage)
{
// Create empty transport with only channel for async processing messages
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
Expand All @@ -8,6 +8,7 @@ namespace Microsoft.Azure.SignalR
internal class DefaultServerNameProvider : IServerNameProvider
{
private readonly string _name = $"{Environment.MachineName}_{Guid.NewGuid():N}";

public string GetName()
{
return _name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Connections;
using Microsoft.Extensions.Options;

namespace Microsoft.Azure.SignalR.Management
{
internal class ManagementConnectionFactory : IConnectionFactory
internal class ManagementConnectionFactory(IOptions<ServiceManagerOptions> context,
ConnectionFactory connectionFactory) : IConnectionFactory
{
private readonly string _productInfo;
private readonly ConnectionFactory _connectionFactory;

public ManagementConnectionFactory(IOptions<ServiceManagerOptions> context, ConnectionFactory connectionFactory)
{
_productInfo = context.Value.ProductInfo;
_connectionFactory = connectionFactory;
}
private readonly string _productInfo = context.Value.ProductInfo;
private readonly ConnectionFactory _connectionFactory = connectionFactory;

public Task<ConnectionContext> ConnectAsync(HubServiceEndpoint endpoint, TransferFormat transferFormat, string connectionId, string target, CancellationToken cancellationToken = default, IDictionary<string, string> headers = null)
public Task<ConnectionContext> ConnectAsync(HubServiceEndpoint endpoint,
TransferFormat transferFormat,
string connectionId,
string target,
CancellationToken cancellationToken = default,
IDictionary<string, string> headers = null)
{
if (headers == null)
{
Expand Down
28 changes: 15 additions & 13 deletions src/Microsoft.Azure.SignalR/ServerConnections/ServiceConnection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
Expand All @@ -8,6 +8,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;
Expand All @@ -27,7 +28,9 @@ internal partial class ServiceConnection : ServiceConnectionBase

// Fix issue: https://github.com/Azure/azure-signalr/issues/198
// .NET Framework has restriction about reserved string as the header name like "User-Agent"
private static readonly Dictionary<string, string> CustomHeader = new Dictionary<string, string> { { Constants.AsrsUserAgent, ProductInfo.GetProductInfo() } };
private readonly Dictionary<string, string> _customHeader = new() {
{ Constants.AsrsUserAgent, ProductInfo.GetProductInfo() }
};

private readonly IConnectionFactory _connectionFactory;

Expand Down Expand Up @@ -102,16 +105,6 @@ public override bool TryRemoveClientConnection(string connectionId, out IClientC
return r;
}

protected override Task<ConnectionContext> CreateConnection(string target = null)
{
return _connectionFactory.ConnectAsync(HubEndpoint, TransferFormat.Binary, ConnectionId, target, headers: CustomHeader);
}

protected override Task DisposeConnection(ConnectionContext connection)
{
return _connectionFactory.DisposeAsync(connection);
}

public override async Task CloseClientConnections(CancellationToken token)
{
var tasks = new List<Task>();
Expand All @@ -137,6 +130,16 @@ public override async Task CloseClientConnections(CancellationToken token)
}
}

protected override Task<ConnectionContext> CreateConnection(string target = null)
{
return _connectionFactory.ConnectAsync(HubEndpoint, TransferFormat.Binary, ConnectionId, target, headers: _customHeader);
}

protected override Task DisposeConnection(ConnectionContext connection)
{
return _connectionFactory.DisposeAsync(connection);
}

protected override Task CleanupClientConnections(string fromInstanceId = null)
{
// To gracefully complete client connections, let the client itself owns the connection lifetime
Expand Down Expand Up @@ -386,5 +389,4 @@ private Task OnConnectionReconnectAsync(ConnectionReconnectMessage message)
}
return Task.CompletedTask;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;
Expand Down Expand Up @@ -59,7 +59,10 @@ public ServiceConnectionFactory(
_hubProtocolResolver = hubProtocolResolver;
}

public virtual IServiceConnection Create(HubServiceEndpoint endpoint, IServiceMessageHandler serviceMessageHandler, AckHandler ackHandler, ServiceConnectionType type)
public virtual IServiceConnection Create(HubServiceEndpoint endpoint,
IServiceMessageHandler serviceMessageHandler,
AckHandler ackHandler,
ServiceConnectionType type)
{
return new ServiceConnection(
_serviceProtocol,
Expand Down
29 changes: 29 additions & 0 deletions test/Microsoft.Azure.SignalR.E2ETests/Common/ITestHubConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Threading.Tasks;

namespace Microsoft.Azure.SignalR.E2ETests.Common;

public interface ITestHubConnection
{
public string ConnectionId { get; }

public string User { get; }

public int MessageCount { get; }

public void Listen(params string[] methods);

public Task StartAsync();

public Task StopAsync();

public Task SendAsync(string method, params string[] messages);

public void ResetInvoke(string method);

public Task ExpectInvokeAsync(string method, params string[] message);

public void ResetMessageCount();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;

namespace Microsoft.Azure.SignalR.E2ETests.Common;

public interface ITestHubConnectionGroup : ITestHubConnection, IEnumerable<ITestHubConnection>
{
}
15 changes: 15 additions & 0 deletions test/Microsoft.Azure.SignalR.E2ETests/Common/ITestServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Microsoft.Azure.SignalR.E2ETests.Common;

public interface ITestServer : IDisposable
{
Task<string> StartAsync(Dictionary<string, string?>? configuration = null);

Task StopAsync();
}
Loading

0 comments on commit e2093a3

Please sign in to comment.