Skip to content

Commit 93ca52e

Browse files
authored
test: added getSnapshotTimestamp() method (#4000)
The snapshot timestamp is the timestamp at which all the reads in a transaction ran when using repeatable read isolation. The method is only intended to be used for internal testing purposes, so the snapshot timestamp will be `null` if attempted to be used externally.
1 parent a71c24d commit 93ca52e

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/CommitResponse.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.cloud.Timestamp;
2020
import com.google.common.base.Preconditions;
2121
import java.util.Objects;
22+
import javax.annotation.Nullable;
2223

2324
/** Represents a response from a commit operation. */
2425
public class CommitResponse {
@@ -41,6 +42,18 @@ public Timestamp getCommitTimestamp() {
4142
return Timestamp.fromProto(proto.getCommitTimestamp());
4243
}
4344

45+
/**
46+
* Returns a {@link Timestamp} representing the timestamp at which all reads in the transaction
47+
* ran at, if the transaction ran at repeatable read isolation in internal test environments, and
48+
* otherwise returns null.
49+
*/
50+
public @Nullable Timestamp getSnapshotTimestamp() {
51+
if (proto.getSnapshotTimestamp() == com.google.protobuf.Timestamp.getDefaultInstance()) {
52+
return null;
53+
}
54+
return Timestamp.fromProto(proto.getSnapshotTimestamp());
55+
}
56+
4457
/**
4558
* @return true if the {@link CommitResponse} includes {@link CommitStats}
4659
*/

google-cloud-spanner/src/test/java/com/google/cloud/spanner/CommitResponseTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,22 @@ public void testHasCommitStats() {
101101
CommitResponse responseWithCommitStats = new CommitResponse(protoWithCommitStats);
102102
assertTrue(responseWithCommitStats.hasCommitStats());
103103
}
104+
105+
@Test
106+
public void testGetSnapshotTimestamp() {
107+
com.google.spanner.v1.CommitResponse protoWithoutSnapshotTimestamp =
108+
com.google.spanner.v1.CommitResponse.getDefaultInstance();
109+
CommitResponse responseWithoutSnapshotTimestamp =
110+
new CommitResponse(protoWithoutSnapshotTimestamp);
111+
assertEquals(null, responseWithoutSnapshotTimestamp.getSnapshotTimestamp());
112+
113+
com.google.protobuf.Timestamp timestamp =
114+
com.google.protobuf.Timestamp.newBuilder().setSeconds(123L).setNanos(456).build();
115+
com.google.spanner.v1.CommitResponse protoWithSnapshotTimestamp =
116+
com.google.spanner.v1.CommitResponse.newBuilder().setSnapshotTimestamp(timestamp).build();
117+
CommitResponse responseWithSnapshotTimestamp = new CommitResponse(protoWithSnapshotTimestamp);
118+
assertEquals(
119+
Timestamp.ofTimeSecondsAndNanos(123L, 456),
120+
responseWithSnapshotTimestamp.getSnapshotTimestamp());
121+
}
104122
}

0 commit comments

Comments
 (0)