Skip to content

Commit 37155c1

Browse files
authored
Merge pull request #248 from zhenlineo/1.5-examples
Add example code for 1.5 driver documentation
2 parents 88d249f + 53a8367 commit 37155c1

File tree

5 files changed

+169
-29
lines changed

5 files changed

+169
-29
lines changed

Neo4j.Driver/Neo4j.Driver.IntegrationTests/Examples.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,70 @@ public void TestBasicAuthExample()
8585
}
8686
}
8787

88+
public class ConfigConnectionPoolExample : BaseExample
89+
{
90+
public ConfigConnectionPoolExample(ITestOutputHelper output, StandAloneIntegrationTestFixture fixture)
91+
: base(output, fixture)
92+
{
93+
}
94+
95+
// tag::config-connection-pool[]
96+
public IDriver CreateDriverWithCustomizedConnectionPool(string uri, string user, string password)
97+
{
98+
return GraphDatabase.Driver(uri, AuthTokens.Basic(user, password),
99+
new Config
100+
{
101+
MaxConnectionLifetime = TimeSpan.FromMinutes(30),
102+
MaxConnectionPoolSize = 50,
103+
ConnectionAcquisitionTimeout = TimeSpan.FromMinutes(2)
104+
});
105+
}
106+
// end::config-connection-pool[]
107+
108+
[RequireServerFact]
109+
public void TestConfigConnectionPoolExample()
110+
{
111+
// Given
112+
using (var driver = CreateDriverWithCustomizedConnectionPool(Uri, User, Password))
113+
using (var session = driver.Session())
114+
{
115+
// When & Then
116+
session.Run("RETURN 1").Single()[0].As<int>().Should().Be(1);
117+
}
118+
}
119+
}
120+
121+
public class ConfigLoadBalancingStrategyExample : BaseExample
122+
{
123+
public ConfigLoadBalancingStrategyExample(ITestOutputHelper output, StandAloneIntegrationTestFixture fixture)
124+
: base(output, fixture)
125+
{
126+
}
127+
128+
// tag::config-load-balancing-strategy[]
129+
public IDriver CreateDriverWithCustomizedLoadBalancingStrategy(string uri, string user, string password)
130+
{
131+
return GraphDatabase.Driver(uri, AuthTokens.Basic(user, password),
132+
new Config
133+
{
134+
LoadBalancingStrategy = LoadBalancingStrategy.LeastConnected
135+
});
136+
}
137+
// end::config-load-balancing-strategy[]
138+
139+
[RequireServerFact]
140+
public void TestConfigLoadBalancingStrategyExample()
141+
{
142+
// Given
143+
using (var driver = CreateDriverWithCustomizedLoadBalancingStrategy(Uri, User, Password))
144+
using (var session = driver.Session())
145+
{
146+
// When & Then
147+
session.Run("RETURN 1").Single()[0].As<int>().Should().Be(1);
148+
}
149+
}
150+
}
151+
88152
public class ConfigConnectionTimeoutExample : BaseExample
89153
{
90154
public ConfigConnectionTimeoutExample(ITestOutputHelper output, StandAloneIntegrationTestFixture fixture)

Neo4j.Driver/Neo4j.Driver.Tests/ConfigTests.cs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,25 @@ public void DefaultConfigShouldGiveCorrectValueBack()
3232
config.EncryptionLevel.Should().Be(EncryptionLevel.Encrypted);
3333
config.TrustStrategy.Should().Be(TrustStrategy.TrustAllCertificates);
3434
config.Logger.Should().BeOfType<DebugLogger>();
35-
config.MaxIdleConnectionPoolSize.Should().Be(10);
35+
config.MaxIdleConnectionPoolSize.Should().Be(500);
3636
config.LoadBalancingStrategy.Should().Be(LoadBalancingStrategy.LeastConnected);
3737
}
38+
39+
[Fact]
40+
public void ShouldUseMaxConnectionValueIfMaxIdleValueIsNotSpecified()
41+
{
42+
var config = new Config {MaxConnectionPoolSize = 50};
43+
config.MaxConnectionPoolSize.Should().Be(50);
44+
config.MaxIdleConnectionPoolSize.Should().Be(50);
45+
}
46+
47+
[Fact]
48+
public void ShouldSetMaxIdleValueWhenSetSeperately()
49+
{
50+
var config = new Config {MaxIdleConnectionPoolSize = 20, MaxConnectionPoolSize = 50};
51+
config.MaxConnectionPoolSize.Should().Be(50);
52+
config.MaxIdleConnectionPoolSize.Should().Be(20);
53+
}
3854
}
3955

4056
public class ConfigBuilderTests
@@ -47,7 +63,23 @@ public void ShouldUseDefaultValueIfNotSpecified()
4763
config.EncryptionLevel.Should().Be(EncryptionLevel.Encrypted);
4864
config.TrustStrategy.Should().Be(TrustStrategy.TrustAllCertificates);
4965
config.Logger.Should().BeOfType<DebugLogger>();
50-
config.MaxIdleConnectionPoolSize.Should().Be(10);
66+
config.MaxIdleConnectionPoolSize.Should().Be(500);
67+
}
68+
69+
[Fact]
70+
public void ShouldUseMaxConnectionValueIfMaxIdleValueIsNotSpecified()
71+
{
72+
var config = Config.Builder.WithMaxConnectionPoolSize(50).ToConfig();
73+
config.MaxConnectionPoolSize.Should().Be(50);
74+
config.MaxIdleConnectionPoolSize.Should().Be(50);
75+
}
76+
77+
[Fact]
78+
public void ShouldSetMaxIdleValueWhenSetSeperately()
79+
{
80+
var config = Config.Builder.WithMaxConnectionPoolSize(50).WithMaxIdleConnectionPoolSize(20).ToConfig();
81+
config.MaxConnectionPoolSize.Should().Be(50);
82+
config.MaxIdleConnectionPoolSize.Should().Be(20);
5183
}
5284

5385
[Fact]
@@ -57,7 +89,7 @@ public void WithLoggingShouldModifyTheSingleValue()
5789
config.EncryptionLevel.Should().Be(EncryptionLevel.Encrypted);
5890
config.TrustStrategy.Should().Be(TrustStrategy.TrustAllCertificates);
5991
config.Logger.Should().BeNull();
60-
config.MaxIdleConnectionPoolSize.Should().Be(10);
92+
config.MaxIdleConnectionPoolSize.Should().Be(500);
6193
}
6294

6395
[Fact]
@@ -77,7 +109,7 @@ public void WithEncryptionLevelShouldModifyTheSingleValue()
77109
config.EncryptionLevel.Should().Be(EncryptionLevel.None);
78110
config.TrustStrategy.Should().Be(TrustStrategy.TrustAllCertificates);
79111
config.Logger.Should().BeOfType<DebugLogger>();
80-
config.MaxIdleConnectionPoolSize.Should().Be(10);
112+
config.MaxIdleConnectionPoolSize.Should().Be(500);
81113
}
82114

83115
[Fact]
@@ -87,7 +119,7 @@ public void WithTrustStrategyShouldModifyTheSingleValue()
87119
config.EncryptionLevel.Should().Be(EncryptionLevel.Encrypted);
88120
config.TrustStrategy.Should().Be(TrustStrategy.TrustSystemCaSignedCertificates);
89121
config.Logger.Should().BeOfType<DebugLogger>();
90-
config.MaxIdleConnectionPoolSize.Should().Be(10);
122+
config.MaxIdleConnectionPoolSize.Should().Be(500);
91123
}
92124

93125
[Fact]
@@ -99,15 +131,15 @@ public void ChangingNewConfigShouldNotAffectOtherConfig()
99131

100132

101133
config2.Logger.Should().BeNull();
102-
config2.MaxIdleConnectionPoolSize.Should().Be(10);
134+
config2.MaxIdleConnectionPoolSize.Should().Be(500);
103135

104136
config1.MaxIdleConnectionPoolSize.Should().Be(3);
105137
config1.Logger.Should().BeOfType<DebugLogger>();
106138

107139
config.EncryptionLevel.Should().Be(EncryptionLevel.Encrypted);
108140
config.TrustStrategy.Should().Be(TrustStrategy.TrustAllCertificates);
109141
config.Logger.Should().BeOfType<DebugLogger>();
110-
config.MaxIdleConnectionPoolSize.Should().Be(10);
142+
config.MaxIdleConnectionPoolSize.Should().Be(500);
111143
}
112144
}
113145
}

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

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ namespace Neo4j.Driver.Tests
3535
{
3636
public class ConnectionPoolTests
3737
{
38+
internal static ConnectionPool NewConnectionPoolWithNoConnectionTimeoutValidation(
39+
IConnection connection,
40+
BlockingCollection<IPooledConnection> availableConnections = null,
41+
ConcurrentSet<IPooledConnection> inUseConnections = null)
42+
{
43+
var testConfigWithIdleTimeoutAndLifetimeCheckDisabled = new Config
44+
{
45+
MaxConnectionLifetime = Config.InfiniteInterval,
46+
ConnectionIdleTimeout = Config.InfiniteInterval
47+
};
48+
return new ConnectionPool(connection, availableConnections, inUseConnections,
49+
poolSettings: new ConnectionPoolSettings(testConfigWithIdleTimeoutAndLifetimeCheckDisabled));
50+
}
3851
public class AcquireMethod
3952
{
4053
private readonly ITestOutputHelper _output;
@@ -45,6 +58,17 @@ private IConnection MockedConnection
4558
{
4659
var mock = new Mock<IPooledConnection>();
4760
mock.Setup(x => x.IsOpen).Returns(true);
61+
mock.Setup(x => x.IdleTimer).Returns(MockedTimer);
62+
mock.Setup(x => x.LifetimeTimer).Returns(MockedTimer);
63+
return mock.Object;
64+
}
65+
}
66+
67+
private ITimer MockedTimer
68+
{
69+
get {
70+
var mock = new Mock<ITimer>();
71+
mock.Setup(t => t.ElapsedMilliseconds).Returns(0);
4872
return mock.Object;
4973
}
5074
}
@@ -72,7 +96,7 @@ public void ShouldCallConnInit()
7296
public void ShouldBlockWhenMaxPoolSizeReached()
7397
{
7498
var connectionPoolSettings = new ConnectionPoolSettings(new Config {MaxConnectionPoolSize = 2});
75-
var pool = new ConnectionPool(MockedConnection, settings: connectionPoolSettings);
99+
var pool = new ConnectionPool(MockedConnection, poolSettings: connectionPoolSettings);
76100
var conn = pool.Acquire();
77101
pool.Acquire();
78102
pool.NumberOfAvailableConnections.Should().Be(0);
@@ -105,7 +129,7 @@ public void ShouldThrowClientExceptionWhenFailedToAcquireWithinTimeout()
105129
MaxConnectionPoolSize = 2,
106130
ConnectionAcquisitionTimeout = TimeSpan.FromMilliseconds(0)
107131
});
108-
var pool = new ConnectionPool(MockedConnection, settings: connectionPoolSettings);
132+
var pool = new ConnectionPool(MockedConnection, poolSettings: connectionPoolSettings);
109133
var conn = pool.Acquire();
110134
pool.Acquire();
111135
pool.NumberOfAvailableConnections.Should().Be(0);
@@ -120,7 +144,7 @@ public void ShouldThrowClientExceptionWhenFailedToAcquireWithinTimeout()
120144
public void ShouldNotExceedIdleLimit()
121145
{
122146
var connectionPoolSettings = new ConnectionPoolSettings(new Config {MaxIdleConnectionPoolSize = 2});
123-
var pool = new ConnectionPool(MockedConnection, settings: connectionPoolSettings);
147+
var pool = new ConnectionPool(MockedConnection, poolSettings: connectionPoolSettings);
124148

125149
var conns = new List<IConnection>();
126150
for (var i = 0; i < 4; i++)
@@ -142,7 +166,7 @@ public void ShouldNotExceedIdleLimit()
142166
public void ShouldAcquireFromPoolIfAvailable()
143167
{
144168
var connectionPoolSettings = new ConnectionPoolSettings(new Config {MaxIdleConnectionPoolSize = 2});
145-
var pool = new ConnectionPool(MockedConnection, settings: connectionPoolSettings);
169+
var pool = new ConnectionPool(MockedConnection, poolSettings: connectionPoolSettings);
146170

147171
for (var i = 0; i < 4; i++)
148172
{
@@ -211,6 +235,8 @@ public void ShouldReuseWhenOpenConnectionInQueue()
211235
var conns = new BlockingCollection<IPooledConnection>();
212236
var mock = new Mock<IPooledConnection>();
213237
mock.Setup(x => x.IsOpen).Returns(true);
238+
mock.Setup(x => x.IdleTimer).Returns(MockedTimer);
239+
mock.Setup(x => x.LifetimeTimer).Returns(MockedTimer);
214240

215241
conns.Add(mock.Object);
216242
var pool = new ConnectionPool(MockedConnection, conns);
@@ -237,7 +263,7 @@ public void ShouldReuseOpenConnectionWhenOpenAndClosedConnectionsInQueue()
237263

238264
conns.Add(unhealthyMock.Object);
239265
conns.Add(healthyMock.Object);
240-
var pool = new ConnectionPool(MockedConnection, conns);
266+
var pool = NewConnectionPoolWithNoConnectionTimeoutValidation(MockedConnection, conns);
241267

242268
pool.NumberOfAvailableConnections.Should().Be(2);
243269
pool.NumberOfInUseConnections.Should().Be(0);
@@ -268,7 +294,7 @@ public void ShouldCloseIdleTooLongConn()
268294
var enableIdleTooLongTest = TimeSpan.FromMilliseconds(100);
269295
var poolSettings = new ConnectionPoolSettings(
270296
new Config {MaxIdleConnectionPoolSize = 2, ConnectionIdleTimeout = enableIdleTooLongTest});
271-
var pool = new ConnectionPool(MockedConnection, conns, settings: poolSettings);
297+
var pool = new ConnectionPool(MockedConnection, conns, poolSettings: poolSettings);
272298

273299
pool.NumberOfAvailableConnections.Should().Be(1);
274300
pool.NumberOfInUseConnections.Should().Be(0);
@@ -301,8 +327,12 @@ public void ShouldReuseIdleNotTooLongConn()
301327
conns.Add(mock.Object);
302328
var enableIdleTooLongTest = TimeSpan.FromMilliseconds(100);
303329
var poolSettings = new ConnectionPoolSettings(
304-
new Config {MaxIdleConnectionPoolSize = 2, ConnectionIdleTimeout = enableIdleTooLongTest});
305-
var pool = new ConnectionPool(MockedConnection, conns, settings: poolSettings);
330+
new Config {
331+
MaxIdleConnectionPoolSize = 2,
332+
ConnectionIdleTimeout = enableIdleTooLongTest,
333+
MaxConnectionLifetime = Config.InfiniteInterval, // disable life time check
334+
});
335+
var pool = new ConnectionPool(MockedConnection, conns, poolSettings: poolSettings);
306336

307337
pool.NumberOfAvailableConnections.Should().Be(1);
308338
pool.NumberOfInUseConnections.Should().Be(0);
@@ -343,7 +373,7 @@ public void ShouldAcquireNewWhenBeingUsedConcurrentlyBy(int numberOfThreads)
343373
mockConns.Enqueue(mock);
344374
}
345375

346-
var pool = new ConnectionPool(MockedConnection, conns);
376+
var pool = NewConnectionPoolWithNoConnectionTimeoutValidation(MockedConnection, conns);
347377

348378
pool.NumberOfAvailableConnections.Should().Be(numberOfThreads);
349379
pool.NumberOfInUseConnections.Should().Be(0);
@@ -407,7 +437,7 @@ public void ShouldCloseAcquiredConnectionIfPoolDisposeStarted()
407437
// Given
408438
var conns = new BlockingCollection<IPooledConnection>();
409439
var healthyMock = new Mock<IPooledConnection>();
410-
var pool = new ConnectionPool(MockedConnection, conns);
440+
var pool = NewConnectionPoolWithNoConnectionTimeoutValidation(MockedConnection, conns);
411441

412442
pool.NumberOfAvailableConnections.Should().Be(0);
413443
pool.NumberOfInUseConnections.Should().Be(0);
@@ -453,6 +483,7 @@ public void ShouldTimeoutAfterAcquireTimeoutIfPoolIsFull()
453483
public void ShouldTimeoutAfterAcquireTimeoutWhenConnectionIsNotValidated()
454484
{
455485
Config config = Config.Builder.WithConnectionAcquisitionTimeout(TimeSpan.FromSeconds(5))
486+
.WithConnectionTimeout(Config.InfiniteInterval)
456487
.ToConfig();
457488

458489
var notValidConnection = new Mock<IPooledConnection>();
@@ -577,12 +608,15 @@ public void ShouldCloseTheConnectionWhenConnectionIsReusableButThePoolIsFull()
577608

578609
var availableConns = new BlockingCollection<IPooledConnection>();
579610
var pooledConnMock = new Mock<IPooledConnection>();
580-
for (int i = 0; i < Config.DefaultConfig.MaxIdleConnectionPoolSize; i++)
611+
var poolSettings = new ConnectionPoolSettings(
612+
new Config {MaxConnectionPoolSize = 10});
613+
614+
for (int i = 0; i < poolSettings.MaxIdleConnectionPoolSize; i++)
581615
{
582616
availableConns.Add(pooledConnMock.Object);
583617
}
584618

585-
var pool = new ConnectionPool(null, availableConns, inUseConns);
619+
var pool = new ConnectionPool(null, availableConns, inUseConns, poolSettings: poolSettings);
586620

587621
pool.NumberOfAvailableConnections.Should().Be(10);
588622
pool.NumberOfInUseConnections.Should().Be(1);
@@ -609,7 +643,7 @@ public void ShouldStartTimerBeforeReturnToPoolWhenIdleDetectionEnabled()
609643
var poolSettings = new ConnectionPoolSettings(
610644
new Config {MaxIdleConnectionPoolSize = 2, ConnectionIdleTimeout = enableIdleTooLongTest});
611645
;
612-
var pool = new ConnectionPool(null, null, inUseConns, settings: poolSettings);
646+
var pool = new ConnectionPool(null, null, inUseConns, poolSettings: poolSettings);
613647

614648
pool.NumberOfAvailableConnections.Should().Be(0);
615649
pool.NumberOfInUseConnections.Should().Be(1);

Neo4j.Driver/Neo4j.Driver/Internal/ConnectionPool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ internal ConnectionPool(
9191
BlockingCollection<IPooledConnection> availableConnections = null,
9292
ConcurrentSet<IPooledConnection> inUseConnections = null,
9393
ILogger logger = null,
94-
ConnectionPoolSettings settings = null,
94+
ConnectionPoolSettings poolSettings = null,
9595
BufferSettings bufferSettings = null)
96-
: this(null, null, settings ?? new ConnectionPoolSettings(Config.DefaultConfig),
96+
: this(null, null, poolSettings ?? new ConnectionPoolSettings(Config.DefaultConfig),
9797
bufferSettings ?? new BufferSettings(Config.DefaultConfig), logger)
9898
{
9999
_fakeConnection = connection;

0 commit comments

Comments
 (0)