1 package org.shiftone.cache.decorator.miss; 2 3 4 5 import junit.framework.TestCase; 6 import org.shiftone.cache.Cache; 7 import org.shiftone.cache.CacheFactory; 8 import org.shiftone.cache.policy.fifo.FifoCacheFactory; 9 import org.shiftone.cache.util.Log; 10 11 12 16 public class MissTestCase extends TestCase 17 { 18 19 private static final Log LOG = new Log(MissTestCase.class); 20 private Cache theTestCache; 21 22 public MissHandlingCache createCache() 23 { 24 25 CacheFactory cacheFactory = new FifoCacheFactory(); 26 MissHandlingCacheFactory missCacheFactory = new MissHandlingCacheFactory(); 27 28 missCacheFactory.setDelegate(cacheFactory); 29 missCacheFactory.setMissHandlerClass(TestMissHandler.class); 30 31 return (MissHandlingCache) missCacheFactory.newInstance("test", 10000, 10000); 32 } 33 34 35 public void testSimple() throws Exception 36 { 37 38 Cache cache = createCache(); 39 40 cache.getObject("A"); 41 cache.getObject("A"); 42 cache.getObject("A"); 43 cache.getObject("A"); 44 cache.getObject("A"); 45 cache.getObject("A"); 46 } 47 48 49 public void testGetLock() throws Exception 50 { 51 52 MissHandlingCache cache = createCache(); 53 Object lock1 = cache.getLock("A"); 54 Object lock2 = cache.getLock("A"); 55 56 assertTrue(lock1 == lock2); 57 cache.unlockKey("A"); 58 cache.unlockKey("A"); 59 60 Object lock3 = cache.getLock("A"); 61 62 assertFalse(lock1 == lock3); 63 } 64 65 66 public void testThreads() throws Exception 67 { 68 69 this.theTestCache = createCache(); 70 71 Thread [] threads = new Thread [100]; 72 73 for (int i = 0; i < threads.length; i++) 74 { 75 threads[i] = new Thread (new GetterThread()); 76 77 threads[i].start(); 78 } 79 80 for (int i = 0; i < threads.length; i++) 81 { 82 LOG.info("waiting for joing " + i + " ..."); 83 threads[i].join(); 84 } 85 86 this.theTestCache = null; 87 } 88 89 90 private class GetterThread implements Runnable 91 { 92 93 public void run() 94 { 95 theTestCache.getObject("X"); 96 } 97 } 98 } 99 | Popular Tags |