All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.enterprise.cache.test.DefaultCacheTest Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

// @(#) $Id: DefaultCacheTest.java 2093 2007-11-28 14:23:36Z s3460 $
package at.spardat.enterprise.cache.test;

import at.spardat.enterprise.cache.*;
import at.spardat.enterprise.test.*;
import junit.framework.TestCase;

/**
 * testcase f?r DefaultCache
 *
 * @author M. Schauer
 */
public class DefaultCacheTest extends TestCase {

    public DefaultCacheTest(String name) {
        super(name);
    }

    public void testConcurrentAccess () throws Exception {
    	for (int i=0; i<1; i++) {
	    	int           numThreads = TestUtil.randomInt (5,10);
	    	int           cacheSize =  TestUtil.randomInt (5,200);
	    	int           maxAge =     TestUtil.randomInt (1000, 20000);
	    	if (TestUtil.randomInt(1,5) == 1) maxAge = -1;  // no age limitation with prob 0.2
	    	if (TestUtil.randomInt(1,5) == 1) cacheSize = -1; // no max size with prob 0.2
	    	doCacheRun (numThreads, cacheSize, maxAge);
    	}
    }
    
    private void doCacheRun (int numThreads, int cacheSize, int maxAge) throws Exception {
    	final int cacheSizeF = cacheSize;
    	final int maxAgeF = maxAge;
    	ICacheDescriptor desc = new ICacheDescriptor() {
            public String getName() { return "testCache"; }
            public boolean isTransparent () { return true; }
            public Object load(java.lang.Object key) { 
            	// delay to simulate load time
            	boolean           fail = (TestUtil.randomInt(1,1000) < 5);
				if (fail) throw new RuntimeException ("faked fail");            	
            	int               delay = TestUtil.randomInt(10, 100);
            	if (TestUtil.randomInt(1,100) > 1) delay = 0;
            	long              timeToFinish = System.currentTimeMillis() + delay;
            	while (System.currentTimeMillis() < timeToFinish) {
            		try { Thread.sleep(10); } catch (InterruptedException ex) {};
            	}
            	return ((String)key) + "Val"; 
            }
            public long getMaxAgeMillis() {
                return maxAgeF;
            }
            public int getMaxAgeSpreadPct() {
                return 50;
            }

            public int getMaxSize() {
                return cacheSizeF;
            }
    	};
    	DefaultCache         c = new DefaultCache(desc);
    	// kick off the threads and wait, until all of them completed
    	threadsRunning_ = 0;
    	for (int i=0; i 0) try { Thread.sleep(200); } catch (InterruptedException ex) {}
    	// print the cache content
System.out.println ("number of threads: " + numThreads);
c.print();
    }
    
    private int              threadsRunning_;
    
    private class CacheGrinder extends Thread {
    	public CacheGrinder (ICache c) {
    		cache_ = c;
    	}
    	public void run () {
    		try {
	    		// here we do a random mix of cache operations
	    		int                 highestKey = TestUtil.randomInt(10, 200);
	    		//int                 highestKey = 50;
	    		for (int i=0; i<10000; i++) {
	    			int             key = TestUtil.randomInt(1, highestKey);
	    			int             operation = TestUtil.randomInt(1,100);
	    			Object          result = null;
	    			//if (operation == 1) cache_.removeAll();
	    			if (operation <= 3) cache_.remove(String.valueOf(key));
	    			if (operation > 3)  {
	    				String        keyS = String.valueOf(key);
	    				boolean       success = false;
	    				try {
		    				result = cache_.lookup(keyS);
		    				success = true;
	    				} catch (RuntimeException ex) {
	    					if (!ex.getMessage().equals("faked fail")) throw ex;
	    				}
	    				if (success) {
		    				if (result == null || !result.equals(key + "Val"))
		    					throw new RuntimeException ("returned value for key " + keyS + " is " + result);
	    				}
	    			}
	    		}
    		} finally {
    			threadsRunning_--;
    		}
    	}
    	private ICache          cache_;
    }

    public static void main (String [] args) throws Exception {
        new DefaultCacheTest ("dummy").testConcurrentAccess();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy