FileDocCategorySizeDatePackage
JUnitTestAsynchronousAspects.javaAPI DocJBoss 4.2.114619Fri Jul 13 21:02:22 BST 2007test.asynchronous

JUnitTestAsynchronousAspects

public class JUnitTestAsynchronousAspects extends TestCase implements org.jboss.aspects.asynchronous.AsynchronousConstants
version
$Revision: 57186 $
author
{Claude Hussenet Independent Consultant}.

Fields Summary
Constructors Summary
public JUnitTestAsynchronousAspects(String arg0)


		super(arg0);

		BusinessModel bm1 = new BusinessModel();



		bm1.processBusinessModel();

		AsynchronousTask aT = ((AsynchronousFacade)bm1).getAsynchronousTask();

		aT.getResponse();



	
Methods Summary
public static voidAsynchronousCallOnStaticMethodWithTimeout()


		Parameter object1 = new Parameter();

		Parameter object2 = new Parameter();

		Parameter object3 = new Parameter();

		Parameter object4 = new Parameter();

		assertTrue(

			"not an instance of AsynchronousFacade",

			object1 instanceof AsynchronousFacade

				|| object2 instanceof AsynchronousFacade);

		((AsynchronousFacade)object1).setTimeout(100);

		((AsynchronousFacade)object3).setTimeout(100);

		((AsynchronousFacade)object4).setTimeout(100);

		long t0 = System.currentTimeMillis();

		BusinessModel.processBusinessModel4(200, object1);

		BusinessModel.processBusinessModel4(150, object2);

		BusinessModelWithStaticCleanup.processBusinessModel4(200, object3);

		BusinessModelWithStaticCleanupWithParameters.processBusinessModel4(

			200,

			object4);

		System.out.println(System.currentTimeMillis()-t0);

		long t1 = System.currentTimeMillis();

		assertTrue(

			"static method not an asynchronous call:" + (t1 - t0) + " ms.",

			(t1 - t0) < 160);

		AsynchronousFacade asynchronousFacade1 = (AsynchronousFacade)object1;

		assertEquals(

			"Method did not timeout !",

			TIMEOUT,

			asynchronousFacade1.getResponseCode());

		AsynchronousFacade asynchronousFacade = (AsynchronousFacade)object2;

		AsynchronousTask asynchronousTask2 =

			asynchronousFacade.getAsynchronousTask();

		assertEquals(

			"Method does not return the right value !",

			150,

			((Long)asynchronousFacade.getReturnValue(asynchronousTask2))

				.longValue());

	
public static voidtest2AsynchronousCallOnSameInstance()


		BusinessModel bm1 = new BusinessModel();

		bm1.processBusinessModel2(15);

		AsynchronousFacade asynchronousFacade = (AsynchronousFacade)bm1;

		AsynchronousTask asynchronousTask1 =

			asynchronousFacade.getAsynchronousTask();

		bm1.processBusinessModel2(10);

		AsynchronousTask asynchronousTask2 =

			asynchronousFacade.getAsynchronousTask();

		assertEquals(

			"Method is not succesfull !",

			OK,

			asynchronousFacade.getResponseCode(asynchronousTask1));

		assertTrue(

			"value returned is not an instance of Long",

			asynchronousFacade.getReturnValue(asynchronousTask1)

				instanceof Long);

		assertEquals(

			"Method does not return the right value !"

				+ ((Long)asynchronousFacade.getReturnValue(asynchronousTask1))

					.longValue(),

			((Long)asynchronousFacade.getReturnValue(asynchronousTask1))

				.longValue(),

			15);

		assertEquals(

			"Method is not succesfull !",

			asynchronousFacade.getResponseCode(asynchronousTask2),

			OK);

		assertTrue(

			"value returned is not an instance of Long",

			asynchronousFacade.getReturnValue(asynchronousTask2)

				instanceof Long);

		assertEquals(

			"Method does not return the right value !"

				+ ((Long)asynchronousFacade.getReturnValue(asynchronousTask2))

					.longValue(),

			((Long)asynchronousFacade.getReturnValue(asynchronousTask2))

				.longValue(),

			10);

	
public static voidtest2AsynchronousCallOnSameInstance2()


		BusinessModel bm1 = new BusinessModel();

		AsynchronousFacade asynchronousFacade = (AsynchronousFacade)bm1;

		asynchronousFacade.setTimeout(50);

		bm1.processBusinessModel2(1000);

		AsynchronousTask asynchronousTask1 =

			asynchronousFacade.getAsynchronousTask();

		asynchronousFacade.setTimeout(200);

		bm1.processBusinessModel2(10);

		AsynchronousTask asynchronousTask2 =

			asynchronousFacade.getAsynchronousTask();

		assertEquals(

			"Method did not timeout !",

			TIMEOUT,

			asynchronousFacade.getResponseCode(asynchronousTask1));

		assertEquals(

			"Method is not succesfull !",

			asynchronousFacade.getResponseCode(asynchronousTask2),

			OK);

		assertTrue(

			"value returned is not an instance of Long",

			asynchronousFacade.getReturnValue(asynchronousTask2)

				instanceof Long);

		assertEquals(

			"Method does not return the right value !"

				+ ((Long)asynchronousFacade.getReturnValue(asynchronousTask2))

					.longValue(),

			((Long)asynchronousFacade.getReturnValue(asynchronousTask2))

				.longValue(),

			10);

	
public static voidtest2AsynchronousCallsOnSameInstanceFrom2DifferentThreads()


		BusinessModel bm1 = new BusinessModel(100);

		BusinessThread businessThread1 =

			new BusinessThread(bm1, 100, 100, "First Call");

		BusinessThread businessThread2 =

			new BusinessThread(bm1, 200, 200, "Second Call");

		Thread th1 = new Thread(businessThread1);

		th1.start();

		Thread th2 = new Thread(businessThread2);

		th2.start();

		BusinessModel.sleep(500);

		assertNotSame(

			"The result from 2 differents threads are the same !",

			businessThread1.result,

			businessThread2.result);

	
public static voidtestAsynchronousCall()


		BusinessModel bm1 = new BusinessModel(200);

		long t0 = System.currentTimeMillis();

		bm1.processBusinessModel();

		long t1 = System.currentTimeMillis();

		assertTrue(

			"Not an asynchronous call:" + (t1 - t0) + " ms.",

			(t1 - t0) < 100);

	
public static voidtestAsynchronousCallOnPrivateMethod()


		BusinessModel bm1 = new BusinessModel(200);

		long t0 = System.currentTimeMillis();

		bm1.callPrivateMethod();

		long t1 = System.currentTimeMillis();

		assertTrue(

			"private method not an asynchronous call:" + (t1 - t0) + " ms.",

			(t1 - t0) < 100);

	
public static voidtestAsynchronousCallOnStaticMethod()


		Parameter object = new Parameter();

		long t0 = System.currentTimeMillis();

		BusinessModel.processBusinessModel4(200, object);

		long t1 = System.currentTimeMillis();

		assertTrue(

			"not an instance of AsynchronousFacade",

			object instanceof AsynchronousFacade);

		assertTrue(

			"static method not an asynchronous call:" + (t1 - t0) + " ms.",

			(t1 - t0) < 100);

		AsynchronousFacade asynchronousFacade = (AsynchronousFacade)object;

		AsynchronousTask asynchronousTask1 =

			asynchronousFacade.getAsynchronousTask();

		assertEquals(

			"Method does not return the right value !",

			200,

			((Long)asynchronousFacade.getReturnValue(asynchronousTask1))

				.longValue());

	
public static voidtestAsynchronousCallOnStaticMethodWithTimeout()


		int nb = 1;

		for (int i = 0; i < nb; i++) {

			 	AsynchronousCallOnStaticMethodWithTimeout();

		}

	
public static voidtestCleanupCallWhenTimeout()


		BusinessModelWithCleanup bm1 = new BusinessModelWithCleanup();

		BusinessModelWithCleanup bm2 = new BusinessModelWithCleanup();

		AsynchronousFacade asynchronousFacade1 = (AsynchronousFacade)bm1;

		AsynchronousFacade asynchronousFacade2 = (AsynchronousFacade)bm2;

		asynchronousFacade1.setTimeout(100);

		bm1.processBusinessModel2(200);

		bm2.processBusinessModel2(200);

		assertEquals(

			"Method did not timeout !",

			TIMEOUT,

			asynchronousFacade1.getResponseCode());

		assertEquals(

			"Method is not successfull !",

			OK,

			asynchronousFacade2.getResponseCode());

		assertEquals("Cleanup method not called", true, bm1.bCleanupCalled);

		assertEquals("Cleanup method called", false, bm2.bCleanupCalled);

	
public static voidtestExceptionRaisedInMethodCall()


		BusinessModel bm1 = new BusinessModel();

		bm1.processBusinessModel2(-1);

		AsynchronousFacade asynchronousFacade = (AsynchronousFacade)bm1;

		assertEquals(

			"EXCEPTIONCAUGHT error not returned !",

			EXCEPTIONCAUGHT,

			asynchronousFacade.getResponseCode());

	
public static voidtestIsDone()


		BusinessModel bm1 = new BusinessModel(200);

		bm1.processBusinessModel();

		AsynchronousFacade asynchronousFacade = (AsynchronousFacade)bm1;

		long t0 = System.currentTimeMillis();

		assertFalse("isDone returns TRUE  !", asynchronousFacade.isDone());

		long t1 = System.currentTimeMillis();

		assertTrue(

			"isDone is a blocking call " + (t1 - t0) + " ms.",

			(t1 - t0) < 20);

		assertEquals(

			"Method is not succesfull !",

			OK,

			asynchronousFacade.getResponseCode());

		assertTrue("isDone returns FALSE  !", asynchronousFacade.isDone());

	
public static voidtestMixinAsynchronousFacadeInterface()


		BusinessModel bm1 = new BusinessModel();

		assertTrue(

			"not an instance of AsynchronousFacade",

			bm1 instanceof AsynchronousFacade);

	
public static voidtestMixinAsynchronousFacadeInterface2()


		BusinessModel bm1 = new BusinessModel();

		BusinessModel bm2 = new BusinessModel();

		assertTrue(

			"bm1 is not an instance of AsynchronousFacade",

			bm1 instanceof AsynchronousFacade);

		assertTrue(

			"bm2 is not an instance of AsynchronousFacade",

			bm2 instanceof AsynchronousFacade);

		AsynchronousFacade asynchronousFacade1 = (AsynchronousFacade)bm1;

		AsynchronousFacade asynchronousFacade2 = (AsynchronousFacade)bm2;

		asynchronousFacade1.setId("OK");

		asynchronousFacade2.setId("KO");

		assertNotSame(

			"same instances(ID) of AsynchronousFacade",

			asynchronousFacade1.getId(),

			asynchronousFacade2.getId());

	
public static voidtestPerformance()


		ThreadManagerFactory.getThreadManager().setMaximumPoolSize(5000);

		long tt0 = System.currentTimeMillis();

		float nbt = 100000;

		for (int i = 0; i < nbt; i++) {

			new BusinessModel(200);

		}

		long tt1 = System.currentTimeMillis();

		float time = (tt1 - tt0) / nbt;

		System.out.println(

			(int)nbt

				+ " advised instances created in "

				+ (tt1 - tt0)

				+ " (ms).Average time "

				+ time

				+ " (ms).");

		BusinessModel bm = new BusinessModel(10);

		AsynchronousFacade Fbm = (AsynchronousFacade)bm;

		long total = 0;

		int iOk = 0;

		int nb1 = 200;

		int nb2 = 5;

		AsynchronousTask[] Tbm = new AsynchronousTask[nb2];

		for (int j = 0; j < nb1; j++) {

			long t0 = System.currentTimeMillis();

			for (int i = 0; i < nb2; i++) {

				bm.processBusinessModel();

				Tbm[i] = Fbm.getAsynchronousTask();

			}

			long t1 = System.currentTimeMillis();

			total += (t1 - t0);

			for (int i = 0; i < nb2; i++) {

				int ok = Fbm.getResponseCode(Tbm[i]);

				if (ok == OK)

					iOk++;

			}

		}

		System.out.println(

			nb1 * nb2

				+ " asynchronous method invocations in "

				+ total

				+ " (ms).Average time "

				+ (total / (float) (nb1 * nb2))

				+ " (ms).");

		assertEquals("Some errors:", nb1 * nb2, iOk);

	
public static voidtestPoolSizeFull()


		ThreadManagerFactory.getThreadManager().setMaximumPoolSize(10);

		for (int i = 0; i < 10; i++) {

			BusinessModel bm1 = new BusinessModel(200);

			bm1.processBusinessModel();



		}

		BusinessModel bm1 = new BusinessModel(200);

		bm1.processBusinessModel();

		AsynchronousFacade asynchronousFacade = (AsynchronousFacade)bm1;

		assertEquals(

			"Pool size not full !",

			CAN_NOT_PROCESS,

			asynchronousFacade.getResponseCode());

	
public static voidtestResponseTimeReturned()


		BusinessModel bm1 = new BusinessModel(200);

		int ERROR = 20;

		long t0 = System.currentTimeMillis();

		bm1.processBusinessModel();

		AsynchronousFacade asynchronousFacade1 = (AsynchronousFacade)bm1;

		assertEquals(

			"Method is not succesfull !",

			OK,

			asynchronousFacade1.getResponseCode());

		long t1 = System.currentTimeMillis();

		long startingTime =

			asynchronousFacade1.getThreadManagerResponse().getStartingTime();

		long endingTime =

			asynchronousFacade1.getThreadManagerResponse().getEndingTime();

		assertTrue(

			"starting time issue ? " + (startingTime - t0),

			(startingTime - t0) < ERROR);

		assertTrue(

			"ending time issue ? " + (t1 - endingTime),

			(t1 - endingTime) < ERROR);

	
public static voidtestReturnValue()


		BusinessModel bm1 = new BusinessModel();

	    bm1.processBusinessModel2(10);

		AsynchronousFacade asynchronousFacade = (AsynchronousFacade)bm1;

		assertEquals(

			"Method is not succesfull !",

			OK,

			asynchronousFacade.getResponseCode());

		assertTrue(

			"value returned is not an instance of Long",

			asynchronousFacade.getReturnValue() instanceof Long);

		assertEquals(

			"Method does not return the right value !"

				+ ((Long)asynchronousFacade.getReturnValue()).longValue(),

			((Long)asynchronousFacade.getReturnValue()).longValue(),

			10);

	
public static voidtestTimeout()


		BusinessModel bm1 = new BusinessModel(200);

		AsynchronousFacade asynchronousFacade = (AsynchronousFacade)bm1;

		asynchronousFacade.setTimeout(100);

		bm1.processBusinessModel();

		long t0 = System.currentTimeMillis();

		//System.out.println(

		//	asynchronousFacade.getAsynchronousTask().getResponse());

		assertEquals(

			"Method did not timeout !",

			TIMEOUT,

			asynchronousFacade.getResponseCode());

		long t1 = System.currentTimeMillis();

		assertTrue("Method time out in " + (t1 - t0) + " ms.", (t1 - t0) < 120);

		assertTrue("Method did not run " + (t1 - t0) + " ms.", (t1 - t0) > 80);