修改野火IM jmeter插件的连接测试case
This commit is contained in:
@@ -188,7 +188,7 @@ public abstract class AbstractMQTTSampler extends AbstractSampler implements Con
|
||||
protected String privateSecret;
|
||||
protected String token;
|
||||
|
||||
protected boolean getToken(String userId) {
|
||||
protected boolean getToken(String userId, String clientId) {
|
||||
String url = "http://" + getServer() + ":18080/admin/user/token";
|
||||
String adminSecret = "123456";
|
||||
HttpPost post = null;
|
||||
@@ -208,7 +208,7 @@ public abstract class AbstractMQTTSampler extends AbstractSampler implements Con
|
||||
post.setHeader("timestamp", "" + timestamp);
|
||||
post.setHeader("sign", sign);
|
||||
|
||||
String jsonStr = new Gson().toJson(new InputGetToken(userId, getClientId()));
|
||||
String jsonStr = new Gson().toJson(new InputGetToken(userId, clientId));
|
||||
logger.info("http request content: " + jsonStr);
|
||||
|
||||
StringEntity entity = new StringEntity(jsonStr, Charset.forName("UTF-8"));
|
||||
@@ -243,30 +243,8 @@ public abstract class AbstractMQTTSampler extends AbstractSampler implements Con
|
||||
}
|
||||
|
||||
OutputGetIMTokenData out = result.getResult();
|
||||
if (out != null && out.getToken() != null) {
|
||||
byte[] data = Base64.getDecoder().decode(token);
|
||||
if (data != null) {
|
||||
data = AES.AESDecrypt(data, commonSecret, false);
|
||||
|
||||
if (data != null) {
|
||||
String s = new String(data);
|
||||
String[] ss = s.split("\\|");
|
||||
if (ss.length == 3) {
|
||||
this.token = ss[0];
|
||||
this.privateSecret = ss[1];
|
||||
return true;
|
||||
} else {
|
||||
logger.info("get token keys not correct (" + s + ")");
|
||||
}
|
||||
} else {
|
||||
logger.info("get token decode token failure");
|
||||
}
|
||||
} else {
|
||||
logger.info("get token decode base64 failure");
|
||||
}
|
||||
} else {
|
||||
logger.info("get token data invalide");
|
||||
}
|
||||
token = out.getToken();
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@@ -285,78 +263,4 @@ public abstract class AbstractMQTTSampler extends AbstractSampler implements Con
|
||||
.build();
|
||||
return new Gson().fromJson(content, type);
|
||||
}
|
||||
|
||||
protected boolean route(String userId, String token) {
|
||||
HttpPost httpPost;
|
||||
try{
|
||||
HttpClient httpClient = new DefaultHttpClient();
|
||||
httpPost = new HttpPost("http://" + getServer() + ":" + getPort() + "/route");
|
||||
String cid = Base64.getEncoder().encodeToString(AES.AESEncrypt(getClientId().getBytes(), commonSecret));
|
||||
httpPost.setHeader("cid", cid);
|
||||
|
||||
String uid = Base64.getEncoder().encodeToString(AES.AESEncrypt(userId.getBytes(), commonSecret));
|
||||
httpPost.setHeader("uid", uid);
|
||||
|
||||
InputRoute inputRoute = new InputRoute();
|
||||
inputRoute.setUserId(userId);
|
||||
inputRoute.setClientId(getClientId());
|
||||
inputRoute.setToken(token);
|
||||
|
||||
WFCMessage.RouteRequest routeRequest = WFCMessage.RouteRequest.newBuilder().setApp("jmeter.test").setSdkVersion("0.1").build();
|
||||
WFCMessage.IMHttpWrapper request = WFCMessage.IMHttpWrapper.newBuilder().setClientId(getClientId()).setToken(token).setRequest("ROUTE").setData(routeRequest.toByteString()).build();
|
||||
byte[] data = AES.AESEncrypt(request.toByteArray(), privateSecret );
|
||||
data = Base64.getEncoder().encode(data);
|
||||
|
||||
|
||||
StringEntity entity = new StringEntity(new String(data), Charset.forName("UTF-8"));
|
||||
entity.setContentEncoding("UTF-8");
|
||||
entity.setContentType("application/json");
|
||||
httpPost.setEntity(entity);
|
||||
|
||||
|
||||
HttpResponse response = httpClient.execute(httpPost);
|
||||
if(response != null){
|
||||
if (response.getStatusLine().getStatusCode() != 200) {
|
||||
logger.error("Http response error {" + response.getStatusLine().getStatusCode() + "}");
|
||||
return false;
|
||||
}
|
||||
HttpEntity resEntity = response.getEntity();
|
||||
if(resEntity != null){
|
||||
|
||||
try {
|
||||
|
||||
byte[] contentbyte = new byte[(int)response.getEntity().getContentLength()];
|
||||
int readLen = response.getEntity().getContent().read(contentbyte);
|
||||
|
||||
data = new byte[contentbyte.length - 1];
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
data[i] = contentbyte[i+1];
|
||||
}
|
||||
|
||||
data = AES.AESDecrypt(data, privateSecret, true);
|
||||
|
||||
WFCMessage.RouteResponse routeResponse = WFCMessage.RouteResponse.parseFrom(data);
|
||||
mqttServerIp = routeResponse.getHost();
|
||||
mqttServerPort = routeResponse.getLongPort();
|
||||
routeResponse.getShortPort();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
logger.error(e.toString());
|
||||
} catch (UnsupportedOperationException e) {
|
||||
e.printStackTrace();
|
||||
logger.error(e.toString());
|
||||
}
|
||||
} else {
|
||||
logger.error("route response entry is nil");
|
||||
}
|
||||
} else {
|
||||
logger.error("Http response nil");
|
||||
}
|
||||
}catch(IOException ex){
|
||||
ex.printStackTrace();
|
||||
logger.error(ex.toString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package net.xmeter.samplers;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import cn.wildfirechat.client.IMClient;
|
||||
import cn.wildfirechat.proto.WFCMessage;
|
||||
import io.moquette.spi.impl.security.AES;
|
||||
import org.apache.jmeter.JMeter;
|
||||
import org.apache.jmeter.samplers.Entry;
|
||||
@@ -21,10 +25,12 @@ import org.fusesource.mqtt.client.Future;
|
||||
import org.fusesource.mqtt.client.FutureConnection;
|
||||
import org.fusesource.mqtt.client.MQTT;
|
||||
|
||||
import static cn.wildfirechat.client.IMClient.ConnectionStatus.ConnectionStatus_Connected;
|
||||
import static cn.wildfirechat.client.IMClient.ConnectionStatus.ConnectionStatus_Connecting;
|
||||
|
||||
public class ConnectionSampler extends AbstractMQTTSampler
|
||||
implements TestStateListener, ThreadListener, Interruptible, SampleListener {
|
||||
private transient static Logger logger = LoggingManager.getLoggerForClass();
|
||||
private transient MQTT mqtt = new MQTT();
|
||||
private transient FutureConnection connection = null;
|
||||
private boolean interrupt = false;
|
||||
//Declare it as static, for the instance variable will be set to initial value 0 in testEnded method.
|
||||
@@ -50,37 +56,46 @@ public class ConnectionSampler extends AbstractMQTTSampler
|
||||
try {
|
||||
result.sampleStart();
|
||||
|
||||
if (!getToken(getUserNameAuth())) {
|
||||
if (!getToken(getUserNameAuth(), getClientId())) {
|
||||
throw new Exception("get token failure!!!");
|
||||
}
|
||||
|
||||
if (!route(getUserNameAuth(), token)) {
|
||||
throw new Exception("route failure!!!");
|
||||
IMClient client = new IMClient(getUserNameAuth(), token, getClientId(), getServer(), Integer.parseInt(getPort()));
|
||||
|
||||
final Object lock = new Object();
|
||||
|
||||
|
||||
final List<Boolean> ret = new ArrayList<>();
|
||||
client.setConnectionStatusCallback(new IMClient.ConnectionStatusCallback() {
|
||||
@Override
|
||||
public void onConnectionStatusChanged(IMClient.ConnectionStatus newStatus) {
|
||||
if (newStatus != ConnectionStatus_Connected && newStatus != ConnectionStatus_Connecting) {
|
||||
synchronized (lock) {
|
||||
ret.add(true);
|
||||
lock.notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
synchronized (lock) {
|
||||
client.connect();
|
||||
lock.wait(Integer.parseInt(getConnKeepTime()) * 1000);
|
||||
}
|
||||
|
||||
mqtt.setHost("tcp://" + mqttServerIp + ":" + mqttServerPort);
|
||||
mqtt.setVersion(getMqttVersion());
|
||||
mqtt.setKeepAlive((short) Integer.parseInt(getConnKeepAlive()));
|
||||
|
||||
mqtt.setClientId(getClientId());
|
||||
mqtt.setConnectAttemptsMax(Integer.parseInt(getConnAttamptMax()));
|
||||
mqtt.setReconnectAttemptsMax(Integer.parseInt(getConnReconnAttamptMax()));
|
||||
|
||||
mqtt.setUserName(getUserNameAuth());
|
||||
|
||||
byte[] password = AES.AESEncrypt(token, privateSecret);
|
||||
mqtt.setPassword(new UTF8Buffer(password));
|
||||
|
||||
connection = mqtt.futureConnection();
|
||||
|
||||
Future<byte[]> f1 = connection.connect();
|
||||
f1.await(Integer.parseInt(getConnTimeout()), TimeUnit.SECONDS);
|
||||
|
||||
result.sampleEnd();
|
||||
result.setSuccessful(true);
|
||||
result.setResponseData("Successful.".getBytes());
|
||||
result.setResponseMessage(MessageFormat.format("Connection {0} connected successfully.", connection));
|
||||
result.setResponseCodeOK();
|
||||
if (ret.size() > 0) {
|
||||
result.setSuccessful(false);
|
||||
result.setResponseMessage(MessageFormat.format("Connection {0} connected failed.", connection));
|
||||
result.setResponseData("Failed.".getBytes());
|
||||
result.setResponseCode("500");
|
||||
} else {
|
||||
result.setSuccessful(true);
|
||||
result.setResponseData("Successful.".getBytes());
|
||||
result.setResponseMessage(MessageFormat.format("Connection {0} connected successfully.", connection));
|
||||
result.setResponseCodeOK();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.log(Priority.ERROR, e.getMessage(), e);
|
||||
result.sampleEnd();
|
||||
|
||||
Reference in New Issue
Block a user