From 0df139edb329efbb7dfad769ef500ea48f6818b7 Mon Sep 17 00:00:00 2001 From: Dirk Bolte Date: Fri, 1 Mar 2024 10:34:55 +0100 Subject: [PATCH] bug: prevent java.util.ConcurrentModificationException in Transaction (#120) - copy context set to work on individual entity - updated demo documentation ## References #119 ## Submitter checklist - [x] Recommended: Join [WireMock Slack](https://slack.wiremock.org/) to get any help in `#help-contributing` or a project-specific channel like `#wiremock-java` - [x] The PR request is well described and justified, including the body and the references - [x] The PR title represents the desired changelog entry - [x] The repository's code style is followed (see the contributing guide) - [x] Test coverage that demonstrates that the change works as expected - [x] For new features, there's necessary documentation in this pull request or in a subsequent PR to [wiremock.org](https://github.com/wiremock/wiremock.org) --- demo/README.md | 24 +++++++++++++++++-- demo/docker_container_test.http | 8 +++++++ .../extensions/TransactionEventListener.java | 1 - .../state/internal/TransactionManager.java | 3 ++- 4 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 demo/docker_container_test.http diff --git a/demo/README.md b/demo/README.md index e2bcf94..dda7683 100644 --- a/demo/README.md +++ b/demo/README.md @@ -89,11 +89,31 @@ cd wiremock-state-extension ### Step 2: Build the Project +From directory of this README: + +```shell +cd .. ./gradlew build +``` + +### Step 3: download wiremock standalone + +From directory of this README: + +```shell +cd .. +curl -o build/libs/wiremock-standalone-3.4.1.jar https://repo1.maven.org/maven2/org/wiremock/wiremock-standalone/3.4.1/wiremock-standalone-3.4.1.jar +``` + +### Step 4: Start WireMock with the State Extension -### Start WireMock with the State Extension +From directory of this README: + +```shell +cd .. +java -cp build/libs/wiremock-state-extension-standalone-0.6.0-SNAPSHOT.jar:build/libs/wiremock-standalone-3.4.1.jar wiremock.Run --verbose --global-response-templating --root-dir demo/stubs +``` -java -jar build/libs/wiremock-standalone-*.jar --extensions="com.github.tomakehurst.wiremock.extension.StateExtension" This command starts WireMock with the State Extension enabled. ### Access the Demo diff --git a/demo/docker_container_test.http b/demo/docker_container_test.http new file mode 100644 index 0000000..842cf50 --- /dev/null +++ b/demo/docker_container_test.http @@ -0,0 +1,8 @@ +### empty get + +POST http://localhost:8080//api/v1/todolist + +{ + "title": "a", + "description": "as" +} diff --git a/src/main/java/org/wiremock/extensions/state/extensions/TransactionEventListener.java b/src/main/java/org/wiremock/extensions/state/extensions/TransactionEventListener.java index 6019ab1..c703af9 100644 --- a/src/main/java/org/wiremock/extensions/state/extensions/TransactionEventListener.java +++ b/src/main/java/org/wiremock/extensions/state/extensions/TransactionEventListener.java @@ -54,6 +54,5 @@ public void afterComplete(ServeEvent serveEvent, Parameters parameters) { String requestId = serveEvent.getId().toString(); var contextNames = transactionManager.getContextNamesByRequestId(requestId); contextNames.forEach((contextName) -> transactionManager.deleteTransaction(requestId, contextName)); - } } diff --git a/src/main/java/org/wiremock/extensions/state/internal/TransactionManager.java b/src/main/java/org/wiremock/extensions/state/internal/TransactionManager.java index 662aaf6..23cac73 100644 --- a/src/main/java/org/wiremock/extensions/state/internal/TransactionManager.java +++ b/src/main/java/org/wiremock/extensions/state/internal/TransactionManager.java @@ -19,6 +19,7 @@ import org.wiremock.extensions.state.internal.model.Transaction; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.function.Consumer; @@ -59,7 +60,7 @@ public Set getContextNamesByRequestId(String requestId) { var transactionKey = createTransactionKey(requestId); synchronized (store) { @SuppressWarnings("unchecked") var requestTransactions = store.get(transactionKey).map(it -> (Map) it).orElse(new HashMap<>()); - return requestTransactions.keySet(); + return new HashSet<>(requestTransactions.keySet()); } }