[Jukebox-cvs] CVS update: J4/src/java/gnu/j4/test/integration .cvsignore Makefile.am ResourcePool.ja
Brought to you by:
vtt
From: CVS B. <vt...@fr...> - 2000-10-25 06:56:46
|
User: vt Date: 00/10/24 23:56:48 Added: src/java/gnu/j4/test/integration .cvsignore Makefile.am ResourcePool.java ResourcePoolFailure.java package.html Log: Moved the files from gnu.j4.examples to gnu.j4.test.unit and gnu.j4.test.integration, where they belong. Also, makeup changes and small fixes. Revision Changes Path 1.1 J4/src/java/gnu/j4/test/integration/.cvsignore CVSWEB Options: ------------------- CVSWeb: Annotate this file: http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/J4/src/java/gnu/j4/test/integration/.cvsignore?annotate=1.1&cvsroot=jukebox4 CVSWeb: View this file: http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/J4/src/java/gnu/j4/test/integration/.cvsignore?rev=1.1&content-type=text/x-cvsweb-markup&cvsroot=jukebox4 ----------------------------------- Index: .cvsignore =================================================================== Makefile Makefile.in LocalConfig.java *.class .deps 1.1 J4/src/java/gnu/j4/test/integration/Makefile.am CVSWEB Options: ------------------- CVSWeb: Annotate this file: http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/J4/src/java/gnu/j4/test/integration/Makefile.am?annotate=1.1&cvsroot=jukebox4 CVSWeb: View this file: http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/J4/src/java/gnu/j4/test/integration/Makefile.am?rev=1.1&content-type=text/x-cvsweb-markup&cvsroot=jukebox4 ----------------------------------- Index: Makefile.am =================================================================== # $Id: Makefile.am,v 1.1 2000/10/25 06:56:48 vt Exp $ EXTRA_DIST = package.html noinst_PROGRAMS = package package_SOURCES = ResourcePool.java \ ResourcePoolFailure.java ECHO = @ECHO@ JAVACX = @JAVACX@ RM = @RM@ SUFFIXES = .java .class .java.class: - @ ${ECHO} "Making $<" @ ${JAVACX} $< package: $(package_OBJECTS) $(package_DEPENDENCIES) clean-compile: ${RM} -f *.class clean-generic: ${RM} -f *~ .*~ 1.1 J4/src/java/gnu/j4/test/integration/ResourcePool.java CVSWEB Options: ------------------- CVSWeb: Annotate this file: http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/J4/src/java/gnu/j4/test/integration/ResourcePool.java?annotate=1.1&cvsroot=jukebox4 CVSWeb: View this file: http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/J4/src/java/gnu/j4/test/integration/ResourcePool.java?rev=1.1&content-type=text/x-cvsweb-markup&cvsroot=jukebox4 ----------------------------------- Index: ResourcePool.java =================================================================== package gnu.j4.examples; import java.util.Random; import gnu.j4.config.Configuration; import gnu.j4.sem.SemaphoreGroup; import gnu.j4.service.ActiveService; import gnu.j4.service.Service; import gnu.j4.service.PassiveService; import gnu.j4.service.ServiceUnavailableException; /** * Resource pool unit test and example. * * <ol> * * <li>Create and start the resource pool. * * <li>Create and start a number of consumers, randomly requesting the * resources from the pool. * * <li>Let it work for a while. * * <li>Stop everything. * * </ol> * @author Copyright © <a href="mailto:vt...@fr...">Vadim Tkachenko</a> 1995-1998 * @version $Id: ResourcePool.java,v 1.1 2000/10/25 06:56:48 vt Exp $ */ public class ResourcePool extends ActiveService { /** * Log facility to use for this class. */ public static final String LOG_RPT = "RPT"; /** * Serial number of the consumer thread. */ protected static int TID = 0; /** * Sample resource pool. */ protected gnu.j4.service.ResourcePool pool; /** * Master configuration. */ protected Configuration conf; /** * Random generator. * * I have to use this to make this example compile under JDK 1.1. * * @see #random */ protected Random R = new Random(); /** * Semaphore group that is used to track the consumer object shutdowns. */ protected SemaphoreGroup done = new SemaphoreGroup(); /** * The configuration keyword to extract the minimum number of spare * services to start. */ public static final String CF_MIN_START = "test.resource.pool.minstart"; /** * The configuration keyword to extract the maximum possible number of * services to run. */ public static final String CF_MAX_KEEP = "test.resource.pool.maxkeep"; /** * The configuration keyword to extract the minimum number of spares to * keep. */ public static final String CF_MIN_SPARES = "test.resource.pool.minspares"; /** * The configuration keyword to extract the maximum number of spares to * keep. */ public static final String CF_MAX_SPARES = "test.resource.pool.maxspares"; /** * The configuration keyword to extract the maximum number of calls to * hit the resources with. */ public static final String CF_LIMIT_THREAD = "test.resource.pool.threadcount"; /** * The configuration keyword to extract the maximum number of calls to * hit the resources with. */ public static final String CF_LIMIT_HIT = "test.resource.pool.hitcount"; /** * The configuration keyword to extract the maximum delay before * requesting the resource. */ public static final String CF_ATTACK = "test.resource.pool.attack"; /** * The configuration keyword to extract the maximum time to hold the * resource. */ public static final String CF_SUSTAIN = "test.resource.pool.sustain"; /** * The idiot-proof test variable. * * Tracks the resource consumption. Should never exceed the value set * for the {@link #CF_MAX_KEEP maxKeep}. */ protected int resourceInstanceCount = 0; /** * Start the resource pool, then the resource consumers. * * @return true * @exception InterruptedException if this thread was interrupted by * another thread. */ protected void startup() throws InterruptedException, Throwable { Configuration defaultConf = new Configuration(); defaultConf.put(CF_MIN_START, "3"); defaultConf.put(CF_MAX_KEEP, "10"); defaultConf.put(CF_MIN_SPARES, "2"); defaultConf.put(CF_MAX_SPARES, "5"); defaultConf.put(CF_LIMIT_THREAD, "100"); defaultConf.put(CF_LIMIT_HIT, "100"); defaultConf.put(CF_ATTACK, "1000"); defaultConf.put(CF_SUSTAIN, "200"); conf = Configuration.createChain("test.resource.pool.conf", defaultConf); pool = new Pool(conf.getInteger(CF_MIN_START), conf.getInteger(CF_MAX_KEEP), conf.getInteger(CF_MIN_SPARES), conf.getInteger(CF_MAX_SPARES)); if ( !pool.start().waitFor() ) { throw new ServiceUnavailableException("Couldn't start the resource pool"); } complain(LOG_WARNING, LOG_RPT, pool.toString()); complain(LOG_WARNING, LOG_RPT, "Threads: " + conf.getInteger(CF_LIMIT_THREAD)); complain(LOG_WARNING, LOG_RPT, "Hits: " + conf.getInteger(CF_LIMIT_HIT)); complain(LOG_WARNING, LOG_RPT, "Attack: " + conf.getInteger(CF_ATTACK)); complain(LOG_WARNING, LOG_RPT, "Sustain: " + conf.getInteger(CF_SUSTAIN)); addDependant(pool); for ( int idx = 0; idx < conf.getInteger(CF_LIMIT_THREAD); idx++ ) { Consumer c = new Consumer(); c.start(); addDependant(c); done.add(c.getSemDown()); } complain( LOG_WARNING,LOG_RPT,"startup OK" ); } /** * Fake the successful execution. * * Log a message, return success. * * @return true * @exception InterruptedException if this thread was interrupted by * another thread. */ protected void execute() throws InterruptedException { complain(LOG_WARNING, LOG_RPT, "Pinging the resource pool with the requests"); done.waitForAll(); complain(LOG_WARNING, LOG_RPT, "The consumer services have finished."); } /** * Shut down everything, implicitly. * * @return true * @exception InterruptedException if this thread was interrupted by * another thread. */ protected void shutdown() throws InterruptedException { complain( LOG_NOTICE,LOG_RPT,"shutdown: cleaning up" ); } protected int random(int limit) { int r = R.nextInt(); if ( r < 0 ) { r = -r; } return (r % limit); } protected class Consumer extends ActiveService { protected int tid; protected void startup() throws InterruptedException { tid = TID++; } protected void execute() throws InterruptedException, Throwable { int REQUEST_COUNT = conf.getInteger(CF_LIMIT_HIT); for ( int idx = 0; idx < REQUEST_COUNT; idx++ ) { Thread.sleep(random(conf.getInteger(CF_ATTACK))); complain(LOG_NOTICE, LOG_RPT, "REQ(" + tid + "), " + idx + " out of " + REQUEST_COUNT); Service s = pool.getResource(); complain(LOG_NOTICE, LOG_RPT, "GOT(" + tid + ")"); Thread.sleep(random(conf.getInteger(CF_SUSTAIN))); complain(LOG_NOTICE, LOG_RPT, "REL(" + tid + ")"); pool.releaseResource(s); } } protected void shutdown() throws InterruptedException { } } protected class Pool extends gnu.j4.service.ResourcePool { public Pool(int minStart, int maxKeep, int minSpare, int maxSpare) { super(minStart, maxKeep, minSpare, maxSpare); } protected Service createResource() { return new SampleResource(); } protected class SampleResource extends PassiveService { protected String LOG_SR = "SampleResource"; protected void startup() throws InterruptedException, Throwable { this.complain(LOG_INFO, LOG_SR, "starting up"); if ( ++resourceInstanceCount > conf.getInteger(CF_MAX_KEEP) ) { throw new ServiceUnavailableException("INCONSISTENCY: MAX_KEEP EXCEEDED"); } } protected void shutdown() throws InterruptedException { this.complain(LOG_INFO, LOG_SR, "shutting down"); resourceInstanceCount--; } } } } 1.1 J4/src/java/gnu/j4/test/integration/ResourcePoolFailure.java CVSWEB Options: ------------------- CVSWeb: Annotate this file: http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/J4/src/java/gnu/j4/test/integration/ResourcePoolFailure.java?annotate=1.1&cvsroot=jukebox4 CVSWeb: View this file: http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/J4/src/java/gnu/j4/test/integration/ResourcePoolFailure.java?rev=1.1&content-type=text/x-cvsweb-markup&cvsroot=jukebox4 ----------------------------------- Index: ResourcePoolFailure.java =================================================================== package gnu.j4.examples; import gnu.j4.service.ActiveService; import gnu.j4.service.PassiveService; import gnu.j4.service.Service; import gnu.j4.service.ServiceUnavailableException; /** * Resource pool stress test. * * Checks if the resource pool can handle the situations when several, up to * all, of the poolable services fail. * * <p> * * Expected behavior: the resource pool is supposed to start successfully * only if <strong>all</strong> resources have started successfully. Let's * be paranoid. * * @author Copyright © <a href="mailto:vt...@fr...">Vadim Tkachenko</a> 2000 * @version $Id: ResourcePoolFailure.java,v 1.1 2000/10/25 06:56:48 vt Exp $ */ public class ResourcePoolFailure extends ActiveService { /** * Log facility to use. */ public static final String CH_FT = "FailureTest"; /** * Resource count. * * Every time the {@link ResourcePoolFailure.Pool.FailingResource * FailingResource} starts, it decrements this variable. If it is * already zero, it refuses to start. */ protected static int rCount = 15; static synchronized int getResourceCount() { return rCount--; } /** * Do nothing here, because we'll have to start and stop the service * twice. */ protected void startup() { } /** * Test the service. * * <ul> * * <li> First time, start it and make <strong>all</strong> the poolable * services fail the startup. * * <li> Second time, make <strong>some</strong> poolable services fail * the startup. */ protected void execute() throws Throwable { gnu.j4.service.ResourcePool pool = new Pool(); // This one is going to start if ( pool.start().waitFor() ) { complain(LOG_NOTICE, CH_FT, "started as expected"); } else { complain(LOG_ALERT, CH_FT, "failed: shouldn't have"); } Thread.sleep(5000); pool.stop(); // This one is going to fail because not all the resources have // started up if ( !pool.start().waitFor() ) { complain(LOG_NOTICE, "FailureTest", "failed as expected"); } else { complain(LOG_ALERT, CH_FT, "started: shouldn't have"); } Thread.sleep(5000); pool.stop(); // This one is going to fail as well pool = new Pool(); pool.start().waitFor(); if ( !pool.start().waitFor() ) { complain(LOG_NOTICE, "FailureTest", "failed as expected"); } else { complain(LOG_ALERT, CH_FT, "started: shouldn't have"); } } /** * Do nothing. */ protected void shutdown() { } protected class Pool extends gnu.j4.service.ResourcePool { /** * Create the instance. * * @param minStart Minimum number of poolable resources to start. */ public Pool() { super(10, 20, 5, 5); } protected Service createResource() { return new FailingResource(); } protected class FailingResource extends PassiveService { protected synchronized void startup() throws Throwable { int rCount = getResourceCount(); complain(LOG_INFO, "Failer/startup", "" + rCount + " resources left"); if ( rCount <= 0 ) { throw new ServiceUnavailableException("No more resources"); } } protected void shutdown() { complain(LOG_INFO, "Failer", "shut down."); } } } } 1.1 J4/src/java/gnu/j4/test/integration/package.html <<Binary file>> |