From c6b12f7479b35c33005b6530cc965ef1b3a6611b Mon Sep 17 00:00:00 2001 From: hanbj Date: Sat, 5 Aug 2017 17:07:59 +0800 Subject: [PATCH 1/5] bug : ThreadContext----->ThreadContextStruct:putHeaders() --- .../elasticsearch/common/util/concurrent/ThreadContext.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/util/concurrent/ThreadContext.java b/core/src/main/java/org/elasticsearch/common/util/concurrent/ThreadContext.java index 1ce119636f734..95c08e8889857 100644 --- a/core/src/main/java/org/elasticsearch/common/util/concurrent/ThreadContext.java +++ b/core/src/main/java/org/elasticsearch/common/util/concurrent/ThreadContext.java @@ -407,11 +407,10 @@ private ThreadContextStruct putHeaders(Map headers) { if (headers.isEmpty()) { return this; } else { - final Map newHeaders = new HashMap<>(); + final Map newHeaders = new HashMap<>(this.requestHeaders); for (Map.Entry entry : headers.entrySet()) { putSingleHeader(entry.getKey(), entry.getValue(), newHeaders); } - newHeaders.putAll(this.requestHeaders); return new ThreadContextStruct(newHeaders, responseHeaders, transientHeaders, isSystemContext); } } From 2d0eaa7e177aa07dee57b795749e452d757249c0 Mon Sep 17 00:00:00 2001 From: hanbj Date: Tue, 15 Aug 2017 10:32:54 +0800 Subject: [PATCH 2/5] test putHeaders --- .../util/concurrent/ThreadContextTests.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java b/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java index bee56c229c02a..a20f5675a944d 100644 --- a/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java +++ b/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java @@ -30,10 +30,7 @@ import java.util.Map; import java.util.function.Supplier; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.sameInstance; +import static org.hamcrest.Matchers.*; public class ThreadContextTests extends ESTestCase { @@ -215,8 +212,8 @@ public void testResponseHeaders() { public void testCopyHeaders() { Settings build = Settings.builder().put("request.headers.default", "1").build(); ThreadContext threadContext = new ThreadContext(build); - threadContext.copyHeaders(Collections.emptyMap().entrySet()); - threadContext.copyHeaders(Collections.singletonMap("foo", "bar").entrySet()); + threadContext.copyHeaders(Collections.emptyMap().entrySet()); + threadContext.copyHeaders(Collections.singletonMap("foo", "bar").entrySet()); assertEquals("bar", threadContext.getHeader("foo")); } @@ -443,7 +440,7 @@ public void onAfter() { assertEquals("bar", threadContext.getHeader("foo")); assertEquals("bar_transient", threadContext.getTransient("foo")); assertNotNull(threadContext.getTransient("failure")); - assertEquals("exception from doRun", ((RuntimeException)threadContext.getTransient("failure")).getMessage()); + assertEquals("exception from doRun", ((RuntimeException) threadContext.getTransient("failure")).getMessage()); assertFalse(threadContext.isDefaultContext()); threadContext.putTransient("after", "after"); } @@ -604,7 +601,7 @@ protected void doRun() throws Exception { public void testMarkAsSystemContext() throws IOException { try (ThreadContext threadContext = new ThreadContext(Settings.EMPTY)) { assertFalse(threadContext.isSystemContext()); - try(ThreadContext.StoredContext context = threadContext.stashContext()){ + try (ThreadContext.StoredContext context = threadContext.stashContext()) { assertFalse(threadContext.isSystemContext()); threadContext.markAsSystemContext(); assertTrue(threadContext.isSystemContext()); @@ -613,6 +610,20 @@ public void testMarkAsSystemContext() throws IOException { } } + public void testPutHeaders() { + Settings build = Settings.builder().put("request.headers.default", "1").build(); + ThreadContext threadContext = new ThreadContext(build); + threadContext.putHeader(Collections.emptyMap()); + threadContext.putHeader(Collections.singletonMap("foo", "bar")); + assertEquals("bar", threadContext.getHeader("foo")); + try { + threadContext.putHeader(Collections.singletonMap("foo", "boom")); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + assertEquals("value for key [foo] already present", e.getMessage()); + } + } + /** * Sometimes wraps a Runnable in an AbstractRunnable. */ From eaba57732150472949cc3751b8c2a572c9897bed Mon Sep 17 00:00:00 2001 From: hanbj Date: Tue, 15 Aug 2017 10:36:46 +0800 Subject: [PATCH 3/5] delete println --- .../elasticsearch/common/util/concurrent/ThreadContextTests.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java b/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java index a20f5675a944d..13e9f4604cb23 100644 --- a/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java +++ b/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java @@ -619,7 +619,6 @@ public void testPutHeaders() { try { threadContext.putHeader(Collections.singletonMap("foo", "boom")); } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); assertEquals("value for key [foo] already present", e.getMessage()); } } From b57ecbcbd7f5baef7263c1724b6f290263d900a7 Mon Sep 17 00:00:00 2001 From: hanbj Date: Thu, 17 Aug 2017 16:08:39 +0800 Subject: [PATCH 4/5] update wildcard imports and instead of try/catch. --- .../common/util/concurrent/ThreadContextTests.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java b/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java index 13e9f4604cb23..8ebee85959663 100644 --- a/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java +++ b/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java @@ -29,8 +29,10 @@ import java.util.List; import java.util.Map; import java.util.function.Supplier; - -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.sameInstance; public class ThreadContextTests extends ESTestCase { @@ -616,11 +618,8 @@ public void testPutHeaders() { threadContext.putHeader(Collections.emptyMap()); threadContext.putHeader(Collections.singletonMap("foo", "bar")); assertEquals("bar", threadContext.getHeader("foo")); - try { - threadContext.putHeader(Collections.singletonMap("foo", "boom")); - } catch (IllegalArgumentException e) { - assertEquals("value for key [foo] already present", e.getMessage()); - } + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> threadContext.putHeader(Collections.singletonMap("foo", "boom"))); + assertEquals("value for key [foo] already present", e.getMessage()); } /** From 09e46a6b58edfa0ef07cb537dd7d351f4af9b0f6 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 9 Oct 2017 08:23:30 -0700 Subject: [PATCH 5/5] Fix line length --- .../common/util/concurrent/ThreadContextTests.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java b/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java index 8ebee85959663..e71efa46424b2 100644 --- a/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java +++ b/core/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java @@ -618,7 +618,8 @@ public void testPutHeaders() { threadContext.putHeader(Collections.emptyMap()); threadContext.putHeader(Collections.singletonMap("foo", "bar")); assertEquals("bar", threadContext.getHeader("foo")); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> threadContext.putHeader(Collections.singletonMap("foo", "boom"))); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> + threadContext.putHeader(Collections.singletonMap("foo", "boom"))); assertEquals("value for key [foo] already present", e.getMessage()); }