Skip to content

Add tests to verify protocol message size #15030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,50 @@ public void ReadCaseInsensitivePropertiesByDefault()
}, 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)
{
var data = Encoding.UTF8.GetBytes(input);
Expand Down Expand Up @@ -345,5 +389,21 @@ public JsonProtocolTestData(string name, HubMessage message, bool useCamelCase,

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,55 @@ public void WriteMessages(string testDataName)

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