mirror of https://github.com/grpc/grpc-java.git
context: add test for the scenario that Context's logger itself is using Context
This will break if7f1ac34d41
,2f6e2c87ab
,a3a5420922
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 ```
This commit is contained in:
parent
7f1ac34d41
commit
db279eb3c1
|
@ -2,5 +2,6 @@ description = 'gRPC: Context'
|
|||
|
||||
dependencies {
|
||||
testCompile project(':grpc-testing')
|
||||
testCompile project(':grpc-core').sourceSets.test.output
|
||||
signature "org.codehaus.mojo.signature:java16:1.1@signature"
|
||||
}
|
||||
|
|
|
@ -45,6 +45,7 @@ import java.util.logging.Handler;
|
|||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.regex.Pattern;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -745,4 +746,52 @@ public class ContextTest {
|
|||
assertTrue(context.isCancelled());
|
||||
assertThat(context.cancellationCause(), instanceOf(TimeoutException.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests initializing the {@link Context} class with a custom logger which uses Context's storage
|
||||
* when logging.
|
||||
*/
|
||||
@Test
|
||||
public void initContextWithCustomClassLoaderWithCustomLogger() throws Exception {
|
||||
StaticTestingClassLoader classLoader =
|
||||
new StaticTestingClassLoader(
|
||||
getClass().getClassLoader(),
|
||||
Pattern.compile("(io\\.grpc\\.Context.*)|(io\\.grpc\\.ThreadLocalContextStorage.*)"));
|
||||
Class<?> runnable =
|
||||
classLoader.loadClass(LoadMeWithStaticTestingClassLoader.class.getName());
|
||||
|
||||
((Runnable) runnable.getDeclaredConstructor().newInstance()).run();
|
||||
}
|
||||
|
||||
// UsedReflectively
|
||||
public static final class LoadMeWithStaticTestingClassLoader implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
Logger logger = Logger.getLogger(Context.class.getName());
|
||||
logger.setLevel(Level.ALL);
|
||||
Handler handler = new Handler() {
|
||||
@Override
|
||||
public void publish(LogRecord record) {
|
||||
Context ctx = Context.current();
|
||||
Context previous = ctx.attach();
|
||||
ctx.detach(previous);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SecurityException {
|
||||
}
|
||||
};
|
||||
logger.addHandler(handler);
|
||||
|
||||
try {
|
||||
assertNotNull(Context.ROOT);
|
||||
} finally {
|
||||
logger.removeHandler(handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue