1 package org.jboss.cache.loader; 2 3 import junit.framework.Assert; 4 import junit.framework.Test; 5 import junit.framework.TestSuite; 6 import org.jboss.cache.CacheException; 7 import org.jboss.cache.CacheImpl; 8 import org.jboss.cache.Fqn; 9 import org.jboss.cache.Modification; 10 import org.jboss.cache.config.Configuration; 11 import org.jboss.cache.statetransfer.StateTransferManager; 12 import org.jboss.util.stream.MarshalledValueInputStream; 13 import org.jboss.util.stream.MarshalledValueOutputStream; 14 15 import java.io.ByteArrayInputStream ; 16 import java.io.ByteArrayOutputStream ; 17 import java.util.Collections ; 18 import java.util.HashMap ; 19 20 public class AsyncFileCacheLoaderTest extends AbstractCacheLoaderTestBase 21 { 22 23 private CacheImpl cache; 24 25 protected void configureCache() throws Exception ![JavaDoc](../../../../../cmn/javadoc.gif) 26 { 27 configureCache(""); 28 } 29 30 protected void configureCache(String props) throws Exception ![JavaDoc](../../../../../cmn/javadoc.gif) 31 { 32 cache = new CacheImpl(); 33 cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL); 34 cache.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", 36 props, true, false, true)); 37 cache.create(); 38 cache.start(); 39 } 40 41 public static Test suite() 42 { 43 return new TestSuite(AsyncFileCacheLoaderTest.class); 44 } 45 46 protected void tearDown() throws Exception ![JavaDoc](../../../../../cmn/javadoc.gif) 47 { 48 if (cache != null) 49 { 50 cache.stop(); 51 } 52 } 53 54 public void testRestrictionOnAddingToQueue() throws Exception ![JavaDoc](../../../../../cmn/javadoc.gif) 55 { 56 configureCache(); 57 CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader(); 58 loader.remove(Fqn.fromString("/blah")); 59 60 loader.put(Fqn.fromString("/blah"), "one", "two"); 61 loader.put(Fqn.fromString("/blah"), "three", "four"); 62 loader.put(Fqn.fromString("/blah"), "five", "six"); 63 loader.put(Fqn.fromString("/blah"), "seven", "eight"); 64 65 loader.stop(); 67 try 68 { 69 loader.remove(Fqn.fromString("/blah")); 70 Assert.assertTrue("Should have restricted this entry from being made", false); 71 } 72 catch (CacheException e) 73 { 74 Assert.assertTrue(true); 75 } 76 77 loader.start(); 79 loader.remove(Fqn.fromString("/blah")); 80 } 81 82 public void testPutImmediate() throws Exception ![JavaDoc](../../../../../cmn/javadoc.gif) 83 { 84 configureCache( 85 "cache.async.put=false\n" + 86 "cache.async.pollWait=10000\n" + 87 ""); 88 CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader(); 89 Fqn fqn = Fqn.fromString("/a/b/c/d"); 90 HashMap map = new HashMap (); 91 map.put("c", "d"); 92 Modification mod = new Modification(Modification.ModificationType.PUT_KEY_VALUE, fqn, "e", "f"); 94 loader.put(fqn, "a", "b"); 95 loader.put(fqn, map); 96 loader.put(Collections.singletonList(mod)); 97 assertEquals("put right away", 3, loader.get(fqn).size()); 98 loader.remove(fqn); 99 } 100 101 public void testBounded() throws Exception ![JavaDoc](../../../../../cmn/javadoc.gif) 102 { 103 configureCache( 104 "cache.async.queueSize=1\n" + 105 "cache.async.pollWait=10\n" + 106 ""); 107 CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader(); 108 Fqn fqn = Fqn.fromString("/bound"); 109 loader.remove(fqn); 110 for (int i = 0; i < 100; i++) 112 { 113 cache.put(fqn, "key" + i, "value1"); 114 } 115 Thread.sleep(1000); 116 assertEquals(100, loader.get(fqn).size()); 117 loader.remove(fqn); 118 } 119 120 public void testNoReturnOld() throws Exception ![JavaDoc](../../../../../cmn/javadoc.gif) 121 { 122 configureCache( 123 "cache.async.returnOld=false\n" + 124 "cache.async.pollWait=10\n" + 125 ""); 126 CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader(); 127 System.out.println("Loader " + loader); 128 cache.put(Fqn.ROOT, "key1", "value1"); 129 Thread.sleep(100); 130 assertEquals(null, loader.put(Fqn.ROOT, "key1", "value1")); 131 assertEquals(null, loader.remove(Fqn.ROOT, "key1")); 132 loader.remove(Fqn.ROOT); 133 } 134 135 public void testStoreState() throws Exception ![JavaDoc](../../../../../cmn/javadoc.gif) 136 { 137 configureCache(); 138 Fqn X = Fqn.fromString("/x"); 139 CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader(); 140 loader.remove(X); 141 cache.put(X, "key1", "value1"); 142 Thread.sleep(1000); 143 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 144 MarshalledValueOutputStream os = new MarshalledValueOutputStream(baos); 145 loader.loadEntireState(os); 146 os.writeObject(StateTransferManager.STREAMING_DELIMETER_NODE); 147 assertTrue(baos.size() > 0); 149 loader.remove(X); 150 151 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 152 MarshalledValueInputStream is = new MarshalledValueInputStream(bais); 153 loader.storeEntireState(is); 154 assertEquals("X found", true, loader.exists(X)); 156 loader.remove(X); 157 } 158 159 191 192 } 193 | Popular Tags |