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

jadex.micro.testcases.tracing.InitiatorAgent 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.tracing;

import java.util.Collection;

import jadex.base.test.TestReport;
import jadex.base.test.Testcase;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IExternalAccess;
import jadex.bridge.ServiceCall;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.component.interceptors.CallAccess;
import jadex.commons.Tuple2;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.Binding;
import jadex.micro.annotation.RequiredService;
import jadex.micro.annotation.RequiredServices;
import jadex.micro.testcases.TestAgent;

/**
 * 
 */
@Agent
@RequiredServices(
{
	@RequiredService(name="ts", type=ITestService.class, binding=@Binding(scope=RequiredServiceInfo.SCOPE_GLOBAL))
})
public class InitiatorAgent extends TestAgent
{
	/**
	 *  Perform the tests.
	 */
	protected IFuture performTests(final Testcase tc)
	{
		final Future ret = new Future();
		
		testLocal(1).addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(TestReport result)
			{
				System.out.println("test local fini");
				tc.addReport(result);
				testRemote(2).addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new ExceptionDelegationResultListener(ret)
				{
					public void customResultAvailable(TestReport result)
					{
						tc.addReport(result);
						ret.setResult(null);
					}
				}));
			}
		}));
		
		return ret;
	}
	
	/**
	 *  Test local.
	 */
	protected IFuture testLocal(final int testno)
	{
		final Future ret = new Future();
		
		performTest(agent.getComponentIdentifier().getRoot(), testno, true)
			.addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new DelegationResultListener(ret)
		{
			public void customResultAvailable(final TestReport result)
			{
				ret.setResult(result);
			}
		}));
		
		return ret;
	}
	
	/**
	 *  Test remote.
	 */
	protected IFuture testRemote(final int testno)
	{
		final Future ret = new Future();
		
		setupRemotePlatform(false).addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(
			new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(final IExternalAccess platform)
			{
				performTest(platform.getComponentIdentifier(), testno, false)
					.addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new DelegationResultListener(ret)));
			}
		}));
		
		return ret;
	}
	
	/**
	 *  Perform the test. Consists of the following steps:
	 *  Create provider agent
	 *  Call methods on it
	 */
	protected IFuture performTest(final IComponentIdentifier root, final int testno, final boolean hassectrans)
	{
		final Future ret = new Future();

		final Future res = new Future();
		
		ret.addResultListener(new DelegationResultListener(res)
		{
			public void exceptionOccurred(Exception exception)
			{
				TestReport tr = new TestReport("#"+testno, "Tests if tracing works.");
				tr.setFailed(exception);
				super.resultAvailable(tr);
			}
		});
		
		final Future>> resfut = new Future>>();
		IResultListener>> reslis = new DelegationResultListener>>(resfut);
		
//		System.out.println("root: "+root+" "+SUtil.arrayToString(root.getAddresses()));
		
//		ServiceCall call = ServiceCall.getInvocation();
//		call.setProperty("extra", "extra");
//		call.setCause(new Cause("abc"+testno, "a", "b", "a", "b"));
//		CallAccess.setServiceCall(call);
		
		System.out.println("create component started with: "+CallAccess.getCurrentInvocation());
		
		createComponent("jadex/micro/testcases/tracing/ProviderAgent.class", root, reslis)
			.addResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(final IComponentIdentifier cid) 
			{
				callService(cid, testno, 5000).addResultListener(new DelegationResultListener(ret));
			}
			
			public void exceptionOccurred(Exception exception)
			{
				exception.printStackTrace();
				super.exceptionOccurred(exception);
			}
		});
		
		return res;
	}
	
	/**
	 *  Call the service methods.
	 */
	protected IFuture callService(IComponentIdentifier cid, final int testno, final long to)
	{
		final Future ret = new Future();
		
		final TestReport tr = new TestReport("#"+testno, "Test if timeout works "+(to==-1? "without ": "with "+to)+" timeout.");
		
		IFuture fut = agent.getComponentFeature(IRequiredServicesFeature.class).searchService(ITestService.class, cid);
		
		fut.addResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(final ITestService ts)
			{
				// create a service call meta object and set the timeout
//				ServiceCall call = ServiceCall.getInvocation();
//				call.setProperty("extra", "extra");
//				call.setCause(new Cause("abc"+testno, "a", "b", "a", "b"));
				
				System.out.println("call started with: "+CallAccess.getCurrentInvocation());
//				System.out.println("call started with: "+CallAccess.getNextInvocation().getCause().getCallId());
				ts.method("test1").addResultListener(new IResultListener()
				{
					public void resultAvailable(Void result)
					{
						ServiceCall sc = CallAccess.getCurrentInvocation();
						System.out.println("call returned with: "+sc);
						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