Skip to content

Commit

Permalink
fix: use millis to prevent rounding errors (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed Jun 11, 2020
1 parent 3a7a8ad commit 22ed458
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Expand Up @@ -63,7 +63,7 @@ static <T> T runTxWithRetriesOnAborted(Callable<T> callable) {
static <T> T runTxWithRetriesOnAborted(Callable<T> callable, RetrySettings retrySettings) {
try {
return RetryHelper.runWithRetries(
callable, txRetrySettings, new TxRetryAlgorithm<>(), NanoClock.getDefaultClock());
callable, retrySettings, new TxRetryAlgorithm<>(), NanoClock.getDefaultClock());
} catch (RetryHelperException e) {
if (e.getCause() != null) {
Throwables.throwIfUnchecked(e.getCause());
Expand Down
Expand Up @@ -188,13 +188,18 @@ public Integer call() throws Exception {

@Test
public void testExceptionWithRetryInfo() {
final int RETRY_DELAY_NANOS = 100_000_000;
final int RETRY_DELAY_MILLIS = 100;
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(RETRY_DELAY_NANOS).build())
.setRetryDelay(
Duration.newBuilder()
.setNanos(
(int)
TimeUnit.NANOSECONDS.convert(RETRY_DELAY_MILLIS, TimeUnit.MILLISECONDS))
.build())
.build();
trailers.put(key, retryInfo);
final SpannerException e =
Expand All @@ -214,8 +219,8 @@ public Integer call() throws Exception {
// retry info of the exception.
Stopwatch watch = Stopwatch.createStarted();
assertThat(SpannerRetryHelper.runTxWithRetriesOnAborted(callable)).isEqualTo(2);
long elapsed = watch.elapsed(TimeUnit.NANOSECONDS);
assertThat(elapsed >= RETRY_DELAY_NANOS).isTrue();
long elapsed = watch.elapsed(TimeUnit.MILLISECONDS);
assertThat(elapsed >= RETRY_DELAY_MILLIS).isTrue();
}

private SpannerException abortedWithRetryInfo(int nanos) {
Expand Down

0 comments on commit 22ed458

Please sign in to comment.