Skip to content

Commit e00cd6a

Browse files
committed
Allow zero and negative values for ConnectionLivenessCheckTimeout
So feature can be easier turned on and off by users. Zero timeout means always do test-on-borrow, negative timeout means never to test-on-borrow.
1 parent 5cd8592 commit e00cd6a

File tree

4 files changed

+14
-33
lines changed

4 files changed

+14
-33
lines changed

driver/src/main/java/org/neo4j/driver/internal/net/pooling/PoolSettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ public long idleTimeBeforeConnectionTest()
5151

5252
public boolean idleTimeBeforeConnectionTestConfigured()
5353
{
54-
return idleTimeBeforeConnectionTest > 0;
54+
return idleTimeBeforeConnectionTest >= 0;
5555
}
5656
}

driver/src/main/java/org/neo4j/driver/v1/Config.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,22 +292,16 @@ public ConfigBuilder withSessionLivenessCheckTimeout( long timeout )
292292
* application seeing connection problems, and performance.
293293
* <p>
294294
* You normally should not need to tune this parameter.
295-
* This feature is turned off by default.
295+
* This feature is turned off by default. Value {@code 0} means connections will always be tested for
296+
* validity and negative values mean connections will never be tested.
296297
*
297298
* @param value the minimum idle time in milliseconds
298299
* @param unit the unit in which the duration is given
299300
* @return this builder
300301
*/
301302
public ConfigBuilder withConnectionLivenessCheckTimeout( long value, TimeUnit unit )
302303
{
303-
long idleTimeBeforeConnectionTestMillis = unit.toMillis( value );
304-
if ( idleTimeBeforeConnectionTestMillis <= 0 )
305-
{
306-
throw new IllegalArgumentException( String.format(
307-
"The timeout value must be positive when converted to ms, but was %d. Given %d %s",
308-
idleTimeBeforeConnectionTestMillis, value, unit ) );
309-
}
310-
this.idleTimeBeforeConnectionTest = idleTimeBeforeConnectionTestMillis;
304+
this.idleTimeBeforeConnectionTest = unit.toMillis( value );
311305
return this;
312306
}
313307

driver/src/test/java/org/neo4j/driver/internal/ConfigTest.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,35 +91,19 @@ public void shouldSupportLivenessCheckTimeoutSetting() throws Throwable
9191
}
9292

9393
@Test
94-
public void shouldThrowForZeroTimeoutInLivenessCheckTimeoutSetting() throws Throwable
94+
public void shouldAllowZeroConnectionLivenessCheckTimeout() throws Throwable
9595
{
96-
Config.ConfigBuilder builder = Config.build();
96+
Config config = Config.build().withConnectionLivenessCheckTimeout( 0, TimeUnit.SECONDS ).toConfig();
9797

98-
try
99-
{
100-
builder.withConnectionLivenessCheckTimeout( 0, TimeUnit.SECONDS );
101-
fail( "Exception expected" );
102-
}
103-
catch ( Exception e )
104-
{
105-
assertThat( e, instanceOf( IllegalArgumentException.class ) );
106-
}
98+
assertEquals( 0, config.idleTimeBeforeConnectionTest() );
10799
}
108100

109101
@Test
110-
public void shouldThrowForNegativeTimeoutInLivenessCheckTimeoutSetting() throws Throwable
102+
public void shouldAllowNegativeConnectionLivenessCheckTimeout() throws Throwable
111103
{
112-
Config.ConfigBuilder builder = Config.build();
104+
Config config = Config.build().withConnectionLivenessCheckTimeout( -42, TimeUnit.SECONDS ).toConfig();
113105

114-
try
115-
{
116-
builder.withConnectionLivenessCheckTimeout( -42, TimeUnit.SECONDS );
117-
fail( "Exception expected" );
118-
}
119-
catch ( Exception e )
120-
{
121-
assertThat( e, instanceOf( IllegalArgumentException.class ) );
122-
}
106+
assertEquals( TimeUnit.SECONDS.toMillis( -42 ), config.idleTimeBeforeConnectionTest() );
123107
}
124108

125109
@Test

driver/src/test/java/org/neo4j/driver/internal/net/pooling/PoolSettingsTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,17 @@ public void idleTimeBeforeConnectionTestWhenConfigured()
3939
@Test
4040
public void idleTimeBeforeConnectionTestWhenSetToZero()
4141
{
42-
testWithIllegalValue( 0 );
42+
PoolSettings settings = new PoolSettings( 10, 0 );
43+
assertTrue( settings.idleTimeBeforeConnectionTestConfigured() );
44+
assertEquals( 0, settings.idleTimeBeforeConnectionTest() );
4345
}
4446

4547
@Test
4648
public void idleTimeBeforeConnectionTestWhenSetToNegativeValue()
4749
{
4850
testWithIllegalValue( -1 );
4951
testWithIllegalValue( -42 );
52+
testWithIllegalValue( Integer.MIN_VALUE );
5053
}
5154

5255
private static void testWithIllegalValue( int value )

0 commit comments

Comments
 (0)