Skip to content

Commit fbdd1ad

Browse files
authored
Update ResponseQueueHanlder to handle multiple requests sequentially (#1503)
This update ensures that `ResponseQueueHanlder` counts requests and dispatches responses when request count is greater than 0.
1 parent 99d32cb commit fbdd1ad

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,28 @@
2626
public class ResponseQueueHanlder {
2727
private final Consumer<TestkitResponse> responseWriter;
2828
private final Queue<TestkitResponse> responseQueue = new ArrayDeque<>();
29-
private boolean responseReady;
29+
private int requestCount;
3030

3131
ResponseQueueHanlder(Consumer<TestkitResponse> responseWriter) {
3232
this.responseWriter = responseWriter;
3333
}
3434

35-
public synchronized void setResponseReadyAndDispatchFirst() {
36-
responseReady = true;
37-
dispatchFirst();
35+
public synchronized void increaseRequestCountAndDispatchFirstResponse() {
36+
requestCount++;
37+
dispatchFirstResponse();
3838
}
3939

40-
public synchronized void offerAndDispatchFirst(TestkitResponse response) {
40+
public synchronized void offerAndDispatchFirstResponse(TestkitResponse response) {
4141
responseQueue.offer(response);
42-
if (responseReady) {
43-
dispatchFirst();
42+
if (requestCount > 0) {
43+
dispatchFirstResponse();
4444
}
4545
}
4646

47-
private synchronized void dispatchFirst() {
47+
private synchronized void dispatchFirstResponse() {
4848
var response = responseQueue.poll();
4949
if (response != null) {
50-
responseReady = false;
50+
requestCount--;
5151
responseWriter.accept(response);
5252
}
5353
}

testkit-backend/src/main/java/neo4j/org/testkit/backend/channel/handler/TestkitRequestProcessorHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
8383
.exceptionally(this::createErrorResponse)
8484
.whenComplete((response, ignored) -> {
8585
if (response != null) {
86-
responseQueueHanlder.offerAndDispatchFirst(response);
86+
responseQueueHanlder.offerAndDispatchFirstResponse(response);
8787
}
8888
});
8989
} catch (Throwable throwable) {
@@ -106,7 +106,7 @@ private static CompletionStage<TestkitResponse> wrapSyncRequest(
106106
@Override
107107
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
108108
var response = createErrorResponse(cause);
109-
responseQueueHanlder.offerAndDispatchFirst(response);
109+
responseQueueHanlder.offerAndDispatchFirstResponse(response);
110110
}
111111

112112
private TestkitResponse createErrorResponse(Throwable throwable) {
@@ -170,7 +170,7 @@ private void writeAndFlush(TestkitResponse response) {
170170
if (channel == null) {
171171
throw new IllegalStateException("Called before channel is initialized");
172172
}
173-
responseQueueHanlder.offerAndDispatchFirst(response);
173+
responseQueueHanlder.offerAndDispatchFirstResponse(response);
174174
}
175175

176176
public enum BackendMode {

testkit-backend/src/main/java/neo4j/org/testkit/backend/channel/handler/TestkitRequestResponseMapperHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public TestkitRequestResponseMapperHandler(Logging logging, ResponseQueueHanlder
4444
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
4545
var testkitMessage = (String) msg;
4646
log.debug("Inbound Testkit message '%s'", testkitMessage.trim());
47-
responseQueueHanlder.setResponseReadyAndDispatchFirst();
47+
responseQueueHanlder.increaseRequestCountAndDispatchFirstResponse();
4848
var testkitRequest = objectMapper.readValue(testkitMessage, TestkitRequest.class);
4949
ctx.fireChannelRead(testkitRequest);
5050
}

0 commit comments

Comments
 (0)