Skip to content

Commit

Permalink
Session disposal (#6525)
Browse files Browse the repository at this point in the history
* resolve storage session per call

* only dispose session once

* approve scope change in test

* change registration back to same scope as the referenced type
  • Loading branch information
timbussmann authored Aug 31, 2022
1 parent a35bc1d commit b693bac
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace NServiceBus.Core.Tests.Reliability
{
using System.Threading.Tasks;
using Extensibility;
using NServiceBus.Outbox;
using NServiceBus.Persistence;
using NUnit.Framework;
using Transport;

[TestFixture]
public class CompletableSynchronizedStorageSessionAdapterTests
{
[Test]
public async Task Should_dispose_adapted_session_only_once()
{
var storageAdapter = new FakeStorageAdapter();
var sessionAdapter = new CompletableSynchronizedStorageSessionAdapter(storageAdapter, null);
await sessionAdapter.TryOpen(new TransportTransaction(), new ContextBag());

sessionAdapter.Dispose();
sessionAdapter.Dispose();

Assert.AreEqual(1, storageAdapter.StorageSession.DisposeCounter);
}
}

public class FakeStorageAdapter : ISynchronizedStorageAdapter
{
public FakeStorageSession StorageSession { get; } = new FakeStorageSession();

public Task<CompletableSynchronizedStorageSession> TryAdapt(OutboxTransaction transaction, ContextBag context) => Task.FromResult<CompletableSynchronizedStorageSession>(StorageSession);

public Task<CompletableSynchronizedStorageSession> TryAdapt(TransportTransaction transportTransaction, ContextBag context) => Task.FromResult<CompletableSynchronizedStorageSession>(StorageSession);
}

public class FakeStorageSession : CompletableSynchronizedStorageSession
{
public int DisposeCounter { get; private set; }

public void Dispose() => DisposeCounter++;

public Task CompleteAsync() => Task.FromResult(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ public CompletableSynchronizedStorageSessionAdapter(ISynchronizedStorageAdapter
}
public CompletableSynchronizedStorageSession AdaptedSession { get; private set; }

public void Dispose() => AdaptedSession?.Dispose();
public void Dispose()
{
if (disposed)
{
return;
}

AdaptedSession?.Dispose();
disposed = true;
}

public Task CompleteAsync() => AdaptedSession.CompleteAsync();

Expand All @@ -37,6 +46,8 @@ public async Task<bool> TryOpen(TransportTransaction transportTransaction, Conte

public async Task Open(ContextBag contextBag) => AdaptedSession = await synchronizedStorage.OpenSession(contextBag).ConfigureAwait(false);

bool disposed;

readonly ISynchronizedStorageAdapter synchronizedStorageAdapter;
readonly ISynchronizedStorage synchronizedStorage;
}
Expand Down

0 comments on commit b693bac

Please sign in to comment.