Skip to content

Commit f0fbf34

Browse files
authored
Merge pull request #260 from zhenlineo/1.5-purge-improve
Do not purge inUse connections
2 parents 8772b73 + 25dea0f commit f0fbf34

15 files changed

+1034
-319
lines changed

Neo4j.Driver/Neo4j.Driver.Tests/ConnectionPoolTests.cs

Lines changed: 591 additions & 136 deletions
Large diffs are not rendered by default.

Neo4j.Driver/Neo4j.Driver.Tests/Routing/ClusterConnectionPoolTests.cs

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void ShouldEnsureInitialRouter()
4747
var pool = new ClusterConnectionPool(connSettings, poolSettings, bufferSettings, uris, null);
4848

4949
pool.ToString().Should().Be(
50-
"[{bolt://123:456/ : _availableConnections: {[]}, _inUseConnections: {[]}}]");
50+
"[{bolt://123:456/ : _idleConnections: {[]}, _inUseConnections: {[]}}]");
5151
}
5252
}
5353

@@ -139,7 +139,7 @@ public void ShouldAddNewConnectionPoolIfDoesNotExist()
139139
var pool = new ClusterConnectionPool(mockedConnectionPool.Object, connectionPoolDict);
140140

141141
// When
142-
pool.Update(new[] {ServerUri});
142+
pool.Update(new[] {ServerUri}, new Uri[0]);
143143

144144
// Then
145145
connectionPoolDict.Count.Should().Be(1);
@@ -148,7 +148,7 @@ public void ShouldAddNewConnectionPoolIfDoesNotExist()
148148
}
149149

150150
[Fact]
151-
public void ShouldRemoveNewlyCreatedPoolnIfDisposeAlreadyCalled()
151+
public void ShouldRemoveNewlyCreatedPoolIfDisposeAlreadyCalled()
152152
{
153153
// Given
154154
var mockedConnectionPool = new Mock<IConnectionPool>();
@@ -157,7 +157,7 @@ public void ShouldRemoveNewlyCreatedPoolnIfDisposeAlreadyCalled()
157157

158158
// When
159159
pool.Dispose();
160-
var exception = Record.Exception(() => pool.Update(new[] {ServerUri}));
160+
var exception = Record.Exception(() => pool.Update(new[] {ServerUri}, new Uri[0]));
161161

162162
// Then
163163
mockedConnectionPool.Verify(x => x.Close());
@@ -173,20 +173,40 @@ public void ShouldRemoveServerPoolIfNotPresentInNewServers()
173173
var mockedConnectionPool = new Mock<IConnectionPool>();
174174
var connectionPoolDict = new ConcurrentDictionary<Uri, IConnectionPool>();
175175
connectionPoolDict.GetOrAdd(ServerUri, mockedConnectionPool.Object);
176+
mockedConnectionPool.Setup(x => x.NumberOfInUseConnections).Returns(0); // no need to explicitly config this
176177
var pool = new ClusterConnectionPool(mockedConnectionPool.Object, connectionPoolDict);
177178

178179
// When
179-
pool.Update(new Uri[0]);
180+
pool.Update(new Uri[0], new[] {ServerUri});
180181

181182
// Then
183+
mockedConnectionPool.Verify(x => x.Deactivate(), Times.Once); // first deactivate then remove
182184
connectionPoolDict.Count.Should().Be(0);
183185
}
186+
187+
[Fact]
188+
public void ShouldDeactivateServerPoolIfNotPresentInNewServersButHasInUseConnections()
189+
{
190+
// Given
191+
var mockedConnectionPool = new Mock<IConnectionPool>();
192+
var connectionPoolDict = new ConcurrentDictionary<Uri, IConnectionPool>();
193+
connectionPoolDict.GetOrAdd(ServerUri, mockedConnectionPool.Object);
194+
mockedConnectionPool.Setup(x => x.NumberOfInUseConnections).Returns(10); // non-zero number
195+
var pool = new ClusterConnectionPool(mockedConnectionPool.Object, connectionPoolDict);
196+
197+
// When
198+
pool.Update(new Uri[0], new[] {ServerUri});
199+
200+
// Then
201+
mockedConnectionPool.Verify(x => x.Deactivate(), Times.Once);
202+
connectionPoolDict.Count.Should().Be(1);
203+
}
184204
}
185205

186-
public class PurgeMethod
206+
public class AddMethod
187207
{
188208
[Fact]
189-
public void ShouldRemovedIfExist()
209+
public void ShouldActivateIfExist()
190210
{
191211
// Given
192212
var mockedConnectionPool = new Mock<IConnectionPool>();
@@ -196,24 +216,64 @@ public void ShouldRemovedIfExist()
196216
var pool = new ClusterConnectionPool(null, connectionPoolDict);
197217

198218
// When
199-
pool.Purge(ServerUri);
219+
pool.Add(new []{ServerUri});
200220

201221
// Then
202-
mockedConnectionPool.Verify(x => x.Close(), Times.Once);
203-
connectionPoolDict.Count.Should().Be(0);
204-
connectionPoolDict.ContainsKey(ServerUri).Should().BeFalse();
222+
mockedConnectionPool.Verify(x => x.Activate(), Times.Once);
223+
connectionPoolDict.Count.Should().Be(1);
224+
connectionPoolDict.ContainsKey(ServerUri).Should().BeTrue();
225+
}
226+
227+
[Fact]
228+
public void ShouldAddIfNotFound()
229+
{
230+
// Given
231+
var connectionPoolDict = new ConcurrentDictionary<Uri, IConnectionPool>();
232+
var fakePoolMock = new Mock<IConnectionPool>();
233+
234+
var pool = new ClusterConnectionPool(fakePoolMock.Object, connectionPoolDict);
235+
236+
// When
237+
pool.Add(new[] {ServerUri});
238+
239+
// Then
240+
connectionPoolDict.Count.Should().Be(1);
241+
connectionPoolDict.ContainsKey(ServerUri).Should().BeTrue();
242+
connectionPoolDict[ServerUri].Should().Be(fakePoolMock.Object);
243+
}
244+
}
245+
246+
public class DeactivateMethod
247+
{
248+
[Fact]
249+
public void ShouldDeactivateIfExist()
250+
{
251+
// Given
252+
var mockedConnectionPool = new Mock<IConnectionPool>();
253+
var connectionPoolDict = new ConcurrentDictionary<Uri, IConnectionPool>();
254+
connectionPoolDict.GetOrAdd(ServerUri, mockedConnectionPool.Object);
255+
256+
var pool = new ClusterConnectionPool(null, connectionPoolDict);
257+
258+
// When
259+
pool.Deactivate(ServerUri);
260+
261+
// Then
262+
mockedConnectionPool.Verify(x => x.Deactivate(), Times.Once);
263+
connectionPoolDict.Count.Should().Be(1);
264+
connectionPoolDict.ContainsKey(ServerUri).Should().BeTrue();
205265
}
206266

207267
[Fact]
208-
public void ShouldRemoveNothingIfNotFound()
268+
public void ShouldDeactivateNothingIfNotFound()
209269
{
210270
// Given
211271
var connectionPoolDict = new ConcurrentDictionary<Uri, IConnectionPool>();
212272

213273
var pool = new ClusterConnectionPool(null, connectionPoolDict);
214274

215275
// When
216-
pool.Purge(ServerUri);
276+
pool.Deactivate(ServerUri);
217277

218278
// Then
219279
connectionPoolDict.Count.Should().Be(0);
@@ -273,4 +333,4 @@ public void ShouldReturnCorrectCountForPresentAddress()
273333
}
274334
}
275335
}
276-
}
336+
}

Neo4j.Driver/Neo4j.Driver.Tests/Routing/LoadBalancerTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void ShouldRmoveFromLoadBalancer()
4444
var loadBalancer = new LoadBalancer(clusterPoolMock.Object, routingTableManagerMock.Object);
4545

4646
loadBalancer.OnConnectionError(uri, new ClientException());
47-
clusterPoolMock.Verify(x => x.Purge(uri), Times.Once);
47+
clusterPoolMock.Verify(x => x.Deactivate(uri), Times.Once);
4848
routingTableMock.Verify(x => x.Remove(uri), Times.Once);
4949
routingTableMock.Verify(x => x.RemoveWriter(uri), Times.Never);
5050
}
@@ -63,7 +63,7 @@ public void ShouldRemoveWriterFromRoutingTable()
6363
var loadBalancer = new LoadBalancer(clusterPoolMock.Object, routingTableManagerMock.Object);
6464

6565
loadBalancer.OnWriteError(uri);
66-
clusterPoolMock.Verify(x => x.Purge(uri), Times.Never);
66+
clusterPoolMock.Verify(x => x.Deactivate(uri), Times.Never);
6767
routingTableMock.Verify(x => x.Remove(uri), Times.Never);
6868
routingTableMock.Verify(x => x.RemoveWriter(uri), Times.Once);
6969
}
@@ -141,7 +141,7 @@ public void ShouldForgetServerWhenFailedToEstablishConn(AccessMode mode)
141141

142142
// should be removed
143143
routingTableMock.Verify(m => m.Remove(uri), Times.Once);
144-
clusterConnPoolMock.Verify(m => m.Purge(uri), Times.Once);
144+
clusterConnPoolMock.Verify(m => m.Deactivate(uri), Times.Once);
145145
}
146146

147147
[Theory]
@@ -170,7 +170,7 @@ public void ShouldThrowErrorDirectlyIfSecurityError(AccessMode mode)
170170

171171
// while the server is not removed
172172
routingTableMock.Verify(m => m.Remove(uri), Times.Never);
173-
clusterConnPoolMock.Verify(m => m.Purge(uri), Times.Never);
173+
clusterConnPoolMock.Verify(m => m.Deactivate(uri), Times.Never);
174174
}
175175

176176
[Theory]
@@ -199,7 +199,7 @@ public void ShouldThrowErrorDirectlyIfProtocolError(AccessMode mode)
199199

200200
// while the server is not removed
201201
routingTableMock.Verify(m => m.Remove(uri), Times.Never);
202-
clusterConnPoolMock.Verify(m => m.Purge(uri), Times.Never);
202+
clusterConnPoolMock.Verify(m => m.Deactivate(uri), Times.Never);
203203
}
204204

205205
[Theory]
@@ -253,4 +253,4 @@ private static IConnection NewConnectionMock(Uri uri)
253253
}
254254
}
255255
}
256-
}
256+
}

0 commit comments

Comments
 (0)