-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from madelson/fix-azure-connection-leak
Fix azure connection leak
- Loading branch information
Showing
8 changed files
with
188 additions
and
16 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
DistributedLock.Tests/AbstractTestCases/ConnectionStringStrategyTestCases.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using Medallion.Threading.Tests.Sql; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.SqlClient; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Medallion.Threading.Tests | ||
{ | ||
[TestClass] | ||
public abstract class ConnectionStringStrategyTestCases<TEngineFactory, TConnectionManagementProvider> : TestBase | ||
where TEngineFactory : ITestingSqlDistributedLockEngineFactory, new() | ||
where TConnectionManagementProvider : ConnectionStringProvider, new() | ||
{ | ||
/// <summary> | ||
/// Tests that internally-owned connections are properly cleaned up by disposing the lock handle | ||
/// </summary> | ||
[TestMethod] | ||
public void TestConnectionDoesNotLeak() | ||
{ | ||
var applicationName = nameof(TestConnectionDoesNotLeak) + Guid.NewGuid(); | ||
var connectionString = new SqlConnectionStringBuilder(ConnectionStringProvider.ConnectionString) | ||
{ | ||
ApplicationName = applicationName, | ||
} | ||
.ConnectionString; | ||
|
||
using (ConnectionStringProvider.UseConnectionString(connectionString)) | ||
using (var engine = this.CreateEngine()) | ||
{ | ||
var @lock = engine.CreateLock(nameof(TestConnectionDoesNotLeak)); | ||
|
||
for (var i = 0; i < 30; ++i) | ||
{ | ||
using (@lock.Acquire()) | ||
{ | ||
CountActiveSessions().ShouldEqual(1, this.GetType().Name); | ||
} | ||
// still alive due to pooling | ||
CountActiveSessions().ShouldEqual(1, this.GetType().Name); | ||
} | ||
} | ||
|
||
using (var connection = new SqlConnection(connectionString)) | ||
{ | ||
SqlConnection.ClearPool(connection); | ||
// checking immediately seems flaky; likely clear pool finishing | ||
// doesn't guarantee that SQL will immediately reflect the clear | ||
var maxWaitForPoolsToClear = TimeSpan.FromSeconds(5); | ||
var stopwatch = Stopwatch.StartNew(); | ||
do | ||
{ | ||
var activeCount = CountActiveSessions(); | ||
if (activeCount == 0) { return; } | ||
Thread.Sleep(25); | ||
} | ||
while (stopwatch.Elapsed < maxWaitForPoolsToClear); | ||
} | ||
|
||
int CountActiveSessions() | ||
{ | ||
using (var connection = new SqlConnection(ConnectionStringProvider.ConnectionString)) | ||
{ | ||
connection.Open(); | ||
using (var command = connection.CreateCommand()) | ||
{ | ||
command.CommandText = $@"SELECT COUNT(*) FROM sys.dm_exec_sessions WHERE program_name = '{applicationName}'"; | ||
return (int)command.ExecuteScalar(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private TestingDistributedLockEngine CreateEngine() => new TEngineFactory().Create<TConnectionManagementProvider>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="MedallionCollections" version="1.1.0" targetFramework="net46" /> | ||
<package id="MedallionShell" version="1.2.1" targetFramework="net45" /> | ||
<package id="System.ValueTuple" version="4.4.0" targetFramework="net46" /> | ||
</packages> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters