Skip to content

Commit e51a9e7

Browse files
committed
Test CommandBehavior.CloseConnection.
This tests a variation of the failing code that reproduces #620.
1 parent 66ed2c7 commit e51a9e7

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

tests/SideBySide/QueryTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,30 @@ public void ReservedWordsSchema()
11581158
}
11591159
#endif
11601160

1161+
[Fact]
1162+
public void CommandBehaviorCloseConnection()
1163+
{
1164+
using (var connection = new MySqlConnection(AppConfig.ConnectionString))
1165+
{
1166+
Assert.Equal(ConnectionState.Closed, connection.State);
1167+
connection.Open();
1168+
Assert.Equal(ConnectionState.Open, connection.State);
1169+
1170+
using (var cmd = new MySqlCommand("SELECT 1;", connection))
1171+
using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
1172+
{
1173+
Assert.True(reader.Read());
1174+
Assert.Equal(1, reader.GetInt32(0));
1175+
Assert.False(reader.Read());
1176+
}
1177+
1178+
Assert.Equal(ConnectionState.Closed, connection.State);
1179+
connection.Open();
1180+
Assert.Equal(ConnectionState.Open, connection.State);
1181+
connection.Close();
1182+
}
1183+
}
1184+
11611185
class BoolTest
11621186
{
11631187
public int Id { get; set; }

tests/SideBySide/TransactionScopeTests.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data;
34
using System.Linq;
5+
using System.Threading;
46
using System.Threading.Tasks;
57
using System.Transactions;
68
using Dapper;
@@ -636,6 +638,40 @@ public void ReusingConnectionInOneTransactionReusesPhysicalConnection(string con
636638
}
637639
}
638640

641+
[Theory]
642+
[MemberData(nameof(ConnectionStrings))]
643+
public async Task CommandBehaviorCloseConnection(string connectionString)
644+
{
645+
using (var connection = new MySqlConnection(AppConfig.ConnectionString + ";" + connectionString))
646+
using (new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
647+
{
648+
using (var command1 = new MySqlCommand("SELECT 1"))
649+
using (var command2 = new MySqlCommand("SELECT 2"))
650+
{
651+
command1.Connection = connection;
652+
command2.Connection = connection;
653+
654+
await connection.OpenAsync();
655+
using (var reader = await command1.ExecuteReaderAsync(CommandBehavior.CloseConnection, CancellationToken.None))
656+
{
657+
Assert.True(await reader.ReadAsync());
658+
Assert.Equal(1, reader.GetInt32(0));
659+
Assert.False(await reader.ReadAsync());
660+
}
661+
662+
Assert.Equal(ConnectionState.Closed, connection.State);
663+
664+
await connection.OpenAsync();
665+
using (var reader = await command2.ExecuteReaderAsync(CommandBehavior.CloseConnection, CancellationToken.None))
666+
{
667+
Assert.True(await reader.ReadAsync());
668+
Assert.Equal(2, reader.GetInt32(0));
669+
Assert.False(await reader.ReadAsync());
670+
}
671+
}
672+
}
673+
}
674+
639675
[SkippableFact(Baseline = "Multiple simultaneous connections or connections with different connection strings inside the same transaction are not currently supported.")]
640676
public void CommitTwoTransactions()
641677
{
@@ -746,7 +782,7 @@ void UseTransaction()
746782
{
747783
// This TransactionScope may be overly configured, but let's stick with the one I am actually using
748784
using (var transactionScope = new TransactionScope(TransactionScopeOption.Required,
749-
new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted },
785+
new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted },
750786
TransactionScopeAsyncFlowOption.Enabled))
751787
{
752788
using (var connection = new MySqlConnection(connectionString))

0 commit comments

Comments
 (0)