Author: jsong Date: Thu Jan 20 18:39:35 2005 New Revision: 125871 URL: http://svn.apache.org/viewcvs?view=rev&rev=125871 Log: Add tests for controls threading.
Added: incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/ incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/MultiThreadControl.java incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/MultiThreadControlImpl.jcs incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/SingleThreadControl.java incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/SingleThreadControlImpl.jcs incubator/beehive/trunk/controls/test/src/drivers/org/apache/beehive/controls/test/driver/threading/ incubator/beehive/trunk/controls/test/src/drivers/org/apache/beehive/controls/test/driver/threading/DriveMultiThread.java incubator/beehive/trunk/controls/test/src/drivers/org/apache/beehive/controls/test/driver/threading/DriveSingleThread.java incubator/beehive/trunk/controls/test/src/units/org/apache/beehive/controls/test/java/threading/ incubator/beehive/trunk/controls/test/src/units/org/apache/beehive/controls/test/java/threading/MultiThreadTest.java incubator/beehive/trunk/controls/test/src/units/org/apache/beehive/controls/test/java/threading/SingleThreadTest.java Added: incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/MultiThreadControl.java Url: http://svn.apache.org/viewcvs/incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/MultiThreadControl.java?view=auto&rev=125871 ============================================================================== --- (empty file) +++ incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/MultiThreadControl.java Thu Jan 20 18:39:35 2005 @@ -0,0 +1,20 @@ +package org.apache.beehive.controls.test.controls.threading; + +import org.apache.beehive.controls.api.bean.ControlInterface; + +/** + * A control interface with two methods declared. + * This control interface is designed to test threading model of + * control framework. + */ + [EMAIL PROTECTED] +public interface MultiThreadControl +{ + public final static String METHOD1="method1"; + public final static String METHOD2="method2"; + public final static long EXPECTED_DELAY=10; + + public long method1(); + public long method2(); +} Added: incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/MultiThreadControlImpl.jcs Url: http://svn.apache.org/viewcvs/incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/MultiThreadControlImpl.jcs?view=auto&rev=125871 ============================================================================== --- (empty file) +++ incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/MultiThreadControlImpl.jcs Thu Jan 20 18:39:35 2005 @@ -0,0 +1,60 @@ +package org.apache.beehive.controls.test.controls.threading; + +import java.lang.Thread; +import java.lang.InterruptedException; +import java.util.Date; +import org.apache.beehive.controls.api.bean.ControlImplementation; +import org.apache.beehive.controls.api.bean.Threading; +import org.apache.beehive.controls.api.bean.ThreadingPolicy; + +/** + * An impl of HelloControl. + * By default, control impl is single thread. + * + * Test objective: method1 and method2 on same instance should + * inter-lock each other when invoked by different thread. + * + */ [EMAIL PROTECTED] [EMAIL PROTECTED](ThreadingPolicy.MULTI_THREADED) +public class MultiThreadControlImpl implements MultiThreadControl +{ + private long lastAccess=0; + + public long method1() + { + Date now=new Date(); + if (lastAccess==0) + lastAccess=now.getTime(); + + try{ + Thread.sleep(MultiThreadControl.EXPECTED_DELAY); + } + catch(InterruptedException e){ + } + + Date nowAgain=new Date(); + + long l=nowAgain.getTime()-lastAccess; + return l; + } + + public long method2() + { + Date now=new Date(); + if (lastAccess==0) + lastAccess=now.getTime(); + + try{ + Thread.sleep(MultiThreadControl.EXPECTED_DELAY); + } + catch(InterruptedException e){ + } + + Date nowAgain=new Date(); + + long l=nowAgain.getTime()-lastAccess; + return l; + } + +} Added: incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/SingleThreadControl.java Url: http://svn.apache.org/viewcvs/incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/SingleThreadControl.java?view=auto&rev=125871 ============================================================================== --- (empty file) +++ incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/SingleThreadControl.java Thu Jan 20 18:39:35 2005 @@ -0,0 +1,20 @@ +package org.apache.beehive.controls.test.controls.threading; + +import org.apache.beehive.controls.api.bean.ControlInterface; + +/** + * A control interface with two methods declared. + * This control interface is designed to test threading model of + * control framework. + */ + [EMAIL PROTECTED] +public interface SingleThreadControl +{ + public final static String METHOD1="method1"; + public final static String METHOD2="method2"; + public final static long EXPECTED_DELAY=10; + + public long method1(); + public long method2(); +} Added: incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/SingleThreadControlImpl.jcs Url: http://svn.apache.org/viewcvs/incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/SingleThreadControlImpl.jcs?view=auto&rev=125871 ============================================================================== --- (empty file) +++ incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/threading/SingleThreadControlImpl.jcs Thu Jan 20 18:39:35 2005 @@ -0,0 +1,58 @@ +package org.apache.beehive.controls.test.controls.threading; + +import java.lang.Thread; +import java.lang.InterruptedException; +import java.util.Date; +import org.apache.beehive.controls.api.bean.ControlImplementation; +//import org.apache.beehive.controls.api.bean.ThreadingModel; + +/** + * An impl of HelloControl. + * By default, control impl is single thread. + * + * Test objective: method1 and method2 on same instance should + * inter-lock each other when invoked by different thread. + * + */ [EMAIL PROTECTED] +public class SingleThreadControlImpl implements SingleThreadControl +{ + private long lastAccess=0; + + public long method1() + { + Date now=new Date(); + if (lastAccess==0) + lastAccess=now.getTime(); + + try{ + Thread.sleep(SingleThreadControl.EXPECTED_DELAY); + } + catch(InterruptedException e){ + } + + Date nowAgain=new Date(); + + long l=nowAgain.getTime()-lastAccess; + return l; + } + + public long method2() + { + Date now=new Date(); + if (lastAccess==0) + lastAccess=now.getTime(); + + try{ + Thread.sleep(SingleThreadControl.EXPECTED_DELAY); + } + catch(InterruptedException e){ + } + + Date nowAgain=new Date(); + + long l=nowAgain.getTime()-lastAccess; + return l; + } + +} Added: incubator/beehive/trunk/controls/test/src/drivers/org/apache/beehive/controls/test/driver/threading/DriveMultiThread.java Url: http://svn.apache.org/viewcvs/incubator/beehive/trunk/controls/test/src/drivers/org/apache/beehive/controls/test/driver/threading/DriveMultiThread.java?view=auto&rev=125871 ============================================================================== --- (empty file) +++ incubator/beehive/trunk/controls/test/src/drivers/org/apache/beehive/controls/test/driver/threading/DriveMultiThread.java Thu Jan 20 18:39:35 2005 @@ -0,0 +1,53 @@ +package org.apache.beehive.controls.test.driver.threading; + + +import java.lang.Thread; +import org.apache.beehive.controls.test.controls.threading.MultiThreadControl; +import org.apache.beehive.controls.test.controls.threading.MultiThreadControlBean; + +import org.apache.beehive.test.tools.milton.common.Report; + +public class DriveMultiThread extends Thread +{ + public final static int LOOPS=3; + private MultiThreadControlBean myControl; + private String method2invoke=""; + private long records[]=new long[LOOPS]; + + + public DriveMultiThread(String name){ + super(name); + } + + public void setControl(MultiThreadControlBean bean){ + myControl=bean; + } + + public void setMethod(String str){ + method2invoke=str; + } + + public void run() { + //System.out.println("DONE! " + getName()); + for(int i=0;i<LOOPS;i++){ + if(method2invoke.equals(MultiThreadControl.METHOD1)) + records[i]=myControl.method1(); + else if (method2invoke.equals(MultiThreadControl.METHOD2)) + records[i]=myControl.method2(); + } + + } + + public Report analyseRecord(){ + + Report report=new Report(); + report.setStatus(Report.FAIL); + for(int i=0;i<LOOPS;i++){ + if (records[i]<MultiThreadControl.EXPECTED_DELAY){ + report.setStatus(Report.PASS); + } + } + return report; + } + +} Added: incubator/beehive/trunk/controls/test/src/drivers/org/apache/beehive/controls/test/driver/threading/DriveSingleThread.java Url: http://svn.apache.org/viewcvs/incubator/beehive/trunk/controls/test/src/drivers/org/apache/beehive/controls/test/driver/threading/DriveSingleThread.java?view=auto&rev=125871 ============================================================================== --- (empty file) +++ incubator/beehive/trunk/controls/test/src/drivers/org/apache/beehive/controls/test/driver/threading/DriveSingleThread.java Thu Jan 20 18:39:35 2005 @@ -0,0 +1,53 @@ +package org.apache.beehive.controls.test.driver.threading; + + +import java.lang.Thread; +import org.apache.beehive.controls.test.controls.threading.SingleThreadControl; +import org.apache.beehive.controls.test.controls.threading.SingleThreadControlBean; + +import org.apache.beehive.test.tools.milton.common.Report; + +public class DriveSingleThread extends Thread +{ + public final static int LOOPS=4; + private SingleThreadControlBean myControl; + private String method2invoke=""; + private long records[]=new long[LOOPS]; + + + public DriveSingleThread(String name){ + super(name); + } + + public void setControl(SingleThreadControlBean bean){ + myControl=bean; + } + + public void setMethod(String str){ + method2invoke=str; + } + + public void run() { + //System.out.println("DONE! " + getName()); + for(int i=0;i<LOOPS;i++){ + if(method2invoke.equals(SingleThreadControl.METHOD1)) + records[i]=myControl.method1(); + else if (method2invoke.equals(SingleThreadControl.METHOD2)) + records[i]=myControl.method2(); + } + + } + + public Report analyseRecord(){ + + Report report=new Report(); + report.setStatus(Report.PASS); + for(int i=0;i<LOOPS;i++){ + if (records[i]<SingleThreadControl.EXPECTED_DELAY){ + report.setStatus(Report.FAIL); + } + } + return report; + } + +} Added: incubator/beehive/trunk/controls/test/src/units/org/apache/beehive/controls/test/java/threading/MultiThreadTest.java Url: http://svn.apache.org/viewcvs/incubator/beehive/trunk/controls/test/src/units/org/apache/beehive/controls/test/java/threading/MultiThreadTest.java?view=auto&rev=125871 ============================================================================== --- (empty file) +++ incubator/beehive/trunk/controls/test/src/units/org/apache/beehive/controls/test/java/threading/MultiThreadTest.java Thu Jan 20 18:39:35 2005 @@ -0,0 +1,64 @@ +package org.apache.beehive.controls.test.java.threading; + + + +import java.lang.Thread; +import java.lang.InterruptedException; +import java.beans.Beans; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.beehive.controls.api.bean.ControlBean; +import org.apache.beehive.controls.test.controls.threading.MultiThreadControl; +import org.apache.beehive.controls.test.controls.threading.MultiThreadControlBean; +import org.apache.beehive.controls.test.driver.threading.DriveMultiThread; + +import org.apache.beehive.test.tools.milton.common.Report; +import org.apache.beehive.test.tools.mantis.annotations.tch.Freq; +import org.apache.beehive.test.tools.mantis.annotations.tch.Status; + + + + [EMAIL PROTECTED]("checkin") +public class MultiThreadTest extends TestCase +{ + public MultiThreadTest( String s ) { super( s ); } + + public void setUp() { } + + + /** + * Makes an instance of a single threaded control, passes it to two different + * threads and run both threads. + */ + @Freq("checkin") + public void testMultiThread() throws Exception + { + + MultiThreadControlBean sBean=(MultiThreadControlBean)java.beans.Beans.instantiate( + Thread.currentThread().getContextClassLoader() , + "org.apache.beehive.controls.test.controls.threading.MultiThreadControlBean"); + + DriveMultiThread driver1=new DriveMultiThread("driver1"); + driver1.setControl(sBean); + driver1.setMethod(MultiThreadControl.METHOD1); + DriveMultiThread driver2=new DriveMultiThread("driver2"); + driver2.setControl(sBean); + driver2.setMethod(MultiThreadControl.METHOD2); + + driver1.start(); + driver2.start(); + + try{Thread.sleep(MultiThreadControl.EXPECTED_DELAY*2+5);} + catch(InterruptedException e){} + + Report report1=driver1.analyseRecord(); + Report report2=driver2.analyseRecord(); + + Assert.assertEquals(Report.PASS, report1.getStatus()); + Assert.assertEquals(Report.PASS, report2.getStatus()); + } + +} Added: incubator/beehive/trunk/controls/test/src/units/org/apache/beehive/controls/test/java/threading/SingleThreadTest.java Url: http://svn.apache.org/viewcvs/incubator/beehive/trunk/controls/test/src/units/org/apache/beehive/controls/test/java/threading/SingleThreadTest.java?view=auto&rev=125871 ============================================================================== --- (empty file) +++ incubator/beehive/trunk/controls/test/src/units/org/apache/beehive/controls/test/java/threading/SingleThreadTest.java Thu Jan 20 18:39:35 2005 @@ -0,0 +1,64 @@ +package org.apache.beehive.controls.test.java.threading; + + + +import java.lang.Thread; +import java.lang.InterruptedException; +import java.beans.Beans; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.beehive.controls.api.bean.ControlBean; +import org.apache.beehive.controls.test.controls.threading.SingleThreadControl; +import org.apache.beehive.controls.test.controls.threading.SingleThreadControlBean; +import org.apache.beehive.controls.test.driver.threading.DriveSingleThread; + +import org.apache.beehive.test.tools.milton.common.Report; +import org.apache.beehive.test.tools.mantis.annotations.tch.Freq; +import org.apache.beehive.test.tools.mantis.annotations.tch.Status; + + + + [EMAIL PROTECTED]("checkin") +public class SingleThreadTest extends TestCase +{ + public SingleThreadTest( String s ) { super( s ); } + + public void setUp() { } + + + /** + * Makes an instance of a single threaded control, passes it to two different + * threads and run both threads. + */ + @Freq("checkin") + public void testSingleThread() throws Exception + { + + SingleThreadControlBean sBean=(SingleThreadControlBean)java.beans.Beans.instantiate( + Thread.currentThread().getContextClassLoader() , + "org.apache.beehive.controls.test.controls.threading.SingleThreadControlBean"); + + DriveSingleThread driver1=new DriveSingleThread("driver1"); + driver1.setControl(sBean); + driver1.setMethod(SingleThreadControl.METHOD1); + DriveSingleThread driver2=new DriveSingleThread("driver2"); + driver2.setControl(sBean); + driver2.setMethod(SingleThreadControl.METHOD2); + + driver1.start(); + driver2.start(); + + try{Thread.sleep(SingleThreadControl.EXPECTED_DELAY*10);} + catch(InterruptedException e){} + + Report report1=driver1.analyseRecord(); + Report report2=driver2.analyseRecord(); + + Assert.assertEquals(Report.PASS, report1.getStatus()); + Assert.assertEquals(Report.PASS, report2.getStatus()); + } + +}
