Skip to content

Commit

Permalink
Updating to non transactional read for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pXius committed Nov 6, 2023
1 parent b16c9b4 commit 1802983
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.Key;
import com.google.cloud.spanner.Mutation;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.Struct;
import com.google.cloud.spanner.TransactionContext;
import net.javacrumbs.shedlock.core.LockConfiguration;
Expand Down Expand Up @@ -148,6 +150,15 @@ protected Optional<Lock> findLock(TransactionContext transaction, String lockNam
.map(Lock::new);
}

protected Optional<Lock> nonTransactionFindLock(String lockName) {
return Optional.ofNullable(databaseClient.singleUse().executeQuery(Statement.newBuilder("SELECT * FROM " + table + " WHERE " + name + " = @name")
.bind(name).to(lockName)
.build()))
.filter(ResultSet::next)
.map(ResultSet::getCurrentRowAsStruct)
.map(Lock::new);
}

/**
* Converts {@code Instant} to {@code Timestamp}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,24 @@ protected StorageBasedLockProvider getLockProvider() {

@Override
protected void assertUnlocked(String lockName) {
Instant now = ClockProvider.now();
SpannerStorageAccessor.Lock lock = findLock(lockName);

assertThat(toInstant(lock.getLockedUntil())).isBefore(now);
assertThat(toInstant(lock.getLockedAt())).isBefore(now);
assertThat(toInstant(lock.getLockedUntil())).isBefore(ClockProvider.now());
assertThat(toInstant(lock.getLockedAt())).isBefore(ClockProvider.now());
assertThat(lock.getLockedBy()).isNotEmpty();
}

@Override
protected void assertLocked(String lockName) {
Instant now = ClockProvider.now();
SpannerStorageAccessor.Lock lock = findLock(lockName);

assertThat(toInstant(lock.getLockedUntil())).isAfter(now);
assertThat(toInstant(lock.getLockedAt())).isBefore(now);
assertThat(toInstant(lock.getLockedUntil())).isAfter(ClockProvider.now());
assertThat(toInstant(lock.getLockedAt())).isBefore(ClockProvider.now());
assertThat(lock.getLockedBy()).isNotEmpty();
}

private SpannerStorageAccessor.Lock findLock(String lockName) {
return getDatabaseClient().readWriteTransaction().run(transactionContext ->
accessor.findLock(transactionContext, lockName)).get();
return accessor.nonTransactionFindLock(lockName).get();
}

private void cleanLockTable() {
Expand Down

0 comments on commit 1802983

Please sign in to comment.