Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Azure/azure-signalr
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 58d4bf9ea453830e2e644fccb61dc10463dff62c
Choose a base ref
..
head repository: Azure/azure-signalr
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: bdf1603bfef1538997365dbe0d24f5c02778f6d9
Choose a head ref
Showing with 40 additions and 13 deletions.
  1. +40 −13 test/Microsoft.Azure.SignalR.Tests/SameServerRerouteTests.cs
53 changes: 40 additions & 13 deletions test/Microsoft.Azure.SignalR.Tests/SameServerRerouteTests.cs
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@

using System;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http.Connections.Client;
@@ -24,6 +25,10 @@ namespace Microsoft.Azure.SignalR.Tests;

public class SameServerRerouteTests : VerifiableLoggedTest
{
private static int ServerCount = 0;

private static int ClientCount = 0;

public SameServerRerouteTests(ITestOutputHelper output) : base(output)
{
}
@@ -40,7 +45,7 @@ public async Task Test()
var logger = loggerFactory.CreateLogger<DefaultHubProtocolResolver>();
var hubProtocolResolver = new DefaultHubProtocolResolver(new IHubProtocol[] { new JsonHubProtocol(), new MessagePackHubProtocol() }, logger);

var handler = new EndlessConnectionHandler<FooHub>(loggerFactory);
var handler = new EndlessConnectionHandler<TestHub>(loggerFactory);
ConnectionDelegate connectionDelegate = handler.OnConnectedAsync;

var factory = new ServiceConnectionFactory(new ServiceProtocol(),
@@ -78,39 +83,61 @@ public async Task Test()
connectionOptions.Headers.Add("Authorization", $"Bearer {accessToken}");
var connectionFactory = new HttpConnectionFactory(Options.Create(connectionOptions), loggerFactory);
var endpoint = new UriEndPoint(new Uri(clientPath));
var serviceProvider = new ServiceCollection().BuildServiceProvider();

var hubConnection = new HubConnection(connectionFactory, new JsonHubProtocol(), endpoint, serviceProvider, loggerFactory);
var hubConnection = new HubConnection(connectionFactory, new JsonHubProtocol(), endpoint, handler.ServiceProvider, loggerFactory);

hubConnection.On("bar", (int x) => Assert.Equal(Interlocked.Increment(ref ClientCount), x));

_ = hubConnection.StartAsync();

while (true)
{
await hubConnection.SendAsync("foo");
try
{
await hubConnection.SendAsync(nameof(TestHub.Foo));
}
catch (Exception e)
{
Console.WriteLine(e);
}
await Task.Delay(1000);
}
}

private class FooHub : Hub
private class TestHub : Hub
{
public Task Foo()
{
var index = Interlocked.Increment(ref ServerCount);
return Clients.Caller.SendAsync("bar", index);
}

public override Task OnConnectedAsync()
{
return Task.CompletedTask;
}
}

private sealed class EndlessConnectionHandler<THub> : ConnectionHandler where THub : Hub
{
private readonly HubLifetimeManager<THub> _hubLifetimeManager;

private readonly ILoggerFactory _loggerFactory;
public ServiceProvider ServiceProvider { get; }

public EndlessConnectionHandler(ILoggerFactory loggerFactory)
{
_loggerFactory = loggerFactory;
var logger = loggerFactory.CreateLogger<DefaultHubLifetimeManager<THub>>();
_hubLifetimeManager = new DefaultHubLifetimeManager<THub>(logger);
var collection = new ServiceCollection();
collection.AddLogging();
collection.AddSingleton(loggerFactory);
collection.AddSingleton<IUserIdProvider, DefaultUserIdProvider>();
collection.AddSingleton<HubConnectionHandler<THub>>();
collection.AddSignalR().AddJsonProtocol();

ServiceProvider = collection.BuildServiceProvider();
}

public override async Task OnConnectedAsync(ConnectionContext connection)
{
var hubConnectionContext = new EndlessHubConnectionContext(connection, new HubConnectionContextOptions(), _loggerFactory);
await _hubLifetimeManager.OnConnectedAsync(hubConnectionContext);
var handler = ServiceProvider.GetRequiredService<HubConnectionHandler<TestHub>>();
await handler.OnConnectedAsync(connection);
}
}