Skip to content

Commit

Permalink
Empty message body fix (#1889)
Browse files Browse the repository at this point in the history
* if body empty on send, return an empty body on receive

* change to treat an empty message as null

* change to use string.Empty instead of null

* fix empty message magic string

* test for "empty message" body translation

* Cleanup and use different asserts

* Reverting to use the "empty message" magic string

---------

Co-authored-by: danielmarbach <daniel.marbach@openplace.net>
  • Loading branch information
jpalac and danielmarbach committed Feb 7, 2023
1 parent 000b70f commit 69b8ca6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/NServiceBus.Transport.SQS.Tests/InputQueuePumpTests.cs
Expand Up @@ -116,7 +116,7 @@ public async Task Expired_messages_are_deleted_without_processing()
{Headers.MessageId, messageId},
{TransportHeaders.TimeToBeReceived, ttbr.ToString()}
},
Body = "empty message"
Body = TransportMessage.EmptyMessage
});

var message = new Message
Expand Down Expand Up @@ -161,7 +161,7 @@ public async Task Processed_messages_are_deleted()
{
{Headers.MessageId, messageId}
},
Body = "empty message"
Body = TransportMessage.EmptyMessage
});

var message = new Message
Expand Down
52 changes: 49 additions & 3 deletions src/NServiceBus.Transport.SQS.Tests/TransportMessageTests.cs
Expand Up @@ -2,11 +2,13 @@
{
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using NServiceBus.Transport.SQS.Extensions;
using NServiceBus;
using NUnit.Framework;
using Performance.TimeToBeReceived;
using SQS;
using Transport;

[TestFixture]
Expand Down Expand Up @@ -131,7 +133,7 @@ public void Can_be_built_from_serialized_v1_message()
{
{Headers.MessageId, Guid.Empty.ToString()}
},
Body = "empty message",
Body = TransportMessage.EmptyMessage,
S3BodyKey = (string)null,
TimeToBeReceived = ExpectedTtbr.ToString(),
ReplyToAddress = new TransportMessage.Address
Expand All @@ -158,7 +160,7 @@ public void Can_be_built_from_serialized_message()
{
{Headers.MessageId, Guid.Empty.ToString()}
},
Body = "empty message",
Body = TransportMessage.EmptyMessage,
S3BodyKey = (string)null
});

Expand All @@ -170,6 +172,50 @@ public void Can_be_built_from_serialized_message()
Assert.IsNull(transportMessage.ReplyToAddress, "ReplyToAddress was not null.");
}

[Test]
public async Task Empty_body_is_received_ok()
{
var messageId = Guid.NewGuid().ToString();
var body = Array.Empty<byte>();
var outgoingMessage = new OutgoingMessage(messageId, new Dictionary<string, string>(), body);

var transportMessage = new TransportMessage(outgoingMessage, new DispatchProperties());

var receivedBodyArray = await transportMessage.RetrieveBody(messageId, null);
var receivedBody = Encoding.Unicode.GetString(receivedBodyArray);

CollectionAssert.AreEqual(receivedBodyArray, body);
Assert.That(receivedBody, Is.Null.Or.Empty);
}

[Test]
public async Task Null_body_is_received_ok()
{
var messageId = Guid.NewGuid().ToString();
var outgoingMessage = new OutgoingMessage(messageId, new Dictionary<string, string>(), null);

var transportMessage = new TransportMessage(outgoingMessage, new DispatchProperties());

var receivedBodyArray = await transportMessage.RetrieveBody(messageId, null);
var receivedBody = Encoding.Unicode.GetString(receivedBodyArray);

Assert.That(receivedBody, Is.Null.Or.Empty);
}

[Test]
public async Task Empty_message_string_body_is_received_as_empty()
{
var transportMessage = new TransportMessage
{
Body = "empty message",
};

var receivedBodyArray = await transportMessage.RetrieveBody(Guid.NewGuid().ToString(), null);
var receivedBody = Encoding.Unicode.GetString(receivedBodyArray);

Assert.That(receivedBody, Is.Null.Or.Empty);
}

const string ExpectedReplyToAddress = "TestReplyToAddress";
static readonly TimeSpan ExpectedTtbr = TimeSpan.MaxValue.Subtract(TimeSpan.FromHours(1));
}
Expand Down
11 changes: 9 additions & 2 deletions src/NServiceBus.Transport.SQS/Extensions/MessageExtensions.cs
Expand Up @@ -15,7 +15,14 @@ static class MessageExtensions
{
if (string.IsNullOrEmpty(transportMessage.S3BodyKey))
{
return Convert.FromBase64String(transportMessage.Body);
if (transportMessage.Body == TransportMessage.EmptyMessage)
{
return Array.Empty<byte>();
}
else
{
return Convert.FromBase64String(transportMessage.Body);
}
}

if (s3Settings == null)
Expand Down Expand Up @@ -51,4 +58,4 @@ public static DateTimeOffset GetAdjustedDateTimeFromServerSetAttributes(this Mes

static readonly DateTimeOffset UnixEpoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
}
}
}
7 changes: 5 additions & 2 deletions src/NServiceBus.Transport.SQS/TransportMessage.cs
Expand Up @@ -6,6 +6,9 @@

class TransportMessage
{
//MessageBody of Amazon.SQS SendRequest must have a minimum length of 1: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessage.html
public const string EmptyMessage = "empty message";

// Empty constructor required for deserialization.
public TransportMessage()
{
Expand All @@ -28,9 +31,9 @@ public TransportMessage(OutgoingMessage outgoingMessage, DispatchProperties prop
}

#if NETFRAMEWORK
Body = outgoingMessage.Body.Length != 0 ? Convert.ToBase64String(outgoingMessage.Body.ToArray()) : "empty message";
Body = outgoingMessage.Body.Length != 0 ? Convert.ToBase64String(outgoingMessage.Body.ToArray()) : EmptyMessage;
#else
Body = outgoingMessage.Body.Length != 0 ? Convert.ToBase64String(outgoingMessage.Body.Span) : "empty message";
Body = outgoingMessage.Body.Length != 0 ? Convert.ToBase64String(outgoingMessage.Body.Span) : EmptyMessage;
#endif
}

Expand Down

0 comments on commit 69b8ca6

Please sign in to comment.