接口自动化测试:Thrift框架RPC协议客户端开发
import java.lang.Thread.State;
import java.util.Iterator;
import java.util.List;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import tdbservice.ErrorReturn;
import tdbservice.TDB_ReqTLine;
import tdbservice.TDB_TLine;
import tdbservice.TdbService;
public class Client {
private TTransport transport ;
private TdbService.Client client;
private String result = new String();
private String url;
private int port;
public Client(String url,int port) throws TTransportException{
this.url = url;
this.port = port;
// transport = new TSocket(url, port);
transport = new TFramedTransport(new TSocket(this.url, this.port));
//以上两行为两种不同transport,书写方法主要取决于服务端的书写格式
if(!transport.isOpen()){
TProtocol protocol = new TBinaryProtocol(transport);
client = new TdbService.Client(protocol);//此处的TdbService的调用由服务端来决定
transport.open();
}
State state = Thread.currentThread().getState();
String threadName = Thread.currentThread().getName();
System.out.println(threadName +"--------"+ state.toString());
}
public TTransport getTTransport(){
return transport;
}
public String testGetAllStock(){
try {
result = client.get_allstock();
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} finally{
transport.close();
}
return result;
}
private List<TDB_TLine> TLineResult;
private StringBuffer sbResult = new StringBuffer();
public String testTLine(TDB_ReqTLine req){//TDB_ReqTLine对象由服务端决定,这里的就是我要测试的接口get_tline的一个参数
try {
TLineResult = client.get_tline(req);//此处执行测试接口的调用
Iterator<TDB_TLine> it = TLineResult.iterator();
while(it.hasNext()){
TDB_TLine t = it.next();
sbResult.append(t.toString());
}
} catch (ErrorReturn e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
}
return sbResult.toString();
}
}