Skip to content

Commit

Permalink
Merge branch 'dev' into tefa/handshake-response-set-connection-id
Browse files Browse the repository at this point in the history
  • Loading branch information
terencefan authored Jan 22, 2025
2 parents 2533a5e + 4c4da4a commit ed77186
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 195 deletions.
50 changes: 34 additions & 16 deletions test/Microsoft.Azure.SignalR.AspNet.Tests/ServiceConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Transports;
using Microsoft.Azure.SignalR.Protocol;
using Microsoft.Azure.SignalR.Tests;
using Microsoft.Azure.SignalR.Tests.Common;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
Expand All @@ -34,13 +35,14 @@ public async Task ServiceConnectionDispatchTest()

var clientConnectionManager = new TestClientConnectionManager();
using var proxy = new TestServiceConnectionProxy(clientConnectionManager, loggerFactory: loggerFactory);

// start the server connection
await proxy.StartServiceAsync().OrTimeout();

var clientConnection = Guid.NewGuid().ToString("N");

// Application layer sends OpenConnectionMessage
var openConnectionMessage = new OpenConnectionMessage(clientConnection, new Claim[0], null, "?transport=webSockets");
var openConnectionMessage = new OpenConnectionMessage(clientConnection, [], null, "?transport=webSockets");
var task = clientConnectionManager.WaitForClientConnectAsync(clientConnection).OrTimeout();
await proxy.WriteMessageAsync(openConnectionMessage);
await task;
Expand Down Expand Up @@ -80,14 +82,16 @@ public async Task ServiceConnectionDispatchGroupMessagesTest()
hubConfig.Resolver.Register(typeof(IClientConnectionManagerAspNet), () => ccm);
DispatcherHelper.PrepareAndGetDispatcher(new TestAppBuilder(), hubConfig, new ServiceOptions { ConnectionString = ConnectionString }, appName, loggerFactory);
using var proxy = new TestServiceConnectionProxy(ccm, loggerFactory: loggerFactory);

// start the server connection
await proxy.StartServiceAsync().OrTimeout();

var clientConnection = Guid.NewGuid().ToString("N");

var connectTask = scm.WaitForTransportOutputMessageAsync(typeof(GroupBroadcastDataMessage)).OrTimeout();

// Application layer sends OpenConnectionMessage
var openConnectionMessage = new OpenConnectionMessage(clientConnection, new Claim[0], null, $"?transport=webSockets&connectionToken=conn1&connectionData=%5B%7B%22name%22%3A%22{hub}%22%7D%5D");
var openConnectionMessage = new OpenConnectionMessage(clientConnection, [], null, $"?transport=webSockets&connectionToken=conn1&connectionData=%5B%7B%22name%22%3A%22{hub}%22%7D%5D");
await proxy.WriteMessageAsync(openConnectionMessage);

var connectMessage = (await connectTask) as GroupBroadcastDataMessage;
Expand Down Expand Up @@ -148,7 +152,7 @@ public async Task ServiceConnectionDispatchGroupMessagesTest()
[Fact]
public async Task ServiceConnectionWithErrorConnectHub()
{
using (StartVerifiableLog(out var loggerFactory, LogLevel.Warning, expectedErrors: c=>true, logChecker:
using (StartVerifiableLog(out var loggerFactory, LogLevel.Warning, expectedErrors: c => true, logChecker:
logs =>
{
Assert.Equal(2, logs.Count);
Expand All @@ -166,6 +170,7 @@ public async Task ServiceConnectionWithErrorConnectHub()
hubConfig.Resolver.Register(typeof(IClientConnectionManagerAspNet), () => ccm);
DispatcherHelper.PrepareAndGetDispatcher(new TestAppBuilder(), hubConfig, new ServiceOptions { ConnectionString = ConnectionString }, appName, loggerFactory);
using var proxy = new TestServiceConnectionProxy(ccm, loggerFactory: loggerFactory);

// start the server connection
await proxy.StartServiceAsync().OrTimeout();

Expand All @@ -174,7 +179,7 @@ public async Task ServiceConnectionWithErrorConnectHub()
var connectTask = proxy.WaitForOutgoingMessageAsync(clientConnection).OrTimeout();

// Application layer sends OpenConnectionMessage
var openConnectionMessage = new OpenConnectionMessage(clientConnection, new Claim[0], null, $"?transport=webSockets&connectionToken=conn1&connectionData=%5B%7B%22name%22%3A%22{hub}%22%7D%5D");
var openConnectionMessage = new OpenConnectionMessage(clientConnection, [], null, $"?transport=webSockets&connectionToken=conn1&connectionData=%5B%7B%22name%22%3A%22{hub}%22%7D%5D");
await proxy.WriteMessageAsync(openConnectionMessage);

// other messages are just ignored because OnConnected failed
Expand All @@ -193,7 +198,7 @@ public async Task ServiceConnectionWithErrorConnectHub()
}
}

[Fact]
[RetryFact]
public async Task ServiceConnectionWithErrorDisconnectHub()
{
using (StartVerifiableLog(out var loggerFactory, LogLevel.Debug, expectedErrors: c => true, logChecker:
Expand All @@ -207,17 +212,19 @@ public async Task ServiceConnectionWithErrorDisconnectHub()
var ccm = new ClientConnectionManager(hubConfig, loggerFactory);
hubConfig.Resolver.Register(typeof(IClientConnectionManagerAspNet), () => ccm);
DispatcherHelper.PrepareAndGetDispatcher(new TestAppBuilder(), hubConfig,
new ServiceOptions {ConnectionString = ConnectionString}, appName, loggerFactory);
new ServiceOptions { ConnectionString = ConnectionString }, appName, loggerFactory);
using var proxy = new TestServiceConnectionProxy(ccm, loggerFactory: loggerFactory);

// start the server connection
await proxy.StartServiceAsync().OrTimeout();

var clientConnection = Guid.NewGuid().ToString("N");

var connectTask = scm.WaitForTransportOutputMessageAsync(typeof(GroupBroadcastDataMessage))
.OrTimeout();

// Application layer sends OpenConnectionMessage
var openConnectionMessage = new OpenConnectionMessage(clientConnection, new Claim[0], null,
var openConnectionMessage = new OpenConnectionMessage(clientConnection, [], null,
$"?transport=webSockets&connectionToken=conn1&connectionData=%5B%7B%22name%22%3A%22{hub}%22%7D%5D");
await proxy.WriteMessageAsync(openConnectionMessage);

Expand Down Expand Up @@ -266,14 +273,15 @@ public async Task ServiceConnectionDispatchOpenConnectionToUnauthorizedHubTest()
var ccm = new ClientConnectionManager(hubConfig, loggerFactory);
hubConfig.Resolver.Register(typeof(IClientConnectionManagerAspNet), () => ccm);
using var proxy = new TestServiceConnectionProxy(ccm, loggerFactory: loggerFactory);

// start the server connection
await proxy.StartServiceAsync().OrTimeout();

var connectionId = Guid.NewGuid().ToString("N");
var connectTask = proxy.WaitForOutgoingMessageAsync(connectionId).OrTimeout();

// Application layer sends OpenConnectionMessage to an authorized hub from anonymous user
var openConnectionMessage = new OpenConnectionMessage(connectionId, new Claim[0], null, "?transport=webSockets&connectionData=%5B%7B%22name%22%3A%22authchat%22%7D%5D");
var openConnectionMessage = new OpenConnectionMessage(connectionId, [], null, "?transport=webSockets&connectionData=%5B%7B%22name%22%3A%22authchat%22%7D%5D");
await proxy.WriteMessageAsync(openConnectionMessage);

var message = await connectTask;
Expand Down Expand Up @@ -301,6 +309,7 @@ public async Task ServiceConnectionWithNormalClientConnection()
var ccm = new ClientConnectionManager(hubConfig, loggerFactory);
hubConfig.Resolver.Register(typeof(IClientConnectionManagerAspNet), () => ccm);
using var proxy = new TestServiceConnectionProxy(ccm, loggerFactory: loggerFactory);

// start the server connection
await proxy.StartServiceAsync().OrTimeout();

Expand All @@ -309,7 +318,7 @@ public async Task ServiceConnectionWithNormalClientConnection()
var connectTask = proxy.WaitForOutgoingMessageAsync(connectionId).OrTimeout();

// Application layer sends OpenConnectionMessage to an authorized hub from anonymous user
var openConnectionMessage = new OpenConnectionMessage(connectionId, new Claim[0], null, "?transport=webSockets&connectionData=%5B%7B%22name%22%3A%22authchat%22%7D%5D");
var openConnectionMessage = new OpenConnectionMessage(connectionId, [], null, "?transport=webSockets&connectionData=%5B%7B%22name%22%3A%22authchat%22%7D%5D");
await proxy.WriteMessageAsync(openConnectionMessage);

var message = await connectTask;
Expand Down Expand Up @@ -337,6 +346,7 @@ public async Task ServiceConnectionWithTransportLayerClosedShouldCleanupNormalCl
DispatcherHelper.PrepareAndGetDispatcher(new TestAppBuilder(), hubConfig,
new ServiceOptions { ConnectionString = ConnectionString }, appName, loggerFactory);
using var proxy = new TestServiceConnectionProxy(ccm, loggerFactory);

// start the server connection
var connectionTask = proxy.StartAsync();
await proxy.ConnectionInitializedTask.OrTimeout();
Expand All @@ -345,8 +355,9 @@ public async Task ServiceConnectionWithTransportLayerClosedShouldCleanupNormalCl

var connectTask = scm.WaitForTransportOutputMessageAsync(typeof(GroupBroadcastDataMessage))
.OrTimeout();

// Application layer sends OpenConnectionMessage
var openConnectionMessage = new OpenConnectionMessage(clientConnection, new Claim[0], null,
var openConnectionMessage = new OpenConnectionMessage(clientConnection, [], null,
$"?transport=webSockets&connectionToken=conn1&connectionData=%5B%7B%22name%22%3A%22{hub}%22%7D%5D");
await proxy.WriteMessageAsync(openConnectionMessage);

Expand Down Expand Up @@ -393,6 +404,7 @@ public async Task ServiceConnectionWithTransportLayerClosedShouldCleanupEndlessC
DispatcherHelper.PrepareAndGetDispatcher(new TestAppBuilder(), hubConfig,
new ServiceOptions { ConnectionString = ConnectionString }, appName, loggerFactory);
using var proxy = new TestServiceConnectionProxy(ccm, loggerFactory);

// start the server connection
var connectionTask = proxy.StartAsync();
await proxy.ConnectionInitializedTask.OrTimeout();
Expand All @@ -401,8 +413,9 @@ public async Task ServiceConnectionWithTransportLayerClosedShouldCleanupEndlessC

var connectTask = scm.WaitForTransportOutputMessageAsync(typeof(GroupBroadcastDataMessage))
.OrTimeout();

// Application layer sends OpenConnectionMessage
var openConnectionMessage = new OpenConnectionMessage(clientConnection, new Claim[0], null,
var openConnectionMessage = new OpenConnectionMessage(clientConnection, [], null,
$"?transport=webSockets&connectionToken=conn1&connectionData=%5B%7B%22name%22%3A%22{hub}%22%7D%5D");
await proxy.WriteMessageAsync(openConnectionMessage);

Expand Down Expand Up @@ -442,6 +455,7 @@ public async Task ServiceConnectionWithTransportLayerClosedShouldCleanupEndlessI
DispatcherHelper.PrepareAndGetDispatcher(new TestAppBuilder(), hubConfig,
new ServiceOptions { ConnectionString = ConnectionString }, appName, loggerFactory);
using var proxy = new TestServiceConnectionProxy(ccm, loggerFactory);

// start the server connection
var connectionTask = proxy.StartAsync();
await proxy.ConnectionInitializedTask.OrTimeout();
Expand All @@ -450,8 +464,9 @@ public async Task ServiceConnectionWithTransportLayerClosedShouldCleanupEndlessI

var connectTask = scm.WaitForTransportOutputMessageAsync(typeof(GroupBroadcastDataMessage))
.OrTimeout();

// Application layer sends OpenConnectionMessage
var openConnectionMessage = new OpenConnectionMessage(clientConnection, new Claim[0], null,
var openConnectionMessage = new OpenConnectionMessage(clientConnection, [], null,
$"?transport=webSockets&connectionToken=conn1&connectionData=%5B%7B%22name%22%3A%22{hub}%22%7D%5D");
await proxy.WriteMessageAsync(openConnectionMessage);

Expand Down Expand Up @@ -500,6 +515,7 @@ public async Task ServiceConnectionWithOfflinePingWillTriggerDisconnectClients()
hubConfig.Resolver.Register(typeof(IClientConnectionManagerAspNet), () => ccm);
DispatcherHelper.PrepareAndGetDispatcher(new TestAppBuilder(), hubConfig, new ServiceOptions { ConnectionString = ConnectionString }, appName, loggerFactory);
using var proxy = new TestServiceConnectionProxy(ccm, loggerFactory: loggerFactory);

// prepare 2 clients with different instancesId connected
var instanceId1 = Guid.NewGuid().ToString();
var connectionId1 = Guid.NewGuid().ToString("N");
Expand All @@ -513,7 +529,7 @@ public async Task ServiceConnectionWithOfflinePingWillTriggerDisconnectClients()

// Application layer sends OpenConnectionMessage for client1
var connectTask = scm.WaitForTransportOutputMessageAsync(typeof(GroupBroadcastDataMessage)).OrTimeout();
var openConnectionMessage = new OpenConnectionMessage(connectionId1, new Claim[0], header1, $"?transport=webSockets&connectionToken=conn1&connectionData=%5B%7B%22name%22%3A%22{hub}%22%7D%5D");
var openConnectionMessage = new OpenConnectionMessage(connectionId1, [], header1, $"?transport=webSockets&connectionToken=conn1&connectionData=%5B%7B%22name%22%3A%22{hub}%22%7D%5D");
await proxy.WriteMessageAsync(openConnectionMessage);

// client1 is connected
Expand All @@ -528,7 +544,7 @@ public async Task ServiceConnectionWithOfflinePingWillTriggerDisconnectClients()

// Application layer sends OpenConnectionMessage for client2
connectTask = scm.WaitForTransportOutputMessageAsync(typeof(GroupBroadcastDataMessage)).OrTimeout();
openConnectionMessage = new OpenConnectionMessage(connectionId2, new Claim[0], header2, $"?transport=webSockets&connectionToken=conn2&connectionData=%5B%7B%22name%22%3A%22{hub}%22%7D%5D");
openConnectionMessage = new OpenConnectionMessage(connectionId2, [], header2, $"?transport=webSockets&connectionToken=conn2&connectionData=%5B%7B%22name%22%3A%22{hub}%22%7D%5D");
await proxy.WriteMessageAsync(openConnectionMessage);

// client2 is connected
Expand All @@ -544,10 +560,10 @@ public async Task ServiceConnectionWithOfflinePingWillTriggerDisconnectClients()
connectTask = scm.WaitForTransportOutputMessageAsync(typeof(GroupBroadcastDataMessage)).OrTimeout();
await proxy.WriteMessageAsync(new PingMessage()
{
Messages = new[] { "offline", instanceId1 }
Messages = ["offline", instanceId1]
});

// Validate client1 disconnect
// Validate client1 disconnect
connectMessage = (await connectTask) as GroupBroadcastDataMessage;
Assert.NotNull(connectMessage);
Assert.Equal($"hg-{hub}.note", connectMessage.GroupName);
Expand All @@ -564,7 +580,9 @@ await proxy.WriteMessageAsync(new PingMessage()
private sealed class HubResponseItem
{
public string H { get; set; }

public string M { get; set; }

public List<string> A { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,14 @@ internal async Task TestOffline(GracefulShutdownMode mode)
};
using var container = new TestServiceConnectionContainer(connections, factory: new SimpleTestServiceConnectionFactory());

foreach (SimpleTestServiceConnection c in connections)
foreach (var c in connections.Cast<SimpleTestServiceConnection>())
{
Assert.False(c.ConnectionOfflineTask.IsCompleted);
}

await container.OfflineAsync(mode, default);

foreach (SimpleTestServiceConnection c in connections)
foreach (var c in connections.Cast<SimpleTestServiceConnection>())
{
Assert.True(c.ConnectionOfflineTask.IsCompleted);
}
Expand Down
Loading

0 comments on commit ed77186

Please sign in to comment.