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

Ensure Rollback #227

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/AdoNetCore.AseClient/AseTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,21 @@ public override void Commit()
/// </summary>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

if (_isDisposed)
{
return;
}

Rollback();
if (disposing)
{
_connection?.Dispose();
}

Rollback();
_isDisposed = true;

base.Dispose(disposing);

}

internal bool IsDisposed => _isDisposed;
Expand All @@ -165,7 +170,7 @@ public override void Rollback()
throw new ObjectDisposedException(nameof(AseTransaction));
}

if (_complete)
if (_complete || _connection.State == ConnectionState.Closed || _connection.State == ConnectionState.Broken)
{
return;
}
Expand Down
75 changes: 72 additions & 3 deletions test/AdoNetCore.AseClient.Tests/Unit/AseTransactionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using System;
using System.Data;
using Moq;
using NUnit.Framework;

Expand Down Expand Up @@ -124,8 +125,8 @@ public void ExplicitRollback_WithValidTransaction_InteractsWithTheDbCommandCorre
var t = new AseTransaction(mockConnection.Object, isolationLevel);
t.Begin();
return t;
});

});
mockConnection.Setup(x => x.State).Returns(() => { return ConnectionState.Open; });
mockConnection
.SetupSequence(x => x.CreateCommand())
.Returns(mockCommandIsolationLevel.Object)
Expand Down Expand Up @@ -193,6 +194,7 @@ public void ImplicitRollback_WithValidTransaction_InteractsWithTheDbCommandCorre
return t;
});

mockConnection.Setup(x => x.State).Returns(() => { return ConnectionState.Open; });
mockConnection
.SetupSequence(x => x.CreateCommand())
.Returns(mockCommandIsolationLevel.Object)
Expand Down Expand Up @@ -222,16 +224,73 @@ public void ImplicitRollback_WithValidTransaction_InteractsWithTheDbCommandCorre
mockCommandRollbackTransaction.Verify();
}

[Test]
public void ImplicitRollback_WithBrokenConnection_DoesNotThrowExceptionDuringDispose()
{
// Arrange
var mockConnection = new Mock<IDbConnection>();
var isolationLevel = IsolationLevel.Serializable;

var mockCommandIsolationLevel = new Mock<IDbCommand>();
var mockCommandBeginTransaction = new Mock<IDbCommand>();
var mockCommandRollbackTransaction = new Mock<IDbCommand>();

mockCommandIsolationLevel
.SetupAllProperties()
.Setup(x => x.ExecuteNonQuery())
.Returns(0);

mockCommandBeginTransaction
.SetupAllProperties()
.Setup(x => x.ExecuteNonQuery())
.Returns(0);

mockCommandRollbackTransaction
.SetupAllProperties()
.Setup(x => x.ExecuteNonQuery())
.Throws( new InvalidOperationException("Cannot execute on a connection which is not open"));


mockConnection.Setup(x => x.State).Returns(() => { return ConnectionState.Broken; });
mockConnection
.Setup(x => x.BeginTransaction(isolationLevel))
.Returns(() =>
{
// Simulate what AseConnection.BeginTransaction() does.
var t = new AseTransaction(mockConnection.Object, isolationLevel);
t.Begin();
return t;
});

mockConnection
.SetupSequence(x => x.CreateCommand())
.Returns(mockCommandIsolationLevel.Object)
.Returns(mockCommandBeginTransaction.Object)
.Returns(mockCommandRollbackTransaction.Object);


// Act
var connection = mockConnection.Object;
var transaction = connection.BeginTransaction(isolationLevel);

Assert.DoesNotThrow(() => { transaction.Dispose(); });
// Implicit rollback
}



[Test]
public void RepeatedDisposal_DoesNotThrow()
{
// Arrange
var mockConnection = new Mock<IDbConnection>();

var isolationLevel = IsolationLevel.Serializable;

var mockCommandIsolationLevel = new Mock<IDbCommand>();
var mockCommandBeginTransaction = new Mock<IDbCommand>();
var mockCommandRollbackTransaction = new Mock<IDbCommand>();


mockCommandIsolationLevel
.SetupAllProperties()
Expand Down Expand Up @@ -271,6 +330,16 @@ public void RepeatedDisposal_DoesNotThrow()

transaction.Dispose(); // Implicit rollback
transaction.Dispose(); // Should do nothing

mockConnection.Verify(t => t.Dispose(), Times.Once, "we called multiple time the dispose on the master");

//mockAseTransaction.Object.
//we tried to make this work but cannot work it as long as the class is sealed and it's sealed for performance constraint.
//if you want to verify it still, use var mockAseTransaction = new Mock<AseTransaction>(MockBehavior.Loose,mockConnection.Object, isolationLevel) { CallBase = true }
//mockAseTransaction.Verify(t => t.Rollback(), Times.Once, "we should have only called once the rollback");



}
}
}