Skip to content

Commit

Permalink
fix: use nanos to prevent truncation errors (#204)
Browse files Browse the repository at this point in the history
SpannerRetryHelperTest.testExceptionWithRetryInfo could sometimes fail on some
environments. I have not been able to reproduce it locally, but one possible
reason for the failure could be truncation when converting from nanoseconds to
milliseconds. Now both the timeout and the verification use nanosecond precision.

This change also removes all uses of hamcres in this test file.

Fixes #200
  • Loading branch information
olavloite committed May 14, 2020
1 parent 670a93e commit a608460
Showing 1 changed file with 9 additions and 10 deletions.
Expand Up @@ -16,9 +16,7 @@

package com.google.cloud.spanner;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.common.base.Stopwatch;
Expand Down Expand Up @@ -125,7 +123,7 @@ public Integer call() throws Exception {
return 1 + 1;
}
};
assertThat(SpannerRetryHelper.runTxWithRetriesOnAborted(callable), is(equalTo(2)));
assertThat(SpannerRetryHelper.runTxWithRetriesOnAborted(callable)).isEqualTo(2);
}

@Test(expected = IllegalStateException.class)
Expand Down Expand Up @@ -153,7 +151,7 @@ public Integer call() throws Exception {
return 1 + 1;
}
};
assertThat(SpannerRetryHelper.runTxWithRetriesOnAborted(callable), is(equalTo(2)));
assertThat(SpannerRetryHelper.runTxWithRetriesOnAborted(callable)).isEqualTo(2);
}

@Test
Expand All @@ -169,7 +167,7 @@ public Integer call() throws Exception {
return 1 + 1;
}
};
assertThat(SpannerRetryHelper.runTxWithRetriesOnAborted(callable), is(equalTo(2)));
assertThat(SpannerRetryHelper.runTxWithRetriesOnAborted(callable)).isEqualTo(2);
}

@Test(expected = IllegalStateException.class)
Expand All @@ -190,12 +188,13 @@ public Integer call() throws Exception {

@Test
public void testExceptionWithRetryInfo() {
final int RETRY_DELAY_NANOS = 100_000_000;
Metadata.Key<RetryInfo> key = ProtoUtils.keyForProto(RetryInfo.getDefaultInstance());
Status status = Status.fromCodeValue(Status.Code.ABORTED.value());
Metadata trailers = new Metadata();
RetryInfo retryInfo =
RetryInfo.newBuilder()
.setRetryDelay(Duration.newBuilder().setNanos(100000000).build())
.setRetryDelay(Duration.newBuilder().setNanos(RETRY_DELAY_NANOS).build())
.build();
trailers.put(key, retryInfo);
final SpannerException e =
Expand All @@ -214,9 +213,9 @@ public Integer call() throws Exception {
// The following call should take at least 100ms, as that is the retry delay specified in the
// retry info of the exception.
Stopwatch watch = Stopwatch.createStarted();
assertThat(SpannerRetryHelper.runTxWithRetriesOnAborted(callable), is(equalTo(2)));
long elapsed = watch.elapsed(TimeUnit.MILLISECONDS);
assertThat(elapsed >= 100L, is(true));
assertThat(SpannerRetryHelper.runTxWithRetriesOnAborted(callable)).isEqualTo(2);
long elapsed = watch.elapsed(TimeUnit.NANOSECONDS);
assertThat(elapsed >= RETRY_DELAY_NANOS).isTrue();
}

private SpannerException abortedWithRetryInfo(int nanos) {
Expand Down

0 comments on commit a608460

Please sign in to comment.