1 17 18 package org.sape.carbon.services.clustering.test; 19 20 import java.util.Random ; 21 22 import org.sape.carbon.core.component.Lookup; 23 import org.sape.carbon.services.clustering.ClusterCache; 24 25 import junit.extensions.ActiveTestSuite; 26 import junit.framework.Test; 27 import junit.framework.TestCase; 28 import junit.framework.TestSuite; 29 30 39 public class ClusterCacheTest extends TestCase { 40 41 public ClusterCacheTest(String name) { 42 super(name); 43 } 44 45 public void testCache() { 46 47 final int SET_COUNT = 5; 48 final int SET_SIZE = 10; 49 50 long[] crcs = new long[SET_SIZE]; 51 52 53 54 try { 55 ClusterCache cache = 56 (ClusterCache) 57 Lookup.getInstance().fetchComponent( 58 "/clustering/ClusterCache"); 59 60 Random rand = new Random (System.currentTimeMillis()); 61 int offset = rand.nextInt(); 62 System.out.println("Testing with offset: " + offset); 63 64 for (int i = 0; i < SET_COUNT; i++) { 65 66 for (int j = 0; j < SET_SIZE; j++) { 67 TestObject data = new TestObject(); 68 crcs[j] = data.getCrc(); 69 cache.put(new Integer (j+offset),data); 70 delay(); 71 } 72 System.out.println("Finished phase: 1 round: " + i); 73 System.out.println("Current Cluster Cache Size: " + cache.size()); 74 75 for (int j = 0; j < SET_SIZE; j++) { 76 TestObject data = (TestObject) cache.get(new Integer (j+offset)); 77 if (data == null) { 78 fail("Unable to retrieve keyed object at key: " + 79 (j+offset)); 80 } 81 long crc = data.getCrc(); 82 if (crcs[j] != crc) { 83 fail("Data object retrieved had different value then the one put in. " + 84 "In Crc: " + crcs[j] + " Out Crc: " + crc); 85 } 86 delay(); 87 } 88 System.out.println("Finished phase: 2 round: " + i); 89 System.out.println("Current Cluster Cache Size: " + cache.size()); 90 91 for (int j = 0; j < SET_SIZE; j++) { 92 TestObject data = (TestObject)cache.remove(new Integer (j+offset)); 93 long crc = data.getCrc(); 94 if (crcs[j] != crc) { 95 fail("Data object retrieved had different value then the one put in. " + 96 "In Crc: " + crcs[j] + " Out Crc: " + crc); 97 } 98 delay(); 99 } 100 System.out.println("Finished phase: 3 round: " + i); 101 System.out.println("Current Cluster Cache Size: " + cache.size()); 102 103 } 104 } catch (Exception e) { 105 e.printStackTrace(); 106 } 107 } 108 109 private void delay() { 110 try { 111 Thread.yield(); 112 Thread.sleep(10); 113 } catch (InterruptedException ie) { } 114 } 115 116 120 public static Test suite() { 121 TestSuite masterSuite = new TestSuite(); 122 123 Test singleThreadedTests = getSingleThreadedTests(); 125 if(singleThreadedTests != null) { 126 masterSuite.addTest(singleThreadedTests); 127 } 128 129 Test multiThreadedTests = getMultiThreadedTests(); 131 if(multiThreadedTests != null) { 132 masterSuite.addTest(multiThreadedTests); 133 } 134 135 return masterSuite; 136 } 137 138 147 private static Test getSingleThreadedTests() { 148 TestSuite suite = new TestSuite(); 149 155 suite.addTest(new ClusterCacheTest("testCache")); 156 157 return suite; 158 } 159 160 169 private static Test getMultiThreadedTests() { 170 TestSuite suite = new ActiveTestSuite(); 171 177 return suite; 178 } 179 180 190 private static void addTest(TestSuite suite, String testName, int number) { 191 for(int count=0; count<number; count++) { 192 suite.addTest(new ClusterCacheTest(testName)); 193 } 194 } 195 } 196 | Popular Tags |