[jboss-cvs] JBossAS SVN: r109463 - branches/JBPAPP_5_1_datagrid/testsuite/src/main/org/jboss/test/cluster/datagrid/test.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Nov 24 19:11:17 EST 2010


Author: rachmatowicz at jboss.com
Date: 2010-11-24 19:11:16 -0500 (Wed, 24 Nov 2010)
New Revision: 109463

Modified:
   branches/JBPAPP_5_1_datagrid/testsuite/src/main/org/jboss/test/cluster/datagrid/test/HotRodClientTestCase.java
Log:
Update HotRodClientTestCase

Modified: branches/JBPAPP_5_1_datagrid/testsuite/src/main/org/jboss/test/cluster/datagrid/test/HotRodClientTestCase.java
===================================================================
--- branches/JBPAPP_5_1_datagrid/testsuite/src/main/org/jboss/test/cluster/datagrid/test/HotRodClientTestCase.java	2010-11-24 20:52:45 UTC (rev 109462)
+++ branches/JBPAPP_5_1_datagrid/testsuite/src/main/org/jboss/test/cluster/datagrid/test/HotRodClientTestCase.java	2010-11-25 00:11:16 UTC (rev 109463)
@@ -26,6 +26,9 @@
 import java.util.Properties;
 import java.util.concurrent.TimeUnit;
 import java.util.Map;
+import java.util.HashMap;
+import java.util.Set;
+import java.util.concurrent.Future;
 
 import org.infinispan.client.hotrod.*;
 import org.infinispan.Cache;
@@ -37,6 +40,19 @@
 import org.infinispan.util.ByteArrayKey;            
 import org.infinispan.util.logging.Log;
 import org.infinispan.util.logging.LogFactory;
+import org.infinispan.util.concurrent.NotifyingFuture;
+import org.infinispan.notifications.Listener;
+// import org.infinispan.notifications.cachemanagerlistener.event.Event;
+import org.infinispan.notifications.cachemanagerlistener.annotation.ViewChanged;
+import org.infinispan.notifications.cachelistener.event.Event;
+import org.infinispan.notifications.cachelistener.annotation.CacheEntryModified;
+import org.infinispan.notifications.cachelistener.annotation.CacheEntryCreated;
+import org.infinispan.notifications.cachelistener.annotation.CacheEntryRemoved;
+import org.infinispan.notifications.cachelistener.annotation.CacheEntryVisited;
+import org.infinispan.notifications.cachelistener.annotation.CacheEntryLoaded;
+import org.infinispan.notifications.cachelistener.annotation.CacheEntryEvicted;
+import org.infinispan.notifications.cachelistener.annotation.CacheEntryActivated;
+import org.infinispan.notifications.cachelistener.annotation.CacheEntryPassivated;
 
 import org.jboss.test.JBossClusteredTestCase;
 import org.jboss.test.JBossTestSetup;
@@ -82,28 +98,28 @@
 		System.out.println("per test set up") ;
 		servers = getServers() ;
 		hotRodServerList = servers[0] + ":" + "11222" + ";" + servers[1] + ":" + "11222";
-		System.out.println("datagrid host0 = " + servers[0]) ;
-		System.out.println("datagrid host1 = " + servers[1]) ;		
-		System.out.println("HotRodServer list = " + hotRodServerList ) ;
 		
 		// get the Remote Cache Manager proxy from the server
 		remoteCacheManager = getRemoteCacheManager();
 		remoteCacheManager.start() ;
 
-		// get the default cache
+		// get the default cache and the named cache
 		defaultRemote = remoteCacheManager.getCache();
-		// get a named cache
 		remoteCache = remoteCacheManager.getCache(REPL_CACHE_NAME);
 		
-		printCacheContents(defaultRemote, "map of defaultCache elements at tearDown") ;
-		printCacheContents(remoteCache, "map of remoteCache elements at tearDown") ;
+		defaultRemote.clear();
+		remoteCache.clear();
+		
+		printCacheContents(defaultRemote, "map of defaultCache elements (setUp)") ;
+		printCacheContents(remoteCache, "map of remoteCache elements (setUp)") ;
 	}
 
 	public void tearDown() throws Exception {
 		System.out.println("per test tearing down") ;
 		
-		printCacheContents(defaultRemote, "map of defaultCache elements at tearDown") ;
-		printCacheContents(remoteCache, "map of remoteCache elements at tearDown") ;
+		// clean up the server side copies - they persist as long is the servers are up
+		defaultRemote.clear();
+		remoteCache.clear() ;
 		
 		remoteCacheManager.stop() ;
 		
@@ -265,26 +281,168 @@
 	}
 
 	/*
-	 * Test the remove operation
-	 * - put key/value, verify put value, remove key, verify key removed
+	 * Test the size operation
 	 */
-	public void testRemove() throws IOException {
+	public void testSize() throws IOException {
+		
+		assert remoteCache.size() == 0;
+		
 		assert null == remoteCache.put("aKey", "aValue");
-		assert remoteCache.get("aKey").equals("aValue");
+		assert remoteCache.containsKey("aKey");
+		assert remoteCache.size() == 1 ;
 
+		assert null == remoteCache.put("anotherKey", "anotherValue");
+		assert remoteCache.containsKey("anotherKey");
+		assert remoteCache.size() == 2 ;		
+		
+		assert null == remoteCache.remove("anotherKey");
+		assert !remoteCache.containsKey("anotherKey");
+		assert remoteCache.size() == 1 ;
+		
 		assert null == remoteCache.remove("aKey");
 		assert !remoteCache.containsKey("aKey");
-	}
-
+		assert remoteCache.size() == 0 ;
+	}	
+	
+	/*
+	 * Test the isEmpty operation
+	 */
+	public void testIsEmpty() throws IOException {
+		
+		assert remoteCache.isEmpty();
+		
+		assert null == remoteCache.put("aKey", "aValue");
+		assert remoteCache.containsKey("aKey");
+		assert !remoteCache.isEmpty();
+		
+		assert null == remoteCache.remove("aKey");
+		assert !remoteCache.containsKey("aKey");
+		assert remoteCache.isEmpty();		
+	}	
+	
+	
+	/*
+	 * Test the contains operations
+	 * - we start with empty cache
+	 */
 	public void testContains() {
 		assert !remoteCache.containsKey("aKey");
 		remoteCache.put("aKey", "aValue");
 		assert remoteCache.containsKey("aKey");
+		assert remoteCache.containsValue("aValue");
+		assert !remoteCache.containsValue("someOtherValue"); 
 	}
+	
+	/*
+	 * Test eviction of cache elements
+	 * - here, we assume there is no cache store available to persist the element if evicted
+	 */
+	public void testEviction() {
+		remoteCache.put("aKey", "aValue");
+		assert remoteCache.containsKey("aKey");
+		remoteCache.evict("aKey");
+		assert !remoteCache.containsKey("aKey");		
+	}
+	
+	/*
+	 * Test bulk put and get
+	 */
+	public void testBulkOperations() {
+		
+		Map<String,String> mapIn = new HashMap<String,String>() ;
+		Map<String,String> mapOut = new HashMap<String,String>() ;
+		mapOut.put("aKey", "aValue");
+		mapOut.put("bKey", "bValue");
+		mapOut.put("cKey", "cValue");
+		
+		remoteCache.putAll(mapOut);
+		mapIn = remoteCache.getBulk();
+		
+		assert mapIn.keySet().equals(mapOut.keySet());
+		assert mapIn.values().equals(mapOut.values());
+	}
+	
+	/*
+	 * Test bulk put and get with lifespan for eviction
+	 */
+	public void testBulkOperationsWithLifespan() {
+		
+		long lifespanInSecs = 3 ;
+		
+		Map<String,String> mapIn = new HashMap<String,String>() ;
+		Map<String,String> mapOut = new HashMap<String,String>() ;
+		mapOut.put("aKey", "aValue");
+		mapOut.put("bKey", "bValue");
+		mapOut.put("cKey", "cValue");
+		
+		remoteCache.putAll(mapOut, lifespanInSecs, TimeUnit.SECONDS);
 
+		// give the elements time to be evicted
+		sleepForSecs(lifespanInSecs) ;
+		
+		mapIn = remoteCache.getBulk();		
+		assert mapIn.size() == 0;
+	}
+
+	
+	/*
+	 * Test asynchronous bulk put and get
+	 */
+	public void testBulkOperationsAsync() {
+		
+		Map<String,String> mapIn = new HashMap<String,String>() ;
+		Map<String,String> mapOut = new HashMap<String,String>() ;
+		mapOut.put("aKey", "aValue");
+		mapOut.put("bKey", "bValue");
+		mapOut.put("cKey", "cValue");
+		
+		NotifyingFuture<Void> future = null ;
+		future = remoteCache.putAllAsync(mapOut);
+		
+		// wait for async operation to complelete
+		waitForNotifyingFutureToBeDone(future) ;		
+
+		mapIn = remoteCache.getBulk();
+		
+		assert mapIn.keySet().equals(mapOut.keySet());
+		assert mapIn.values().equals(mapOut.values());
+	}
+	
+	/*
+	 * Test bulk put and get with lifespan for eviction in async mode
+	 */
+	public void testBulkOperationsWithLifespanAsync() {
+		
+		long lifespanInSecs = 3 ;
+		
+		Map<String,String> mapIn = new HashMap<String,String>() ;
+		Map<String,String> mapOut = new HashMap<String,String>() ;
+		mapOut.put("aKey", "aValue");
+		mapOut.put("bKey", "bValue");
+		mapOut.put("cKey", "cValue");
+		
+		NotifyingFuture<Void> future = null ;
+		future = remoteCache.putAllAsync(mapOut, lifespanInSecs, TimeUnit.SECONDS);
+
+		waitForNotifyingFutureToBeDone(future) ;
+		
+		sleepForSecs(lifespanInSecs) ;
+				
+		mapIn = remoteCache.getBulk();
+		assert mapIn.size() == 0;
+	}
+	
+	
+	/*
+	 * Test the versioned cache entries
+	 * - check that versions differ even if the key value pairs are the same
+	 * - check that versions differ when key value pairs are different
+	 */
 	public void testGetVersionedCacheEntry() {
+		
 		VersionedValue value = remoteCache.getVersioned("aKey");
 		assertNull(remoteCache.getVersioned("aKey"), "expected null but received: " + value);
+		
 		remoteCache.put("aKey", "aValue");
 		assert remoteCache.get("aKey").equals("aValue");
 		VersionedValue valueBinary = remoteCache.getVersioned("aKey");
@@ -308,7 +466,13 @@
 		assert !entry3.equals(entry2);
 	}
 
+	/*
+	 * Test replace operation
+	 * - this replaces one value with another only if the key value pair exists
+	 * - returns the previous value, or null if no k/v pair
+	 */
 	public void testReplace() {
+		// this should return null, indicating no k/v pair in the map
 		assert null == remoteCache.replace("aKey", "anotherValue");
 		remoteCache.put("aKey", "aValue");
 		assert null == remoteCache.replace("aKey", "anotherValue");
@@ -316,22 +480,67 @@
 	}
 
 	/*
-	 * Tests replaceIfUnmodified operation
+	 * Tests replaceWithVersion operation
+	 * - replaces one value with another only if not modified since last time
+	 * version was read
 	 */
-	public void testReplaceIfUnmodified() {
+	public void testReplaceWithVersion() {
 		assert null == remoteCache.replace("aKey", "aValue");
 
 		remoteCache.put("aKey", "aValue");
 		VersionedValue valueBinary = remoteCache.getVersioned("aKey");
+		// replacement should take place (and so return true)
 		assert remoteCache.replaceWithVersion("aKey", "aNewValue", valueBinary.getVersion());
 
+		// version should have changed; value should have changed
 		VersionedValue entry2 = remoteCache.getVersioned("aKey");
 		assert entry2.getVersion() != valueBinary.getVersion();
 		assertEquals(entry2.getValue(), "aNewValue");
 
+		// replacement should not take place because we have changed the value
 		assert !remoteCache.replaceWithVersion("aKey", "aNewValue", valueBinary.getVersion());
 	}
 
+	/*
+	 * Tests replaceWithVersion operation in async mode
+	 * - replaces one value with another only if not modified since last time
+	 * version was read
+	 */
+	public void testReplaceWithVersionAsync() throws Exception {
+		assert null == remoteCache.replace("aKey", "aValue");
+
+		remoteCache.put("aKey", "aValue");
+		VersionedValue valueBinary = remoteCache.getVersioned("aKey");
+		// replacement should take place (and so return true)
+		NotifyingFuture<Boolean> future = null ;
+		future = remoteCache.replaceWithVersionAsync("aKey", "aNewValue", valueBinary.getVersion());
+		assert future.get() ;
+		
+		// version should have changed; value should have changed
+		VersionedValue entry2 = remoteCache.getVersioned("aKey");
+		assert entry2.getVersion() != valueBinary.getVersion();
+		assertEquals(entry2.getValue(), "aNewValue");
+
+		// replacement should not take place because we have changed the value
+		future = remoteCache.replaceWithVersionAsync("aKey", "aNewValue", valueBinary.getVersion());
+		assert !future.get() ;
+	}
+	
+	/* to here */
+	
+	/*
+	 * Test the remove operation
+	 * - put key/value, verify put value, remove key, verify key removed
+	 */
+	public void testRemove() throws IOException {
+		assert null == remoteCache.put("aKey", "aValue");
+		assert remoteCache.get("aKey").equals("aValue");
+
+		assert null == remoteCache.remove("aKey");
+		assert !remoteCache.containsKey("aKey");
+	}
+
+
 	public void testRemoveIfUnmodified() {
 		assert !remoteCache.removeWithVersion("aKey", 12321212l);
 
@@ -348,17 +557,23 @@
 		assert  !remoteCache.removeWithVersion("aKey", valueBinary.getVersion());
 	}
 
+	/*
+	 * Test putIfAbsent operatiion, which only puts if the key is not already associated
+	 * with a value
+	 */
 	public void testPutIfAbsent() {
-		remoteCache.put("aKey", "aValue");
+		
+		remoteCache.putIfAbsent("aKey", "aValue");
+		assert remoteCache.size() == 1;
+		assertEquals(remoteCache.get("aKey"),"aValue");		
+		
 		assert null == remoteCache.putIfAbsent("aKey", "anotherValue");
 		assertEquals(remoteCache.get("aKey"),"aValue");
-
-		assertEquals(remoteCache.get("aKey"),"aValue");
-		assert remoteCache.containsKey("aKey");
-
-		assert true : remoteCache.replace("aKey", "anotherValue");
 	}
 
+	/*
+	 * Tests the cache clear operation
+	 */
 	public void testClear() {
 		remoteCache.put("aKey", "aValue");
 		remoteCache.put("aKey2", "aValue");
@@ -366,32 +581,100 @@
 		assert !remoteCache.containsKey("aKey");
 		assert !remoteCache.containsKey("aKey2");
 	}
+	
 
 	/*
-	   private void assertCacheContains(Cache cache, String key, String value) throws IOException {
-	      Marshaller marshaller = new JBossMarshaller();
-	      byte[] keyBytes = marshaller.objectToByteBuffer(key, 64);
-	      byte[] valueBytes = marshaller.objectToByteBuffer(value, 64);
-	      ByteArrayKey cacheKey = new ByteArrayKey(keyBytes);
-	      CacheValue cacheValue = (CacheValue) cache.get(cacheKey);
-	      if (value == null) {
-	         assert cacheValue == null : "Expected null value but received: " + cacheValue;
-	      } else {
-	         assert Arrays.equals(valueBytes, (byte[])cacheValue.data());
-	      }
-	   }
+	 * Tests getting a pointer to the originating RemoteCachemanager instance
 	 */
+	public void testGetRemoteCacheManager() {
+		
+		RemoteCacheManager manager = null ;
+		
+		manager = defaultRemote.getRemoteCacheManager() ;
+		assert manager == this.remoteCacheManager : "getRemoteCachemanager() returned incorrect value" ;
+
+		manager = remoteCache.getRemoteCacheManager() ;
+		assert manager == this.remoteCacheManager : "getRemoteCachemanager() returned incorrect value" ;
+	}
+
+	public void testStats() {
+		
+		ServerStatistics defaultStats = defaultRemote.stats();
+		assert null != defaultStats ;
+		System.out.println("default stats = " + defaultStats.getStatsMap()) ;
+		
+		ServerStatistics remoteStats = remoteCache.stats();
+		assert null != remoteStats ;
+		System.out.println("named stats = " + remoteStats.getStatsMap()) ;
+	}
 	
+	
+	/*
+	 * Looks as thought CacheListeners are not supported with RemoteCache
+	 */
+	public void XtestCacheListener() {
+		
+		SingleEventListener listener = new SingleEventListener();
+		Set<Object> listeners = null ;
+		
+		remoteCache.addListener(listener);
+		listeners = remoteCache.getListeners() ;
+		assert listeners.size() == 1 ;
+		
+		remoteCache.put("aKey", "aValue");
+		org.infinispan.notifications.cachelistener.event.Event e = listener.getLastEvent() ;
+		assert null != e ;
+		assert (e.getType() == org.infinispan.notifications.cachelistener.event.Event.Type.CACHE_ENTRY_CREATED || 
+				e.getType() == org.infinispan.notifications.cachelistener.event.Event.Type.CACHE_ENTRY_MODIFIED) ;
+		
+		remoteCache.removeListener(listener);
+		listeners = remoteCache.getListeners() ;
+		assert listeners.size() == 0 ;		
+	}
+			
 	private void assertNull(Object obj, String msg) {
 		assert obj == null : msg ;
 	}
+
+	private void sleepForSecs(long numSecs) {
+		// give the elements time to be evicted
+		try {
+			Thread.sleep(numSecs * 1000) ;
+		}
+		catch(InterruptedException e) {
+		}
+	}
 	
+	
+	private void waitForNotifyingFutureToBeDone(NotifyingFuture future) {
+		// wait for operation to complete
+		while (!future.isDone()) {
+			// wait a bit
+			try {
+				Thread.sleep(1000) ;
+			}
+			catch(InterruptedException e) {
+			}
+		}		
+	}
+	
+//	private void waitForNotifyingFutureToBeDone(NotifyingFuture<Void> future) {
+//		// wait for operation to complete
+//		while (!future.isDone()) {
+//			// wait a bit
+//			try {
+//				Thread.sleep(1000) ;
+//			}
+//			catch(InterruptedException e) {
+//			}
+//		}		
+//	}	
+	
 	private void printCacheContents(RemoteCache c, String description) {
         Map<String,String> cacheElements = c.getBulk();
         System.out.println(description + " : " + cacheElements);
 	}
 	
-	
     public static Test suite() throws Exception {
         return HotRodClientTestSetup.getDeploySetup(HotRodClientTestCase.class,"") ;
     }
@@ -461,7 +744,46 @@
 	}
 }
 
+ at Listener
+class SingleEventListener {
+	
+	Event event = null ;
+	
+	public Event getLastEvent() {
+		return event ;
+	}
+	
+	@CacheEntryModified
+	@CacheEntryCreated
+	@CacheEntryLoaded
+	@CacheEntryVisited
+	public void logAccessEvent(Event event) {
+		this.event = event ;
+		System.out.println("Listener got access event: " + event) ;
+	}
 
+	@CacheEntryActivated
+	@CacheEntryPassivated
+	public void logPassiveActiveEvent(Event event) {
+		this.event = event ;
+		System.out.println("Listener got passivate/activate event: " + event) ;
+	}
 
+	@CacheEntryLoaded
+	@CacheEntryEvicted
+	public void logLoadedEvictedEvent(Event event) {
+		this.event = event ;
+		System.out.println("Listener got evict/load event: " + event) ;
+	}
 
+	@ViewChanged
+	public void logViewEvent(Event event) {
+		this.event = event ;
+		System.out.println("Listener got view event: " + event) ;
+	}
+}
 
+
+
+
+



More information about the jboss-cvs-commits mailing list