Ensure defautls are also serialized across the network

This commit is contained in:
Simon Willnauer 2016-01-21 17:12:42 +01:00
parent e8b9880211
commit 6ec13e62a7
2 changed files with 61 additions and 1 deletions

View File

@ -127,7 +127,7 @@ public final class ThreadContext implements Closeable, Writeable<ThreadContext.T
@Override
public void writeTo(StreamOutput out) throws IOException {
threadLocal.get().writeTo(out);
threadLocal.get().writeTo(out, defaultHeader);
}
@Override
@ -284,6 +284,18 @@ public final class ThreadContext implements Closeable, Writeable<ThreadContext.T
@Override
public void writeTo(StreamOutput out) throws IOException {
throw new UnsupportedOperationException("use the other write to");
}
public void writeTo(StreamOutput out, Map<String, String> defaultHeaders) throws IOException {
final Map<String, String> headers;
if (defaultHeaders.isEmpty()) {
headers = this.headers;
} else {
headers = new HashMap<>(defaultHeaders);
headers.putAll(this.headers);
}
int keys = headers.size();
out.writeVInt(keys);
for (Map.Entry<String, String> entry : headers.entrySet()) {

View File

@ -161,6 +161,54 @@ public class ThreadContextTests extends ESTestCase {
assertEquals("1", threadContext.getHeader("default"));
}
public void testSerializeInDifferentContext() throws IOException {
BytesStreamOutput out = new BytesStreamOutput();
{
Settings build = Settings.builder().put("request.headers.default", "1").build();
ThreadContext threadContext = new ThreadContext(build);
threadContext.putHeader("foo", "bar");
threadContext.putTransient("ctx.foo", new Integer(1));
assertEquals("bar", threadContext.getHeader("foo"));
assertNotNull(threadContext.getTransient("ctx.foo"));
assertEquals("1", threadContext.getHeader("default"));
threadContext.writeTo(out);
}
{
Settings otherSettings = Settings.builder().put("request.headers.default", "5").build();
ThreadContext otherhreadContext = new ThreadContext(otherSettings);
otherhreadContext.readHeaders(StreamInput.wrap(out.bytes()));
assertEquals("bar", otherhreadContext.getHeader("foo"));
assertNull(otherhreadContext.getTransient("ctx.foo"));
assertEquals("1", otherhreadContext.getHeader("default"));
}
}
public void testSerializeInDifferentContextNoDefaults() throws IOException {
BytesStreamOutput out = new BytesStreamOutput();
{
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
threadContext.putHeader("foo", "bar");
threadContext.putTransient("ctx.foo", new Integer(1));
assertEquals("bar", threadContext.getHeader("foo"));
assertNotNull(threadContext.getTransient("ctx.foo"));
assertNull(threadContext.getHeader("default"));
threadContext.writeTo(out);
}
{
Settings otherSettings = Settings.builder().put("request.headers.default", "5").build();
ThreadContext otherhreadContext = new ThreadContext(otherSettings);
otherhreadContext.readHeaders(StreamInput.wrap(out.bytes()));
assertEquals("bar", otherhreadContext.getHeader("foo"));
assertNull(otherhreadContext.getTransient("ctx.foo"));
assertEquals("5", otherhreadContext.getHeader("default"));
}
}
public void testCanResetDefault() {
Settings build = Settings.builder().put("request.headers.default", "1").build();
ThreadContext threadContext = new ThreadContext(build);