Skip to content

Commit 966f46d

Browse files
test: try to deflake BuiltinMetricsTracerTest (#2516)
Change-Id: Iad86b73ed68d026097a7cbbb65b3e968b9323203 Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/java-bigtable/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) - [ ] Rollback plan is reviewed and LGTMed - [ ] All new data plane features have a completed end to end testing plan Fixes #<issue_number_goes_here> ☕️ If you write sample code, please follow the [samples format]( https://togithub.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md).
1 parent b2af258 commit 966f46d

File tree

3 files changed

+54
-18
lines changed

3 files changed

+54
-18
lines changed

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/FakeServiceBuilder.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ public Server start() throws IOException {
6464
return startWithoutRetries();
6565
} catch (IOException e) {
6666
lastError = e;
67-
if (!(e.getCause() instanceof BindException)) {
68-
break;
67+
if (e.getCause() instanceof BindException) {
68+
continue;
6969
}
70+
if (e.getMessage().contains("Failed to bind to address")) {
71+
continue;
72+
}
73+
break;
7074
}
7175
}
7276

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsTestUtils.getMetricData;
3636
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsTestUtils.verifyAttributes;
3737
import static com.google.common.truth.Truth.assertThat;
38+
import static com.google.common.truth.Truth.assertWithMessage;
3839

3940
import com.google.api.client.util.Lists;
4041
import com.google.api.core.ApiFunction;
42+
import com.google.api.core.ApiFuture;
4143
import com.google.api.core.SettableApiFuture;
4244
import com.google.api.gax.batching.Batcher;
4345
import com.google.api.gax.batching.BatchingException;
@@ -98,6 +100,7 @@
98100
import java.net.SocketAddress;
99101
import java.nio.charset.Charset;
100102
import java.time.Duration;
103+
import java.time.Instant;
101104
import java.util.ArrayList;
102105
import java.util.Collections;
103106
import java.util.Iterator;
@@ -134,7 +137,7 @@ public class BuiltinMetricsTracerTest {
134137
private static final long APPLICATION_LATENCY = 200;
135138
private static final long SLEEP_VARIABILITY = 15;
136139
private static final String CLIENT_NAME = "java-bigtable/" + Version.VERSION;
137-
private static final long CHANNEL_BLOCKING_LATENCY = 200;
140+
private static final Duration CHANNEL_BLOCKING_LATENCY = Duration.ofMillis(200);
138141

139142
@Rule public final MockitoRule mockitoRule = MockitoJUnit.rule();
140143

@@ -149,6 +152,8 @@ public class BuiltinMetricsTracerTest {
149152

150153
private InMemoryMetricReader metricReader;
151154

155+
private DelayProxyDetector delayProxyDetector;
156+
152157
@Before
153158
public void setUp() throws Exception {
154159
metricReader = InMemoryMetricReader.create();
@@ -253,15 +258,16 @@ public void sendHeaders(Metadata headers) {
253258
final ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> oldConfigurator =
254259
channelProvider.getChannelConfigurator();
255260

261+
delayProxyDetector = new DelayProxyDetector();
262+
256263
channelProvider.setChannelConfigurator(
257264
(builder) -> {
258265
if (oldConfigurator != null) {
259266
builder = oldConfigurator.apply(builder);
260267
}
261-
return builder.proxyDetector(new DelayProxyDetector());
268+
return builder.proxyDetector(delayProxyDetector);
262269
});
263270
stubSettingsBuilder.setTransportChannelProvider(channelProvider.build());
264-
265271
EnhancedBigtableStubSettings stubSettings = stubSettingsBuilder.build();
266272
stub = new EnhancedBigtableStub(stubSettings, ClientContext.create(stubSettings));
267273
}
@@ -696,8 +702,10 @@ public void testBatchBlockingLatencies() throws InterruptedException {
696702
}
697703

698704
@Test
699-
public void testQueuedOnChannelServerStreamLatencies() {
700-
stub.readRowsCallable().all().call(Query.create(TABLE));
705+
public void testQueuedOnChannelServerStreamLatencies() throws Exception {
706+
ApiFuture<List<Row>> f = stub.readRowsCallable().all().futureCall(Query.create(TABLE));
707+
Duration proxyDelayPriorTest = delayProxyDetector.getCurrentDelayUsed();
708+
f.get();
701709

702710
MetricData clientLatency = getMetricData(metricReader, CLIENT_BLOCKING_LATENCIES_NAME);
703711

@@ -711,14 +719,17 @@ public void testQueuedOnChannelServerStreamLatencies() {
711719
.put(CLIENT_NAME_KEY, CLIENT_NAME)
712720
.build();
713721

714-
long value = getAggregatedValue(clientLatency, attributes);
715-
assertThat(value).isAtLeast(CHANNEL_BLOCKING_LATENCY);
722+
Duration value = Duration.ofMillis(getAggregatedValue(clientLatency, attributes));
723+
assertThat(value).isAtLeast(CHANNEL_BLOCKING_LATENCY.minus(proxyDelayPriorTest));
716724
}
717725

718726
@Test
719-
public void testQueuedOnChannelUnaryLatencies() {
720-
721-
stub.mutateRowCallable().call(RowMutation.create(TABLE, "a-key").setCell("f", "q", "v"));
727+
public void testQueuedOnChannelUnaryLatencies() throws Exception {
728+
ApiFuture<Void> f =
729+
stub.mutateRowCallable()
730+
.futureCall(RowMutation.create(TABLE, "a-key").setCell("f", "q", "v"));
731+
Duration proxyDelayPriorTest = delayProxyDetector.getCurrentDelayUsed();
732+
f.get();
722733

723734
MetricData clientLatency = getMetricData(metricReader, CLIENT_BLOCKING_LATENCIES_NAME);
724735

@@ -732,8 +743,8 @@ public void testQueuedOnChannelUnaryLatencies() {
732743
.put(CLIENT_NAME_KEY, CLIENT_NAME)
733744
.build();
734745

735-
long actual = getAggregatedValue(clientLatency, attributes);
736-
assertThat(actual).isAtLeast(CHANNEL_BLOCKING_LATENCY);
746+
Duration actual = Duration.ofMillis(getAggregatedValue(clientLatency, attributes));
747+
assertThat(actual).isAtLeast(CHANNEL_BLOCKING_LATENCY.minus(proxyDelayPriorTest));
737748
}
738749

739750
@Test
@@ -809,7 +820,7 @@ public void testRemainingDeadline() {
809820

810821
double okRemainingDeadline = okHistogramPointData.getSum();
811822
// first attempt latency + retry delay
812-
double expected = 9000 - SERVER_LATENCY - CHANNEL_BLOCKING_LATENCY - 10;
823+
double expected = 9000 - SERVER_LATENCY - CHANNEL_BLOCKING_LATENCY.toMillis() - 10;
813824
assertThat(okRemainingDeadline).isIn(Range.closed(expected - 500, expected + 10));
814825
}
815826

@@ -934,16 +945,33 @@ public AtomicInteger getResponseCounter() {
934945
}
935946

936947
class DelayProxyDetector implements ProxyDetector {
948+
private volatile Instant lastProxyDelay = null;
937949

938950
@Nullable
939951
@Override
940952
public ProxiedSocketAddress proxyFor(SocketAddress socketAddress) throws IOException {
953+
lastProxyDelay = Instant.now();
941954
try {
942-
Thread.sleep(CHANNEL_BLOCKING_LATENCY);
955+
Thread.sleep(CHANNEL_BLOCKING_LATENCY.toMillis());
943956
} catch (InterruptedException e) {
944957

945958
}
946959
return null;
947960
}
961+
962+
Duration getCurrentDelayUsed() {
963+
Instant local = lastProxyDelay;
964+
// If the delay was never injected
965+
if (local == null) {
966+
return Duration.ZERO;
967+
}
968+
Duration duration = Duration.between(local, Instant.now());
969+
970+
assertWithMessage("test burned through all channel blocking latency during setup")
971+
.that(duration)
972+
.isLessThan(CHANNEL_BLOCKING_LATENCY);
973+
974+
return duration;
975+
}
948976
}
949977
}

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/StatsHeadersCallableTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,12 @@ public void setUp() throws Exception {
9797

9898
@After
9999
public void tearDown() {
100-
stub.close();
101-
server.shutdown();
100+
if (stub != null) {
101+
stub.close();
102+
}
103+
if (server != null) {
104+
server.shutdown();
105+
}
102106
}
103107

104108
@Test

0 commit comments

Comments
 (0)