This content has been marked as final.
Show 5 replies
-
1. Re: Listener is not notified with nodeRemoved
belaban Aug 26, 2005 7:46 AM (in response to canghel)Can you provide a unit test ? TreeCacheListenerTest tests this, and we *do* get the notification. Can you take a look at TreeCacheListenerTest ?
-
2. Re: Listener is not notified with nodeRemoved
canghel Aug 26, 2005 9:26 AM (in response to canghel)I did not have the chance to look over the TreeCacheListener test but below I put the source of the unit test which fails with the problems I reported:
import junit.framework.TestCase; import org.jboss.cache.TreeCache; import org.jboss.cache.TreeCacheListener; import org.jboss.cache.Fqn; import org.jboss.cache.CacheException; import org.jgroups.View; import java.util.List; import java.util.ArrayList; /** * Responsible to test the {@link TreeCache} outside AS. * * @author Claudiu Anghel * @version 1.0 */ public class TreeCacheListenerTest extends TestCase { private TreeCache treeCache; protected void setUp() throws Exception { super.setUp(); treeCache = new UnitTestableTreeCache(); } public void testNodeCreated() throws Exception { SimpleTreeCacheListener listener = new SimpleTreeCacheListener(treeCache); treeCache.addTreeCacheListener(listener); String key = String.valueOf(System.currentTimeMillis()); treeCache.put("/UT/TestObjects/String:" + key, key, "TestValue"); assertNotNull("The added object could not be retrieved by cache listener upon nodeCreated event.", listener.getObjRetrievedByNodeCreated()); } public void testNodeRemoved() throws Exception { SimpleTreeCacheListener listener = new SimpleTreeCacheListener(treeCache); treeCache.addTreeCacheListener(listener); String key = String.valueOf(System.currentTimeMillis()); String fqn = "/UT/TestObjects/String:" + key; treeCache.put(fqn, key, "TestValue"); treeCache.remove(fqn, key); List removedNodeFqns = listener.getRemovedNodeFqns(); assertEquals("Wrong number of nodes are reported as removed.", 1, removedNodeFqns.size()); } private static class UnitTestableTreeCache extends TreeCache { public UnitTestableTreeCache() throws Exception { createInterceptorChain(); } } private static class SimpleTreeCacheListener implements TreeCacheListener { private TreeCache treeCache; private Object objRetrievedByNodeCreated; private List removedNodeFqns = new ArrayList(); public SimpleTreeCacheListener(TreeCache treeCache) { this.treeCache = treeCache; } public void nodeCreated(Fqn fqn) { try { if (fqn.size() < 3) { return; } objRetrievedByNodeCreated = treeCache.get(fqn, getKey(fqn)); } catch (CacheException e) { e.printStackTrace(); } } public void nodeRemoved(Fqn fqn) { if (fqn.size() < 3) { return; } removedNodeFqns.add(fqn); } public void nodeLoaded(Fqn fqn) { /* do nothing */ } public void nodeEvicted(Fqn fqn) { /* do nothing */ } public void nodeModified(Fqn fqn) { /* do nothing */ } public void nodeVisited(Fqn fqn) { /* do nothing */ } public void cacheStarted(TreeCache cache) { /* do nothing */ } public void cacheStopped(TreeCache cache) { /* do nothing */ } public void viewChange(View new_view) // might be MergeView after merging { /* do nothing */ } public Object getObjRetrievedByNodeCreated() { return objRetrievedByNodeCreated; } public List getRemovedNodeFqns() { return removedNodeFqns; } private String getKey(Fqn fqn) { String lastPart = (String) fqn.get(2); return lastPart.substring(lastPart.indexOf(":")); } } }
I hope you can give me more details based on this unit test.
thanks,
Claudiu -
3. Re: Listener is not notified with nodeRemoved
canghel Aug 26, 2005 9:40 AM (in response to canghel)I had a small bug in the UT; I fixed it and the UT still reproduces the problems:
import junit.framework.TestCase; import org.jboss.cache.TreeCache; import org.jboss.cache.TreeCacheListener; import org.jboss.cache.Fqn; import org.jboss.cache.CacheException; import org.jgroups.View; import java.util.List; import java.util.ArrayList; /** * Responsible to test the {@link TreeCache} outside AS. * * @author Claudiu Anghel * @version 1.0 */ public class TreeCacheListenerTest extends TestCase { private TreeCache treeCache; protected void setUp() throws Exception { super.setUp(); treeCache = new UnitTestableTreeCache(); } public void testNodeCreated() throws Exception { SimpleTreeCacheListener listener = new SimpleTreeCacheListener(treeCache); treeCache.addTreeCacheListener(listener); String key = String.valueOf(System.currentTimeMillis()); treeCache.put("/UT/TestObjects/String:" + key, key, "TestValue"); assertNotNull("The added object could not be retrieved by cache listener upon nodeCreated event.", listener.getObjRetrievedByNodeCreated()); } public void testNodeRemoved() throws Exception { SimpleTreeCacheListener listener = new SimpleTreeCacheListener(treeCache); treeCache.addTreeCacheListener(listener); String key = String.valueOf(System.currentTimeMillis()); String fqn = "/UT/TestObjects/String:" + key; treeCache.put(fqn, key, "TestValue"); treeCache.remove(fqn, key); List removedNodeFqns = listener.getRemovedNodeFqns(); assertEquals("Wrong number of nodes are reported as removed.", 1, removedNodeFqns.size()); } private static class UnitTestableTreeCache extends TreeCache { public UnitTestableTreeCache() throws Exception { createInterceptorChain(); } } private static class SimpleTreeCacheListener implements TreeCacheListener { private TreeCache treeCache; private Object objRetrievedByNodeCreated; private List removedNodeFqns = new ArrayList(); public SimpleTreeCacheListener(TreeCache treeCache) { this.treeCache = treeCache; } public void nodeCreated(Fqn fqn) { try { if (fqn.size() < 3) { return; } objRetrievedByNodeCreated = treeCache.get(fqn, getKey(fqn)); } catch (CacheException e) { e.printStackTrace(); } } public void nodeRemoved(Fqn fqn) { if (fqn.size() < 3) { return; } removedNodeFqns.add(fqn); } public void nodeLoaded(Fqn fqn) { /* do nothing */ } public void nodeEvicted(Fqn fqn) { /* do nothing */ } public void nodeModified(Fqn fqn) { /* do nothing */ } public void nodeVisited(Fqn fqn) { /* do nothing */ } public void cacheStarted(TreeCache cache) { /* do nothing */ } public void cacheStopped(TreeCache cache) { /* do nothing */ } public void viewChange(View new_view) // might be MergeView after merging { /* do nothing */ } public Object getObjRetrievedByNodeCreated() { return objRetrievedByNodeCreated; } public List getRemovedNodeFqns() { return removedNodeFqns; } private String getKey(Fqn fqn) { String lastPart = (String) fqn.get(2); return lastPart.substring(lastPart.indexOf(":") + 1); } } }
thanks,
Claudiu -
4. Re: Listener is not notified with nodeRemoved
canghel Aug 29, 2005 3:19 AM (in response to canghel)Hi,
I think in case of remove(String fqn, Object key) only nodeModified is called; nodeRemoved is called only for remove(Fqn fqn). Initially I thought that for all remove methods nodeRemoved should be called but it makes sense that nodeRemoved is called only in case of remove(Fqn fqn).
Please correct me if I am wrong.
thanks,
Claudiu -
5. Re: Listener is not notified with nodeRemoved
belaban Aug 29, 2005 3:45 AM (in response to canghel)Yes, those are the semantics