com.google.cloud.datastore.Datastore.TransactionCallable Java Examples

The following examples show how to use com.google.cloud.datastore.Datastore.TransactionCallable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void performTransactionTest() {

	DatastoreReaderWriter transactionContext = mock(DatastoreReaderWriter.class);

	when(this.datastore.runInTransaction(any())).thenAnswer((invocation) -> {
		TransactionCallable<String> callable = invocation.getArgument(0);
		return callable.run(transactionContext);
	});

	List<Entity> e1 = Collections
			.singletonList(this.e1);
	when(transactionContext.fetch(ArgumentMatchers.<Key[]>any())).thenReturn(e1);

	String finalResult = this.datastoreTemplate
			.performTransaction((datastoreOperations) -> {
				datastoreOperations.save(this.ob2);
				datastoreOperations.findById("ignored", TestEntity.class);
				return "all done";
			});

	assertThat(finalResult).isEqualTo("all done");
	verify(transactionContext, times(1)).put(ArgumentMatchers.<FullEntity[]>any());
	verify(transactionContext, times(2)).fetch((Key[]) any());
}
 
Example #2
Source File: InstrumentedDatastoreTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void runInTransaction() {
  when(datastore.runInTransaction(any())).then(a ->
      a.<TransactionCallable<String>>getArgument(0)
          .run(transaction));

  var foobar = instrumentedDatastore.runInTransaction(tx -> {
    testReaderWriter(tx, transaction);
    return "foobar";
  });

  verify(datastore).runInTransaction(any());
  verifyNoMoreInteractions(datastore);

  assertThat(foobar, is("foobar"));
}
 
Example #3
Source File: InstrumentedDatastoreTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void runInTransactionWithOptions() {
  when(datastore.runInTransaction(any(), any())).then(a ->
      a.<TransactionCallable<String>>getArgument(0)
          .run(transaction));

  var transactionOptions = TransactionOptions.newBuilder().build();

  var foobar = instrumentedDatastore.runInTransaction(tx -> {
    testReaderWriter(tx, transaction);
    return "foobar";
  }, transactionOptions);

  verify(datastore).runInTransaction(any(), eq(transactionOptions));
  verifyNoMoreInteractions(datastore);

  assertThat(foobar, is("foobar"));
}
 
Example #4
Source File: DatastoreSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of running in a transaction. */
// [TARGET runInTransaction(TransactionCallable)]
// [VARIABLE "my_callable_result"]
public String runInTransaction(final String callableResult) {
  // [START runInTransaction]
  TransactionCallable<String> callable =
      new TransactionCallable<String>() {
        public String run(DatastoreReaderWriter readerWriter) {
          // use readerWriter to run in transaction
          return callableResult;
        }
      };
  String result = datastore.runInTransaction(callable);
  // [END runInTransaction]
  return result;
}