WebTest for SignalR possible?
Asked Answered
F

2

6

if I send a request, and I expect the response to come trough SignalR, is it possible to test this using a LoadTest or PerformanceTest in Visual Studio?

Foundry answered 17/2, 2013 at 14:18 Comment(0)
R
2

Short answer: Yes

I've done this several times in CodedWebTests but it would also be possible to do in a declarative WebTest. You can use a custom PreWebTest Event Handler to create your signalR client and connect to your SignalR hub. What you choose to do with the signalR notification is up to you but I like to save it to the WebTestContext as well as display it on the test results screen using the AddCommentToResult method.

The method below creates a hubConnection invokes the "addToGroup" function on the hub and then tells the client what to do when it receives a message.

using Microsoft.AspNet.SignalR.Client;

public class SignalRPlugin : WebtTestPlugin
{
    public override void PreWebTest(object sender, PreWebTestEventArgs e)
    {
        var hubConnection = new HubConnection("yourSignalRUrl");
        var hubProxy = hubConnection.CreateHubProxy("notifications");

        hubConnection.Start().Wait();
        hubProxy.Invoke("addToGroup", "me");

        hubProxy.On<string>("message", s =>
        {
            e.Webtest.AddCommentToResult(s);
            e.Webtest.Context.Add("signalRMessages", s);
        });
    }
}

Use it by attaching the event handler in your test constructor.

public MyWebTest()
{
    PreWebTest += new SignalRPlugin().PreWebTest;
}

Then once you have the signalR messages you can use a custom validation rule to validate that the response was received. Just have a while loop checking the WebTestContext for the "signalRMessages" key. I strongly suggest making sure you add a timeout feature so you are not waiting forever if the messages never come in.

The other option if you are writing CodedWebTests is to create a WaitForNotifications method that basically does the same thing as the validation rule. The advantage with this is that you can use an extraction rule to get data out of the last response and then use that data in validating your signalR messages. If you still need to fail a test in your WaitForNotification method use WebTest.InternalSetOutcome(Outcome.Fail);

Romano answered 25/8, 2017 at 16:44 Comment(0)
H
0

The best way to load test a SignalR application is by building on the crank project included in the source.

This is a simple ramp up solution built with the .Net client but it is relatively easy to modify to call whatever hub methods you require and to analyse the responses.

You can always attach the Visual Studio profiler to your iis express instance to get detailed profiling data if required.

Hostile answered 17/2, 2013 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.