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

jadex.micro.testcases.terminate.IntermediateInvokerAgent Maven / Gradle / Ivy

Go to download

The Jadex micro applications package contains several example applications, benchmarks and testcases using micro agents.

There is a newer version: 4.0.267
Show newest version
package jadex.micro.testcases.terminate;


import java.util.Collection;

import jadex.base.test.TestReport;
import jadex.bridge.IComponentStep;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IExecutionFeature;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.FutureTerminatedException;
import jadex.commons.future.IFuture;
import jadex.commons.future.IIntermediateResultListener;
import jadex.commons.future.ITerminableIntermediateFuture;
import jadex.commons.future.IntermediateExceptionDelegationResultListener;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.Description;

/**
 *  The intermediate invoker agent tests if intermediate futures can be terminated
 *  in local and remote cases.
 */
@Agent
@Description("The intermediate invoker agent tests if intermediate futures can be terminated " +
	"in local and remote cases.")
public class IntermediateInvokerAgent	extends InvokerAgent
{
	/**
	 *  Test terminating a future.
	 */
	protected IFuture	testTerminate(int testno, ITerminableService service, long delay)
	{
		final Future tmp = new Future();
		
		int	max	= 3;
		final ITerminableIntermediateFuture fut = service.getResults(delay, max);
		fut.addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new IIntermediateResultListener()
		{
			public void resultAvailable(Collection result)
			{
				tmp.setException(new RuntimeException("Termination did not occur: "+result));
			}
			public void intermediateResultAvailable(String result)
			{
//				System.out.println("intermediate result: "+result);
			}
			public void finished()
			{
				tmp.setException(new RuntimeException("Termination did not occur"));
			}
			public void exceptionOccurred(Exception exception)
			{
				if(exception instanceof FutureTerminatedException)
				{
					tmp.setResult(null);
				}
				else
				{
					tmp.setException(new RuntimeException("Wrong exception occurred: "+exception));
				}
			}
		}));
		
		agent.getComponentFeature(IExecutionFeature.class).waitForDelay(delay*(max-1)+delay/2, new IComponentStep()
		{
			public IFuture execute(IInternalAccess ia)
			{
				fut.terminate();
				return IFuture.DONE;
			}
		});
		
		final Future	ret	= new Future();
		final TestReport tr = new TestReport("#"+testno, "Tests if intermediate future is terminated");
		tmp.addResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(Void result)
			{
				tr.setSucceeded(true);
				ret.setResult(tr);
			}
			public void exceptionOccurred(Exception exception)
			{
				tr.setFailed(exception.getMessage());
				ret.setResult(tr);
			}
		});
		return ret;
	}

	
	/**
	 *  Test if terminate action is called.
	 */
	protected IFuture	testTerminateAction(int testno, ITerminableService service, long delay)
	{
		final Future tmp = new Future();
		
		final ITerminableIntermediateFuture fut = service.getResults(delay, 3);
		service.terminateCalled().addResultListener(new IntermediateExceptionDelegationResultListener(tmp)
		{
			public void intermediateResultAvailable(Void result)
			{
				fut.terminate();
			}
			public void customResultAvailable(Collection result)
			{
				tmp.setResult(null);
			}
			public void finished()
			{
				tmp.setResult(null);
			}
		});

		final Future	ret	= new Future();
		final TestReport tr = new TestReport("#"+testno, "Tests if terminating action of intermediate future is called");
		tmp.addResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(Void result)
			{
				tr.setSucceeded(true);
				ret.setResult(tr);
			}
			public void exceptionOccurred(Exception exception)
			{
				tr.setFailed(exception.getMessage());
				ret.setResult(tr);
			}
		});
		return ret;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy