Skip to content

Commit 09407e7

Browse files
committed
Add Async Testkit Backend support (neo4j#987)
This update brings async Testkit backend support that uses async driver for testing and is compatible with the current Testkit protocol. This allows async driver testing using the same Testkit tests transparently. Not all requests are implemented in this PR. For instance, the resolver implementation will be added separately. The current update enables a substantial amount of tests to be executed. In addition, a separate PR is planned to migrate the sync backend to the new Netty based implementation.
1 parent b8fb1e9 commit 09407e7

38 files changed

+1037
-83
lines changed

testkit-backend/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
<artifactId>neo4j-java-driver</artifactId>
2626
<version>${project.version}</version>
2727
</dependency>
28+
<dependency>
29+
<groupId>io.netty</groupId>
30+
<artifactId>netty-handler</artifactId>
31+
</dependency>
2832
<dependency>
2933
<groupId>com.fasterxml.jackson.core</groupId>
3034
<artifactId>jackson-core</artifactId>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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;
20+
21+
import io.netty.bootstrap.ServerBootstrap;
22+
import io.netty.channel.ChannelFuture;
23+
import io.netty.channel.ChannelInitializer;
24+
import io.netty.channel.EventLoopGroup;
25+
import io.netty.channel.nio.NioEventLoopGroup;
26+
import io.netty.channel.socket.SocketChannel;
27+
import io.netty.channel.socket.nio.NioServerSocketChannel;
28+
import neo4j.org.testkit.backend.channel.handler.TestkitMessageInboundHandler;
29+
import neo4j.org.testkit.backend.channel.handler.TestkitMessageOutboundHandler;
30+
import neo4j.org.testkit.backend.channel.handler.TestkitRequestProcessorHandler;
31+
import neo4j.org.testkit.backend.channel.handler.TestkitRequestResponseMapperHandler;
32+
33+
public class AsyncBackendServer
34+
{
35+
public void run() throws InterruptedException
36+
{
37+
EventLoopGroup group = new NioEventLoopGroup();
38+
try
39+
{
40+
ServerBootstrap bootstrap = new ServerBootstrap();
41+
bootstrap.group( group )
42+
.channel( NioServerSocketChannel.class )
43+
.localAddress( 9876 )
44+
.childHandler( new ChannelInitializer<SocketChannel>()
45+
{
46+
@Override
47+
protected void initChannel( SocketChannel channel )
48+
{
49+
channel.pipeline().addLast( new TestkitMessageInboundHandler() );
50+
channel.pipeline().addLast( new TestkitMessageOutboundHandler() );
51+
channel.pipeline().addLast( new TestkitRequestResponseMapperHandler() );
52+
channel.pipeline().addLast( new TestkitRequestProcessorHandler() );
53+
}
54+
} );
55+
ChannelFuture server = bootstrap.bind().sync();
56+
server.channel().closeFuture().sync();
57+
}
58+
finally
59+
{
60+
group.shutdownGracefully().sync();
61+
}
62+
}
63+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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;
20+
21+
import lombok.Getter;
22+
import lombok.Setter;
23+
24+
import java.util.concurrent.CompletableFuture;
25+
26+
import org.neo4j.driver.async.AsyncSession;
27+
28+
@Getter
29+
@Setter
30+
public class AsyncSessionState
31+
{
32+
public AsyncSession session;
33+
public CompletableFuture<Void> txWorkFuture;
34+
35+
public AsyncSessionState( AsyncSession session )
36+
{
37+
this.session = session;
38+
}
39+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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;
20+
21+
import java.io.BufferedReader;
22+
import java.io.BufferedWriter;
23+
import java.io.IOException;
24+
import java.io.InputStreamReader;
25+
import java.io.OutputStreamWriter;
26+
import java.io.UncheckedIOException;
27+
import java.net.ServerSocket;
28+
import java.net.Socket;
29+
import java.util.concurrent.CompletableFuture;
30+
31+
public class BackendServer
32+
{
33+
public void run() throws IOException
34+
{
35+
ServerSocket serverSocket = new ServerSocket( 9876 );
36+
37+
System.out.println( "Java TestKit Backend Started on port: " + serverSocket.getLocalPort() );
38+
39+
while ( true )
40+
{
41+
final Socket clientSocket = serverSocket.accept();
42+
CompletableFuture.runAsync( () -> handleClient( clientSocket ) );
43+
}
44+
}
45+
46+
private void handleClient( Socket clientSocket )
47+
{
48+
try
49+
{
50+
System.out.println( "Handling connection from: " + clientSocket.getRemoteSocketAddress() );
51+
BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream() ) );
52+
BufferedWriter out = new BufferedWriter( new OutputStreamWriter( clientSocket.getOutputStream() ) );
53+
CommandProcessor commandProcessor = new CommandProcessor( in, out );
54+
55+
boolean cont = true;
56+
while ( cont )
57+
{
58+
try
59+
{
60+
cont = commandProcessor.process();
61+
}
62+
catch ( Exception e )
63+
{
64+
e.printStackTrace();
65+
clientSocket.close();
66+
cont = false;
67+
}
68+
}
69+
}
70+
catch ( IOException ex )
71+
{
72+
throw new UncheckedIOException( ex );
73+
}
74+
}
75+
}

testkit-backend/src/main/java/neo4j/org/testkit/backend/Runner.java

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,58 +18,19 @@
1818
*/
1919
package neo4j.org.testkit.backend;
2020

21-
import java.io.BufferedReader;
22-
import java.io.BufferedWriter;
2321
import java.io.IOException;
24-
import java.io.InputStreamReader;
25-
import java.io.OutputStreamWriter;
26-
import java.io.UncheckedIOException;
27-
import java.net.ServerSocket;
28-
import java.net.Socket;
29-
import java.util.concurrent.CompletableFuture;
3022

3123
public class Runner
3224
{
33-
public static void main( String[] args ) throws IOException
25+
public static void main( String[] args ) throws IOException, InterruptedException
3426
{
35-
ServerSocket serverSocket = new ServerSocket( 9876 );
36-
37-
System.out.println( "Java TestKit Backend Started on port: " + serverSocket.getLocalPort() );
38-
39-
while ( true )
40-
{
41-
final Socket clientSocket = serverSocket.accept();
42-
CompletableFuture.runAsync( () -> handleClient( clientSocket ) );
43-
}
44-
}
45-
46-
private static void handleClient( Socket clientSocket )
47-
{
48-
try
27+
if ( args.length > 0 && args[0].equals( "async" ) )
4928
{
50-
System.out.println( "Handling connection from: " + clientSocket.getRemoteSocketAddress() );
51-
BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream() ) );
52-
BufferedWriter out = new BufferedWriter( new OutputStreamWriter( clientSocket.getOutputStream() ) );
53-
CommandProcessor commandProcessor = new CommandProcessor( in, out );
54-
55-
boolean cont = true;
56-
while ( cont )
57-
{
58-
try
59-
{
60-
cont = commandProcessor.process();
61-
}
62-
catch ( Exception e )
63-
{
64-
e.printStackTrace();
65-
clientSocket.close();
66-
cont = false;
67-
}
68-
}
29+
new AsyncBackendServer().run();
6930
}
70-
catch ( IOException ex )
31+
else
7132
{
72-
throw new UncheckedIOException( ex );
33+
new BackendServer().run();
7334
}
7435
}
7536
}

testkit-backend/src/main/java/neo4j/org/testkit/backend/TestkitState.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.neo4j.driver.Driver;
3232
import org.neo4j.driver.Result;
3333
import org.neo4j.driver.Transaction;
34+
import org.neo4j.driver.async.AsyncTransaction;
35+
import org.neo4j.driver.async.ResultCursor;
3436
import org.neo4j.driver.exceptions.Neo4jException;
3537
import org.neo4j.driver.internal.cluster.RoutingTableRegistry;
3638
import org.neo4j.driver.net.ServerAddress;
@@ -41,8 +43,11 @@ public class TestkitState
4143
private final Map<String,Driver> drivers = new HashMap<>();
4244
private final Map<String,RoutingTableRegistry> routingTableRegistry = new HashMap<>();
4345
private final Map<String,SessionState> sessionStates = new HashMap<>();
46+
private final Map<String,AsyncSessionState> asyncSessionStates = new HashMap<>();
4447
private final Map<String,Result> results = new HashMap<>();
48+
private final Map<String,ResultCursor> resultCursors = new HashMap<>();
4549
private final Map<String,Transaction> transactions = new HashMap<>();
50+
private final Map<String,AsyncTransaction> asyncTransactions = new HashMap<>();
4651
private final Map<String,Neo4jException> errors = new HashMap<>();
4752
private int idGenerator = 0;
4853
private final Consumer<TestkitResponse> responseWriter;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.channel.handler;
20+
21+
import io.netty.buffer.ByteBuf;
22+
import io.netty.channel.ChannelHandlerContext;
23+
import io.netty.channel.SimpleChannelInboundHandler;
24+
import io.netty.util.CharsetUtil;
25+
26+
import java.util.ArrayList;
27+
import java.util.List;
28+
import java.util.Optional;
29+
30+
public class TestkitMessageInboundHandler extends SimpleChannelInboundHandler<ByteBuf>
31+
{
32+
private final StringBuilder requestBuffer = new StringBuilder();
33+
34+
@Override
35+
public void channelRead0( ChannelHandlerContext ctx, ByteBuf byteBuf )
36+
{
37+
String requestStr = byteBuf.toString( CharsetUtil.UTF_8 );
38+
requestBuffer.append( requestStr );
39+
40+
List<String> testkitMessages = new ArrayList<>();
41+
Optional<String> testkitMessageOpt = extractTestkitMessage();
42+
while ( testkitMessageOpt.isPresent() )
43+
{
44+
testkitMessages.add( testkitMessageOpt.get() );
45+
testkitMessageOpt = extractTestkitMessage();
46+
}
47+
48+
testkitMessages.forEach( ctx::fireChannelRead );
49+
}
50+
51+
private Optional<String> extractTestkitMessage()
52+
{
53+
String requestEndMarker = "#request end\n";
54+
int endMarkerIndex = requestBuffer.indexOf( requestEndMarker );
55+
if ( endMarkerIndex < 0 )
56+
{
57+
return Optional.empty();
58+
}
59+
String requestBeginMarker = "#request begin\n";
60+
int beginMarkerIndex = requestBuffer.indexOf( requestBeginMarker );
61+
if ( beginMarkerIndex != 0 )
62+
{
63+
throw new RuntimeException( "Unexpected data in message buffer" );
64+
}
65+
// extract Testkit message without markers
66+
String testkitMessage = requestBuffer.substring( requestBeginMarker.length(), endMarkerIndex );
67+
if ( testkitMessage.contains( requestBeginMarker ) || testkitMessage.contains( requestEndMarker ) )
68+
{
69+
throw new RuntimeException( "Testkit message contains request markers" );
70+
}
71+
// remove Testkit message from buffer
72+
requestBuffer.delete( 0, endMarkerIndex + requestEndMarker.length() + 1 );
73+
return Optional.of( testkitMessage );
74+
}
75+
76+
@Override
77+
public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause )
78+
{
79+
ctx.close();
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.channel.handler;
20+
21+
import io.netty.buffer.ByteBuf;
22+
import io.netty.buffer.Unpooled;
23+
import io.netty.channel.ChannelHandlerContext;
24+
import io.netty.channel.ChannelOutboundHandlerAdapter;
25+
import io.netty.channel.ChannelPromise;
26+
27+
import java.nio.charset.StandardCharsets;
28+
29+
public class TestkitMessageOutboundHandler extends ChannelOutboundHandlerAdapter
30+
{
31+
@Override
32+
public void write( ChannelHandlerContext ctx, Object msg, ChannelPromise promise )
33+
{
34+
String testkitResponseStr = (String) msg;
35+
String testkitMessage = String.format( "#response begin\n%s\n#response end\n", testkitResponseStr );
36+
ByteBuf byteBuf = Unpooled.copiedBuffer( testkitMessage, StandardCharsets.UTF_8 );
37+
ctx.writeAndFlush( byteBuf, promise );
38+
}
39+
}

0 commit comments

Comments
 (0)