Skip to content

Commit

Permalink
- database availability
Browse files Browse the repository at this point in the history
  • Loading branch information
eben-roux committed Jun 16, 2022
1 parent e777ba9 commit 3ff781f
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 4 deletions.
27 changes: 26 additions & 1 deletion Shuttle.Core.Data.Tests/DatabaseContextFactoryFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using NUnit.Framework;
using System;
using System.Data;
using System.Threading;
using Moq;
using NUnit.Framework;

namespace Shuttle.Core.Data.Tests
{
Expand Down Expand Up @@ -37,5 +41,26 @@ public void Should_be_able_to_get_an_existing_database_context()
Assert.AreSame(existingContext.Connection, context.Connection);
}
}

[Test]
public void Should_be_able_to_check_connection_availability()
{
var databaseContextFactory = new Mock<IDatabaseContextFactory>();

Assert.That(databaseContextFactory.Object.IsAvailable(new CancellationToken(), 0, 0), Is.True);
Assert.That(databaseContextFactory.Object.IsAvailable("name", new CancellationToken(), 0, 0), Is.True);
Assert.That(databaseContextFactory.Object.IsAvailable("provider-name", new Mock<IDbConnection>().Object, new CancellationToken(), 0, 0), Is.True);
Assert.That(databaseContextFactory.Object.IsAvailable("provider-name", "connection-string", new CancellationToken(), 0, 0), Is.True);

databaseContextFactory.Setup(m => m.Create()).Throws(new Exception());
databaseContextFactory.Setup(m => m.Create(It.IsAny<string>())).Throws(new Exception());
databaseContextFactory.Setup(m => m.Create(It.IsAny<string>(), It.IsAny<IDbConnection>())).Throws(new Exception());
databaseContextFactory.Setup(m => m.Create(It.IsAny<string>(), It.IsAny<string>())).Throws(new Exception());

Assert.That(databaseContextFactory.Object.IsAvailable(new CancellationToken(), 0, 0), Is.False);
Assert.That(databaseContextFactory.Object.IsAvailable("name", new CancellationToken(), 0, 0), Is.False);
Assert.That(databaseContextFactory.Object.IsAvailable("provider-name", new Mock<IDbConnection>().Object, new CancellationToken(), 0, 0), Is.False);
Assert.That(databaseContextFactory.Object.IsAvailable("provider-name", "connection-string", new CancellationToken(), 0, 0), Is.False);
}
}
}
2 changes: 1 addition & 1 deletion Shuttle.Core.Data/.package/package.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<package>
<metadata>
<id>Shuttle.Core.Data</id>
<version>12.0.2</version>
<version>12.1.0</version>
<authors>Eben Roux</authors>
<owners>Eben Roux</owners>
<license type="expression">BSD-3-Clause</license>
Expand Down
93 changes: 93 additions & 0 deletions Shuttle.Core.Data/DatabaseContextFactoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Data;
using System.Threading;
using Shuttle.Core.Contract;

namespace Shuttle.Core.Data
{
public static class DatabaseContextFactoryExtensions
{
public static bool IsAvailable(this IDatabaseContextFactory databaseContextFactory,
CancellationToken cancellationToken, int retries = 4, int secondsBetweenRetries = 15)
{
Guard.AgainstNull(databaseContextFactory, nameof(databaseContextFactory));

return IsAvailable(() =>
{
using (databaseContextFactory.Create())
{
}
}, cancellationToken, retries, secondsBetweenRetries);
}

public static bool IsAvailable(this IDatabaseContextFactory databaseContextFactory, string name,
CancellationToken cancellationToken, int retries = 4, int secondsBetweenRetries = 15)
{
Guard.AgainstNull(databaseContextFactory, nameof(databaseContextFactory));

return IsAvailable(() =>
{
using (databaseContextFactory.Create(name))
{
}
}, cancellationToken, retries, secondsBetweenRetries);
}

public static bool IsAvailable(this IDatabaseContextFactory databaseContextFactory, string providerName, IDbConnection dbConnection,
CancellationToken cancellationToken, int retries = 4, int secondsBetweenRetries = 15)
{
Guard.AgainstNull(databaseContextFactory, nameof(databaseContextFactory));

return IsAvailable(() =>
{
using (databaseContextFactory.Create(providerName, dbConnection))
{
}
}, cancellationToken, retries, secondsBetweenRetries);
}

public static bool IsAvailable(this IDatabaseContextFactory databaseContextFactory, string providerName, string connectionString,
CancellationToken cancellationToken, int retries = 4, int secondsBetweenRetries = 15)
{
Guard.AgainstNull(databaseContextFactory, nameof(databaseContextFactory));

return IsAvailable(() =>
{
using (databaseContextFactory.Create(providerName, connectionString))
{
}
}, cancellationToken, retries, secondsBetweenRetries);
}

private static bool IsAvailable(Action action, CancellationToken cancellationToken, int retries = 4, int secondsBetweenRetries = 15)
{
var attempt = 0;

do
{
try
{
action.Invoke();

break;
}
catch
{
attempt++;

if (attempt < retries)
{
var wait = DateTime.Now.AddSeconds(secondsBetweenRetries);

while (!cancellationToken.IsCancellationRequested && DateTime.Now < wait)
{
Thread.Sleep(250);
}
}
}
} while (attempt < retries);

return attempt <= retries;
}
}
}
4 changes: 2 additions & 2 deletions Shuttle.Core.Data/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
[assembly: AssemblyTitle(".NET Standard")]
#endif

[assembly: AssemblyVersion("12.0.2.0")]
[assembly: AssemblyVersion("12.1.0.0")]
[assembly: AssemblyCopyright("Copyright (c) 2022, Eben Roux")]
[assembly: AssemblyProduct("Shuttle.Core.Data")]
[assembly: AssemblyCompany("Eben Roux")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyInformationalVersion("12.0.2")]
[assembly: AssemblyInformationalVersion("12.1.0")]
[assembly: ComVisible(false)]

0 comments on commit 3ff781f

Please sign in to comment.