Skip to content

Commit 4d6352a

Browse files
committed
chore: Fix flaky metrics tests
1 parent eabcf88 commit 4d6352a

File tree

4 files changed

+41
-29
lines changed

4 files changed

+41
-29
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.fail;
2020

2121
import com.google.api.gax.rpc.ClientContext;
22+
import com.google.api.gax.rpc.ServerStream;
2223
import com.google.api.gax.rpc.UnavailableException;
2324
import com.google.bigtable.v2.BigtableGrpc.BigtableImplBase;
2425
import com.google.bigtable.v2.CheckAndMutateRowRequest;
@@ -54,7 +55,6 @@
5455
import io.grpc.Status;
5556
import io.grpc.StatusRuntimeException;
5657
import io.grpc.stub.StreamObserver;
57-
import io.opencensus.impl.stats.StatsComponentImpl;
5858
import io.opencensus.stats.StatsComponent;
5959
import io.opencensus.tags.TagKey;
6060
import io.opencensus.tags.TagValue;
@@ -74,7 +74,7 @@ public class BigtableTracerCallableTest {
7474

7575
private FakeService fakeService = new FakeService();
7676

77-
private final StatsComponent localStats = new StatsComponentImpl();
77+
private final StatsComponent localStats = new SimpleStatsComponent();
7878
private EnhancedBigtableStub stub;
7979
private EnhancedBigtableStub noHeaderStub;
8080
private int attempts;
@@ -157,10 +157,9 @@ public void tearDown() {
157157
}
158158

159159
@Test
160-
public void testGFELatencyMetricReadRows() throws InterruptedException {
161-
stub.readRowsCallable().call(Query.create(TABLE_ID));
162-
163-
Thread.sleep(WAIT_FOR_METRICS_TIME_MS);
160+
public void testGFELatencyMetricReadRows() {
161+
ServerStream<?> call = stub.readRowsCallable().call(Query.create(TABLE_ID));
162+
call.forEach(r -> {});
164163

165164
long latency =
166165
StatsTestUtils.getAggregationValueAsLong(

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public class BuiltinMetricsTracerTest {
109109
private static final long FAKE_SERVER_TIMING = 50;
110110
private static final long SERVER_LATENCY = 100;
111111
private static final long APPLICATION_LATENCY = 200;
112+
private static final long SLEEP_VARIABILITY = 15;
112113

113114
private static final long CHANNEL_BLOCKING_LATENCY = 75;
114115

@@ -353,7 +354,11 @@ public void onComplete() {
353354
.recordOperation(status.capture(), tableId.capture(), zone.capture(), cluster.capture());
354355

355356
assertThat(counter.get()).isEqualTo(fakeService.getResponseCounter().get());
356-
assertThat(applicationLatency.getValue()).isAtLeast(APPLICATION_LATENCY * counter.get());
357+
// Thread.sleep might not sleep for the requested amount depending on the interrupt period
358+
// defined by the OS.
359+
// On linux this is ~1ms but on windows may be as high as 15-20ms.
360+
assertThat(applicationLatency.getValue())
361+
.isAtLeast((APPLICATION_LATENCY - SLEEP_VARIABILITY) * counter.get());
357362
assertThat(applicationLatency.getValue())
358363
.isAtMost(operationLatency.getValue() - SERVER_LATENCY);
359364
}

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

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
import io.grpc.Status;
5656
import io.grpc.StatusRuntimeException;
5757
import io.grpc.stub.StreamObserver;
58-
import io.opencensus.impl.stats.StatsComponentImpl;
58+
import io.opencensus.stats.StatsComponent;
5959
import io.opencensus.tags.TagKey;
6060
import io.opencensus.tags.TagValue;
6161
import io.opencensus.tags.Tags;
@@ -105,7 +105,7 @@ public class MetricsTracerTest {
105105
@Mock(answer = Answers.CALLS_REAL_METHODS)
106106
private BigtableGrpc.BigtableImplBase mockService;
107107

108-
private final StatsComponentImpl localStats = new StatsComponentImpl();
108+
private final StatsComponent localStats = new SimpleStatsComponent();
109109
private EnhancedBigtableStub stub;
110110
private BigtableDataSettings settings;
111111

@@ -157,9 +157,6 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
157157
Lists.newArrayList(stub.readRowsCallable().call(Query.create(TABLE_ID)));
158158
long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
159159

160-
// Give OpenCensus a chance to update the views asynchronously.
161-
Thread.sleep(100);
162-
163160
long opLatency =
164161
StatsTestUtils.getAggregationValueAsLong(
165162
localStats,
@@ -193,9 +190,6 @@ public Object answer(InvocationOnMock invocation) {
193190
Lists.newArrayList(stub.readRowsCallable().call(Query.create(TABLE_ID)));
194191
Lists.newArrayList(stub.readRowsCallable().call(Query.create(TABLE_ID)));
195192

196-
// Give OpenCensus a chance to update the views asynchronously.
197-
Thread.sleep(100);
198-
199193
long opLatency =
200194
StatsTestUtils.getAggregationValueAsLong(
201195
localStats,
@@ -247,8 +241,6 @@ public void testReadRowsFirstRow() throws InterruptedException {
247241
}
248242
long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
249243

250-
// Give OpenCensus a chance to update the views asynchronously.
251-
Thread.sleep(100);
252244
executor.shutdown();
253245

254246
long firstRowLatency =
@@ -292,9 +284,6 @@ public Object answer(InvocationOnMock invocation) {
292284

293285
Lists.newArrayList(stub.readRowsCallable().call(Query.create(TABLE_ID)));
294286

295-
// Give OpenCensus a chance to update the views asynchronously.
296-
Thread.sleep(100);
297-
298287
long opLatency =
299288
StatsTestUtils.getAggregationValueAsLong(
300289
localStats,
@@ -341,9 +330,6 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
341330
Lists.newArrayList(stub.readRowsCallable().call(Query.create(TABLE_ID)));
342331
long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
343332

344-
// Give OpenCensus a chance to update the views asynchronously.
345-
Thread.sleep(100);
346-
347333
long attemptLatency =
348334
StatsTestUtils.getAggregationValueAsLong(
349335
localStats,
@@ -360,12 +346,11 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
360346
}
361347

362348
@Test
363-
public void testInvalidRequest() throws InterruptedException {
349+
public void testInvalidRequest() {
364350
try {
365351
stub.bulkMutateRowsCallable().call(BulkMutation.create(TABLE_ID));
366352
Assert.fail("Invalid request should throw exception");
367353
} catch (IllegalStateException e) {
368-
Thread.sleep(100);
369354
// Verify that the latency is recorded with an error code (in this case UNKNOWN)
370355
long attemptLatency =
371356
StatsTestUtils.getAggregationValueAsLong(
@@ -403,9 +388,6 @@ public Object answer(InvocationOnMock invocation) {
403388
batcher.add(ByteString.copyFromUtf8("row1"));
404389
batcher.sendOutstanding();
405390

406-
// Give OpenCensus a chance to update the views asynchronously.
407-
Thread.sleep(100);
408-
409391
long throttledTimeMetric =
410392
StatsTestUtils.getAggregationValueAsLong(
411393
localStats,
@@ -471,7 +453,6 @@ public Object answer(InvocationOnMock invocation) {
471453
batcher.add(RowMutationEntry.create("key"));
472454
batcher.sendOutstanding();
473455

474-
Thread.sleep(100);
475456
long throttledTimeMetric =
476457
StatsTestUtils.getAggregationValueAsLong(
477458
localStats,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.bigtable.data.v2.stub.metrics;
17+
18+
import io.opencensus.implcore.common.MillisClock;
19+
import io.opencensus.implcore.internal.SimpleEventQueue;
20+
import io.opencensus.implcore.stats.StatsComponentImplBase;
21+
22+
/** A StatsComponent implementation for testing that executes all events inline. */
23+
public class SimpleStatsComponent extends StatsComponentImplBase {
24+
public SimpleStatsComponent() {
25+
super(new SimpleEventQueue(), MillisClock.getInstance());
26+
}
27+
}

0 commit comments

Comments
 (0)