diff options
Diffstat (limited to 'javatests/com/google/gerrit/server/logging/LoggingContextAwareExecutorServiceTest.java')
-rw-r--r-- | javatests/com/google/gerrit/server/logging/LoggingContextAwareExecutorServiceTest.java | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/javatests/com/google/gerrit/server/logging/LoggingContextAwareExecutorServiceTest.java b/javatests/com/google/gerrit/server/logging/LoggingContextAwareExecutorServiceTest.java new file mode 100644 index 0000000000..5117c01971 --- /dev/null +++ b/javatests/com/google/gerrit/server/logging/LoggingContextAwareExecutorServiceTest.java @@ -0,0 +1,71 @@ +// Copyright (C) 2018 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.server.logging; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.truth.Expect; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import org.junit.Rule; +import org.junit.Test; + +public class LoggingContextAwareExecutorServiceTest { + @Rule public final Expect expect = Expect.create(); + + @Test + public void loggingContextPropagationToBackgroundThread() throws Exception { + assertThat(LoggingContext.getInstance().getTags().isEmpty()).isTrue(); + assertForceLogging(false); + try (TraceContext traceContext = TraceContext.open().forceLogging().addTag("foo", "bar")) { + SortedMap<String, SortedSet<Object>> tagMap = LoggingContext.getInstance().getTags().asMap(); + assertThat(tagMap.keySet()).containsExactly("foo"); + assertThat(tagMap.get("foo")).containsExactly("bar"); + assertForceLogging(true); + + ExecutorService executor = + new LoggingContextAwareExecutorService(Executors.newFixedThreadPool(1)); + executor + .submit( + () -> { + // Verify that the tags and force logging flag have been propagated to the new + // thread. + SortedMap<String, SortedSet<Object>> threadTagMap = + LoggingContext.getInstance().getTags().asMap(); + expect.that(threadTagMap.keySet()).containsExactly("foo"); + expect.that(threadTagMap.get("foo")).containsExactly("bar"); + expect + .that(LoggingContext.getInstance().shouldForceLogging(null, null, false)) + .isTrue(); + }) + .get(); + + // Verify that tags and force logging flag in the outer thread are still set. + tagMap = LoggingContext.getInstance().getTags().asMap(); + assertThat(tagMap.keySet()).containsExactly("foo"); + assertThat(tagMap.get("foo")).containsExactly("bar"); + assertForceLogging(true); + } + assertThat(LoggingContext.getInstance().getTags().isEmpty()).isTrue(); + assertForceLogging(false); + } + + private void assertForceLogging(boolean expected) { + assertThat(LoggingContext.getInstance().shouldForceLogging(null, null, false)) + .isEqualTo(expected); + } +} |