5555import io .grpc .Status ;
5656import io .grpc .StatusRuntimeException ;
5757import io .grpc .stub .StreamObserver ;
58- import io .opencensus .impl . stats .StatsComponentImpl ;
58+ import io .opencensus .stats .StatsComponent ;
5959import io .opencensus .tags .TagKey ;
6060import io .opencensus .tags .TagValue ;
6161import io .opencensus .tags .Tags ;
@@ -85,6 +85,7 @@ public class MetricsTracerTest {
8585 private static final String INSTANCE_ID = "fake-instance" ;
8686 private static final String APP_PROFILE_ID = "default" ;
8787 private static final String TABLE_ID = "fake-table" ;
88+ private static final long SLEEP_VARIABILITY = 15 ;
8889
8990 private static final ReadRowsResponse DEFAULT_READ_ROWS_RESPONSES =
9091 ReadRowsResponse .newBuilder ()
@@ -105,7 +106,7 @@ public class MetricsTracerTest {
105106 @ Mock (answer = Answers .CALLS_REAL_METHODS )
106107 private BigtableGrpc .BigtableImplBase mockService ;
107108
108- private final StatsComponentImpl localStats = new StatsComponentImpl ();
109+ private final StatsComponent localStats = new SimpleStatsComponent ();
109110 private EnhancedBigtableStub stub ;
110111 private BigtableDataSettings settings ;
111112
@@ -157,9 +158,6 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
157158 Lists .newArrayList (stub .readRowsCallable ().call (Query .create (TABLE_ID )));
158159 long elapsed = stopwatch .elapsed (TimeUnit .MILLISECONDS );
159160
160- // Give OpenCensus a chance to update the views asynchronously.
161- Thread .sleep (100 );
162-
163161 long opLatency =
164162 StatsTestUtils .getAggregationValueAsLong (
165163 localStats ,
@@ -193,9 +191,6 @@ public Object answer(InvocationOnMock invocation) {
193191 Lists .newArrayList (stub .readRowsCallable ().call (Query .create (TABLE_ID )));
194192 Lists .newArrayList (stub .readRowsCallable ().call (Query .create (TABLE_ID )));
195193
196- // Give OpenCensus a chance to update the views asynchronously.
197- Thread .sleep (100 );
198-
199194 long opLatency =
200195 StatsTestUtils .getAggregationValueAsLong (
201196 localStats ,
@@ -247,8 +242,6 @@ public void testReadRowsFirstRow() throws InterruptedException {
247242 }
248243 long elapsed = stopwatch .elapsed (TimeUnit .MILLISECONDS );
249244
250- // Give OpenCensus a chance to update the views asynchronously.
251- Thread .sleep (100 );
252245 executor .shutdown ();
253246
254247 long firstRowLatency =
@@ -260,7 +253,10 @@ public void testReadRowsFirstRow() throws InterruptedException {
260253 INSTANCE_ID ,
261254 APP_PROFILE_ID );
262255
263- assertThat (firstRowLatency ).isIn (Range .closed (beforeSleep , elapsed - afterSleep ));
256+ assertThat (firstRowLatency )
257+ .isIn (
258+ Range .closed (
259+ beforeSleep - SLEEP_VARIABILITY , elapsed - afterSleep + SLEEP_VARIABILITY ));
264260 }
265261
266262 @ Test
@@ -292,9 +288,6 @@ public Object answer(InvocationOnMock invocation) {
292288
293289 Lists .newArrayList (stub .readRowsCallable ().call (Query .create (TABLE_ID )));
294290
295- // Give OpenCensus a chance to update the views asynchronously.
296- Thread .sleep (100 );
297-
298291 long opLatency =
299292 StatsTestUtils .getAggregationValueAsLong (
300293 localStats ,
@@ -341,9 +334,6 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
341334 Lists .newArrayList (stub .readRowsCallable ().call (Query .create (TABLE_ID )));
342335 long elapsed = stopwatch .elapsed (TimeUnit .MILLISECONDS );
343336
344- // Give OpenCensus a chance to update the views asynchronously.
345- Thread .sleep (100 );
346-
347337 long attemptLatency =
348338 StatsTestUtils .getAggregationValueAsLong (
349339 localStats ,
@@ -360,12 +350,11 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
360350 }
361351
362352 @ Test
363- public void testInvalidRequest () throws InterruptedException {
353+ public void testInvalidRequest () {
364354 try {
365355 stub .bulkMutateRowsCallable ().call (BulkMutation .create (TABLE_ID ));
366356 Assert .fail ("Invalid request should throw exception" );
367357 } catch (IllegalStateException e ) {
368- Thread .sleep (100 );
369358 // Verify that the latency is recorded with an error code (in this case UNKNOWN)
370359 long attemptLatency =
371360 StatsTestUtils .getAggregationValueAsLong (
@@ -403,9 +392,6 @@ public Object answer(InvocationOnMock invocation) {
403392 batcher .add (ByteString .copyFromUtf8 ("row1" ));
404393 batcher .sendOutstanding ();
405394
406- // Give OpenCensus a chance to update the views asynchronously.
407- Thread .sleep (100 );
408-
409395 long throttledTimeMetric =
410396 StatsTestUtils .getAggregationValueAsLong (
411397 localStats ,
@@ -476,7 +462,6 @@ public Object answer(InvocationOnMock invocation) {
476462 batcher .add (RowMutationEntry .create ("key" ));
477463 batcher .sendOutstanding ();
478464
479- Thread .sleep (100 );
480465 long throttledTimeMetric =
481466 StatsTestUtils .getAggregationValueAsLong (
482467 localStats ,
0 commit comments