Skip to content

Commit 3ff781f

Browse files
committed
- database availability
1 parent e777ba9 commit 3ff781f

File tree

4 files changed

+122
-4
lines changed

4 files changed

+122
-4
lines changed

Shuttle.Core.Data.Tests/DatabaseContextFactoryFixture.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using NUnit.Framework;
1+
using System;
2+
using System.Data;
3+
using System.Threading;
4+
using Moq;
5+
using NUnit.Framework;
26

37
namespace Shuttle.Core.Data.Tests
48
{
@@ -37,5 +41,26 @@ public void Should_be_able_to_get_an_existing_database_context()
3741
Assert.AreSame(existingContext.Connection, context.Connection);
3842
}
3943
}
44+
45+
[Test]
46+
public void Should_be_able_to_check_connection_availability()
47+
{
48+
var databaseContextFactory = new Mock<IDatabaseContextFactory>();
49+
50+
Assert.That(databaseContextFactory.Object.IsAvailable(new CancellationToken(), 0, 0), Is.True);
51+
Assert.That(databaseContextFactory.Object.IsAvailable("name", new CancellationToken(), 0, 0), Is.True);
52+
Assert.That(databaseContextFactory.Object.IsAvailable("provider-name", new Mock<IDbConnection>().Object, new CancellationToken(), 0, 0), Is.True);
53+
Assert.That(databaseContextFactory.Object.IsAvailable("provider-name", "connection-string", new CancellationToken(), 0, 0), Is.True);
54+
55+
databaseContextFactory.Setup(m => m.Create()).Throws(new Exception());
56+
databaseContextFactory.Setup(m => m.Create(It.IsAny<string>())).Throws(new Exception());
57+
databaseContextFactory.Setup(m => m.Create(It.IsAny<string>(), It.IsAny<IDbConnection>())).Throws(new Exception());
58+
databaseContextFactory.Setup(m => m.Create(It.IsAny<string>(), It.IsAny<string>())).Throws(new Exception());
59+
60+
Assert.That(databaseContextFactory.Object.IsAvailable(new CancellationToken(), 0, 0), Is.False);
61+
Assert.That(databaseContextFactory.Object.IsAvailable("name", new CancellationToken(), 0, 0), Is.False);
62+
Assert.That(databaseContextFactory.Object.IsAvailable("provider-name", new Mock<IDbConnection>().Object, new CancellationToken(), 0, 0), Is.False);
63+
Assert.That(databaseContextFactory.Object.IsAvailable("provider-name", "connection-string", new CancellationToken(), 0, 0), Is.False);
64+
}
4065
}
4166
}

Shuttle.Core.Data/.package/package.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<package>
44
<metadata>
55
<id>Shuttle.Core.Data</id>
6-
<version>12.0.2</version>
6+
<version>12.1.0</version>
77
<authors>Eben Roux</authors>
88
<owners>Eben Roux</owners>
99
<license type="expression">BSD-3-Clause</license>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Data;
3+
using System.Threading;
4+
using Shuttle.Core.Contract;
5+
6+
namespace Shuttle.Core.Data
7+
{
8+
public static class DatabaseContextFactoryExtensions
9+
{
10+
public static bool IsAvailable(this IDatabaseContextFactory databaseContextFactory,
11+
CancellationToken cancellationToken, int retries = 4, int secondsBetweenRetries = 15)
12+
{
13+
Guard.AgainstNull(databaseContextFactory, nameof(databaseContextFactory));
14+
15+
return IsAvailable(() =>
16+
{
17+
using (databaseContextFactory.Create())
18+
{
19+
}
20+
}, cancellationToken, retries, secondsBetweenRetries);
21+
}
22+
23+
public static bool IsAvailable(this IDatabaseContextFactory databaseContextFactory, string name,
24+
CancellationToken cancellationToken, int retries = 4, int secondsBetweenRetries = 15)
25+
{
26+
Guard.AgainstNull(databaseContextFactory, nameof(databaseContextFactory));
27+
28+
return IsAvailable(() =>
29+
{
30+
using (databaseContextFactory.Create(name))
31+
{
32+
}
33+
}, cancellationToken, retries, secondsBetweenRetries);
34+
}
35+
36+
public static bool IsAvailable(this IDatabaseContextFactory databaseContextFactory, string providerName, IDbConnection dbConnection,
37+
CancellationToken cancellationToken, int retries = 4, int secondsBetweenRetries = 15)
38+
{
39+
Guard.AgainstNull(databaseContextFactory, nameof(databaseContextFactory));
40+
41+
return IsAvailable(() =>
42+
{
43+
using (databaseContextFactory.Create(providerName, dbConnection))
44+
{
45+
}
46+
}, cancellationToken, retries, secondsBetweenRetries);
47+
}
48+
49+
public static bool IsAvailable(this IDatabaseContextFactory databaseContextFactory, string providerName, string connectionString,
50+
CancellationToken cancellationToken, int retries = 4, int secondsBetweenRetries = 15)
51+
{
52+
Guard.AgainstNull(databaseContextFactory, nameof(databaseContextFactory));
53+
54+
return IsAvailable(() =>
55+
{
56+
using (databaseContextFactory.Create(providerName, connectionString))
57+
{
58+
}
59+
}, cancellationToken, retries, secondsBetweenRetries);
60+
}
61+
62+
private static bool IsAvailable(Action action, CancellationToken cancellationToken, int retries = 4, int secondsBetweenRetries = 15)
63+
{
64+
var attempt = 0;
65+
66+
do
67+
{
68+
try
69+
{
70+
action.Invoke();
71+
72+
break;
73+
}
74+
catch
75+
{
76+
attempt++;
77+
78+
if (attempt < retries)
79+
{
80+
var wait = DateTime.Now.AddSeconds(secondsBetweenRetries);
81+
82+
while (!cancellationToken.IsCancellationRequested && DateTime.Now < wait)
83+
{
84+
Thread.Sleep(250);
85+
}
86+
}
87+
}
88+
} while (attempt < retries);
89+
90+
return attempt <= retries;
91+
}
92+
}
93+
}

Shuttle.Core.Data/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
[assembly: AssemblyTitle(".NET Standard")]
1414
#endif
1515

16-
[assembly: AssemblyVersion("12.0.2.0")]
16+
[assembly: AssemblyVersion("12.1.0.0")]
1717
[assembly: AssemblyCopyright("Copyright (c) 2022, Eben Roux")]
1818
[assembly: AssemblyProduct("Shuttle.Core.Data")]
1919
[assembly: AssemblyCompany("Eben Roux")]
2020
[assembly: AssemblyConfiguration("Release")]
21-
[assembly: AssemblyInformationalVersion("12.0.2")]
21+
[assembly: AssemblyInformationalVersion("12.1.0")]
2222
[assembly: ComVisible(false)]

0 commit comments

Comments
 (0)