From 6eab353e0e087d2a413675db3df3949bf271a2c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E4=BA=91=E9=87=91YunjinXu?= Date: Fri, 6 Dec 2024 11:07:11 +0800 Subject: [PATCH] fix(BranchBarrier): fix QueryPrepared method not return real insert exception --- src/DtmCommon/Barrier/BranchBarrier.cs | 28 ++++++++++++------------ tests/Dtmcli.Tests/BranchBarrierTests.cs | 26 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/DtmCommon/Barrier/BranchBarrier.cs b/src/DtmCommon/Barrier/BranchBarrier.cs index b0bfced..ceedf04 100644 --- a/src/DtmCommon/Barrier/BranchBarrier.cs +++ b/src/DtmCommon/Barrier/BranchBarrier.cs @@ -179,21 +179,21 @@ public async Task Call(DbConnection db, Func busiCall, TransactionScopeOpt public async Task 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; diff --git a/tests/Dtmcli.Tests/BranchBarrierTests.cs b/tests/Dtmcli.Tests/BranchBarrierTests.cs index d96c234..d49661b 100644 --- a/tests/Dtmcli.Tests/BranchBarrierTests.cs +++ b/tests/Dtmcli.Tests/BranchBarrierTests.cs @@ -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()), 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(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); + } } } \ No newline at end of file