Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebTest for SignalR possible?

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?

like image 209
Ryan Avatar asked Feb 17 '13 14:02

Ryan


1 Answers

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);

like image 128
Dan Gossner Avatar answered Nov 25 '22 03:11

Dan Gossner