Skip to content

Commit db279eb

Browse files
authored
context: add test for the scenario that Context's logger itself is using Context
This will break if 7f1ac34, 2f6e2c8, a3a5420 are reverted: ``` io.grpc.ContextTest > initContextWithCustomClassLoaderWithCustomLogger FAILED java.lang.ExceptionInInitializerError at io.grpc.ContextTest$LoadMeWithStaticTestingClassLoader.run(ContextTest.java:789) at io.grpc.ContextTest.initContextWithCustomClassLoaderWithCustomLogger(ContextTest.java:760) Caused by: java.lang.RuntimeException: Storage override had failed to initialize at io.grpc.Context.storage(Context.java:134) at io.grpc.Context.current(Context.java:163) at io.grpc.ContextTest$LoadMeWithStaticTestingClassLoader$1.publish(ContextTest.java:773) at java.util.logging.Logger.log(Logger.java:738) at java.util.logging.Logger.doLog(Logger.java:765) at java.util.logging.Logger.log(Logger.java:875) at io.grpc.Context.<clinit>(Context.java:122) ... 2 more ```
1 parent 7f1ac34 commit db279eb

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

context/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ description = 'gRPC: Context'
22

33
dependencies {
44
testCompile project(':grpc-testing')
5+
testCompile project(':grpc-core').sourceSets.test.output
56
signature "org.codehaus.mojo.signature:java16:1.1@signature"
67
}

context/src/test/java/io/grpc/ContextTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.logging.Level;
4646
import java.util.logging.LogRecord;
4747
import java.util.logging.Logger;
48+
import java.util.regex.Pattern;
4849
import org.junit.After;
4950
import org.junit.Before;
5051
import org.junit.Test;
@@ -745,4 +746,52 @@ public void expiredDeadlineShouldCancelContextImmediately() {
745746
assertTrue(context.isCancelled());
746747
assertThat(context.cancellationCause(), instanceOf(TimeoutException.class));
747748
}
749+
750+
/**
751+
* Tests initializing the {@link Context} class with a custom logger which uses Context's storage
752+
* when logging.
753+
*/
754+
@Test
755+
public void initContextWithCustomClassLoaderWithCustomLogger() throws Exception {
756+
StaticTestingClassLoader classLoader =
757+
new StaticTestingClassLoader(
758+
getClass().getClassLoader(),
759+
Pattern.compile("(io\\.grpc\\.Context.*)|(io\\.grpc\\.ThreadLocalContextStorage.*)"));
760+
Class<?> runnable =
761+
classLoader.loadClass(LoadMeWithStaticTestingClassLoader.class.getName());
762+
763+
((Runnable) runnable.getDeclaredConstructor().newInstance()).run();
764+
}
765+
766+
// UsedReflectively
767+
public static final class LoadMeWithStaticTestingClassLoader implements Runnable {
768+
@Override
769+
public void run() {
770+
Logger logger = Logger.getLogger(Context.class.getName());
771+
logger.setLevel(Level.ALL);
772+
Handler handler = new Handler() {
773+
@Override
774+
public void publish(LogRecord record) {
775+
Context ctx = Context.current();
776+
Context previous = ctx.attach();
777+
ctx.detach(previous);
778+
}
779+
780+
@Override
781+
public void flush() {
782+
}
783+
784+
@Override
785+
public void close() throws SecurityException {
786+
}
787+
};
788+
logger.addHandler(handler);
789+
790+
try {
791+
assertNotNull(Context.ROOT);
792+
} finally {
793+
logger.removeHandler(handler);
794+
}
795+
}
796+
}
748797
}

0 commit comments

Comments
 (0)