Skip to content

Commit 138a347

Browse files
kmdtkgcincuranet
authored andcommitted
Fixed IsAlive calculation in FbConnectionPoolManager.Pool.IsAlive
1 parent 1bd5286 commit 138a347

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ obj
77
packages
88
DDEX/installer/out/
99
DDEX/installer/in/
10-
Provider/deploy/out/
10+
Provider/deploy/out/
11+
.idea

Provider/src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbConnectionPoolManager.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void CleanupPool()
128128
var available = _available.ToArray();
129129
if (available.Count() <= _connectionString.MinPoolSize)
130130
return;
131-
var keep = available.Where(x => IsAlive(_connectionString.ConnectionLifeTime, x.Created, now)).ToArray();
131+
var keep = available.Where(x => ConnectionPoolLifetimeHelper.IsAlive(_connectionString.ConnectionLifeTime, x.Created, now)).ToArray();
132132
var keepCount = keep.Count();
133133
if (keepCount < _connectionString.MinPoolSize)
134134
{
@@ -159,13 +159,6 @@ static FbConnectionInternal CreateNewConnection(FbConnectionString connectionStr
159159
return result;
160160
}
161161

162-
static bool IsAlive(long connectionLifeTime, long created, long now)
163-
{
164-
if (connectionLifeTime == 0)
165-
return true;
166-
return (now - created) > (connectionLifeTime * 1000);
167-
}
168-
169162
static long GetTicks()
170163
{
171164
var ticks = Environment.TickCount;
@@ -267,4 +260,14 @@ void CheckDisposed()
267260
throw new ObjectDisposedException(nameof(FbConnectionPoolManager));
268261
}
269262
}
263+
264+
internal static class ConnectionPoolLifetimeHelper
265+
{
266+
internal static bool IsAlive(long connectionLifeTime, long created, long now)
267+
{
268+
if (connectionLifeTime == 0)
269+
return true;
270+
return (now - created) < (connectionLifeTime * 1000);
271+
}
272+
}
270273
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using FirebirdSql.Data.FirebirdClient;
3+
using NUnit.Framework;
4+
5+
namespace FirebirdSql.Data.UnitTests
6+
{
7+
[TestFixture()]
8+
public class ConnectionPoolLifetimeHelperTest
9+
{
10+
[Test]
11+
public void IsAliveTrueIfLifetimeNotExceed()
12+
{
13+
var timeAgo = Environment.TickCount - (10 * 1000); //10 seconds
14+
var now = Environment.TickCount;
15+
var isAlive = ConnectionPoolLifetimeHelper.IsAlive(20, timeAgo, now);
16+
Assert.IsTrue(isAlive);
17+
}
18+
19+
[Test]
20+
public void IsAliveFalseIfLifetimeIsExceed()
21+
{
22+
var timeAgo = Environment.TickCount - (30 * 1000); //30 seconds
23+
var now = Environment.TickCount;
24+
var isAlive = ConnectionPoolLifetimeHelper.IsAlive(20, timeAgo, now);
25+
Assert.IsFalse(isAlive);
26+
}
27+
}
28+
}

Provider/src/FirebirdSql.Data.UnitTests/FirebirdSql.Data.UnitTests_NET45.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<Compile Include="FbBooleanSupportTests.cs" />
5858
<Compile Include="FbConnectionStringTests.cs" />
5959
<Compile Include="FbConnectionStringBuilderTests.cs" />
60+
<Compile Include="FbConnectionPoolManagerTest.cs" />
6061
<Compile Include="FbExceptionTests.cs" />
6162
<Compile Include="FbRemoteEventTests.cs" />
6263
<Compile Include="FbTestFixtureAttribute.cs" />

0 commit comments

Comments
 (0)