I would like to know some of the different methods that have been used to test a SignalR hubs-based application.
In short, if using Hubs, using the .Net client will suffice.
In my case, I have a newsfeed hub that dishes out client-specific data based on the user's profile ID. In my test case, I load up a bunch of profile ID's (6000 in this case), invoke a hub method called JoinNewsfeed() along with the client-specific connection ID and profile ID. Every 100ms a new connection is established.
[TestMethod]
public void TestJoinNewsfeedHub()
{
int successfulConnections = 0;
// get profile ID's
memberService = new MemberServiceClient();
List<int> profileIDs = memberService.GetProfileIDs(6000).ToList<int>();
HubConnection hubConnection = new HubConnection(HUB_URL);
IHubProxy newsfeedHub = hubConnection.CreateProxy("NewsfeedHub");
foreach (int profileID in profileIDs)
{
hubConnection.Start().Wait();
//hubConnection = EstablishHubConnection();
newsfeedHub.Invoke<string>("JoinNewsfeed", hubConnection.ConnectionId, profileID).ContinueWith(task2 =>
{
if (task2.IsFaulted)
{
System.Diagnostics.Debug.Write(String.Format("An error occurred during the method call {0}", task2.Exception.GetBaseException()));
}
else
{
successfulConnections++;
System.Diagnostics.Debug.Write(String.Format("Successfully called MethodOnServer: {0}", successfulConnections));
}
});
Thread.Sleep(100);
}
Assert.IsNotNull(newsfeedHub);
}
Performance metrics listed here do the trick on the server. To ensure that a client has in fact connected and the process of populating client object on the server has successfully completed, I have a server-side method that invokes a client-side function with the number and list of connected clients derived from the connected client collection.
@ElHaix From what I have seen in my own tests your method is not creating a new connection, but reusing the existing connection. As you loop over the collection of profileIDs you should see that hubConnection.ConnectionID stays the same. In order to create a new connection you would need to create an instance of HubConnection inside the foreach loop.
int successfulConnections = 0;
const int loopId = 10;
Console.WriteLine("Starting...");
for (int i = 1; i <= loopId; i++)
{
Console.WriteLine("loop " + i);
var hubConnection = new HubConnection(HUB_URL);
IHubProxy chatHub = hubConnection.CreateProxy(HUB_NAME);
Console.WriteLine("Starting connection");
hubConnection.Start().Wait();
Console.WriteLine("Connection started: " + hubConnection.ConnectionId);
chatHub.Invoke("Register", "testroom").ContinueWith(task2 =>
{
if (task2.IsFaulted)
{
Console.WriteLine(String.Format("An error occurred during the method call {0}", task2.Exception.GetBaseException()));
}
else
{
Console.WriteLine("Connected: " + hubConnection.ConnectionId);
successfulConnections++;
}
});
Thread.Sleep(1000);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With