The code is really simply, but here the idea:
The idea is to simulate dataStore latency in development servers, so you can
run your test and now
if all is ok, and if something is wrong, you can debug it in your computer.
Also helps to find Concurrency problems.
Are all yours green tests ready??? :P
How to use:
PersistenceManager pmToDecorate = PMF.get().getPersistenceManager();
PersistenceManager pm = new
LatencyPersistenceManagerDecorator(pmToDecorate);
NOTE: it doesn't send all the messages to the concrete persistenceManager,
it sends those which are relevant in my application, so those who aren't
send throw a runtime exception with message "unsupported".
It easy to fix that for your application!! example:
@Override
public Transaction currentTransaction() {
throw new RuntimeException("Unsuported operation");
}
should be like this:
@Override
public Transaction currentTransaction() {
return *pm*.currentTransaction();
}
//////// CODE //////////////////////
import java.util.Collection;
import java.util.Date;
import java.util.EnumSet;
import java.util.Random;
import java.util.Set;
import javax.jdo.Extent;
import javax.jdo.FetchGroup;
import javax.jdo.FetchPlan;
import javax.jdo.JDOException;
import javax.jdo.ObjectState;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;
import javax.jdo.datastore.JDOConnection;
import javax.jdo.datastore.Sequence;
import javax.jdo.listener.InstanceLifecycleListener;
public class LatencyPersistenceManagerDecorator implements
PersistenceManager {
private PersistenceManager pm;
private Random random = new Random();
public LatencyPersistenceManagerDecorator(PersistenceManager
aPersistentManager) {
this.pm = aPersistentManager;
}
@Override
public void addInstanceLifecycleListener(InstanceLifecycleListener
listener, Class... classes) {
this.pm.addInstanceLifecycleListener(listener, classes);
}
@Override
public void checkConsistency() {
throw new RuntimeException("Unsuported operation");
}
@Override
public void close() {
this.pm.close();
}
@Override
public Transaction currentTransaction() {
return pm.currentTransaction();
}
@Override
public void deletePersistent(Object pc) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void deletePersistentAll(Object... pcs) {
pm.deletePersistentAll(pcs);
}
@Override
public void deletePersistentAll(Collection pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public <T> T detachCopy(T pc) {
return pm.detachCopy(pc);
}
@Override
public <T> Collection<T> detachCopyAll(Collection<T> pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public <T> T[] detachCopyAll(T... pcs) {
return pm.detachCopyAll(pcs);
}
@Override
public void evict(Object pc) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void evictAll() {
throw new RuntimeException("Unsuported operation");
}
@Override
public void evictAll(Object... pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void evictAll(Collection pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void evictAll(boolean subclasses, Class pcClass) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void flush() {
throw new RuntimeException("Unsuported operation");
}
@Override
public boolean getCopyOnAttach() {
throw new RuntimeException("Unsuported operation");
}
@Override
public JDOConnection getDataStoreConnection() {
throw new RuntimeException("Unsuported operation");
}
@Override
public boolean getDetachAllOnCommit() {
throw new RuntimeException("Unsuported operation");
}
@Override
public <T> Extent<T> getExtent(Class<T> persistenceCapableClass) {
throw new RuntimeException("Unsuported operation");
}
@Override
public <T> Extent<T> getExtent(Class<T> persistenceCapableClass, boolean
subclasses) {
throw new RuntimeException("Unsuported operation");
}
@Override
public FetchGroup getFetchGroup(Class cls, String name) {
throw new RuntimeException("Unsuported operation");
}
@Override
public FetchPlan getFetchPlan() {
throw new RuntimeException("Unsuported operation");
}
@Override
public boolean getIgnoreCache() {
throw new RuntimeException("Unsuported operation");
}
@Override
public Set getManagedObjects() {
throw new RuntimeException("Unsuported operation");
}
@Override
public Set getManagedObjects(EnumSet<ObjectState> states) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Set getManagedObjects(Class... classes) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Set getManagedObjects(EnumSet<ObjectState> states, Class...
classes) {
throw new RuntimeException("Unsuported operation");
}
@Override
public boolean getMultithreaded() {
throw new RuntimeException("Unsuported operation");
}
@Override
public Object getObjectById(Object oid) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Object getObjectById(Object oid, boolean validate) {
throw new RuntimeException("Unsuported operation");
}
@Override
public <T> T getObjectById(Class<T> cls, Object key) {
return pm.getObjectById(cls, key);
}
@Override
public Object getObjectId(Object pc) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Class getObjectIdClass(Class cls) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Collection getObjectsById(Collection oids) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Object[] getObjectsById(Object... oids) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Collection getObjectsById(Collection oids, boolean validate) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Object[] getObjectsById(Object[] oids, boolean validate) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Object[] getObjectsById(boolean validate, Object... oids) {
throw new RuntimeException("Unsuported operation");
}
@Override
public PersistenceManagerFactory getPersistenceManagerFactory() {
throw new RuntimeException("Unsuported operation");
}
@Override
public Integer getQueryTimeoutMillis() {
throw new RuntimeException("Unsuported operation");
}
@Override
public Sequence getSequence(String name) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Date getServerDate() {
throw new RuntimeException("Unsuported operation");
}
@Override
public Object getTransactionalObjectId(Object pc) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Object getUserObject() {
throw new RuntimeException("Unsuported operation");
}
@Override
public Object getUserObject(Object key) {
throw new RuntimeException("Unsuported operation");
}
@Override
public boolean isClosed() {
throw new RuntimeException("Unsuported operation");
}
@Override
public void makeNontransactional(Object pc) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void makeNontransactionalAll(Object... pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void makeNontransactionalAll(Collection pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public <T> T makePersistent(T pc) {
waitRandomTimeForLatency();
return pm.makePersistent(pc);
}
@Override
public <T> T[] makePersistentAll(T... pcs) {
waitRandomTimeForLatency();
return pm.makePersistentAll(pcs);
}
@Override
public <T> Collection<T> makePersistentAll(Collection<T> pcs) {
waitRandomTimeForLatency();
return pm.makePersistentAll(pcs);
}
@Override
public void makeTransactional(Object pc) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void makeTransactionalAll(Object... pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void makeTransactionalAll(Collection pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void makeTransient(Object pc) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void makeTransient(Object pc, boolean useFetchPlan) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void makeTransientAll(Object... pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void makeTransientAll(Collection pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void makeTransientAll(Object[] pcs, boolean useFetchPlan) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void makeTransientAll(boolean useFetchPlan, Object... pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void makeTransientAll(Collection pcs, boolean useFetchPlan) {
throw new RuntimeException("Unsuported operation");
}
@Override
public <T> T newInstance(Class<T> pcClass) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Query newNamedQuery(Class cls, String queryName) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Object newObjectIdInstance(Class pcClass, Object key) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Query newQuery() {
throw new RuntimeException("Unsuported operation");
}
@Override
public Query newQuery(Object compiled) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Query newQuery(String query) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Query newQuery(Class cls) {
waitRandomTimeForLatency();
return pm.newQuery(cls);
}
@Override
public Query newQuery(Extent cln) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Query newQuery(String language, Object query) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Query newQuery(Class cls, Collection cln) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Query newQuery(Class cls, String filter) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Query newQuery(Extent cln, String filter) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Query newQuery(Class cls, Collection cln, String filter) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Object putUserObject(Object key, Object val) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void refresh(Object pc) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void refreshAll() {
throw new RuntimeException("Unsuported operation");
}
@Override
public void refreshAll(Object... pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void refreshAll(Collection pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void refreshAll(JDOException jdoe) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void removeInstanceLifecycleListener(InstanceLifecycleListener
listener) {
throw new RuntimeException("Unsuported operation");
}
@Override
public Object removeUserObject(Object key) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void retrieve(Object pc) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void retrieve(Object pc, boolean useFetchPlan) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void retrieveAll(Collection pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void retrieveAll(Object... pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void retrieveAll(Collection pcs, boolean useFetchPlan) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void retrieveAll(Object[] pcs, boolean useFetchPlan) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void retrieveAll(boolean useFetchPlan, Object... pcs) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void setCopyOnAttach(boolean flag) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void setDetachAllOnCommit(boolean flag) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void setIgnoreCache(boolean flag) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void setMultithreaded(boolean flag) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void setQueryTimeoutMillis(Integer interval) {
throw new RuntimeException("Unsuported operation");
}
@Override
public void setUserObject(Object o) {
throw new RuntimeException("Unsuported operation");
}
private void waitRandomTimeForLatency() {
try {
int nextInt = random.nextInt(120);
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.