Skip to content

Commit dc1fe21

Browse files
authored
Handle more type in the AMQP 1.0 GetSize (#158)
* Fixes #157 Add Size Value For bool, short, fload and double in the AMQP 1.0 codec. Signed-off-by: Gabriele Santomaggio <G.santomaggio@gmail.com>
1 parent da980eb commit dc1fe21

File tree

3 files changed

+100
-6
lines changed

3 files changed

+100
-6
lines changed

RabbitMQ.Stream.Client/AMQP/AmqpWireFormattingWrite.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,39 @@ public static int GetAnySize(object value)
330330
uint u => GetUIntSize(u),
331331
ulong ul => GetUInt64Size(ul),
332332
long l => GetInt64Size(l),
333+
double => GetDoubleSize(),
333334
ushort => GetUInt16Size(),
335+
short => GetInt16Size(),
336+
bool => GetBoolSize(),
337+
float => GetFloatSize(),
334338
byte[] bArr => GetBytesSize(bArr),
335339
byte => 1,
336340
DateTime d => GetTimestampSize(d),
337-
_ => throw new AmqpParseException($"WriteAny Invalid type {value}")
341+
_ => throw new AmqpParseException($"GetAnySize Invalid type {value}")
338342
};
339343
}
344+
345+
private static int GetFloatSize()
346+
{
347+
return 1 // FormatCode.Float
348+
+ 4; //value
349+
}
350+
351+
private static int GetInt16Size()
352+
{
353+
return 1 // FormatCode.Short
354+
+ 2; //value
355+
}
356+
357+
private static int GetBoolSize()
358+
{
359+
return 1; // FormatCode.Bool
360+
}
361+
362+
private static int GetDoubleSize()
363+
{
364+
return 1 // FormatCode.Double
365+
+ 8; //value
366+
}
340367
}
341368
}

Tests/Amqp10Tests.cs

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,8 @@ public void ValidateMessagesFromGo()
351351
Assert.Equal("test", msgStaticTest.Properties.GroupId);
352352
Assert.Equal("test", msgStaticTest.Properties.ReplyToGroupId);
353353
Assert.Equal("test", Encoding.Default.GetString(msgStaticTest.Properties.UserId));
354-
foreach (var (key, value) in msgStaticTest.ApplicationProperties)
355-
{
356-
Assert.Equal("test", key);
357-
Assert.Equal("test", value);
358-
}
354+
Assert.Equal("test", msgStaticTest.ApplicationProperties["test"]);
355+
Assert.Equal(64.646464, msgStaticTest.ApplicationProperties["double"]);
359356

360357
Assert.Equal("test", msgStaticTest.Annotations["test"]);
361358
Assert.Equal((long)1, msgStaticTest.Annotations[(long)1]);
@@ -374,6 +371,76 @@ public void ValidateMessagesFromGo()
374371
Assert.Equal("amqpValue", msgHeader.AmqpValue);
375372
}
376373

374+
[Fact]
375+
public void ValidateMapsType()
376+
{
377+
const double DoubleValue = 6665555.34566;
378+
var dt = DateTime.Now;
379+
var app = new ApplicationProperties()
380+
{
381+
["double_value"] = DoubleValue,
382+
["string_value"] = "test",
383+
["bool_value"] = true,
384+
["byte_value"] = (byte)1,
385+
["short_value"] = (short)1,
386+
["int_value"] = 1,
387+
["long_value"] = 1L,
388+
["ulong_value"] = 1UL,
389+
["float_value"] = 1.0f,
390+
["date_value"] = dt
391+
};
392+
var m = new Message(Encoding.Default.GetBytes("hello")) { ApplicationProperties = app };
393+
Assert.NotNull(m.ApplicationProperties);
394+
395+
Assert.Equal(14, AmqpWireFormatting.GetAnySize("string_value"));
396+
Assert.Equal(9, AmqpWireFormatting.GetAnySize(DoubleValue));
397+
Assert.Equal(1, AmqpWireFormatting.GetAnySize(true));
398+
Assert.Equal(1, AmqpWireFormatting.GetAnySize((byte)1));
399+
Assert.Equal(3, AmqpWireFormatting.GetAnySize((short)1));
400+
// In this case is a byte
401+
Assert.Equal(2, AmqpWireFormatting.GetAnySize(1));
402+
// In this case is a byte less than 128
403+
Assert.Equal(2, AmqpWireFormatting.GetAnySize(1L));
404+
// In this case is a long
405+
Assert.Equal(9, AmqpWireFormatting.GetAnySize(1000L));
406+
407+
// byte
408+
Assert.Equal(2, AmqpWireFormatting.GetAnySize(1UL));
409+
// ulong
410+
Assert.Equal(9, AmqpWireFormatting.GetAnySize(1000UL));
411+
412+
Assert.Equal(5, AmqpWireFormatting.GetAnySize(1.0f));
413+
414+
Assert.Equal(9, AmqpWireFormatting.GetAnySize(dt));
415+
416+
var size = DescribedFormatCode.Size;
417+
size += sizeof(byte); //FormatCode.List32
418+
size += sizeof(uint); // field numbers
419+
size += sizeof(uint); // PropertySize
420+
size += AmqpWireFormatting.GetAnySize("double_value");
421+
size += AmqpWireFormatting.GetAnySize(DoubleValue);
422+
size += AmqpWireFormatting.GetAnySize("string_value");
423+
size += AmqpWireFormatting.GetAnySize("test");
424+
size += AmqpWireFormatting.GetAnySize("bool_value");
425+
size += AmqpWireFormatting.GetAnySize(true);
426+
size += AmqpWireFormatting.GetAnySize("byte_value");
427+
size += AmqpWireFormatting.GetAnySize((byte)1);
428+
size += AmqpWireFormatting.GetAnySize("short_value");
429+
size += AmqpWireFormatting.GetAnySize((short)1);
430+
size += AmqpWireFormatting.GetAnySize("int_value");
431+
size += AmqpWireFormatting.GetAnySize(1);
432+
size += AmqpWireFormatting.GetAnySize("long_value");
433+
size += AmqpWireFormatting.GetAnySize(1L);
434+
size += AmqpWireFormatting.GetAnySize("ulong_value");
435+
size += AmqpWireFormatting.GetAnySize(1UL);
436+
size += AmqpWireFormatting.GetAnySize("float_value");
437+
size += AmqpWireFormatting.GetAnySize(1.0f);
438+
size += AmqpWireFormatting.GetAnySize("date_value");
439+
size += AmqpWireFormatting.GetAnySize(dt);
440+
441+
Assert.Equal(size, m.ApplicationProperties.Size);
442+
}
443+
377444
[Fact]
378445
public void MapEntriesWithAnEmptyKeyShouldNotBeWrittenToTheWire()
379446
{
17 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)