-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #385 from Particular/backport-fix
Make sure PendingTransportOperations clearing doesn't leak state from previous executions
- Loading branch information
Showing
3 changed files
with
79 additions
and
15 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...ServiceBus.Persistence.CosmosDB.Tests/Outbox/PendingTransportOperationsExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
namespace NServiceBus.Persistence.CosmosDB.Tests | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using NUnit.Framework; | ||
using Routing; | ||
using Transport; | ||
|
||
[TestFixture] | ||
public class PendingTransportOperationsExtensionsTests | ||
{ | ||
[Test] | ||
public void Should_clear_existing_operations() | ||
{ | ||
var operations = new PendingTransportOperations(); | ||
operations.Add(new TransportOperation( | ||
new OutgoingMessage("", new Dictionary<string, string>(), Array.Empty<byte>()), | ||
new UnicastAddressTag("someQueue"))); | ||
|
||
operations.Clear(); | ||
|
||
Assert.That(operations.Operations, Is.Empty); | ||
} | ||
|
||
[Test] | ||
public void Should_support_adding_after_clearing() | ||
{ | ||
var operations = new PendingTransportOperations(); | ||
operations.Add(new TransportOperation( | ||
new OutgoingMessage("1", new Dictionary<string, string>(), Array.Empty<byte>()), | ||
new UnicastAddressTag("someQueue"))); | ||
|
||
operations.Clear(); | ||
|
||
operations.Add(new TransportOperation( | ||
new OutgoingMessage("2", new Dictionary<string, string>(), Array.Empty<byte>()), | ||
new UnicastAddressTag("someQueue"))); | ||
|
||
operations.Clear(); | ||
|
||
operations.Add(new TransportOperation( | ||
new OutgoingMessage("3", new Dictionary<string, string>(), Array.Empty<byte>()), | ||
new UnicastAddressTag("someQueue"))); | ||
|
||
Assert.That(operations.Operations, Has.Length.EqualTo(1)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/NServiceBus.Persistence.CosmosDB/Outbox/PendingTransportOperationsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace NServiceBus.Persistence.CosmosDB | ||
{ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using Transport; | ||
|
||
static class PendingTransportOperationsExtensions | ||
{ | ||
static PendingTransportOperationsExtensions() | ||
{ | ||
var field = typeof(PendingTransportOperations).GetField("operations", | ||
BindingFlags.NonPublic | BindingFlags.Instance); | ||
var targetExp = Expression.Parameter(typeof(PendingTransportOperations), "target"); | ||
var fieldExp = Expression.Field(targetExp, field); | ||
getter = Expression | ||
.Lambda<Func<PendingTransportOperations, ConcurrentStack<TransportOperation>>>(fieldExp, targetExp) | ||
.Compile(); | ||
} | ||
|
||
public static void Clear(this PendingTransportOperations operations) | ||
{ | ||
var collection = getter(operations); | ||
collection.Clear(); | ||
} | ||
|
||
static readonly Func<PendingTransportOperations, ConcurrentStack<TransportOperation>> getter; | ||
} | ||
} |