1717
1818import static com .google .common .truth .TruthJUnit .assume ;
1919
20+ import com .google .common .base .Joiner ;
21+ import com .google .common .base .Strings ;
22+ import com .google .common .collect .ImmutableSet ;
23+ import java .io .IOException ;
24+ import java .nio .file .Files ;
25+ import java .nio .file .Path ;
26+ import java .nio .file .Paths ;
27+ import java .util .Set ;
28+ import java .util .logging .FileHandler ;
29+ import java .util .logging .Handler ;
2030import java .util .logging .Level ;
2131import java .util .logging .Logger ;
22- import org .junit .rules .ExternalResource ;
32+ import java .util .logging .SimpleFormatter ;
33+ import org .junit .rules .TestRule ;
34+ import org .junit .runner .Description ;
35+ import org .junit .runners .model .Statement ;
2336
2437/**
25- * Simple JUnit rule to start and stop the target test environment.
38+ * JUnit rule to start and stop the target test environment.
2639 *
2740 * <p>The environment can be specified via the system property {@code bigtable.env}. The choices
2841 * are:
3750 * </ul>
3851 *
3952 * <p>By default, {@code emulator} will be used
53+ *
54+ * <p>Also, when the system property {@code bigtable.grpc-log-dir} is set, it will enable fine
55+ * grained gRPC logging to the configured path.
4056 */
41- public class TestEnvRule extends ExternalResource {
57+ public class TestEnvRule implements TestRule {
4258
4359 private static final Logger LOGGER = Logger .getLogger (TestEnvRule .class .getName ());
60+ private static final String BIGTABLE_GRPC_LOG_DIR = System .getProperty ("bigtable.grpc-log-dir" );
4461 private static final String BIGTABLE_EMULATOR_HOST_ENV_VAR = "BIGTABLE_EMULATOR_HOST" ;
4562 private static final String ENV_PROPERTY = "bigtable.env" ;
4663 private static final String env = System .getProperty (ENV_PROPERTY , "emulator" );
4764
4865 private AbstractTestEnv testEnv ;
4966
67+ private Handler grpcLogHandler ;
68+ private static final Set <String > GRPC_LOGGER_NAMES =
69+ ImmutableSet .of ("io.grpc" , "io.grpc.netty.shaded" );
70+
5071 @ Override
51- protected void before () throws Throwable {
72+ public Statement apply (final Statement base , final Description description ) {
73+ return new Statement () {
74+ public void evaluate () throws Throwable {
75+ TestEnvRule .this .before (description );
76+
77+ try {
78+ base .evaluate ();
79+ } finally {
80+ TestEnvRule .this .after ();
81+ }
82+ }
83+ };
84+ }
85+
86+ protected void before (Description description ) throws Throwable {
5287 assume ()
5388 .withMessage (
5489 "Integration tests can't run with the BIGTABLE_EMULATOR_HOST environment variable set"
5590 + ". Please use the emulator-it maven profile instead" )
5691 .that (System .getenv ())
5792 .doesNotContainKey (BIGTABLE_EMULATOR_HOST_ENV_VAR );
5893
94+ configureLogging (description );
95+
5996 switch (env ) {
6097 case "emulator" :
6198 testEnv = EmulatorEnv .createBundled ();
@@ -72,8 +109,29 @@ protected void before() throws Throwable {
72109 testEnv .start ();
73110 }
74111
75- @ Override
76- protected void after () {
112+ private void configureLogging (Description description ) throws IOException {
113+ if (Strings .isNullOrEmpty (BIGTABLE_GRPC_LOG_DIR )) {
114+ return ;
115+ }
116+
117+ Files .createDirectories (Paths .get (BIGTABLE_GRPC_LOG_DIR ));
118+
119+ String basename =
120+ Joiner .on ("-" ).useForNull ("" ).join (description .getClassName (), description .getMethodName ());
121+ Path logPath = Paths .get (BIGTABLE_GRPC_LOG_DIR , basename + ".log" );
122+
123+ grpcLogHandler = new FileHandler (logPath .toString ());
124+ grpcLogHandler .setFormatter (new SimpleFormatter ());
125+ grpcLogHandler .setLevel (Level .ALL );
126+
127+ for (String grpcLoggerName : GRPC_LOGGER_NAMES ) {
128+ Logger logger = Logger .getLogger (grpcLoggerName );
129+ logger .setLevel (Level .ALL );
130+ logger .addHandler (grpcLogHandler );
131+ }
132+ }
133+
134+ private void after () {
77135 try {
78136 env ().cleanUpStale ();
79137 } catch (Exception e ) {
@@ -86,6 +144,19 @@ protected void after() {
86144 LOGGER .log (Level .WARNING , "Failed to stop the environment" , e );
87145 }
88146 testEnv = null ;
147+ teardownLogging ();
148+ }
149+
150+ private void teardownLogging () {
151+ if (grpcLogHandler == null ) {
152+ return ;
153+ }
154+
155+ for (String grpcLoggerName : GRPC_LOGGER_NAMES ) {
156+ Logger .getLogger (grpcLoggerName ).removeHandler (grpcLogHandler );
157+ }
158+ grpcLogHandler .flush ();
159+ grpcLogHandler = null ;
89160 }
90161
91162 public AbstractTestEnv env () {
0 commit comments