使用定制代码指定结束策略

您可以编写定制 Java™ 类来指定套接字接收元素何时停止接收。这提供最大灵活性,但是要求您使用 Rational® Performance Tester 扩展 API 编写自己的 Java 类。

开始之前

结束策略指定接收元素如何停止接收并允许测试继续。有若干个预定义的结束策略可供选择,例如在接收一定数量的字节后,或在检测到特定字符串时。但是,在某些情况下,必须定义复杂条件。这可以通过将决策委派给定制代码来实现。

过程

要创建新定制代码类,请执行以下操作:

  1. 在测试编辑器中,选择套接字接收元素。
  2. 结束策略部分中,选择委派给定制代码,然后单击生成代码 这将创建一个遵循 Rational Performance Tester 扩展 API 的 Java 类模板。Java 类创建在当前项目的 src 文件夹中。
  3. 通过扩展生成类来编写定制代码。 请参阅使用定制代码扩展测试执行以获取有关通过 Java 代码扩展 Rational Performance Tester 的更多信息。
  4. 保存定制代码和测试。 以后您可以单击查看代码来编辑该 Java 类。

示例

以下示例是样本定制类,它演示如何为因特网时间协议配置定制结束策略:
package test;

import java.text.DateFormat;
import java.util.Date;
import java.util.TimeZone;

import com.ibm.rational.test.lt.execution.socket.custom.ISckCustomReceivePolicy;
import com.ibm.rational.test.lt.execution.socket.custom.ISckReceiveAction;
import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;

/**
 *  Custom receive policy CustomReceive_TimeReceiver.
 *  For javadoc of ITestExecutionServices, select 'Help Contents' in the Help menu and select
 *  'Extending Rational Performance Tester functionality' -> 'Extending test execution with custom code'
 */
public class CustomReceive_TimeReceiver implements ISckCustomReceivePolicy {

	// static {
	//	 static blocks are called once on each run and allow for example to bind
	//	 to an external dynamic library
	// }

	ISckReceiveAction receiveAction;
	ITestExecutionServices testExecutionServices;

	public CustomReceive_TimeReceiver() {
		// The constructor is called during the test creation, not at the time of the execution of
		// the customized receive action
	}

	public void setup(ITestExecutionServices tesRef,
			ISckReceiveAction receiveActionRef) {
		testExecutionServices = tesRef;
		receiveAction = receiveActionRef;
	}

	public boolean onRead(int readByte) {
		// TIME protocol (RFC 868): a connected server returns 4 bytes and closes the connection
		// Those 4 bytes are the number of seconds since 1900/1/1
		// The test is simply made of a connection to one TIME server on port 37
		// (public servers are listed here: Got time server host name from http://tf.nist.gov/service/time-servers.html),
		// Then a receive delegated to this custom code class,
		// Then a close
		try {
			if (readByte == EndOfStream) {
				/* In case of success: */
				receiveAction.receiveSuccess();
				String message = extractAndCheckTime(receiveAction.getConnectionHolder().getFinallyReceivedBytes());
				/* A message is appended in the Test Log just after this receive action: */
				testExecutionServices.getTestLogManager().reportMessage(message);
				return true;
			}
		} catch (Throwable t) {
			/* In case of exception: */
			receiveAction.handleException(t);
			return true;
		}
		if (receiveAction.getConnectionHolder().getCurrentlyReceivedBytesCount() > 4) {
			/* Unexpected condition: */
			receiveAction.handleException(new Exception("Time protocol server returned more than 4 bytes"));
			return true;
		}
		/* We need further bytes to complete this receive */
		return false;
	}

	private String extractAndCheckTime(byte[] bytes) {
		// This is network order, i.e. big endian
		long remoteTime = ((((long)bytes[0]) & 0x00000000000000ff) << 24) +
						  ((((long)bytes[1]) & 0x00000000000000ff) << 16) +
						  ((((long)bytes[2]) & 0x00000000000000ff) << 8) +
						   (((long)bytes[3]) & 0x00000000000000ff);
		// 1900 to 1970: a difference of reference, see RFC 868 and java.util.Date javadoc
		remoteTime -= 2208988800L;
		Date remoteDate = new Date(remoteTime*1000);
		Date localDate = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance();
		dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
		String message = "Remote time: " + dateFormat.format(remoteDate) + " GMT (TIME server is " +
											receiveAction.getConnectionHolder().getHostName() + ", port 37)\n" +
						 "Local time:  " + dateFormat.format(localDate) + " GMT\n";
		long diff = localDate.getTime()/1000 - remoteTime;
		if (diff == 0) {
			message += "-> No difference";
		} else {
			message += "-> Difference (seconds): " + diff;
		}
		return message;
	}

}

反馈