Skip to content

Commit a21bf1c

Browse files
chore: improve tests (#81)
* clean up test configs * Enable fine grained gRPC logging for integration tests
1 parent da5b6eb commit a21bf1c

File tree

2 files changed

+87
-29
lines changed

2 files changed

+87
-29
lines changed

google-cloud-bigtable/pom.xml

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
<configuration>
223223
<systemPropertyVariables>
224224
<bigtable.env>emulator</bigtable.env>
225+
<bigtable.grpc-log-dir>${project.build.directory}/test-grpc-logs/emulator-it</bigtable.grpc-log-dir>
225226
</systemPropertyVariables>
226227
<includes>
227228
<include>com.google.cloud.bigtable.**.it.*IT</include>
@@ -254,6 +255,7 @@
254255
<bigtable.env>cloud</bigtable.env>
255256
<bigtable.data-endpoint>${bigtable.cfe-data-endpoint}</bigtable.data-endpoint>
256257
<bigtable.admin-endpoint>${bigtable.cfe-admin-endpoint}</bigtable.admin-endpoint>
258+
<bigtable.grpc-log-dir>${project.build.directory}/test-grpc-logs/prod-it</bigtable.grpc-log-dir>
257259
</systemPropertyVariables>
258260
<includes>
259261
<include>com.google.cloud.bigtable.**.it.*IT</include>
@@ -286,6 +288,7 @@
286288
<bigtable.env>cloud</bigtable.env>
287289
<bigtable.data-endpoint>${bigtable.directpath-data-endpoint}</bigtable.data-endpoint>
288290
<bigtable.admin-endpoint>${bigtable.directpath-admin-endpoint}</bigtable.admin-endpoint>
291+
<bigtable.grpc-log-dir>${project.build.directory}/test-grpc-logs/directpath-it</bigtable.grpc-log-dir>
289292
</systemPropertyVariables>
290293
<!-- Enable directpath for bigtable -->
291294
<environmentVariables>
@@ -340,42 +343,26 @@
340343
</plugin>
341344

342345
<plugin>
343-
<groupId>org.apache.maven.plugins</groupId>
344346
<artifactId>maven-failsafe-plugin</artifactId>
345-
<version>3.0.0-M3</version>
346347
<configuration>
347348
<parallel>classes</parallel>
348-
<perCoreThreadCount>true</perCoreThreadCount>
349-
<threadCount>2</threadCount>
349+
<forkCount>2C</forkCount>
350+
<reuseForks>true</reuseForks>
351+
350352
<trimStackTrace>false</trimStackTrace>
351353
</configuration>
352-
<dependencies>
353-
<dependency>
354-
<groupId>org.apache.maven.surefire</groupId>
355-
<artifactId>surefire-junit47</artifactId>
356-
<version>3.0.0-M3</version>
357-
</dependency>
358-
</dependencies>
359354
</plugin>
360355

361356
<plugin>
362357
<artifactId>maven-surefire-plugin</artifactId>
363-
<version>3.0.0-M3</version>
364-
<dependencies>
365-
<dependency>
366-
<groupId>org.apache.maven.surefire</groupId>
367-
<artifactId>surefire-junit47</artifactId>
368-
<version>3.0.0-M3</version>
369-
</dependency>
370-
</dependencies>
371358

372359
<configuration>
360+
<parallel>classes</parallel>
361+
<forkCount>2C</forkCount>
362+
<reuseForks>true</reuseForks>
363+
373364
<!-- enable the ability to skip unit tests, while running integration tests -->
374365
<skipTests>${skipUnitTests}</skipTests>
375-
<!--
376-
TODO(igorbernstein): enable parallel tests once the generate client tests use unique
377-
names for the mock server
378-
-->
379366
<trimStackTrace>false</trimStackTrace>
380367
</configuration>
381368
</plugin>

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/TestEnvRule.java

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,25 @@
1717

1818
import 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;
2030
import java.util.logging.Level;
2131
import 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:
@@ -37,25 +50,49 @@
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

Comments
 (0)