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

fix(BranchBarrier): fix QueryPrepared method not return real insert exception #82

Merged
Merged
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
28 changes: 14 additions & 14 deletions src/DtmCommon/Barrier/BranchBarrier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,21 @@ public async Task Call(DbConnection db, Func<Task> busiCall, TransactionScopeOpt

public async Task<string> QueryPrepared(DbConnection db)
{
try
if (db == null) return "db is null";

(int _, Exception insertException) = await DbUtils.InsertBarrier(
db,
this.TransType,
this.Gid,
Constant.Barrier.MSG_BRANCHID,
Constant.TYPE_MSG,
Constant.Barrier.MSG_BARRIER_ID,
Constant.Barrier.MSG_BARRIER_REASON);

if (insertException != null)
{
var tmp = await DbUtils.InsertBarrier(
db,
this.TransType,
this.Gid,
Constant.Barrier.MSG_BRANCHID,
Constant.TYPE_MSG,
Constant.Barrier.MSG_BARRIER_ID,
Constant.Barrier.MSG_BARRIER_REASON);
}
catch (Exception ex)
{
Logger?.LogWarning(ex, "Insert Barrier error, gid={gid}", this.Gid);
return ex.Message;
Logger?.LogWarning(insertException, "Insert Barrier error, gid={gid}", this.Gid);
return insertException.Message;
}

var reason = string.Empty;
Expand Down
26 changes: 26 additions & 0 deletions tests/Dtmcli.Tests/BranchBarrierTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,31 @@ public async void Call_Should_Throw_Duplicated_Exception_When_QueryPrepared_At_F
Assert.Equal(DtmCommon.Constant.ResultDuplicated, ex.Message);
mockBusiCall.Verify(x => x.Invoke(It.IsAny<System.Data.Common.DbTransaction>()), Times.Never);
}

[Fact]
public async void QueryPrepared_InsertException()
{
var branchBarrier = _factory.CreateBranchBarrier("msg", "gid", "bid", "msg");

var connQ = GetDbConnection();
connQ.Mocks.When(cmd => cmd.CommandText.Contains("insert", StringComparison.Ordinal))
.ThrowsException(new Exception("DB account no insert permission"));
connQ.Mocks.When(cmd => cmd.CommandText.Contains("select", StringComparison.OrdinalIgnoreCase))
.ReturnsScalar<string>(cmd => null);

var qRes = await branchBarrier.QueryPrepared(connQ);
Assert.NotEqual("Object reference not set to an instance of an object.", qRes);
Assert.Equal("DB account no insert permission", qRes);
}

[Fact]
public async void QueryPrepared_DbIsNull()
{
var branchBarrier = _factory.CreateBranchBarrier("msg", "gid", "bid", "msg");

var qRes = await branchBarrier.QueryPrepared(db: null);
Assert.NotEqual("Object reference not set to an instance of an object.", qRes);
Assert.Equal("db is null", qRes);
}
}
}
Loading