Testing SLSB methods that make agent calls

John Sanda jsanda at redhat.com
Thu Oct 27 01:57:32 UTC 2011


We have a lot of tests for SLSB methods that involve making agent calls. 
A lot of these tests use stub implementations of the agent service 
interfaces. Three times recently I got bit by trying to send objects 
that were still attached to the persistence context to the agent. This 
resulted in the following (now very familiar) exception,

Caused by: org.hibernate.PersistentObjectException: detached entity 
passed to persist: org.rhq.core.domain.configuration.Configuration

Once I saw what was going on the problem was easy to fix. One thing 
bothered me though. I have been putting a lot of effort into producing 
thorough tests for the drift SLSB code, and I wasn't able to reproduce 
this in my automated tests. Jay made the very shrewd suggestion of using 
HibernateDetachUtility in my agent service stubs. Doing so allowed me to 
reproduce the same failures I hit during live, end-to-end testing. Here 
is one example of what I did,

agentServiceContainer.driftService = new TestDefService() {
     @Override
     public void unscheduleDriftDetection(int resourceId, 
DriftDefinition driftDef) {
         detach(driftDef);
     }

     @Override
     public void updateDriftDetection(int resourceId, DriftDefinition 
driftDef) {
         detach(driftDef);
     }

     @Override
     public void updateDriftDetection(int resourceId, DriftDefinition 
driftDef, DriftSnapshot driftSnapshot) {
         detach(driftDef);
         detach(driftSnapshot);
     }

     private void detach(Object object) {
         try {
             HibernateDetachUtility.nullOutUninitializedFields(object, 
SERIALIZATION);
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }
};

With the above code in place I was able to reproduce the same 
PersistentObjectException. If you are writing or updating any tests that 
exercise code that makes calls to the agent, I strongly encourage you to 
follow Jay's suggestion as well. It will make the tests a lot more robust.

- John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://fedorahosted.org/pipermail/rhq-devel/attachments/20111026/9d78f7f3/attachment.html 


More information about the rhq-devel mailing list