Skip to content

Commit b8fb1e9

Browse files
authored
Migrate tests from Java Driver to Testkit (#982) (#985)
New features: - `Temporary:DriverMaxTxRetryTime` - configures maximum transaction retry time. Replaced tests: - shouldCloseChannelWhenResetFails -> ChannelErrorHandler.channelInactive (existing unit test) Migrated tests: - shouldRetryReadTransactionUntilFailure -> test_should_fail_when_reading_from_unexpectedly_interrupting_readers_using_tx_function - shouldRetryWriteTransactionUntilFailure -> test_should_fail_when_writing_to_unexpectedly_interrupting_writers_using_tx_function - useSessionAfterDriverIsClosed -> test_should_fail_when_driver_closed_using_session_run
1 parent 5b4cfa7 commit b8fb1e9

File tree

13 files changed

+34
-276
lines changed

13 files changed

+34
-276
lines changed

driver/src/main/java/org/neo4j/driver/internal/async/pool/ConnectionPoolImpl.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252

5353
public class ConnectionPoolImpl implements ConnectionPool
5454
{
55+
public static final String CONNECTION_POOL_CLOSED_ERROR_MESSAGE = "Pool closed";
56+
5557
private final ChannelConnector connector;
5658
private final Bootstrap bootstrap;
5759
private final NettyChannelTracker nettyChannelTracker;
@@ -227,7 +229,7 @@ private void assertNotClosed()
227229
{
228230
if ( closed.get() )
229231
{
230-
throw new IllegalStateException( "Pool closed" );
232+
throw new IllegalStateException( CONNECTION_POOL_CLOSED_ERROR_MESSAGE );
231233
}
232234
}
233235

driver/src/test/java/org/neo4j/driver/integration/RoutingDriverBoltKitIT.java

-119
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,14 @@
2727

2828
import java.io.IOException;
2929
import java.net.URI;
30-
import java.util.Comparator;
3130
import java.util.List;
3231
import java.util.concurrent.TimeUnit;
33-
import java.util.concurrent.atomic.AtomicInteger;
3432

35-
import org.neo4j.driver.AccessMode;
36-
import org.neo4j.driver.AuthToken;
37-
import org.neo4j.driver.AuthTokens;
3833
import org.neo4j.driver.Config;
3934
import org.neo4j.driver.Driver;
4035
import org.neo4j.driver.GraphDatabase;
4136
import org.neo4j.driver.Record;
42-
import org.neo4j.driver.Session;
43-
import org.neo4j.driver.TransactionWork;
4437
import org.neo4j.driver.async.AsyncSession;
45-
import org.neo4j.driver.exceptions.SessionExpiredException;
46-
import org.neo4j.driver.internal.DriverFactory;
47-
import org.neo4j.driver.internal.cluster.RoutingSettings;
48-
import org.neo4j.driver.internal.retry.RetrySettings;
49-
import org.neo4j.driver.internal.security.SecurityPlanImpl;
50-
import org.neo4j.driver.internal.util.DriverFactoryWithFixedRetryLogic;
5138
import org.neo4j.driver.internal.util.Futures;
5239
import org.neo4j.driver.net.ServerAddress;
5340
import org.neo4j.driver.net.ServerAddressResolver;
@@ -66,7 +53,6 @@
6653
import static org.mockito.Mockito.verify;
6754
import static org.mockito.Mockito.when;
6855
import static org.neo4j.driver.SessionConfig.builder;
69-
import static org.neo4j.driver.util.StubServer.INSECURE_CONFIG;
7056
import static org.neo4j.driver.util.StubServer.insecureBuilder;
7157

7258
/**
@@ -166,50 +152,6 @@ void shouldHandleLeaderSwitchAndRetryWhenWritingInTxFunctionRX() throws IOExcept
166152
assertThat( writeServer.exitStatus(), equalTo( 0 ) );
167153
}
168154

169-
// fixed retries are not currently supported in testkit
170-
@Test
171-
void shouldRetryReadTransactionUntilFailure() throws Exception
172-
{
173-
StubServer router = stubController.startStub( "acquire_endpoints_v3.script", 9001 );
174-
StubServer brokenReader1 = stubController.startStub( "dead_read_server_tx.script", 9005 );
175-
StubServer brokenReader2 = stubController.startStub( "dead_read_server_tx.script", 9006 );
176-
177-
try ( Driver driver = newDriverWithFixedRetries( "neo4j://127.0.0.1:9001", 1 ); Session session = driver.session() )
178-
{
179-
AtomicInteger invocations = new AtomicInteger();
180-
assertThrows( SessionExpiredException.class, () -> session.readTransaction( queryWork( "MATCH (n) RETURN n.name", invocations ) ) );
181-
assertEquals( 2, invocations.get() );
182-
}
183-
finally
184-
{
185-
assertEquals( 0, router.exitStatus() );
186-
assertEquals( 0, brokenReader1.exitStatus() );
187-
assertEquals( 0, brokenReader2.exitStatus() );
188-
}
189-
}
190-
191-
// fixed retries are not currently supported in testkit
192-
@Test
193-
void shouldRetryWriteTransactionUntilFailure() throws Exception
194-
{
195-
StubServer router = stubController.startStub( "acquire_endpoints_v3.script", 9001 );
196-
StubServer brokenWriter1 = stubController.startStub( "dead_write_server.script", 9007 );
197-
StubServer brokenWriter2 = stubController.startStub( "dead_write_server.script", 9008 );
198-
199-
try ( Driver driver = newDriverWithFixedRetries( "neo4j://127.0.0.1:9001", 1 ); Session session = driver.session() )
200-
{
201-
AtomicInteger invocations = new AtomicInteger();
202-
assertThrows( SessionExpiredException.class, () -> session.writeTransaction( queryWork( "CREATE (n {name:'Bob'})", invocations ) ) );
203-
assertEquals( 2, invocations.get() );
204-
}
205-
finally
206-
{
207-
assertEquals( 0, router.exitStatus() );
208-
assertEquals( 0, brokenWriter1.exitStatus() );
209-
assertEquals( 0, brokenWriter2.exitStatus() );
210-
}
211-
}
212-
213155
@Test
214156
void shouldFailInitialDiscoveryWhenConfiguredResolverThrows()
215157
{
@@ -223,65 +165,4 @@ void shouldFailInitialDiscoveryWhenConfiguredResolverThrows()
223165
assertEquals( "Resolution failure!", error.getMessage() );
224166
verify( resolver ).resolve( ServerAddress.of( "my.server.com", 9001 ) );
225167
}
226-
227-
// general error reporting and handling should be improved before this can be moved to testkit
228-
// also, backend closes socket on general errors and it negatively impacts testkit's teardown process
229-
@Test
230-
void useSessionAfterDriverIsClosed() throws Exception
231-
{
232-
StubServer router = stubController.startStub( "acquire_endpoints_v3.script", 9001 );
233-
StubServer readServer = stubController.startStub( "read_server_v3_read.script", 9005 );
234-
235-
try ( Driver driver = GraphDatabase.driver( "neo4j://127.0.0.1:9001", INSECURE_CONFIG ) )
236-
{
237-
try ( Session session = driver.session( builder().withDefaultAccessMode( AccessMode.READ ).build() ) )
238-
{
239-
List<Record> records = session.run( "MATCH (n) RETURN n.name" ).list();
240-
assertEquals( 3, records.size() );
241-
}
242-
243-
Session session = driver.session( builder().withDefaultAccessMode( AccessMode.READ ).build() );
244-
245-
driver.close();
246-
247-
assertThrows( IllegalStateException.class, () -> session.run( "MATCH (n) RETURN n.name" ) );
248-
}
249-
finally
250-
{
251-
assertEquals( 0, readServer.exitStatus() );
252-
assertEquals( 0, router.exitStatus() );
253-
}
254-
}
255-
256-
private static Driver newDriverWithFixedRetries( String uriString, int retries )
257-
{
258-
DriverFactory driverFactory = new DriverFactoryWithFixedRetryLogic( retries );
259-
return newDriver( uriString, driverFactory, INSECURE_CONFIG );
260-
}
261-
262-
private static Driver newDriver( String uriString, DriverFactory driverFactory, Config config )
263-
{
264-
URI uri = URI.create( uriString );
265-
RoutingSettings routingConf = new RoutingSettings( 1, 1, 0, null );
266-
AuthToken auth = AuthTokens.none();
267-
return driverFactory.newInstance( uri, auth, routingConf, RetrySettings.DEFAULT, config, SecurityPlanImpl.insecure() );
268-
}
269-
270-
private static TransactionWork<List<Record>> queryWork( final String query, final AtomicInteger invocations )
271-
{
272-
return tx ->
273-
{
274-
invocations.incrementAndGet();
275-
return tx.run( query ).list();
276-
};
277-
}
278-
279-
static class PortBasedServerAddressComparator implements Comparator<ServerAddress>
280-
{
281-
@Override
282-
public int compare( ServerAddress a1, ServerAddress a2 )
283-
{
284-
return Integer.compare( a1.port(), a2.port() );
285-
}
286-
}
287168
}

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

-44
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,29 @@
1818
*/
1919
package org.neo4j.driver.internal;
2020

21-
import io.netty.channel.Channel;
2221
import org.junit.jupiter.api.AfterEach;
2322
import org.junit.jupiter.api.BeforeAll;
2423
import org.junit.jupiter.api.Test;
2524
import reactor.core.publisher.Flux;
2625
import reactor.core.publisher.Mono;
2726
import reactor.test.StepVerifier;
2827

29-
import java.net.URI;
3028
import java.util.ArrayList;
3129
import java.util.List;
3230

33-
import org.neo4j.driver.AuthTokens;
34-
import org.neo4j.driver.Config;
3531
import org.neo4j.driver.Driver;
3632
import org.neo4j.driver.GraphDatabase;
37-
import org.neo4j.driver.Session;
3833
import org.neo4j.driver.async.AsyncSession;
3934
import org.neo4j.driver.async.ResultCursor;
40-
import org.neo4j.driver.internal.cluster.RoutingSettings;
41-
import org.neo4j.driver.internal.retry.RetrySettings;
42-
import org.neo4j.driver.internal.security.SecurityPlanImpl;
43-
import org.neo4j.driver.internal.util.Clock;
44-
import org.neo4j.driver.internal.util.io.ChannelTrackingDriverFactory;
4535
import org.neo4j.driver.reactive.RxResult;
4636
import org.neo4j.driver.reactive.RxSession;
4737
import org.neo4j.driver.util.StubServer;
4838
import org.neo4j.driver.util.StubServerController;
4939

5040
import static java.util.Arrays.asList;
5141
import static java.util.Collections.singletonList;
52-
import static java.util.concurrent.TimeUnit.SECONDS;
5342
import static org.junit.jupiter.api.Assertions.assertEquals;
54-
import static org.junit.jupiter.api.Assertions.assertNull;
5543
import static org.neo4j.driver.SessionConfig.builder;
56-
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
5744
import static org.neo4j.driver.util.StubServer.INSECURE_CONFIG;
5845
import static org.neo4j.driver.util.TestUtil.await;
5946

@@ -73,37 +60,6 @@ public void killServers()
7360
stubController.reset();
7461
}
7562

76-
@Test
77-
void shouldCloseChannelWhenResetFails() throws Exception
78-
{
79-
StubServer server = stubController.startStub( "reset_error.script", 9001 );
80-
try
81-
{
82-
URI uri = URI.create( "bolt://localhost:9001" );
83-
Config config = Config.builder().withLogging( DEV_NULL_LOGGING ).withoutEncryption().build();
84-
ChannelTrackingDriverFactory driverFactory = new ChannelTrackingDriverFactory( 1, Clock.SYSTEM );
85-
86-
try ( Driver driver = driverFactory.newInstance( uri, AuthTokens.none(), RoutingSettings.DEFAULT, RetrySettings.DEFAULT, config,
87-
SecurityPlanImpl.insecure() ) )
88-
{
89-
try ( Session session = driver.session() )
90-
{
91-
assertEquals( 42, session.run( "RETURN 42 AS answer" ).single().get( 0 ).asInt() );
92-
}
93-
94-
List<Channel> channels = driverFactory.pollChannels();
95-
// there should be a single channel
96-
assertEquals( 1, channels.size() );
97-
// and it should be closed because it failed to RESET
98-
assertNull( channels.get( 0 ).closeFuture().get( 30, SECONDS ) );
99-
}
100-
}
101-
finally
102-
{
103-
assertEquals( 0, server.exitStatus() );
104-
}
105-
}
106-
10763
@Test
10864
void shouldStreamingRecordsInBatchesRx() throws Exception
10965
{

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

-50
This file was deleted.

driver/src/test/resources/acquire_endpoints_v3.script

-10
This file was deleted.

driver/src/test/resources/dead_read_server_tx.script

-9
This file was deleted.

driver/src/test/resources/dead_write_server.script

-9
This file was deleted.

driver/src/test/resources/read_server_v3_read.script

-12
This file was deleted.

driver/src/test/resources/reset_error.script

-12
This file was deleted.

driver/src/test/resources/untrusted_server.script

-6
This file was deleted.

0 commit comments

Comments
 (0)