Skip to content

Commit

Permalink
tests: fix flaky restoreTestDatabase failures (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed May 14, 2020
1 parent 211dfdf commit 670a93e
Showing 1 changed file with 70 additions and 59 deletions.
Expand Up @@ -16,8 +16,8 @@

package com.google.cloud.spanner;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.api.core.ApiFunction;
import com.google.api.gax.longrunning.OperationFuture;
Expand Down Expand Up @@ -56,50 +56,17 @@
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.threeten.bp.Duration;

@RunWith(JUnit4.class)
public class DatabaseAdminClientTest {
private static class SpannerExecutionExceptionMatcher extends BaseMatcher<Throwable> {
private final ErrorCode expectedCode;

private static SpannerExecutionExceptionMatcher forCode(ErrorCode code) {
return new SpannerExecutionExceptionMatcher(code);
}

private SpannerExecutionExceptionMatcher(ErrorCode code) {
this.expectedCode = checkNotNull(code);
}

@Override
public boolean matches(Object item) {
if (item instanceof ExecutionException) {
ExecutionException e = (ExecutionException) item;
if (e.getCause() instanceof SpannerException) {
SpannerException se = (SpannerException) e.getCause();
return se.getErrorCode() == expectedCode;
}
}
return false;
}

@Override
public void describeTo(Description description) {
description.appendText("SpannerException[" + expectedCode + "]");
}
}

private static final String PROJECT_ID = "my-project";
private static final String INSTANCE_ID = "my-instance";
private static final String DB_ID = "test-db";
Expand All @@ -117,7 +84,6 @@ public void describeTo(Description description) {

private Spanner spanner;
private DatabaseAdminClient client;
@Rule public ExpectedException exception = ExpectedException.none();
private OperationFuture<Database, CreateDatabaseMetadata> createDatabaseOperation;
private OperationFuture<Backup, CreateBackupMetadata> createBackupOperation;
private OperationFuture<Database, RestoreDatabaseMetadata> restoreDatabaseOperation;
Expand Down Expand Up @@ -150,8 +116,8 @@ public void setUp() throws IOException {
SpannerOptions.Builder builder = SpannerOptions.newBuilder();
RetrySettings longRunningInitialRetrySettings =
RetrySettings.newBuilder()
.setInitialRpcTimeout(Duration.ofMillis(60L))
.setMaxRpcTimeout(Duration.ofMillis(600L))
.setInitialRpcTimeout(Duration.ofMillis(600L))
.setMaxRpcTimeout(Duration.ofMillis(6000L))
.setInitialRetryDelay(Duration.ofMillis(20L))
.setMaxRetryDelay(Duration.ofMillis(45L))
.setRetryDelayMultiplier(1.5)
Expand Down Expand Up @@ -348,8 +314,14 @@ public void databaseBackup() throws InterruptedException, ExecutionException {
public void dbAdminCreateBackupAlreadyExists() throws InterruptedException, ExecutionException {
OperationFuture<Backup, CreateBackupMetadata> op =
client.createBackup(INSTANCE_ID, BCK_ID, DB_ID, after7Days());
exception.expect(SpannerExecutionExceptionMatcher.forCode(ErrorCode.ALREADY_EXISTS));
op.get();
try {
op.get();
fail("missing expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
assertThat(((SpannerException) e.getCause()).getErrorCode())
.isEqualTo(ErrorCode.ALREADY_EXISTS);
}
}

@Test
Expand All @@ -360,9 +332,14 @@ public void backupCreateAlreadyExists() throws InterruptedException, ExecutionEx
.setDatabase(DatabaseId.of(PROJECT_ID, INSTANCE_ID, DB_ID))
.setExpireTime(after7Days())
.build();
OperationFuture<Backup, CreateBackupMetadata> op = backup.create();
exception.expect(SpannerExecutionExceptionMatcher.forCode(ErrorCode.ALREADY_EXISTS));
op.get();
try {
backup.create().get();
fail("missing expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
assertThat(((SpannerException) e.getCause()).getErrorCode())
.isEqualTo(ErrorCode.ALREADY_EXISTS);
}
}

@Test
Expand All @@ -374,17 +351,28 @@ public void databaseBackupAlreadyExists() throws InterruptedException, Execution
.newBackupBuilder(BackupId.of(PROJECT_ID, INSTANCE_ID, BCK_ID))
.setExpireTime(after7Days())
.build());
exception.expect(SpannerExecutionExceptionMatcher.forCode(ErrorCode.ALREADY_EXISTS));
op.get();
try {
op.get();
fail("missing expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
assertThat(((SpannerException) e.getCause()).getErrorCode())
.isEqualTo(ErrorCode.ALREADY_EXISTS);
}
}

@Test
public void dbAdminCreateBackupDbNotFound() throws InterruptedException, ExecutionException {
final String backupId = "other-backup-id";
OperationFuture<Backup, CreateBackupMetadata> op =
client.createBackup(INSTANCE_ID, backupId, "does-not-exist", after7Days());
exception.expect(SpannerExecutionExceptionMatcher.forCode(ErrorCode.NOT_FOUND));
op.get();
try {
op.get();
fail("missing expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
assertThat(((SpannerException) e.getCause()).getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
}
}

@Test
Expand All @@ -396,9 +384,13 @@ public void backupCreateDbNotFound() throws InterruptedException, ExecutionExcep
.setDatabase(DatabaseId.of(PROJECT_ID, INSTANCE_ID, "does-not-exist"))
.setExpireTime(after7Days())
.build();
OperationFuture<Backup, CreateBackupMetadata> op = backup.create();
exception.expect(SpannerExecutionExceptionMatcher.forCode(ErrorCode.NOT_FOUND));
op.get();
try {
backup.create().get();
fail("missing expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
assertThat(((SpannerException) e.getCause()).getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
}
}

@Test
Expand All @@ -413,8 +405,13 @@ public void databaseBackupDbNotFound() throws InterruptedException, ExecutionExc
.newBackupBuilder(BackupId.of(PROJECT_ID, INSTANCE_ID, backupId))
.setExpireTime(after7Days())
.build());
exception.expect(SpannerExecutionExceptionMatcher.forCode(ErrorCode.NOT_FOUND));
op.get();
try {
op.get();
fail("missing expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
assertThat(((SpannerException) e.getCause()).getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
}
}

@Test
Expand All @@ -435,16 +432,24 @@ public void backupDelete() {

@Test
public void dbAdminDeleteBackupNotFound() {
exception.expect(SpannerMatchers.isSpannerException(ErrorCode.NOT_FOUND));
client.deleteBackup(INSTANCE_ID, "does-not-exist");
try {
client.deleteBackup(INSTANCE_ID, "does-not-exist");
fail("missing expected exception");
} catch (SpannerException e) {
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
}
}

@Test
public void backupDeleteNotFound() {
Backup backup =
client.newBackupBuilder(BackupId.of(PROJECT_ID, INSTANCE_ID, "does-not-exist")).build();
exception.expect(SpannerMatchers.isSpannerException(ErrorCode.NOT_FOUND));
backup.delete();
try {
backup.delete();
fail("missing expected exception");
} catch (SpannerException e) {
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
}
}

@Test
Expand All @@ -463,16 +468,22 @@ public void backupReload() {

@Test
public void dbAdminGetBackupNotFound() {
exception.expect(SpannerMatchers.isSpannerException(ErrorCode.NOT_FOUND));
client.getBackup(INSTANCE_ID, "does-not-exist");
try {
client.getBackup(INSTANCE_ID, "does-not-exist");
} catch (SpannerException e) {
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
}
}

@Test
public void backupReloadNotFound() {
Backup backup =
client.newBackupBuilder(BackupId.of(PROJECT_ID, INSTANCE_ID, "does-not-exist")).build();
exception.expect(SpannerMatchers.isSpannerException(ErrorCode.NOT_FOUND));
backup.reload();
try {
backup.reload();
} catch (SpannerException e) {
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
}
}

@Test
Expand Down

0 comments on commit 670a93e

Please sign in to comment.