Skip to content

Commit 67351c0

Browse files
authored
gcp-observability: Optimize GcpObservabilityTest.enableObservability execution time (#11783)
1 parent bf8eb24 commit 67351c0

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

gcp-observability/src/main/java/io/grpc/gcp/observability/GcpObservability.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ static GcpObservability grpcInit(
127127
/** Un-initialize/shutdown grpc-observability. */
128128
@Override
129129
public void close() {
130+
closeWithSleepTime(2 * METRICS_EXPORT_INTERVAL, TimeUnit.SECONDS);
131+
}
132+
133+
/**
134+
* Method to close along with sleep time explicitly.
135+
*
136+
* @param sleepTime sleepTime
137+
*/
138+
void closeWithSleepTime(long sleepTime, TimeUnit timeUnit) {
130139
synchronized (GcpObservability.class) {
131140
if (instance == null) {
132141
throw new IllegalStateException("GcpObservability already closed!");
@@ -135,8 +144,7 @@ public void close() {
135144
if (config.isEnableCloudMonitoring() || config.isEnableCloudTracing()) {
136145
try {
137146
// Sleeping before shutdown to ensure all metrics and traces are flushed
138-
Thread.sleep(
139-
TimeUnit.MILLISECONDS.convert(2 * METRICS_EXPORT_INTERVAL, TimeUnit.SECONDS));
147+
timeUnit.sleep(sleepTime);
140148
} catch (InterruptedException e) {
141149
Thread.currentThread().interrupt();
142150
logger.log(Level.SEVERE, "Caught exception during sleep", e);

gcp-observability/src/test/java/io/grpc/gcp/observability/GcpObservabilityTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import io.opencensus.trace.samplers.Samplers;
4646
import java.io.IOException;
4747
import java.util.List;
48+
import java.util.concurrent.TimeUnit;
4849
import java.util.regex.Pattern;
4950
import org.junit.Test;
5051
import org.junit.runner.RunWith;
@@ -196,9 +197,9 @@ public void run() {
196197
mock(InternalLoggingServerInterceptor.Factory.class);
197198
when(serverInterceptorFactory.create()).thenReturn(serverInterceptor);
198199

199-
try (GcpObservability unused =
200-
GcpObservability.grpcInit(
201-
sink, config, channelInterceptorFactory, serverInterceptorFactory)) {
200+
try {
201+
GcpObservability gcpObservability = GcpObservability.grpcInit(
202+
sink, config, channelInterceptorFactory, serverInterceptorFactory);
202203
List<?> configurators = InternalConfiguratorRegistry.getConfigurators();
203204
assertThat(configurators).hasSize(1);
204205
ObservabilityConfigurator configurator = (ObservabilityConfigurator) configurators.get(0);
@@ -208,9 +209,11 @@ public void run() {
208209
assertThat(list.get(2)).isInstanceOf(ConditionalClientInterceptor.class);
209210
assertThat(configurator.serverInterceptors).hasSize(1);
210211
assertThat(configurator.tracerFactories).hasSize(2);
212+
gcpObservability.closeWithSleepTime(3000, TimeUnit.MILLISECONDS);
211213
} catch (Exception e) {
212214
fail("Encountered exception: " + e);
213215
}
216+
verify(sink).close();
214217
}
215218
}
216219

0 commit comments

Comments
 (0)