Skip to content

Commit b2ad72e

Browse files
test: attempt to fix flaky test (#1584)
* test: attempt to fix flaky test * fix format * fix BuiltinMetricsIT * revert BuiltinMetricsIT test * address comment * re-throw exception * create a new DynamicFlowControlStats every time * clean up * create a new instance * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent d638c40 commit b2ad72e

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.cloud.bigtable.test_helpers.env.TestEnvRule;
2929
import java.util.List;
3030
import java.util.UUID;
31+
import java.util.concurrent.ExecutionException;
3132
import java.util.concurrent.TimeUnit;
3233
import org.junit.BeforeClass;
3334
import org.junit.ClassRule;
@@ -69,29 +70,49 @@ public void testSuccess() throws Exception {
6970
List<Cluster> clusters = clustersFuture.get(1, TimeUnit.MINUTES);
7071

7172
// give opencensus some time to populate view data
72-
Thread.sleep(100);
73+
for (int i = 0; i < 10; i++) {
74+
if (StatsWrapper.getOperationLatencyViewTagValueStrings()
75+
.contains(clusters.get(0).getZone())) {
76+
break;
77+
}
78+
Thread.sleep(100);
79+
}
7380

7481
List<String> tagValueStrings = StatsWrapper.getOperationLatencyViewTagValueStrings();
7582
assertThat(tagValueStrings).contains(clusters.get(0).getZone());
7683
assertThat(tagValueStrings).contains(clusters.get(0).getId());
7784
}
7885

7986
@Test
80-
public void testFailure() throws InterruptedException {
87+
public void testFailure() throws Exception {
8188
String rowKey = UUID.randomUUID().toString();
8289
String familyId = testEnvRule.env().getFamilyId();
8390

91+
ApiFuture<Void> future =
92+
testEnvRule
93+
.env()
94+
.getDataClient()
95+
.mutateRowCallable()
96+
.futureCall(
97+
RowMutation.create("non-exist-table", rowKey).setCell(familyId, "q", "myVal"));
98+
8499
try {
85-
testEnvRule
86-
.env()
87-
.getDataClient()
88-
.mutateRowCallable()
89-
.call(RowMutation.create("non-exist-table", rowKey).setCell(familyId, "q", "myVal"));
90-
} catch (NotFoundException e) {
100+
future.get(1, TimeUnit.MINUTES);
101+
} catch (ExecutionException e) {
102+
if (e.getCause() instanceof NotFoundException) {
103+
// ignore NotFoundException
104+
} else {
105+
throw e;
106+
}
91107
}
92108

93109
// give opencensus some time to populate view data
94-
Thread.sleep(100);
110+
for (int i = 0; i < 10; i++) {
111+
if (StatsWrapper.getOperationLatencyViewTagValueStrings().contains("unspecified")) {
112+
break;
113+
}
114+
Thread.sleep(100);
115+
}
95116

96117
List<String> tagValueStrings = StatsWrapper.getOperationLatencyViewTagValueStrings();
97118
assertThat(tagValueStrings).contains("unspecified");

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.google.api.core.ApiFuture;
2121
import com.google.api.core.ApiFutures;
2222
import com.google.api.gax.batching.DynamicFlowControlSettings;
23-
import com.google.api.gax.batching.FlowControlEventStats;
2423
import com.google.api.gax.batching.FlowController;
2524
import com.google.api.gax.batching.FlowController.LimitExceededBehavior;
2625
import com.google.api.gax.grpc.GrpcCallContext;
@@ -40,6 +39,7 @@
4039
import java.util.concurrent.Future;
4140
import java.util.concurrent.TimeUnit;
4241
import java.util.concurrent.atomic.AtomicBoolean;
42+
import org.junit.After;
4343
import org.junit.Before;
4444
import org.junit.Rule;
4545
import org.junit.Test;
@@ -60,7 +60,6 @@ public class DynamicFlowControlCallableTest {
6060
private static final int DEADLINE_EXCEEDED_LATENCY = 501;
6161

6262
private FlowController flowController;
63-
private FlowControlEventStats flowControlEvents;
6463
private DynamicFlowControlStats stats;
6564
private UnaryCallable innerCallable;
6665
private ApiCallContext context;
@@ -81,7 +80,6 @@ public void setup() {
8180
.setMinOutstandingRequestBytes(15L)
8281
.setLimitExceededBehavior(LimitExceededBehavior.Block)
8382
.build());
84-
flowControlEvents = flowController.getFlowControlEventStats();
8583
stats = new DynamicFlowControlStats();
8684
context = GrpcCallContext.createDefault();
8785
innerCallable = new MockInnerCallable();
@@ -94,8 +92,18 @@ public void setup() {
9492
innerCallable, flowController, stats, TARGET_LATENCY_MS, ADJUSTING_INTERVAL_MS);
9593
}
9694

95+
@After
96+
public void cleanup() {
97+
// reset last adjustedTimestamp after each test
98+
stats.setLastAdjustedTimestampMs(stats.getLastAdjustedTimestampMs(), 0);
99+
}
100+
97101
@Test
98102
public void testLatenciesAreRecorded() throws Exception {
103+
DynamicFlowControlStats stats = new DynamicFlowControlStats();
104+
DynamicFlowControlCallable callableToTest =
105+
new DynamicFlowControlCallable(
106+
innerCallable, flowController, stats, TARGET_LATENCY_MS, ADJUSTING_INTERVAL_MS);
99107
Map<String, List<String>> extraHeaders = new HashMap<>();
100108
extraHeaders.put(LATENCY_HEADER, Arrays.asList("5"));
101109
ApiCallContext newContext = context.withExtraHeaders(extraHeaders);

0 commit comments

Comments
 (0)