Skip to content

Commit

Permalink
test(DtmSample): add test case for MSG MSSQL DB connection error, exp…
Browse files Browse the repository at this point in the history
…ected status should be prepared
  • Loading branch information
wooln committed Dec 9, 2024
1 parent 2a1418b commit ef74a45
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
5 changes: 5 additions & 0 deletions samples/DtmSample/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
{
public class AppSettings
{
public string DtmUrl { get; set; }

public string BusiUrl { get; set; }

public string SqlBarrierConn { get; set; }


public string SqlBarrierErrorConn { get; set; }

public string MongoBarrierConn { get; set; }
}
}
40 changes: 39 additions & 1 deletion samples/DtmSample/Controllers/MsgTestController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System;
using System.Net;
using Dtmcli;
using DtmMongoBarrier;
using DtmSample.Dtos;
Expand Down Expand Up @@ -37,6 +38,9 @@ public MsgTestController(ILogger<MsgTestController> logger, IOptions<AppSettings
private MySqlConnection GetMysqlConn() => new(_settings.SqlBarrierConn);

private SqlConnection GetMssqlConn() => new(_settings.SqlBarrierConn);

private SqlConnection GetBadMssqlConn() => new(_settings.SqlBarrierErrorConn);


private MySqlConnection GetErrConn() => new("");

Expand Down Expand Up @@ -116,6 +120,40 @@ await msg.DoAndSubmitDB(_settings.BusiUrl + "/msg-mssqlqueryprepared", conn, asy
return Ok(TransResponse.BuildSucceedResponse());
}


/// <summary>
/// MSG DoAndSubmitDB (mssql). db connection error, DTM server Status should be prepared.
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost("msg-db-mssql-db-connection-error")]
public async Task<IActionResult> MsgDbMsSql_DbConnectionError(CancellationToken cancellationToken)
{
var gid = await _dtmClient.GenGid(cancellationToken);

var msg = _transFactory.NewMsg(gid)
.Add(_settings.BusiUrl + "/TransOut", new TransRequest("1", -30))
.Add(_settings.BusiUrl + "/TransIn", new TransRequest("2", 30));

try
{
using (SqlConnection conn = GetBadMssqlConn())
{
await msg.DoAndSubmitDB(_settings.BusiUrl + "/msg-mssqlqueryprepared", conn, async tx => { await Task.CompletedTask; });
}
}
catch (SqlException e)

Check warning on line 145 in samples/DtmSample/Controllers/MsgTestController.cs

View workflow job for this annotation

GitHub Actions / build on windows-latest

The variable 'e' is declared but never used

Check warning on line 145 in samples/DtmSample/Controllers/MsgTestController.cs

View workflow job for this annotation

GitHub Actions / build on windows-latest

The variable 'e' is declared but never used

Check warning on line 145 in samples/DtmSample/Controllers/MsgTestController.cs

View workflow job for this annotation

GitHub Actions / build on ubuntu-latest

The variable 'e' is declared but never used

Check warning on line 145 in samples/DtmSample/Controllers/MsgTestController.cs

View workflow job for this annotation

GitHub Actions / build on ubuntu-latest

The variable 'e' is declared but never used
{
Thread.Sleep(5 * 1000);
_logger.LogInformation("{}/admin/global-transactions/detail/{}, status should be prepared", _settings.DtmUrl, gid);
throw;
}

_logger.LogInformation("result gid is {0}", gid);
return Ok(TransResponse.BuildSucceedResponse());
}


/// <summary>
/// MSG DoAndSubmit (mongo)
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions samples/DtmSample/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"SqlDbType": "sqlserver",
"BarrierSqlTableName": "dbo.barrier",
"SqlBarrierConn": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=dtm_barrier;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
"SqlBarrierErrorConn": "Data Source=.;Initial Catalog=dtm_barrier;User ID=sa;Password=my_error_password;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
"MongoBarrierConn": "mongodb://localhost:27017"
}
}

0 comments on commit ef74a45

Please sign in to comment.