GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #2977 from wiremock/thread-local-xml · wiremock/wiremock@66c9b9e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • main/java/com/github/tomakehurst/wiremock/common/xml
      • Xml.java
    • test/java/com/github/tomakehurst/wiremock/common/xml
      • XmlTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/main/java/com/github/tomakehurst/wiremock/common/xml/Xml.java‎

Copy file name to clipboardExpand all lines: src/main/java/com/github/tomakehurst/wiremock/common/xml/Xml.java
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@ public class Xml {
4343

4444
public static final DocumentBuilderFactory DEFAULT_DOCUMENT_BUILDER_FACTORY =
4545
new DelegateDocumentBuilderFactory(newDocumentBuilderFactory()) {
46-
private final DocumentBuilder documentBuilder;
46+
// DocumentBuilder is NOT thread safe.
47+
private final ThreadLocal<DocumentBuilder> documentBuilder =
48+
ThreadLocal.withInitial(this::build);
4749

48-
{
49-
DocumentBuilder db;
50+
private DocumentBuilder build() {
5051
try {
51-
db = delegate.newDocumentBuilder();
52+
return delegate.newDocumentBuilder();
5253
} catch (ParserConfigurationException e) {
53-
db = throwUnchecked(e, DocumentBuilder.class);
54+
return throwUnchecked(e, DocumentBuilder.class);
5455
}
55-
documentBuilder = db;
5656
}
5757

5858
@Override
5959
public DocumentBuilder newDocumentBuilder() {
60-
return documentBuilder;
60+
return documentBuilder.get();
6161
}
6262
};
6363

Collapse file

‎src/test/java/com/github/tomakehurst/wiremock/common/xml/XmlTest.java‎

Copy file name to clipboardExpand all lines: src/test/java/com/github/tomakehurst/wiremock/common/xml/XmlTest.java
+21-1Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2021 Thomas Akehurst
2+
* Copyright (C) 2020-2025 Thomas Akehurst
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,10 @@
1818
import static com.github.tomakehurst.wiremock.testsupport.WireMatchers.equalsMultiLine;
1919
import static org.hamcrest.MatcherAssert.assertThat;
2020
import static org.hamcrest.Matchers.is;
21+
import static org.hamcrest.Matchers.notNullValue;
2122

2223
import com.github.tomakehurst.wiremock.common.ListOrSingle;
24+
import java.util.stream.IntStream;
2325
import org.junit.jupiter.api.Test;
2426
import org.junit.jupiter.api.condition.DisabledForJreRange;
2527
import org.junit.jupiter.api.condition.JRE;
@@ -131,4 +133,22 @@ public void printsNamespacedXmlWhenPrefixDeclarationNotInScope() {
131133

132134
assertThat(xmlNodes.toString(), is("<fl:inner fl:code=\"D1\" id=\"123\">Innards</fl:inner>"));
133135
}
136+
137+
@Test
138+
void canReadConcurrently() {
139+
String xml =
140+
"<?xml version=\"1.0\"?>\n"
141+
+ "<things xmlns:s=\"https://stuff.biz\" id=\"1\">\n"
142+
+ " <stuff id=\"1\"/>\n"
143+
+ " <fl:fluff xmlns:fl=\"https://fluff.abc\" id=\"2\">\n"
144+
+ " <fl:inner id=\"123\" fl:code=\"D1\">Innards</fl:inner>\n"
145+
+ " <fl:inner>More Innards</fl:inner>\n"
146+
+ " </fl:fluff>\n"
147+
+ "</things>";
148+
int iterations = 10000;
149+
IntStream.range(0, iterations)
150+
.parallel()
151+
.mapToObj(i -> Xml.read(xml))
152+
.forEach(document -> assertThat(document, notNullValue()));
153+
}
134154
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.