From 859ad761bb9abd33203500b3874b88cafdf8b4be Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Tue, 4 Aug 2020 13:39:52 +0200 Subject: [PATCH] Fix Broken Stream Close in writeRawValue (#60625) (#60644) Small oversight in #56078 that only showed up during backporting where a stream copy was turned from a non-closing to a closing one. Enhanced part of a test in this PR to make it show up in master also even though we practically never use this method with stream targets that actually close. --- .../common/xcontent/BaseXContentTestCase.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/server/src/test/java/org/elasticsearch/common/xcontent/BaseXContentTestCase.java b/server/src/test/java/org/elasticsearch/common/xcontent/BaseXContentTestCase.java index 8326dbb9889..6c4fde611a0 100644 --- a/server/src/test/java/org/elasticsearch/common/xcontent/BaseXContentTestCase.java +++ b/server/src/test/java/org/elasticsearch/common/xcontent/BaseXContentTestCase.java @@ -75,6 +75,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import static java.util.Collections.emptyMap; import static java.util.Collections.singletonMap; @@ -947,11 +948,18 @@ public abstract class BaseXContentTestCase extends ESTestCase { assertNull(parser.nextToken()); } - os = new ByteArrayOutputStream(); + final AtomicBoolean closed = new AtomicBoolean(false); + os = new ByteArrayOutputStream() { + @Override + public void close() { + closed.set(true); + } + }; try (XContentGenerator generator = xcontentType().xContent().createGenerator(os)) { generator.writeStartObject(); generator.writeFieldName("test"); generator.writeRawValue(new BytesArray(rawData).streamInput(), source.type()); + assertFalse("Generator should not have closed the output stream", closed.get()); generator.writeEndObject(); }