Skip to content

Commit

Permalink
fix default return address on one-way client
Browse files Browse the repository at this point in the history
  • Loading branch information
mookid8000 committed Apr 10, 2024
1 parent da961be commit 1c7b15c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,9 @@
## 8.4.1
* Fix bug that would cause the decryption step to try to decrypt un-encrypted contents when using encryption together with 2nd level retries

## 8.4.2
* Fix bug that would prevent using a default return address on a one-way client

---

[AndreaCuneo]: https://github.com/AndreaCuneo
Expand Down
36 changes: 35 additions & 1 deletion Rebus.Tests/Routing/TestDefaultReturnAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task AssignsDefaultReturnAddressOnSentMessage()
var receiver = Using(new BuiltinHandlerActivator());

var returnAddress = "";

using var done = new ManualResetEvent(false);

receiver.Handle<string>(async (bus, context, message) =>
Expand All @@ -52,4 +52,38 @@ public async Task AssignsDefaultReturnAddressOnSentMessage()

Assert.That(returnAddress, Is.EqualTo("a totally different queue name"), "Expected a totally different queue name here");
}

[Test]
public async Task AssignsDefaultReturnAddressOnSentMessage_OneWayClient()
{
var network = new InMemNetwork();

using var client = Configure.OneWayClient()
.Transport(t => t.UseInMemoryTransportAsOneWayClient(network))
.Routing(r => r.TypeBased().Map<string>("queue-b"))
.Options(o => o.SetDefaultReturnAddress("a totally different queue name"))
.Start();

var receiver = Using(new BuiltinHandlerActivator());

var returnAddress = "";

using var done = new ManualResetEvent(false);

receiver.Handle<string>(async (bus, context, message) =>
{
returnAddress = context.Headers[Headers.ReturnAddress];
done.Set();
});

Configure.With(receiver)
.Transport(t => t.UseInMemoryTransport(network, "queue-b"))
.Start();

await client.Send("HEJ MED DIG MIN VEEEEN!");

done.WaitOrDie(TimeSpan.FromSeconds(2));

Assert.That(returnAddress, Is.EqualTo("a totally different queue name"), "Expected a totally different queue name here");
}
}
11 changes: 7 additions & 4 deletions Rebus/Pipeline/Send/AssignDefaultHeadersStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Rebus.Serialization;
using Rebus.Transport;
using Rebus.Time;
// ReSharper disable CanSimplifyDictionaryLookupWithTryAdd

namespace Rebus.Pipeline.Send;

Expand Down Expand Up @@ -31,7 +32,8 @@ public class AssignDefaultHeadersStep : IOutgoingStep
readonly IRebusTime _rebusTime;
readonly string _senderAddress;
readonly string _returnAddress;
readonly bool _hasOwnAddress;
readonly bool _hasSenderAddress;
readonly bool _hasReturnAddress;

/// <summary>
/// Constructs the step, getting the input queue address from the given <see cref="ITransport"/>
Expand All @@ -42,7 +44,8 @@ public AssignDefaultHeadersStep(ITransport transport, IRebusTime rebusTime, IMes
_messageTypeNameConvention = messageTypeNameConvention ?? throw new ArgumentNullException(nameof(messageTypeNameConvention));
_senderAddress = transport.Address;
_returnAddress = defaultReturnAddressOrNull ?? transport.Address;
_hasOwnAddress = !string.IsNullOrWhiteSpace(_senderAddress);
_hasSenderAddress = !string.IsNullOrWhiteSpace(_senderAddress);
_hasReturnAddress = !string.IsNullOrWhiteSpace(_returnAddress);
}

/// <summary>
Expand All @@ -58,7 +61,7 @@ public async Task Process(OutgoingStepContext context, Func<Task> next)
headers[Headers.MessageId] = Guid.NewGuid().ToString();
}

if (_hasOwnAddress && !headers.ContainsKey(Headers.ReturnAddress))
if (_hasReturnAddress && !headers.ContainsKey(Headers.ReturnAddress))
{
headers[Headers.ReturnAddress] = _returnAddress;
}
Expand All @@ -68,7 +71,7 @@ public async Task Process(OutgoingStepContext context, Func<Task> next)
headers[Headers.SentTime] = _rebusTime.Now.ToString("O");
}

if (_hasOwnAddress)
if (_hasSenderAddress)
{
headers[Headers.SenderAddress] = _senderAddress;
}
Expand Down

0 comments on commit 1c7b15c

Please sign in to comment.