@TestScoped does not play nice with thread pools

6 views
Skip to first unread message

Timofey Basanov

unread,
May 25, 2017, 2:23:05 AM5/25/17
to guiceber...@googlegroups.com
Hello,


I use GuiceBerry to test interceptors for gRPC server.
One of the quirks of an in-process gRPC server is that sometimes it enforces usage of a static internal thread pool. As a result two tests may inadvertently share the same thread pool.
It would be nice to have a GuiceBerry API to allow to set a test id for a current thread.

// T1: on the main test thread
t = Thread.currentThread();

// T2: on a thread in a thread pool passing t via some other means
TestScope.resetParentThread(t);
// now this thread share TestScope with the parent thread

TestScope depends on InheritableThreadState in com.google.guiceberry.GuiceBerryUniverse#currentTestDescriptionThreadLocal as a result once a test used a thread any subsequent test would use test-scoped objects from a previous test.
Sometimes I want to tell GuiceBerry to reset the internal test scope ThreadLocal and set it to a specific test.

The simplest way to do this is to call currentTestDescriptionThreadLocal.get() to get a known good state and pass to currentTestDescriptionThreadLocal.set() when a thread state should be reset.
I have not found a way to do this without reflection.


--
Timothy Basanov
Reply all
Reply to author
Forward
0 new messages