From dc1fe216d73cf37cb757267325830680df593641 Mon Sep 17 00:00:00 2001 From: Gabriele Santomaggio Date: Thu, 25 Aug 2022 10:26:49 +0200 Subject: [PATCH] Handle more type in the AMQP 1.0 GetSize (#158) * Fixes https://github.com/rabbitmq/rabbitmq-stream-dotnet-client/issues/157 Add Size Value For bool, short, fload and double in the AMQP 1.0 codec. Signed-off-by: Gabriele Santomaggio --- .../AMQP/AmqpWireFormattingWrite.cs | 29 ++++++- Tests/Amqp10Tests.cs | 77 ++++++++++++++++-- Tests/Resources/static_test_message_compare | Bin 151 -> 168 bytes 3 files changed, 100 insertions(+), 6 deletions(-) diff --git a/RabbitMQ.Stream.Client/AMQP/AmqpWireFormattingWrite.cs b/RabbitMQ.Stream.Client/AMQP/AmqpWireFormattingWrite.cs index d95a164a..de9d37f9 100644 --- a/RabbitMQ.Stream.Client/AMQP/AmqpWireFormattingWrite.cs +++ b/RabbitMQ.Stream.Client/AMQP/AmqpWireFormattingWrite.cs @@ -330,12 +330,39 @@ public static int GetAnySize(object value) uint u => GetUIntSize(u), ulong ul => GetUInt64Size(ul), long l => GetInt64Size(l), + double => GetDoubleSize(), ushort => GetUInt16Size(), + short => GetInt16Size(), + bool => GetBoolSize(), + float => GetFloatSize(), byte[] bArr => GetBytesSize(bArr), byte => 1, DateTime d => GetTimestampSize(d), - _ => throw new AmqpParseException($"WriteAny Invalid type {value}") + _ => throw new AmqpParseException($"GetAnySize Invalid type {value}") }; } + + private static int GetFloatSize() + { + return 1 // FormatCode.Float + + 4; //value + } + + private static int GetInt16Size() + { + return 1 // FormatCode.Short + + 2; //value + } + + private static int GetBoolSize() + { + return 1; // FormatCode.Bool + } + + private static int GetDoubleSize() + { + return 1 // FormatCode.Double + + 8; //value + } } } diff --git a/Tests/Amqp10Tests.cs b/Tests/Amqp10Tests.cs index 588c47e1..8121964c 100644 --- a/Tests/Amqp10Tests.cs +++ b/Tests/Amqp10Tests.cs @@ -351,11 +351,8 @@ public void ValidateMessagesFromGo() Assert.Equal("test", msgStaticTest.Properties.GroupId); Assert.Equal("test", msgStaticTest.Properties.ReplyToGroupId); Assert.Equal("test", Encoding.Default.GetString(msgStaticTest.Properties.UserId)); - foreach (var (key, value) in msgStaticTest.ApplicationProperties) - { - Assert.Equal("test", key); - Assert.Equal("test", value); - } + Assert.Equal("test", msgStaticTest.ApplicationProperties["test"]); + Assert.Equal(64.646464, msgStaticTest.ApplicationProperties["double"]); Assert.Equal("test", msgStaticTest.Annotations["test"]); Assert.Equal((long)1, msgStaticTest.Annotations[(long)1]); @@ -374,6 +371,76 @@ public void ValidateMessagesFromGo() Assert.Equal("amqpValue", msgHeader.AmqpValue); } + [Fact] + public void ValidateMapsType() + { + const double DoubleValue = 6665555.34566; + var dt = DateTime.Now; + var app = new ApplicationProperties() + { + ["double_value"] = DoubleValue, + ["string_value"] = "test", + ["bool_value"] = true, + ["byte_value"] = (byte)1, + ["short_value"] = (short)1, + ["int_value"] = 1, + ["long_value"] = 1L, + ["ulong_value"] = 1UL, + ["float_value"] = 1.0f, + ["date_value"] = dt + }; + var m = new Message(Encoding.Default.GetBytes("hello")) { ApplicationProperties = app }; + Assert.NotNull(m.ApplicationProperties); + + Assert.Equal(14, AmqpWireFormatting.GetAnySize("string_value")); + Assert.Equal(9, AmqpWireFormatting.GetAnySize(DoubleValue)); + Assert.Equal(1, AmqpWireFormatting.GetAnySize(true)); + Assert.Equal(1, AmqpWireFormatting.GetAnySize((byte)1)); + Assert.Equal(3, AmqpWireFormatting.GetAnySize((short)1)); + // In this case is a byte + Assert.Equal(2, AmqpWireFormatting.GetAnySize(1)); + // In this case is a byte less than 128 + Assert.Equal(2, AmqpWireFormatting.GetAnySize(1L)); + // In this case is a long + Assert.Equal(9, AmqpWireFormatting.GetAnySize(1000L)); + + // byte + Assert.Equal(2, AmqpWireFormatting.GetAnySize(1UL)); + // ulong + Assert.Equal(9, AmqpWireFormatting.GetAnySize(1000UL)); + + Assert.Equal(5, AmqpWireFormatting.GetAnySize(1.0f)); + + Assert.Equal(9, AmqpWireFormatting.GetAnySize(dt)); + + var size = DescribedFormatCode.Size; + size += sizeof(byte); //FormatCode.List32 + size += sizeof(uint); // field numbers + size += sizeof(uint); // PropertySize + size += AmqpWireFormatting.GetAnySize("double_value"); + size += AmqpWireFormatting.GetAnySize(DoubleValue); + size += AmqpWireFormatting.GetAnySize("string_value"); + size += AmqpWireFormatting.GetAnySize("test"); + size += AmqpWireFormatting.GetAnySize("bool_value"); + size += AmqpWireFormatting.GetAnySize(true); + size += AmqpWireFormatting.GetAnySize("byte_value"); + size += AmqpWireFormatting.GetAnySize((byte)1); + size += AmqpWireFormatting.GetAnySize("short_value"); + size += AmqpWireFormatting.GetAnySize((short)1); + size += AmqpWireFormatting.GetAnySize("int_value"); + size += AmqpWireFormatting.GetAnySize(1); + size += AmqpWireFormatting.GetAnySize("long_value"); + size += AmqpWireFormatting.GetAnySize(1L); + size += AmqpWireFormatting.GetAnySize("ulong_value"); + size += AmqpWireFormatting.GetAnySize(1UL); + size += AmqpWireFormatting.GetAnySize("float_value"); + size += AmqpWireFormatting.GetAnySize(1.0f); + size += AmqpWireFormatting.GetAnySize("date_value"); + size += AmqpWireFormatting.GetAnySize(dt); + + Assert.Equal(size, m.ApplicationProperties.Size); + } + [Fact] public void MapEntriesWithAnEmptyKeyShouldNotBeWrittenToTheWire() { diff --git a/Tests/Resources/static_test_message_compare b/Tests/Resources/static_test_message_compare index 2fc584af3ae0154bd5fd21e090c857fc102663cf..3f999b99c0d9fbcdf913eb42e4e62406fc3fa994 100644 GIT binary patch delta 50 xcmbQvxPoy)t+pZq0|U!KmXg%s5-7=*l3$vXliK7Epc%iat7$1iaOncDJOH1v5v%|J delta 33 hcmZ3%IGu4qt&{)*0|V1SmXg%s5(vo