You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lost as to how to perform functional test for a blazor server page that contains a signalR client hub connection.
So far I have tried spinning up a WebApplicationFactory(WAF) from the SUT Startup class. I then copy the required services from the WAF over into the services of the BUnit test context.
The issue comes when the Blazor server page tries to connect to the signalR hub provided by the WAF. A System.Net.Http.HttpRequestException: Connection refused exception occurs (see listings below). I think this is because the WAF is not using sockets, i.e. it offers an in-memory server??
How are others performing tests for a similar scenario with signalR and Blazor?
Test so far
[Fact]publicasyncTaskTest_RendersImage(){// initialise BUnit with services from WebApplicationFactoryInitialiseBUnitServices();// publish an mqtt message to trigger sending event from signalR to Blazor server page// this is triggering back-end services as expectedawait_Server.MqttClient.PublishAsync(CreateMessageFromStream(_Stream));awaitTask.Delay(3000);// request bUnit to render the client page and listen for data from signal R here// fails to connect to signalR hub herevarcut=RenderComponent<WebApp.Pages.Index>();// output markup to check that client page rendered the incoming data_Output.WriteLine($"{cut.Markup}");// await CompletedTask to allow compilation of this async test stub awaitTask.CompletedTask;}/// <summary>Create an mqtt message from stream</summary>privateManagedMqttApplicationMessageCreateMessageFromStream(MemoryStreamstream){conststringTopic="shinobi/group/monitor/trigger";varmessage=newMqttApplicationMessageBuilder().WithTopic(Topic).WithPayload(stream.ToArray()).Build();returnnewManagedMqttApplicationMessageBuilder().WithApplicationMessage(message).Build();}privatevoidInitialiseBUnitServices(){// get services from WebApplicationFactory and inject into bUnitvarhubProxyFactory=_Server.Factory.Services.GetRequiredService<IHubProxyFactory>();varloggerDetectionConverter=_Server.Factory.Services.GetRequiredService<ILogger<MotionDetectionConverter>>();varloggerIndexPage=_Server.Factory.Services.GetRequiredService<ILogger<WebApp.Pages.Index>>();varloggerInfoConverter=_Server.Factory.Services.GetRequiredService<ILogger<MotionInfoConverter>>();varloggerJsonVisitor=_Server.Factory.Services.GetRequiredService<ILogger<JsonVisitor>>();varloggerRepository=_Server.Factory.Services.GetRequiredService<ILogger<MotionDetectionRepository>>();varnavManager=_Server.Factory.Services.GetRequiredService<NavigationManager>();varrepository=_Server.Factory.Services.GetRequiredService<IMotionDetectionRepository>();Services.AddSingleton(typeof(IHubProxyFactory),hubProxyFactory);Services.AddScoped<ILogger<MotionDetectionConverter>>(sp =>loggerDetectionConverter);Services.AddScoped<ILogger<WebApp.Pages.Index>>(sp =>loggerIndexPage);Services.AddScoped<ILogger<MotionInfoConverter>>(sp =>loggerInfoConverter);Services.AddScoped<ILogger<JsonVisitor>>(sp =>loggerJsonVisitor);Services.AddScoped<ILogger<IMotionDetectionRepository>>(sp =>loggerRepository);Services.AddScoped<MockNavigationManager>(sp =>newMockNavigationManager());Services.AddScoped<IMotionDetectionRepository>(sp =>repository);}
Blazor Server Page
protectedoverrideasyncTaskOnAfterRenderAsync(boolfirstRender){if(firstRender){/// <summary>/// Looks like abstract base class NavigationManager also needs a mock or sub class for unit testing./// Cannot resolve RemoteNavigationManager from container, it is an internal class. So for mocking purposes have injected a subclass for bUnit tests./// </summary>varhubUrl=NavigationManager.BaseUri.TrimEnd('/')+"/motionhub";hubUrl="http://localhost:5000/motionhub";// hard code url for now try{/// <summary> Using wrapper to allow unit testing!</summary>Logger.LogInformation("Index.razor page is performing initial render, connecting to secondary signalR hub");Logger.LogInformation($"hubUrl is {hubUrl}");hubConnection=HubConnectionBuilder.Create(hubUrl,JsonConvertersFactory.CreateDefaultJsonConverters(LoggerMotionDetection,LoggerMotionInfo,LoggerJsonVisitor));hubConnection.On<MotionDetection>("ReceiveMotionDetection",ReceiveMessage);hubConnection.Closed+=CloseHandler;Logger.LogInformation("Starting HubConnection");awaithubConnection.StartAsync();Logger.LogInformation("Index Razor Page initialised, listening on signalR hub => "+hubUrl.ToString());}catch(Exceptione){Logger.LogError(e,"Encountered exception => "+e);}}}
Encountered exception =>System.Net.Http.HttpRequestException: Connection refused
---> System.Net.Sockets.SocketException(61): Connection refused
at System.Net.Http.ConnectHelper.ConnectAsync(Stringhost,Int32port,CancellationTokencancellationToken)---End of inner exception stack trace ---atSystem.Net.Http.ConnectHelper.ConnectAsync(Stringhost,Int32port,CancellationTokencancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessagerequest,BooleanallowHttp2,CancellationTokencancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessagerequest,CancellationTokencancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessagerequest,CancellationTokencancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessagerequest,BooleandoRequestAuth,CancellationTokencancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessagerequest,CancellationTokencancellationToken)
at Microsoft.AspNetCore.Http.Connections.Client.Internal.AccessTokenHttpMessageHandler.SendAsync(HttpRequestMessagerequest,CancellationTokencancellationToken)
at Microsoft.AspNetCore.Http.Connections.Client.Internal.LoggingHttpMessageHandler.SendAsync(HttpRequestMessagerequest,CancellationTokencancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1sendTask,HttpRequestMessagerequest,CancellationTokenSourcects,BooleandisposeCts)
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.NegotiateAsync(Uriurl,HttpClienthttpClient,ILoggerlogger,CancellationTokencancellationToken)
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.GetNegotiationResponseAsync(Uriuri,CancellationTokencancellationToken)
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.SelectAndStartTransport(TransferFormattransferFormat,CancellationTokencancellationToken)
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsyncCore(TransferFormattransferFormat,CancellationTokencancellationToken)
at System.Threading.Tasks.ForceAsyncAwaiter.GetResult()
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsync(TransferFormattransferFormat,CancellationTokencancellationToken)at Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync(EndPointendPoint,CancellationTokencancellationToken)at Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync(EndPointendPoint,CancellationTokencancellationToken)at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncCore(CancellationTokencancellationToken)
at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncInner(CancellationTokencancellationToken)
at System.Threading.Tasks.ForceAsyncAwaiter.GetResult()at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsync(CancellationTokencancellationToken)
at WebApp.Pages.Index.OnAfterRenderAsync(BooleanfirstRender)in/Users/simon/Development/Dotnet/CamFrontEnd/Src/WebApp/Pages/Index.razor.cs:line167
System.Net.Http.HttpRequestException: Connection refused
---> System.Net.Sockets.SocketException (61): Connection refused
at System.Net.Http.ConnectHelper.ConnectAsync(Stringhost,Int32port,CancellationTokencancellationToken)---End of inner exception stack trace ---at System.Net.Http.ConnectHelper.ConnectAsync(Stringhost,Int32port,CancellationTokencancellationToken)at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessagerequest,BooleanallowHttp2,CancellationTokencancellationToken)at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessagerequest,CancellationTokencancellationToken)at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessagerequest,CancellationTokencancellationToken)at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessagerequest,BooleandoRequestAuth,CancellationTokencancellationToken)at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessagerequest,CancellationTokencancellationToken)at Microsoft.AspNetCore.Http.Connections.Client.Internal.AccessTokenHttpMessageHandler.SendAsync(HttpRequestMessagerequest,CancellationTokencancellationToken)at Microsoft.AspNetCore.Http.Connections.Client.Internal.LoggingHttpMessageHandler.SendAsync(HttpRequestMessagerequest,CancellationTokencancellationToken)at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1sendTask,HttpRequestMessagerequest,CancellationTokenSourcects,BooleandisposeCts)at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.NegotiateAsync(Uriurl,HttpClienthttpClient,ILoggerlogger,CancellationTokencancellationToken)at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.GetNegotiationResponseAsync(Uriuri,CancellationTokencancellationToken)at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.SelectAndStartTransport(TransferFormattransferFormat,CancellationTokencancellationToken)at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsyncCore(TransferFormattransferFormat,CancellationTokencancellationToken)at System.Threading.Tasks.ForceAsyncAwaiter.GetResult()atMicrosoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsync(TransferFormattransferFormat,CancellationTokencancellationToken)at Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync(EndPointendPoint,CancellationTokencancellationToken)at Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync(EndPointendPoint,CancellationTokencancellationToken)at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncCore(CancellationTokencancellationToken)
at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncInner(CancellationTokencancellationToken)
at System.Threading.Tasks.ForceAsyncAwaiter.GetResult()at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsync(CancellationTokencancellationToken)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi
Lost as to how to perform functional test for a blazor server page that contains a signalR client hub connection.
So far I have tried spinning up a
WebApplicationFactory
(WAF) from the SUT Startup class. I then copy the required services from the WAF over into the services of the BUnit test context.The issue comes when the Blazor server page tries to connect to the signalR hub provided by the WAF. A System.Net.Http.HttpRequestException: Connection refused exception occurs (see listings below). I think this is because the WAF is not using sockets, i.e. it offers an in-memory server??
How are others performing tests for a similar scenario with signalR and Blazor?
Test so far
Blazor Server Page
Beta Was this translation helpful? Give feedback.
All reactions