Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
glucaci committed Oct 2, 2023
1 parent 4089a71 commit 361f56b
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 8 deletions.
138 changes: 138 additions & 0 deletions src/Session.Tests/MongoSessionProviderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Extensions.Context;
using Squadron;
using Xunit;

namespace MongoDB.Extensions.Session.Tests;

public class MongoSessionProviderTests : IClassFixture<MongoReplicaSetResource>
{
private readonly IServiceProvider _serviceProvider;

public MongoSessionProviderTests(MongoReplicaSetResource mongoResource)
{
var mongoOptions = new MongoOptions
{
ConnectionString = mongoResource.ConnectionString,
DatabaseName = mongoResource.CreateDatabase().DatabaseNamespace.DatabaseName
};

_serviceProvider = new ServiceCollection()
.AddSingleton(mongoOptions)
.AddMongoSessionProvider<TestDbContext>()
.BuildServiceProvider();
}

[Fact]
public async Task BeginTransactionAsync_ShouldBeginTransaction()
{
// Arrange
ISessionProvider<TestDbContext> sessionProvider = _serviceProvider
.GetRequiredService<ISessionProvider<TestDbContext>>();

// Act
ITransactionSession transactionSession = await sessionProvider
.BeginTransactionAsync(CancellationToken.None);

// Assert
transactionSession.Should().NotBeNull();
IClientSessionHandle clientSessionHandle = transactionSession.GetSessionHandle();
clientSessionHandle.ServerSession.Id["id"].AsGuid.Should().NotBeEmpty();
clientSessionHandle.IsInTransaction.Should().BeTrue();
}

[Fact]
public async Task StartSessionAsync_ShouldStartSession()
{
// Arrange
ISessionProvider<TestDbContext> sessionProvider = _serviceProvider
.GetRequiredService<ISessionProvider<TestDbContext>>();

// Act
ISession session = await sessionProvider
.StartSessionAsync(CancellationToken.None);

// Assert
session.Should().NotBeNull();
IClientSessionHandle clientSessionHandle = session.GetSessionHandle();
clientSessionHandle.ServerSession.Id["id"].AsGuid.Should().NotBeEmpty();
clientSessionHandle.IsInTransaction.Should().BeFalse();
}

[Fact]
public async Task MongoSession_Dispose_ShouldDisposeSession()
{
// Arrange
ISessionProvider<TestDbContext> sessionProvider = _serviceProvider
.GetRequiredService<ISessionProvider<TestDbContext>>();

ISession session = await sessionProvider
.StartSessionAsync(CancellationToken.None);

// Act
session.Dispose();

// Assert
IClientSessionHandle clientSessionHandle = session.GetSessionHandle();
Assert.Throws<ObjectDisposedException>(() => clientSessionHandle.ServerSession);
}

[Fact]
public async Task MongoTransactionSession_Dispose_ShouldDisposeSession()
{
// Arrange
ISessionProvider<TestDbContext> sessionProvider = _serviceProvider
.GetRequiredService<ISessionProvider<TestDbContext>>();

ITransactionSession transactionSession = await sessionProvider
.BeginTransactionAsync(CancellationToken.None);

// Act
transactionSession.Dispose();

// Assert
IClientSessionHandle clientSessionHandle = transactionSession.GetSessionHandle();
Assert.Throws<ObjectDisposedException>(() => clientSessionHandle.ServerSession);
}

[Fact]
public async Task MongoTransactionSession_NotCommitting_ShouldNotAffectDatabase()
{
// Arrange
ISessionProvider<TestDbContext> sessionProvider = _serviceProvider
.GetRequiredService<ISessionProvider<TestDbContext>>();

ITransactionSession transactionSession = await sessionProvider
.BeginTransactionAsync(CancellationToken.None);
TestDbContext context = _serviceProvider.GetRequiredService<TestDbContext>();
IMongoCollection<BsonDocument> collection = context.CreateCollection<BsonDocument>();
await collection.InsertOneAsync(transactionSession.GetSessionHandle(), new BsonDocument());

// Act
// Not committing the transaction

// Assert
(await collection
.Find(FilterDefinition<BsonDocument>.Empty)
.ToListAsync())
.Count.Should().Be(0);
}

private class TestDbContext : MongoDbContext
{
public TestDbContext(MongoOptions mongoOptions)
: base(mongoOptions)
{
}

protected override void OnConfiguring(IMongoDatabaseBuilder mongoDatabaseBuilder)
{
}
}
}
9 changes: 5 additions & 4 deletions src/Session/Internal/MongoSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace MongoDB.Extensions.Session;

internal sealed class MongoSession : ISession
{
private readonly IClientSessionHandle _session;
private bool _disposed;

private static TransactionOptions TransactionOptions { get; } = new(
Expand All @@ -18,14 +17,16 @@ internal sealed class MongoSession : ISession

public MongoSession(IClientSessionHandle clientSession)
{
_session = clientSession;
Session = clientSession;
}

public IClientSessionHandle Session { get; }

public Task<T> WithTransactionAsync<T>(
Func<ISession, CancellationToken, Task<T>> action,
CancellationToken cancellationToken)
{
return _session.WithTransactionAsync<T>(
return Session.WithTransactionAsync<T>(
(_, ct) => action(this, ct),
TransactionOptions,
cancellationToken);
Expand All @@ -37,7 +38,7 @@ private void Dispose(bool disposing)
{
if (disposing)
{
_session.Dispose();
Session.Dispose();
}

_disposed = true;
Expand Down
9 changes: 5 additions & 4 deletions src/Session/Internal/MongoTransactionSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ namespace MongoDB.Extensions.Session;

internal class MongoTransactionSession : ITransactionSession
{
private readonly IClientSessionHandle _session;
private readonly CancellationToken _cancellationToken;
private bool _disposed;

public MongoTransactionSession(
IClientSessionHandle clientSession,
CancellationToken cancellationToken)
{
_session = clientSession;
Session = clientSession;
_cancellationToken = cancellationToken;
}

public IClientSessionHandle Session { get; }

public async Task CommitAsync()
{
await _session.CommitTransactionAsync(_cancellationToken);
await Session.CommitTransactionAsync(_cancellationToken);
}

protected virtual void Dispose(bool disposing)
Expand All @@ -30,7 +31,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_session.Dispose();
Session.Dispose();
}

_disposed = true;
Expand Down
31 changes: 31 additions & 0 deletions src/Session/TransactionSessionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using MongoDB.Driver;

namespace MongoDB.Extensions.Session;

public static class TransactionSessionExtensions
{
public static IClientSessionHandle GetSessionHandle(
this ITransactionSession session)
{
if (session is MongoTransactionSession mongoTransactionSession)
{
return mongoTransactionSession.Session;
}

throw new InvalidOperationException(
$"Unknown session type {session.GetType().Name}");
}

public static IClientSessionHandle GetSessionHandle(
this ISession session)
{
if (session is MongoSession mongoSession)
{
return mongoSession.Session;
}

throw new InvalidOperationException(
$"Unknown session type {session.GetType().Name}");
}
}

0 comments on commit 361f56b

Please sign in to comment.