Skip to content
This repository was archived by the owner on Sep 9, 2024. It is now read-only.

Commit f8ce25d

Browse files
committed
chore: Fix threading issue in unreleased verifier
1 parent a880f3d commit f8ce25d

5 files changed

Lines changed: 46 additions & 33 deletions

File tree

src/main/java/io/contract_testing/contractcase/ContractVerifier.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package io.contract_testing.contractcase;
22

33
import io.contract_testing.contractcase.case_boundary.BoundaryResult;
4-
import io.contract_testing.contractcase.case_boundary.BoundarySuccess;
54
import io.contract_testing.contractcase.case_boundary.IInvokeCoreTest;
65
import io.contract_testing.contractcase.case_boundary.IRunTestCallback;
76
import io.contract_testing.contractcase.client.InternalVerifierClient;
7+
import io.contract_testing.contractcase.client.MaintainerLog;
88
import java.util.List;
99
import org.jetbrains.annotations.NotNull;
1010

@@ -19,18 +19,17 @@ public ContractVerifier(ContractCaseConfig config) {
1919
try {
2020
verification = new InternalVerifierClient(
2121
BoundaryConfigMapper.map(config, "VERIFICATION"),
22+
// TODO: Move the runTestCallback into the internals, maybe?
2223
new IRunTestCallback() {
2324
@Override
2425
public @NotNull BoundaryResult runTest(@NotNull String testName,
2526
@NotNull IInvokeCoreTest invoker) {
2627
// TODO replace this with something that knows about JUnit
2728
try {
28-
try {
29-
BoundaryResultMapper.mapVoid(invoker.verify());
30-
} catch (Exception e) {
31-
System.err.println(e);
32-
}
33-
return new BoundarySuccess();
29+
MaintainerLog.log("Invoking verifier");
30+
var result = invoker.verify();
31+
MaintainerLog.log("Verifier result was: " + result);
32+
return result;
3433
} catch (Exception e) {
3534
return BoundaryExceptionMapper.map(e);
3635
}
@@ -56,8 +55,8 @@ public List<ContractDescription> availableContractDescriptions() {
5655

5756
public void runVerification(ContractCaseConfig configOverrides) {
5857
try {
59-
this.verifier.runVerification(
60-
BoundaryConfigMapper.map(configOverrides, "VERIFICATION"));
58+
BoundaryResultMapper.mapVoid(this.verifier.runVerification(
59+
BoundaryConfigMapper.map(configOverrides, "VERIFICATION")));
6160
} catch (Throwable e) {
6261
BoundaryCrashReporter.handleAndRethrow(e);
6362
}

src/main/java/io/contract_testing/contractcase/client/AbstractRpcConnector.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ io.contract_testing.contractcase.case_boundary.BoundaryResult executeCallAndWait
8181
requestObserver.onNext(setId(builder, ConnectorOutgoingMapper.map(id)));
8282

8383
try {
84+
MaintainerLog.log("Waiting for: " + id);
8485
return ConnectorIncomingMapper.mapBoundaryResult(future.get(60, TimeUnit.SECONDS));
8586
} catch (TimeoutException e) {
87+
MaintainerLog.log("Timed out waiting for: " + id);
8688
if (errorStatus != null) {
8789
return new BoundaryFailure(
8890
BoundaryFailureKindConstants.CASE_CONFIGURATION_ERROR,
@@ -97,6 +99,7 @@ io.contract_testing.contractcase.case_boundary.BoundaryResult executeCallAndWait
9799
MaintainerLog.CONTRACT_CASE_JAVA_WRAPPER
98100
);
99101
} catch (ExecutionException e) {
102+
MaintainerLog.log("Execution exception waiting for: " + id + "\n" + e);
100103
return new BoundaryFailure(
101104
BoundaryFailureKindConstants.CASE_CORE_ERROR,
102105
"Failed waiting for a response '" + id + "':" + e.getMessage(),
@@ -112,7 +115,8 @@ io.contract_testing.contractcase.case_boundary.BoundaryResult executeCallAndWait
112115
}
113116

114117
void completeWait(String id, BoundaryResult result) {
115-
MaintainerLog.log("Completing wait for: " + id);
118+
MaintainerLog.log(
119+
"Completing wait for id '" + id + "', with result: " + result.getValueCase().name());
116120

117121
final var future = responseFutures.get(id);
118122
if (future == null) {
@@ -125,6 +129,7 @@ void completeWait(String id, BoundaryResult result) {
125129
}
126130

127131
void sendResponse(B builder, String id) {
132+
MaintainerLog.log("Sending (" + id + ") " + builder);
128133
requestObserver.onNext(setId(builder, ConnectorOutgoingMapper.map(id)));
129134
}
130135

src/main/java/io/contract_testing/contractcase/client/ContractResponseStreamObserver.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import io.contract_testing.contractcase.grpc.ContractCaseStream.StateHandlerHandle.Stage;
2020
import io.grpc.Status;
2121
import io.grpc.stub.StreamObserver;
22+
import java.util.concurrent.ExecutorService;
23+
import java.util.concurrent.Executors;
2224
import org.jetbrains.annotations.NotNull;
2325

2426
class ContractResponseStreamObserver<T extends AbstractMessage, B extends Builder<B>> implements
@@ -29,6 +31,7 @@ class ContractResponseStreamObserver<T extends AbstractMessage, B extends Builde
2931
private final IResultPrinter resultPrinter;
3032
private final ConfigHandle configHandle;
3133
private final IRunTestCallback runTestCallback;
34+
private final ExecutorService executor;
3235

3336

3437
public ContractResponseStreamObserver(
@@ -42,15 +45,17 @@ public ContractResponseStreamObserver(
4245
this.resultPrinter = resultPrinter;
4346
this.configHandle = configHandle;
4447
this.runTestCallback = runTestCallback;
48+
this.executor = Executors.newCachedThreadPool();
4549
}
4650

4751
@Override
48-
public void onNext(final ContractResponse note) {
52+
public void onNext(final ContractResponse coreResponse) {
4953
/* For when we receive messages from the server */
50-
final var requestId = ConnectorIncomingMapper.map(note.getId());
51-
switch (note.getKindCase()) {
54+
final var requestId = ConnectorIncomingMapper.map(coreResponse.getId());
55+
MaintainerLog.log("Received id(" + requestId + "), which was: " + coreResponse);
56+
switch (coreResponse.getKindCase()) {
5257
case RUN_STATE_HANDLER -> {
53-
final var stateHandlerRunRequest = note.getRunStateHandler();
58+
final var stateHandlerRunRequest = coreResponse.getRunStateHandler();
5459
var stateName = stateHandlerRunRequest.getStateHandlerHandle()
5560
.getHandle()
5661
.getValue();
@@ -69,7 +74,7 @@ public void onNext(final ContractResponse note) {
6974
);
7075
}
7176
case LOG_REQUEST -> {
72-
final var logRequest = note.getLogRequest();
77+
final var logRequest = coreResponse.getLogRequest();
7378
rpcConnector.sendResponse(
7479
ResultResponse.newBuilder().setResult(
7580
mapResult(
@@ -88,7 +93,7 @@ public void onNext(final ContractResponse note) {
8893
);
8994
}
9095
case PRINT_MATCH_ERROR_REQUEST -> {
91-
final var printMatchErrorRequest = note.getPrintMatchErrorRequest();
96+
final var printMatchErrorRequest = coreResponse.getPrintMatchErrorRequest();
9297
rpcConnector.sendResponse(
9398
mapResultResponse(
9499
resultPrinter.printMatchError(
@@ -103,20 +108,20 @@ public void onNext(final ContractResponse note) {
103108
mapResultResponse(
104109
resultPrinter.printMessageError(
105110
mapMessageErrorRequest(
106-
note.getPrintMessageErrorRequest()
111+
coreResponse.getPrintMessageErrorRequest()
107112
)
108113
)
109114
),
110115
requestId
111116
);
112117
}
113118
case PRINT_TEST_TITLE_REQUEST -> {
114-
final var printTestTitleRequest = note.getPrintTestTitleRequest();
119+
final var printTestTitleRequest = coreResponse.getPrintTestTitleRequest();
115120
rpcConnector.sendResponse(mapResultResponse(resultPrinter.printTestTitle(
116121
mapPrintableTestTitle(printTestTitleRequest))), requestId);
117122
}
118123
case TRIGGER_FUNCTION_REQUEST -> {
119-
var triggerFunctionRequest = note.getTriggerFunctionRequest();
124+
var triggerFunctionRequest = coreResponse.getTriggerFunctionRequest();
120125
var handle = ConnectorIncomingMapper.map(triggerFunctionRequest.getTriggerFunction()
121126
.getHandle());
122127
if (handle == null) {
@@ -139,24 +144,19 @@ public void onNext(final ContractResponse note) {
139144
);
140145
}
141146
case RESULT_RESPONSE -> {
142-
rpcConnector.completeWait(requestId, note.getResultResponse().getResult());
147+
rpcConnector.completeWait(requestId, coreResponse.getResultResponse().getResult());
143148
}
144149
case START_TEST_EVENT -> {
145-
var startTestEvent = note.getStartTestEvent();
146-
rpcConnector.sendResponse(
150+
var startTestEvent = coreResponse.getStartTestEvent();
151+
executor.submit(() -> rpcConnector.sendResponse(
147152
mapResultResponse(
148153
runTestCallback.runTest(
149154
startTestEvent.getTestName().getValue(),
150155
() -> rpcConnector.executeCallAndWait(rpcConnector.makeInvokeTest(
151156
startTestEvent.getInvokerId()))
152157
)),
153158
requestId
154-
);
155-
// TODO: Implement this
156-
throw new ContractCaseCoreError(
157-
"Received start test event incorrectly during a define contract",
158-
"Java Internal Connector"
159-
);
159+
));
160160
}
161161
case KIND_NOT_SET -> {
162162
throw new ContractCaseCoreError(
@@ -191,6 +191,8 @@ public void onError(final Throwable t) {
191191

192192
@Override
193193
public void onCompleted() {
194+
MaintainerLog.log("Closing listener and pool");
195+
executor.shutdown();
194196
}
195197

196198
@NotNull

src/main/java/io/contract_testing/contractcase/client/InternalVerifierClient.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public InternalVerifierClient(
2424
@NotNull ILogPrinter logPrinter,
2525
@NotNull IResultPrinter resultPrinter,
2626
@NotNull List<String> parentVersions) {
27-
27+
2828
ResultTypeConstantsCopy.validate();
2929
this.parentVersions = parentVersions;
3030
this.rpcConnector = new RpcForVerification(
@@ -45,16 +45,21 @@ public InternalVerifierClient(
4545
}
4646

4747
public @NotNull BoundaryResult runVerification(@NotNull ContractCaseBoundaryConfig configOverrides) {
48-
return rpcConnector.executeCallAndWait(VerificationRequest.newBuilder().setRunVerification(
48+
MaintainerLog.log("Verification run");
49+
var response = rpcConnector.executeCallAndWait(VerificationRequest.newBuilder()
50+
.setRunVerification(
4951
RunVerification.newBuilder()
5052
.setConfig(
5153
ConnectorOutgoingMapper.mapConfig(configOverrides)
5254
)
5355
)
5456
);
57+
MaintainerLog.log("Response from verification was: " + response.getResultType());
58+
return response;
5559
}
5660

5761
private BoundaryResult begin(ContractCaseConfig wireConfig) {
62+
MaintainerLog.log("Beginning verification setup");
5863
return rpcConnector.executeCallAndWait(VerificationRequest.newBuilder()
5964
.setBeginVerification(BeginVerificationRequest.newBuilder()
6065
.addAllCallerVersions(
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.contract_testing.contractcase.client;
22

3-
class MaintainerLog {
3+
public class MaintainerLog {
4+
45
public static final String CONTRACT_CASE_JAVA_WRAPPER = "ContractCase Java DSL";
5-
static void log(String s) {
6-
/*System.err.println(s);*/
6+
7+
public static void log(String s) {
8+
System.err.println(s);
79
}
810
}

0 commit comments

Comments
 (0)