Skip to content

Commit 61e2706

Browse files
authored
Migrate tests from Java Driver to Testkit (#990) (#991)
New features: - `Temporary:ResultList` Migrated tests: - shouldHandleLeaderSwitchAndRetryWhenWritingInTxFunctionAsync -> test_should_write_successfully_on_leader_switch_using_tx_function (existing test, bolt bump to 4.1) - shouldOnlyPullRecordsWhenNeededAsyncSession -> test_should_accept_custom_fetch_size_using_session_configuration (existing test, bolt bump to 4.1) - shouldPullAllRecordsOnListAsyncWhenOverWatermark -> test_should_pull_custom_size_and_then_all_using_session_configuration
1 parent f53891b commit 61e2706

File tree

9 files changed

+124
-161
lines changed

9 files changed

+124
-161
lines changed

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

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,19 @@
2727

2828
import java.io.IOException;
2929
import java.net.URI;
30-
import java.util.List;
3130
import java.util.concurrent.TimeUnit;
3231

3332
import org.neo4j.driver.Config;
3433
import org.neo4j.driver.Driver;
3534
import org.neo4j.driver.GraphDatabase;
3635
import org.neo4j.driver.Record;
37-
import org.neo4j.driver.async.AsyncSession;
38-
import org.neo4j.driver.internal.util.Futures;
3936
import org.neo4j.driver.net.ServerAddress;
4037
import org.neo4j.driver.net.ServerAddressResolver;
4138
import org.neo4j.driver.reactive.RxResult;
4239
import org.neo4j.driver.reactive.RxSession;
4340
import org.neo4j.driver.util.StubServer;
4441
import org.neo4j.driver.util.StubServerController;
4542

46-
import static java.util.Arrays.asList;
4743
import static org.hamcrest.core.IsEqual.equalTo;
4844
import static org.hamcrest.junit.MatcherAssert.assertThat;
4945
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -79,41 +75,6 @@ public void killServers()
7975
stubController.reset();
8076
}
8177

82-
// Async is not currently supported in testkit.
83-
@Test
84-
void shouldHandleLeaderSwitchAndRetryWhenWritingInTxFunctionAsync() throws IOException, InterruptedException
85-
{
86-
// Given
87-
StubServer server = stubController.startStub( "acquire_endpoints_twice_v4.script", 9001 );
88-
89-
// START a write server that fails on the first write attempt but then succeeds on the second
90-
StubServer writeServer = stubController.startStub( "not_able_to_write_server_tx_func_retries.script", 9007 );
91-
URI uri = URI.create( "neo4j://127.0.0.1:9001" );
92-
93-
Driver driver = GraphDatabase.driver( uri, Config.builder().withMaxTransactionRetryTime( 1, TimeUnit.MILLISECONDS ).build() );
94-
AsyncSession session = driver.asyncSession( builder().withDatabase( "mydatabase" ).build() );
95-
List<String> names = Futures.blockingGet( session.writeTransactionAsync(
96-
tx -> tx.runAsync( "RETURN 1" )
97-
.thenComposeAsync( ignored -> {
98-
try
99-
{
100-
Thread.sleep( 100 );
101-
}
102-
catch ( InterruptedException ex )
103-
{
104-
}
105-
return tx.runAsync( "MATCH (n) RETURN n.name" );
106-
} )
107-
.thenComposeAsync( cursor -> cursor.listAsync( RoutingDriverBoltKitIT::extractNameField ) ) ) );
108-
109-
assertEquals( asList( "Foo", "Bar" ), names );
110-
111-
// Finally
112-
driver.close();
113-
assertThat( server.exitStatus(), equalTo( 0 ) );
114-
assertThat( writeServer.exitStatus(), equalTo( 0 ) );
115-
}
116-
11778
private static String extractNameField( Record record )
11879
{
11980
return record.get( 0 ).asString();

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

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,18 @@
2525
import reactor.core.publisher.Mono;
2626
import reactor.test.StepVerifier;
2727

28-
import java.util.ArrayList;
2928
import java.util.List;
3029

3130
import org.neo4j.driver.Driver;
3231
import org.neo4j.driver.GraphDatabase;
33-
import org.neo4j.driver.async.AsyncSession;
34-
import org.neo4j.driver.async.ResultCursor;
3532
import org.neo4j.driver.reactive.RxResult;
3633
import org.neo4j.driver.reactive.RxSession;
3734
import org.neo4j.driver.util.StubServer;
3835
import org.neo4j.driver.util.StubServerController;
3936

40-
import static java.util.Arrays.asList;
4137
import static java.util.Collections.singletonList;
4238
import static org.junit.jupiter.api.Assertions.assertEquals;
43-
import static org.neo4j.driver.SessionConfig.builder;
4439
import static org.neo4j.driver.util.StubServer.INSECURE_CONFIG;
45-
import static org.neo4j.driver.util.TestUtil.await;
4640

4741
class DirectDriverBoltKitIT
4842
{
@@ -80,53 +74,6 @@ void shouldStreamingRecordsInBatchesRx() throws Exception
8074
}
8175
}
8276

83-
@Test
84-
void shouldOnlyPullRecordsWhenNeededAsyncSession() throws Exception
85-
{
86-
StubServer server = stubController.startStub( "streaming_records_v4_buffering.script", 9001 );
87-
try
88-
{
89-
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) )
90-
{
91-
AsyncSession session = driver.asyncSession( builder().withFetchSize( 2 ).build() );
92-
93-
ArrayList<String> resultList = new ArrayList<>();
94-
95-
await( session.runAsync( "MATCH (n) RETURN n.name" )
96-
.thenCompose( resultCursor ->
97-
resultCursor.forEachAsync( record -> resultList.add( record.get( 0 ).asString() ) ) ) );
98-
99-
assertEquals( resultList, asList( "Bob", "Alice", "Tina", "Frank", "Daisy", "Clive" ) );
100-
}
101-
}
102-
finally
103-
{
104-
assertEquals( 0, server.exitStatus() );
105-
}
106-
}
107-
108-
@Test
109-
void shouldPullAllRecordsOnListAsyncWhenOverWatermark() throws Exception
110-
{
111-
StubServer server = stubController.startStub( "streaming_records_v4_list_async.script", 9001 );
112-
try
113-
{
114-
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) )
115-
{
116-
AsyncSession session = driver.asyncSession( builder().withFetchSize( 10 ).build() );
117-
118-
ResultCursor cursor = await( session.runAsync( "MATCH (n) RETURN n.name" ) );
119-
List<String> records = await( cursor.listAsync( record -> record.get( 0 ).asString() ) );
120-
121-
assertEquals( records, asList( "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L" ) );
122-
}
123-
}
124-
finally
125-
{
126-
assertEquals( 0, server.exitStatus() );
127-
}
128-
}
129-
13077
@Test
13178
void shouldDiscardIfPullNotFinished() throws Throwable
13279
{

driver/src/test/resources/not_able_to_write_server_tx_func_retries.script

Lines changed: 0 additions & 25 deletions
This file was deleted.

driver/src/test/resources/streaming_records_v4_buffering.script

Lines changed: 0 additions & 19 deletions
This file was deleted.

driver/src/test/resources/streaming_records_v4_list_async.script

Lines changed: 0 additions & 23 deletions
This file was deleted.

testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/GetFeatures.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public class GetFeatures implements TestkitRequest
4343
"Optimization:PullPipelining",
4444
"ConfHint:connection.recv_timeout_seconds",
4545
"Temporary:DriverFetchSize",
46-
"Temporary:DriverMaxTxRetryTime"
46+
"Temporary:DriverMaxTxRetryTime",
47+
"Temporary:ResultList"
4748
) );
4849

4950
private static final Set<String> SYNC_FEATURES = new HashSet<>( Collections.singletonList(
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package neo4j.org.testkit.backend.messages.requests;
20+
21+
import lombok.Getter;
22+
import lombok.NoArgsConstructor;
23+
import lombok.Setter;
24+
import neo4j.org.testkit.backend.TestkitState;
25+
import neo4j.org.testkit.backend.messages.responses.Record;
26+
import neo4j.org.testkit.backend.messages.responses.RecordList;
27+
import neo4j.org.testkit.backend.messages.responses.TestkitResponse;
28+
29+
import java.util.List;
30+
import java.util.Optional;
31+
import java.util.concurrent.CompletionStage;
32+
import java.util.stream.Collectors;
33+
34+
@Setter
35+
@Getter
36+
@NoArgsConstructor
37+
public class ResultList implements TestkitRequest
38+
{
39+
private ResultListBody data;
40+
41+
@Override
42+
public TestkitResponse process( TestkitState testkitState )
43+
{
44+
return createResponse( testkitState.getResults().get( data.getResultId() ).list() );
45+
}
46+
47+
@Override
48+
public CompletionStage<Optional<TestkitResponse>> processAsync( TestkitState testkitState )
49+
{
50+
return testkitState.getResultCursors().get( data.getResultId() )
51+
.listAsync()
52+
.thenApply( this::createResponse )
53+
.thenApply( Optional::of );
54+
}
55+
56+
private RecordList createResponse( List<org.neo4j.driver.Record> records )
57+
{
58+
List<Record.RecordBody> mappedRecords = records.stream()
59+
.map( record -> Record.RecordBody.builder().values( record ).build() )
60+
.collect( Collectors.toList() );
61+
return RecordList.builder()
62+
.data( RecordList.RecordListBody.builder().records( mappedRecords ).build() )
63+
.build();
64+
}
65+
66+
@Setter
67+
@Getter
68+
@NoArgsConstructor
69+
public static class ResultListBody
70+
{
71+
private String resultId;
72+
}
73+
}

testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/TestkitRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
@JsonSubTypes.Type( ResolverResolutionCompleted.class ), @JsonSubTypes.Type( CheckMultiDBSupport.class ),
4040
@JsonSubTypes.Type( DomainNameResolutionCompleted.class ), @JsonSubTypes.Type( StartTest.class ),
4141
@JsonSubTypes.Type( TransactionRollback.class ), @JsonSubTypes.Type( GetFeatures.class ),
42-
@JsonSubTypes.Type( GetRoutingTable.class ), @JsonSubTypes.Type( TransactionClose.class )
42+
@JsonSubTypes.Type( GetRoutingTable.class ), @JsonSubTypes.Type( TransactionClose.class ),
43+
@JsonSubTypes.Type( ResultList.class )
4344
} )
4445
public interface TestkitRequest
4546
{
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package neo4j.org.testkit.backend.messages.responses;
20+
21+
import lombok.Builder;
22+
import lombok.Getter;
23+
import lombok.Setter;
24+
25+
import java.util.List;
26+
27+
@Setter
28+
@Getter
29+
@Builder
30+
public class RecordList implements TestkitResponse
31+
{
32+
private final RecordListBody data;
33+
34+
@Override
35+
public String testkitName()
36+
{
37+
return "RecordList";
38+
}
39+
40+
@Setter
41+
@Getter
42+
@Builder
43+
public static class RecordListBody
44+
{
45+
private final List<Record.RecordBody> records;
46+
}
47+
}

0 commit comments

Comments
 (0)