/** * Need this for beingTransaction() */ private class TransactionFutureWrapper extends SimpleFutureWrapper<Transaction, Transaction> { TransactionWrapper xact; public TransactionFutureWrapper(Future<Transaction> base) { super(base); } @Override protected Transaction wrap(Transaction t) { if (xact == null) xact = new TransactionWrapper(memcache, t); return xact; } }
public Future<Key> run(AsyncDatastoreService ads) { Future<Key> k1 = ads.put(e1); Assert.assertNotNull(k1); return k1; } };
Future<List<com.google.appengine.api.datastore.Key>> raw = this.ads.put(this.txn, entityList); Future<Map<Key<T>, T>> wrapped = new SimpleFutureWrapper<List<com.google.appengine.api.datastore.Key>, Map<Key<T>, T>>(raw) { @Override
@SuppressWarnings("unchecked") private <T> FutureObject<T> saveEntityAsync(ObjectHolder objectHolder, Entity entity) { Future<Key> futureKey = asyncDatastore().put(entity); return new FutureObject<>(r, new FutureKeyToIdRef(r, futureKey, objectHolder.getModel()), (T) objectHolder.getObject()); }
@Test public void testFutureCancel() { clearData(); AsyncDatastoreService asyncService = DatastoreServiceFactory.getAsyncDatastoreService(); Entity newRec = new Entity(ASYNC_ENTITY); newRec.setProperty("count", -1); newRec.setProperty("timestamp", new Date()); Future<Key> future = asyncService.put(newRec); future.cancel(true); // The actual call may already succeeded, so just verify that cancel has been called. assertTrue(future.isCancelled()); }
@Test public void testDataMultiplePut() throws Exception { Entity parent = createTestEntityWithUniqueMethodNameKey(ASYNC_ENTITY, "testDataMultiplePut"); Key key = parent.getKey(); final int recordCount = 10; List<Entity> entityList = new ArrayList<Entity>(); for (int i = 0; i < recordCount; i++) { Entity newRec = new Entity(ASYNC_ENTITY, key); newRec.setProperty("count", i); newRec.setProperty("timestamp", new Date()); entityList.add(newRec); } Future<List<Key>> eKeys = asyncService.put(entityList); eKeys.get(); assertTaskIsDoneAndNotCancelled(eKeys); assertEquals(recordCount, service.prepare(simpleQuery(key)).countEntities(withDefaults())); }
@Test public void testDataPut() throws Exception { Entity parent = createTestEntityWithUniqueMethodNameKey(ASYNC_ENTITY, "testDataPut"); Key key = parent.getKey(); Entity newRec = new Entity(ASYNC_ENTITY, key); newRec.setProperty("count", 0); newRec.setProperty("timestamp", new Date()); Future<Key> future = asyncService.put(newRec); future.get(); assertTaskIsDoneAndNotCancelled(future); assertEquals(1, service.prepare(simpleQuery(key)).countEntities(withDefaults())); }
@Test public void testCRUD() throws Exception { AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); Entity e1 = new Entity("ASYNC"); e1.setProperty("foo", "bar"); Future<Key> k1 = service.put(e1); Assert.assertNotNull(k1); Key key = k1.get(); Assert.assertNotNull(key); Future<Entity> fe1 = service.get(key); Assert.assertNotNull(fe1); Assert.assertEquals(e1, fe1.get()); Future<Void> fd1 = service.delete(key); Assert.assertNotNull(fd1); fd1.get(); assertStoreDoesNotContain(key); }
newRec.setProperty("timestamp", new Date()); Transaction trans = asyncService.beginTransaction().get(); Future<Key> firstE = asyncService.put(trans, newRec); trans.rollback(); assertEquals(0, service.prepare(new Query(ASYNC_ENTITY)).countEntities(withDefaults())); Future<List<Key>> eKeys = asyncService.put(trans, lisRec); trans.commit(); List<Key> realKey = eKeys.get();
@Test public void testRollbackTx() throws Exception { AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); Transaction tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withDefaults())); Key key = null; try { Future<Key> fKey = service.put(tx, new Entity("AsyncTx")); key = waitOnFuture(fKey); } finally { waitOnFuture(tx.rollbackAsync()); } if (key != null) { Assert.assertNull(getSingleEntity(service, key)); } } }
@Test public void testCommitTx() throws Exception { AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); Transaction tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withDefaults())); Key key; try { Future<Key> fKey = service.put(tx, new Entity("AsyncTx")); key = waitOnFuture(fKey); waitOnFuture(tx.commitAsync()); } catch (Exception e) { waitOnFuture(tx.rollbackAsync()); throw e; } if (key != null) { Assert.assertNotNull(getSingleEntity(service, key)); } }