Skip to content

Commit 58d4bf9

Browse files
committed
Test same server reroute
1 parent a789992 commit 58d4bf9

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

test/Microsoft.Azure.SignalR.Tests/Microsoft.Azure.SignalR.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
<ItemGroup>
2020
<FrameworkReference Include="Microsoft.AspNetCore.App" />
21+
<PackageReference Include="Microsoft.AspNetCore.Http.Connections.Client" Version="8.0.8" />
22+
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client.Core" Version="8.0.8" />
2123
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPackageVersion)" />
2224
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="$(MicrosoftAspNetCoreTestHostPackageVersion)" />
2325
<PackageReference Include="Moq" Version="$(MoqPackageVersion)" />
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Security.Claims;
6+
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Connections;
8+
using Microsoft.AspNetCore.Http.Connections.Client;
9+
using Microsoft.AspNetCore.SignalR;
10+
using Microsoft.AspNetCore.SignalR.Client;
11+
using Microsoft.AspNetCore.SignalR.Internal;
12+
using Microsoft.AspNetCore.SignalR.Protocol;
13+
using Microsoft.Azure.SignalR.Protocol;
14+
using Microsoft.Azure.SignalR.Tests.Common;
15+
using Microsoft.Extensions.DependencyInjection;
16+
using Microsoft.Extensions.Logging;
17+
using Microsoft.Extensions.Options;
18+
using Xunit;
19+
using Xunit.Abstractions;
20+
21+
namespace Microsoft.Azure.SignalR.Tests;
22+
23+
#nullable enable
24+
25+
public class SameServerRerouteTests : VerifiableLoggedTest
26+
{
27+
public SameServerRerouteTests(ITestOutputHelper output) : base(output)
28+
{
29+
}
30+
31+
[Fact]
32+
public async Task Test()
33+
{
34+
var hub = "foo";
35+
var connectionString = "Endpoint=http://localhost:8080;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGH;Version=1.0;";
36+
37+
using var provider = StartVerifiableLog(out var loggerFactory);
38+
39+
var nameProvider = new DefaultServerNameProvider();
40+
var logger = loggerFactory.CreateLogger<DefaultHubProtocolResolver>();
41+
var hubProtocolResolver = new DefaultHubProtocolResolver(new IHubProtocol[] { new JsonHubProtocol(), new MessagePackHubProtocol() }, logger);
42+
43+
var handler = new EndlessConnectionHandler<FooHub>(loggerFactory);
44+
ConnectionDelegate connectionDelegate = handler.OnConnectedAsync;
45+
46+
var factory = new ServiceConnectionFactory(new ServiceProtocol(),
47+
new ClientConnectionManager(),
48+
new ConnectionFactory(nameProvider, loggerFactory),
49+
loggerFactory,
50+
connectionDelegate,
51+
new ClientConnectionFactory(loggerFactory),
52+
nameProvider,
53+
new DefaultServiceEventHandler(loggerFactory),
54+
new DummyClientInvocationManager(),
55+
hubProtocolResolver);
56+
57+
var serviceEndpoint = new ServiceEndpoint(connectionString);
58+
var serviceEndpointProvider = new ServiceEndpointProvider(serviceEndpoint, new ServiceOptions());
59+
var hubServiceEndpoint = new HubServiceEndpoint(hub, serviceEndpointProvider, serviceEndpoint);
60+
61+
var messageHandler = new StrongServiceConnectionContainer(factory, 1, 1, hubServiceEndpoint, logger);
62+
var ackHandler = new AckHandler();
63+
64+
var connection1 = factory.Create(hubServiceEndpoint, messageHandler, ackHandler, ServiceConnectionType.Default);
65+
var connection2 = factory.Create(hubServiceEndpoint, messageHandler, ackHandler, ServiceConnectionType.Default);
66+
67+
_ = connection1.StartAsync();
68+
_ = connection2.StartAsync();
69+
70+
await Task.Delay(1000);
71+
72+
var audience = $"http://localhost/client/?hub={hub}";
73+
var clientPath = $"http://localhost:8080/client/?hub={hub}";
74+
var accessKey = ConnectionStringParser.Parse(connectionString).AccessKey;
75+
var accessToken = await accessKey.GenerateAccessTokenAsync(audience, Array.Empty<Claim>(), TimeSpan.FromMinutes(1), AccessTokenAlgorithm.HS256);
76+
77+
var connectionOptions = new HttpConnectionOptions();
78+
connectionOptions.Headers.Add("Authorization", $"Bearer {accessToken}");
79+
var connectionFactory = new HttpConnectionFactory(Options.Create(connectionOptions), loggerFactory);
80+
var endpoint = new UriEndPoint(new Uri(clientPath));
81+
var serviceProvider = new ServiceCollection().BuildServiceProvider();
82+
83+
var hubConnection = new HubConnection(connectionFactory, new JsonHubProtocol(), endpoint, serviceProvider, loggerFactory);
84+
_ = hubConnection.StartAsync();
85+
86+
while (true)
87+
{
88+
await hubConnection.SendAsync("foo");
89+
await Task.Delay(1000);
90+
}
91+
}
92+
93+
private class FooHub : Hub
94+
{
95+
}
96+
97+
private sealed class EndlessConnectionHandler<THub> : ConnectionHandler where THub : Hub
98+
{
99+
private readonly HubLifetimeManager<THub> _hubLifetimeManager;
100+
101+
private readonly ILoggerFactory _loggerFactory;
102+
103+
public EndlessConnectionHandler(ILoggerFactory loggerFactory)
104+
{
105+
_loggerFactory = loggerFactory;
106+
var logger = loggerFactory.CreateLogger<DefaultHubLifetimeManager<THub>>();
107+
_hubLifetimeManager = new DefaultHubLifetimeManager<THub>(logger);
108+
}
109+
110+
public override async Task OnConnectedAsync(ConnectionContext connection)
111+
{
112+
var hubConnectionContext = new EndlessHubConnectionContext(connection, new HubConnectionContextOptions(), _loggerFactory);
113+
await _hubLifetimeManager.OnConnectedAsync(hubConnectionContext);
114+
}
115+
}
116+
117+
private sealed class EndlessHubConnectionContext : HubConnectionContext
118+
{
119+
public EndlessHubConnectionContext(ConnectionContext connectionContext,
120+
HubConnectionContextOptions contextOptions,
121+
ILoggerFactory loggerFactory) : base(connectionContext, contextOptions, loggerFactory)
122+
{
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)