Add test for supported scenario (#12931)

This commit is contained in:
Brennan 2019-08-08 14:04:16 -07:00 committed by GitHub
parent c6e1bcb66c
commit bd01c67e88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 0 deletions

View File

@ -1050,4 +1050,32 @@ namespace Microsoft.AspNetCore.SignalR.Tests
public bool TokenStateInDisconnected { get; set; }
}
public class CallerServiceHub : Hub
{
private readonly CallerService _service;
public CallerServiceHub(CallerService service)
{
_service = service;
}
public override Task OnConnectedAsync()
{
_service.SetCaller(Clients.Caller);
var tcs = (TaskCompletionSource<bool>)Context.Items["ConnectedTask"];
tcs?.TrySetResult(true);
return base.OnConnectedAsync();
}
}
public class CallerService
{
public IClientProxy Caller { get; private set; }
public void SetCaller(IClientProxy caller)
{
Caller = caller;
}
}
}

View File

@ -3625,6 +3625,35 @@ namespace Microsoft.AspNetCore.SignalR.Tests
}
}
[Fact]
public async Task ClientsCallerPropertyCanBeUsedOutsideOfHub()
{
CallerService callerService = new CallerService();
var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(services =>
{
services.AddSingleton(callerService);
});
var connectionHandler = serviceProvider.GetService<HubConnectionHandler<CallerServiceHub>>();
using (StartVerifiableLog())
{
using (var client = new TestClient())
{
var connectionHandlerTask = await client.ConnectAsync(connectionHandler);
// Wait for a connection, or for the endpoint to fail.
await client.Connected.OrThrowIfOtherFails(connectionHandlerTask).OrTimeout();
await callerService.Caller.SendAsync("Echo", "message").OrTimeout();
var message = Assert.IsType<InvocationMessage>(await client.ReadAsync().OrTimeout());
Assert.Equal("Echo", message.Target);
Assert.Equal("message", message.Arguments[0]);
}
}
}
private class CustomHubActivator<THub> : IHubActivator<THub> where THub : Hub
{
public int ReleaseCount;