Skip to content
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

Empty message body fix #1889

Merged
merged 7 commits into from Feb 7, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
danielmarbach marked this conversation as resolved.
Show resolved Hide resolved
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"
danielmarbach marked this conversation as resolved.
Show resolved Hide resolved
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";
mikeminutillo marked this conversation as resolved.
Show resolved Hide resolved
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
danielmarbach marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down