Search in sources :

Example 1 with TransactionOptions

use of com.google.cloud.firestore.TransactionOptions in project java-firestore by googleapis.

the class ITSystemTest method readOnlyTransaction_failureWhenAttemptReadOlderThan60Seconds.

@Test
public void readOnlyTransaction_failureWhenAttemptReadOlderThan60Seconds() throws ExecutionException, InterruptedException, TimeoutException {
    final DocumentReference documentReference = randomColl.add(SINGLE_FIELD_MAP).get();
    final TransactionOptions options = TransactionOptions.createReadOnlyOptionsBuilder().setReadTime(com.google.protobuf.Timestamp.newBuilder().setSeconds(1).setNanos(0)).build();
    final ApiFuture<Void> runTransaction = firestore.runTransaction(transaction -> {
        transaction.get(documentReference).get(5, TimeUnit.SECONDS);
        return null;
    }, options);
    try {
        runTransaction.get(10, TimeUnit.SECONDS);
    } catch (ExecutionException e) {
        final Throwable rootCause = ExceptionUtils.getRootCause(e);
        assertThat(rootCause).isInstanceOf(StatusRuntimeException.class);
        final StatusRuntimeException invalidArgument = (StatusRuntimeException) rootCause;
        final Status status = invalidArgument.getStatus();
        assertThat(status.getCode()).isEqualTo(Code.FAILED_PRECONDITION);
    }
}
Also used : Status(io.grpc.Status) TransactionOptions(com.google.cloud.firestore.TransactionOptions) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) DocumentReference(com.google.cloud.firestore.DocumentReference) Test(org.junit.Test)

Example 2 with TransactionOptions

use of com.google.cloud.firestore.TransactionOptions in project java-firestore by googleapis.

the class ITSystemTest method readOnlyTransaction_successfulRead.

@Test
public void readOnlyTransaction_successfulRead() throws Exception {
    DocumentReference documentReference = randomColl.add(SINGLE_FIELD_MAP).get();
    Timestamp firstWriteTime = documentReference.set(Collections.singletonMap("counter", 1)).get().getUpdateTime();
    documentReference.set(Collections.singletonMap("counter", 2)).get();
    final TransactionOptions options = TransactionOptions.createReadOnlyOptionsBuilder().setReadTime(com.google.protobuf.Timestamp.newBuilder().setSeconds(firstWriteTime.getSeconds()).setNanos(firstWriteTime.getNanos())).build();
    final ApiFuture<Long> runTransaction = firestore.runTransaction(transaction -> {
        final DocumentSnapshot snapshot = transaction.get(documentReference).get(5, TimeUnit.SECONDS);
        return snapshot.getLong("counter");
    }, options);
    assertEquals(1, runTransaction.get(10, TimeUnit.SECONDS).longValue());
    DocumentSnapshot documentSnapshot = documentReference.get().get();
    assertEquals(2, (long) documentSnapshot.getData().get("counter"));
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) TransactionOptions(com.google.cloud.firestore.TransactionOptions) Timestamp(com.google.cloud.Timestamp) DocumentReference(com.google.cloud.firestore.DocumentReference) Test(org.junit.Test)

Aggregations

DocumentReference (com.google.cloud.firestore.DocumentReference)2 TransactionOptions (com.google.cloud.firestore.TransactionOptions)2 Test (org.junit.Test)2 Timestamp (com.google.cloud.Timestamp)1 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)1 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)1 Status (io.grpc.Status)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 ExecutionException (java.util.concurrent.ExecutionException)1