Add tests to verify protocol message size (#15030)
This commit is contained in:
parent
666fe9b03c
commit
10863cfaea
|
|
@ -312,6 +312,50 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol
|
||||||
}, streamItemMessage.Item);
|
}, streamItemMessage.Item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IDictionary<string, MessageSizeTestData> MessageSizeData => new[]
|
||||||
|
{
|
||||||
|
new MessageSizeTestData("InvocationMessage_WithoutInvocationId", new InvocationMessage("Target", new object[] { 1 }), 45),
|
||||||
|
new MessageSizeTestData("InvocationMessage_WithInvocationId", new InvocationMessage("1", "Target", new object[] { 1 }), 64),
|
||||||
|
new MessageSizeTestData("InvocationMessage_WithInvocationIdAndStreamId", new InvocationMessage("1", "Target", new object[] { 1 }, new string[] { "2" }), 82),
|
||||||
|
|
||||||
|
new MessageSizeTestData("CloseMessage_Empty", CloseMessage.Empty, 11),
|
||||||
|
new MessageSizeTestData("CloseMessage_WithError", new CloseMessage("error"), 27),
|
||||||
|
|
||||||
|
new MessageSizeTestData("StreamItemMessage_WithNullItem", new StreamItemMessage("1", null), 42),
|
||||||
|
new MessageSizeTestData("StreamItemMessage_WithItem", new StreamItemMessage("1", 1), 39),
|
||||||
|
|
||||||
|
new MessageSizeTestData("CompletionMessage_Empty", CompletionMessage.Empty("1"), 30),
|
||||||
|
new MessageSizeTestData("CompletionMessage_WithResult", CompletionMessage.WithResult("1", 1), 41),
|
||||||
|
new MessageSizeTestData("CompletionMessage_WithError", CompletionMessage.WithError("1", "error"), 46),
|
||||||
|
|
||||||
|
new MessageSizeTestData("StreamInvocationMessage", new StreamInvocationMessage("1", "target", Array.Empty<object>()), 63),
|
||||||
|
new MessageSizeTestData("StreamInvocationMessage_WithStreamId", new StreamInvocationMessage("1", "target", Array.Empty<object>(), new [] { "2" }), 81),
|
||||||
|
|
||||||
|
new MessageSizeTestData("CancelInvocationMessage", new CancelInvocationMessage("1"), 30),
|
||||||
|
|
||||||
|
new MessageSizeTestData("PingMessage", PingMessage.Instance, 11),
|
||||||
|
}.ToDictionary(t => t.Name);
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> MessageSizeDataNames => MessageSizeData.Keys.Select(name => new object[] { name });
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(MessageSizeDataNames))]
|
||||||
|
public void VerifyMessageSize(string testDataName)
|
||||||
|
{
|
||||||
|
var testData = MessageSizeData[testDataName];
|
||||||
|
|
||||||
|
var writer = MemoryBufferWriter.Get();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
JsonHubProtocol.WriteMessage(testData.Message, writer);
|
||||||
|
Assert.Equal(testData.Size, writer.Length);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
MemoryBufferWriter.Return(writer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static string Frame(string input)
|
public static string Frame(string input)
|
||||||
{
|
{
|
||||||
var data = Encoding.UTF8.GetBytes(input);
|
var data = Encoding.UTF8.GetBytes(input);
|
||||||
|
|
@ -345,5 +389,21 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol
|
||||||
|
|
||||||
public override string ToString() => Name;
|
public override string ToString() => Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class MessageSizeTestData
|
||||||
|
{
|
||||||
|
public string Name { get; }
|
||||||
|
public HubMessage Message { get; }
|
||||||
|
public int Size { get; }
|
||||||
|
|
||||||
|
public MessageSizeTestData(string name, HubMessage message, int size)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
Message = message;
|
||||||
|
Size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString() => Name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -195,5 +195,55 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol
|
||||||
|
|
||||||
TestWriteMessages(testData);
|
TestWriteMessages(testData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IDictionary<string, MessageSizeTestData> MessageSizeData => new[]
|
||||||
|
{
|
||||||
|
new MessageSizeTestData("InvocationMessage_WithoutInvocationId", new InvocationMessage("Target", new object[] { 1 }), 15),
|
||||||
|
new MessageSizeTestData("InvocationMessage_WithInvocationId", new InvocationMessage("1", "Target", new object[] { 1 }), 16),
|
||||||
|
new MessageSizeTestData("InvocationMessage_WithInvocationIdAndStreamId", new InvocationMessage("1", "Target", new object[] { 1 }, new string[] { "2" }), 18),
|
||||||
|
|
||||||
|
new MessageSizeTestData("CloseMessage_Empty", CloseMessage.Empty, 4),
|
||||||
|
new MessageSizeTestData("CloseMessage_WithError", new CloseMessage("error"), 9),
|
||||||
|
|
||||||
|
new MessageSizeTestData("StreamItemMessage_WithNullItem", new StreamItemMessage("1", null), 7),
|
||||||
|
new MessageSizeTestData("StreamItemMessage_WithItem", new StreamItemMessage("1", 1), 7),
|
||||||
|
|
||||||
|
new MessageSizeTestData("CompletionMessage_Empty", CompletionMessage.Empty("1"), 7),
|
||||||
|
new MessageSizeTestData("CompletionMessage_WithResult", CompletionMessage.WithResult("1", 1), 8),
|
||||||
|
new MessageSizeTestData("CompletionMessage_WithError", CompletionMessage.WithError("1", "error"), 13),
|
||||||
|
|
||||||
|
new MessageSizeTestData("StreamInvocationMessage", new StreamInvocationMessage("1", "target", Array.Empty<object>()), 15),
|
||||||
|
new MessageSizeTestData("StreamInvocationMessage_WithStreamId", new StreamInvocationMessage("1", "target", Array.Empty<object>(), new [] { "2" }), 17),
|
||||||
|
|
||||||
|
new MessageSizeTestData("CancelInvocationMessage", new CancelInvocationMessage("1"), 6),
|
||||||
|
|
||||||
|
new MessageSizeTestData("PingMessage", PingMessage.Instance, 3),
|
||||||
|
}.ToDictionary(t => t.Name);
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> MessageSizeDataNames => MessageSizeData.Keys.Select(name => new object[] { name });
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(MessageSizeDataNames))]
|
||||||
|
public void VerifyMessageSize(string testDataName)
|
||||||
|
{
|
||||||
|
var testData = MessageSizeData[testDataName];
|
||||||
|
Assert.Equal(testData.Size, Write(testData.Message).Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MessageSizeTestData
|
||||||
|
{
|
||||||
|
public string Name { get; }
|
||||||
|
public HubMessage Message { get; }
|
||||||
|
public int Size { get; }
|
||||||
|
|
||||||
|
public MessageSizeTestData(string name, HubMessage message, int size)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
Message = message;
|
||||||
|
Size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString() => Name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue