dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.2k stars 9.94k forks source link

SignalR - Clients Not Receiving Messages in Integration Tests #47260

Closed hkashif closed 1 year ago

hkashif commented 1 year ago

Is there an existing issue for this?

Describe the bug

Normal Operation: When running the project, and connecting an external SignalR client, the client receives messages as expected.

Tests - API Call: When running an integration test, and connecting a client for testing, it doesn't work. I can see during debugging that _proxy._lifetimeManager._connections is empty in the controller (what sends the signalHub message to clients).

Tests - Direct Service Call: Interestingly, I created another test where I get an instance of the HubContext directly from the Test Web Application Factory. Sending a message through this HubContext instance works, and the message is received by the test client.

Expected Behavior

Tests - API Call: When running an integration test, and connecting a client for testing, after I trigger the API that sends a signalR message to connected clients, I expect the clients to receive the message. And I, accordingly, expect to see the right number of connections in _proxy._lifetimeManager._connections when debugging.

Steps To Reproduce

Here is a minimal project that reproduces the issue: https://github.com/hkashif/signalr-integration-tests

There are two tests as described earlier:

Exceptions (if any)

No response

.NET Version

6.0.400

Anything else?

Target Framework: net6.0 IDE: VS Code

dotnet --info:

.NET SDK (reflecting any global.json):
 Version:   6.0.400
 Commit:    7771abd614

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22621
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.400\

global.json file:
  Not found

Host:
  Version:      6.0.8
  Architecture: x64
  Commit:       55fb7ef977

.NET SDKs installed:
  3.1.422 [C:\Program Files\dotnet\sdk]
  5.0.408 [C:\Program Files\dotnet\sdk]
  6.0.400 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.28 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
BrennanConroy commented 1 year ago

Is this because you're actually creating a new test factory in

factory
.WithWebHostBuilder(builder =>
{
    builder.ConfigureTestServices(services =>
    {
        services.AddMvc().AddApplicationPart(typeof(Startup).Assembly);
    });
})
.CreateClient();

So the controller endpoint is a different server instance than the signalr endpoing?

hkashif commented 1 year ago

Yes. I just realized that. Thanks!