Search in sources :

Example 1 with BeginTransactionRequest

use of com.google.firestore.v1beta1.BeginTransactionRequest in project java-spanner by googleapis.

the class SpannerClientTest method beginTransactionTest.

@Test
public void beginTransactionTest() throws Exception {
    Transaction expectedResponse = Transaction.newBuilder().setId(ByteString.EMPTY).setReadTimestamp(Timestamp.newBuilder().build()).build();
    mockSpanner.addResponse(expectedResponse);
    SessionName session = SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]");
    TransactionOptions options = TransactionOptions.newBuilder().build();
    Transaction actualResponse = client.beginTransaction(session, options);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockSpanner.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    BeginTransactionRequest actualRequest = ((BeginTransactionRequest) actualRequests.get(0));
    Assert.assertEquals(session.toString(), actualRequest.getSession());
    Assert.assertEquals(options, actualRequest.getOptions());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) Transaction(com.google.spanner.v1.Transaction) TransactionOptions(com.google.spanner.v1.TransactionOptions) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) SessionName(com.google.spanner.v1.SessionName) Test(org.junit.Test)

Example 2 with BeginTransactionRequest

use of com.google.firestore.v1beta1.BeginTransactionRequest in project java-spanner by googleapis.

the class SpannerClientTest method beginTransactionTest2.

@Test
public void beginTransactionTest2() throws Exception {
    Transaction expectedResponse = Transaction.newBuilder().setId(ByteString.EMPTY).setReadTimestamp(Timestamp.newBuilder().build()).build();
    mockSpanner.addResponse(expectedResponse);
    String session = "session1984987798";
    TransactionOptions options = TransactionOptions.newBuilder().build();
    Transaction actualResponse = client.beginTransaction(session, options);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockSpanner.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    BeginTransactionRequest actualRequest = ((BeginTransactionRequest) actualRequests.get(0));
    Assert.assertEquals(session, actualRequest.getSession());
    Assert.assertEquals(options, actualRequest.getOptions());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) Transaction(com.google.spanner.v1.Transaction) TransactionOptions(com.google.spanner.v1.TransactionOptions) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 3 with BeginTransactionRequest

use of com.google.firestore.v1beta1.BeginTransactionRequest in project java-spanner by googleapis.

the class PartitionedDmlTransaction method initTransaction.

private ByteString initTransaction() {
    final BeginTransactionRequest request = BeginTransactionRequest.newBuilder().setSession(session.getName()).setOptions(TransactionOptions.newBuilder().setPartitionedDml(TransactionOptions.PartitionedDml.getDefaultInstance())).build();
    Transaction tx = rpc.beginTransaction(request, session.getOptions());
    if (tx.getId().isEmpty()) {
        throw SpannerExceptionFactory.newSpannerException(ErrorCode.INTERNAL, "Failed to init transaction, missing transaction id\n" + session.getName());
    }
    return tx.getId();
}
Also used : Transaction(com.google.spanner.v1.Transaction) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest)

Example 4 with BeginTransactionRequest

use of com.google.firestore.v1beta1.BeginTransactionRequest in project appengine-java-standard by GoogleCloudPlatform.

the class CloudDatastoreV1ClientImplTest method testRetries_MaxRetriesExceeded.

@Test
public void testRetries_MaxRetriesExceeded() throws Exception {
    BeginTransactionRequest req = BeginTransactionRequest.getDefaultInstance();
    CloudDatastoreV1ClientImpl client = new CloudDatastoreV1ClientImpl(datastore, 2);
    DatastoreException lastException = retryableException();
    when(datastore.beginTransaction(req)).thenThrow(retryableException()).thenThrow(retryableException()).thenThrow(lastException);
    try {
        client.beginTransaction(req).get();
        fail("expected exception");
    } catch (ExecutionException e) {
        assertThat(e).hasCauseThat().hasCauseThat().isEqualTo(lastException);
    }
}
Also used : BeginTransactionRequest(com.google.datastore.v1.BeginTransactionRequest) DatastoreException(com.google.datastore.v1.client.DatastoreException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 5 with BeginTransactionRequest

use of com.google.firestore.v1beta1.BeginTransactionRequest in project appengine-java-standard by GoogleCloudPlatform.

the class CloudDatastoreV1ClientImplTest method testStackTrace.

@Test
public void testStackTrace() throws Throwable {
    BeginTransactionRequest req = BeginTransactionRequest.getDefaultInstance();
    when(datastore.beginTransaction(req)).thenThrow(new DatastoreException("beginTransaction", Code.INVALID_ARGUMENT, "message", null));
    CloudDatastoreV1ClientImpl client = newClient(datastore);
    Future<BeginTransactionResponse> future = makeBeginTransactionCallForTest(client, req);
    try {
        future.get();
        fail("expected exception");
    } catch (ExecutionException e) {
        assertThat(e).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
        // The test helper method used to call beginTransaction() appears in the error message
        // (even though it is no longer on the stack).
        assertThat(e).hasMessageThat().contains("makeBeginTransactionCallForTest");
        // The underlying cause is also present.
        assertThat(e).hasCauseThat().hasCauseThat().isInstanceOf(DatastoreException.class);
    }
}
Also used : BeginTransactionRequest(com.google.datastore.v1.BeginTransactionRequest) DatastoreException(com.google.datastore.v1.client.DatastoreException) ExecutionException(java.util.concurrent.ExecutionException) BeginTransactionResponse(com.google.datastore.v1.BeginTransactionResponse) Test(org.junit.Test)

Aggregations

BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)8 Transaction (com.google.spanner.v1.Transaction)7 Test (org.junit.Test)7 BeginTransactionRequest (com.google.datastore.v1.BeginTransactionRequest)5 ByteString (com.google.protobuf.ByteString)5 ExecutionException (java.util.concurrent.ExecutionException)5 DatastoreException (com.google.datastore.v1.client.DatastoreException)4 AbstractMessage (com.google.protobuf.AbstractMessage)4 BeginTransactionRequest (com.google.firestore.v1.BeginTransactionRequest)3 BeginTransactionResponse (com.google.firestore.v1.BeginTransactionResponse)3 Session (com.google.spanner.v1.Session)3 TransactionOptions (com.google.spanner.v1.TransactionOptions)3 CommitRequest (com.google.firestore.v1.CommitRequest)2 CommitResponse (com.google.firestore.v1.CommitResponse)2 FirestoreGrpc (com.google.firestore.v1.FirestoreGrpc)2 RollbackRequest (com.google.firestore.v1.RollbackRequest)2 TransactionOptions (com.google.firestore.v1.TransactionOptions)2 Empty (com.google.protobuf.Empty)2 Nullable (org.springframework.lang.Nullable)2 TransactionDefinition (org.springframework.transaction.TransactionDefinition)2