单元测试
This commit is contained in:
parent
e2c8695f47
commit
9fbcd1baad
@ -259,11 +259,10 @@ public class Test4Jemter extends AbstractJavaSamplerClient {
|
||||
//detach验签
|
||||
boolean ver1 = api.get().detachedVerify(orgData, detachedSign);
|
||||
|
||||
|
||||
testImportCert();
|
||||
// testImportCert();
|
||||
}
|
||||
|
||||
public void testImportCert() {
|
||||
private void testImportCert() {
|
||||
String cert = "-----BEGIN CERTIFICATE-----\n" +
|
||||
"MIICMDCCAdagAwIBAgIJAOWoGwJCnjlLMAoGCCqBHM9VAYN1MGcxCzAJBgNVBAYT\n" +
|
||||
"AkNOMRAwDgYDVQQIDAdCZWlqaW5nMRAwDgYDVQQHDAdIYWlEaWFuMRMwEQYDVQQK\n" +
|
||||
@ -278,18 +277,18 @@ public class Test4Jemter extends AbstractJavaSamplerClient {
|
||||
"BggqgRzPVQGDdQNIADBFAiEA4kgTdn/iA/XYfjTEeOcELwBXd0tTFghkpfP8DhBF\n" +
|
||||
"KFQCIAXZTL6VPtW67tM/DF4ZgFt5S/YIiFombKa2bWOfL81u\n" +
|
||||
"-----END CERTIFICATE-----";
|
||||
List<String> masterNodes = new ArrayList<>();
|
||||
masterNodes.add("172.1.41.139:8889");
|
||||
int timeout = 1000;
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("snmp.target.ip", "172.1.41.191");
|
||||
map.put("snmp.target.port", "162");
|
||||
map.put("enterprise.oid", "1.3.6.1.4.1.9999");
|
||||
map.put("snmp.community", "public");
|
||||
map.put("snmp.version", "1");
|
||||
List<String> masterNodes = new ArrayList<>();
|
||||
masterNodes.add("172.1.41.139:8889");
|
||||
|
||||
String snmpConfig = new Gson().toJson(map);
|
||||
SydApi4j sydApi4j = new SydApi4j();
|
||||
sydApi4j.importCertToServers(masterNodes, 100, cert, "12345678", snmpConfig);
|
||||
sydApi4j.importCertToServers(masterNodes, timeout, cert, "12345678", snmpConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -138,7 +138,7 @@ public class SydApiException extends RuntimeException{
|
||||
}
|
||||
|
||||
public SydApiException(String msg, int retCode, boolean flag){
|
||||
super(String.format(" %04X", retCode) + ":" + ( null == msg ? transMsg(retCode) : msg ));
|
||||
super(null == msg ? transMsg(retCode) : msg);
|
||||
this.retCode = retCode;
|
||||
if (null == msg || "".equals(msg)){
|
||||
transMsg();
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package racal.sunyard.main;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.sunyard.RetWrap;
|
||||
@ -85,8 +86,6 @@ public class SydApi4j implements SydApi {
|
||||
|
||||
private final Object reqLock = new Object();
|
||||
|
||||
private final Lock connectLock = new ReentrantLock();
|
||||
|
||||
/** --------------------------主备切换字段--------------------------------------------*/
|
||||
|
||||
// 网络失败时重试次数
|
||||
@ -128,7 +127,7 @@ public class SydApi4j implements SydApi {
|
||||
* @param ip2
|
||||
* @param port
|
||||
* @param timeout
|
||||
* todo 不应该这样使用
|
||||
* todo 目前主备切换只适配了安徽农信接口
|
||||
* @return
|
||||
*/
|
||||
public static int SYD_Set_Parameter_HA(
|
||||
@ -137,12 +136,13 @@ public class SydApi4j implements SydApi {
|
||||
int port,
|
||||
int timeout
|
||||
) {
|
||||
|
||||
Assert.notBlank(ip1, "主机IP不能为空");
|
||||
Assert.notBlank(ip2, "备机IP不能为空");
|
||||
Assert.notNull(port, "端口不能为空");
|
||||
SydApi4j.ha_ip1 = ip1;
|
||||
SydApi4j.ha_ip2 = ip2;
|
||||
SydApi4j.ha_port = port;
|
||||
SydApi4j.ha_timeout = timeout;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -713,7 +713,7 @@ public class SydApi4j implements SydApi {
|
||||
ByteBuffer readData = null;
|
||||
int maxRetries = 2;
|
||||
int retryCount = 0;
|
||||
while (retryCount <= maxRetries) {
|
||||
while (retryCount < maxRetries) {
|
||||
try {
|
||||
synchronized(reqLock) {
|
||||
byte[] sentData = this.syncSend(data);
|
||||
@ -858,10 +858,11 @@ public class SydApi4j implements SydApi {
|
||||
data.put("returnSign$option", 0);
|
||||
ByteBuffer bb = proto.rend(packet);
|
||||
|
||||
synchronized(this) {
|
||||
byte[] b = this.syncSend(bb);
|
||||
bb = this.syncRead(b);
|
||||
}
|
||||
// synchronized(this) {
|
||||
// byte[] b = this.syncSend(bb);
|
||||
// bb = this.syncRead(b);
|
||||
// }
|
||||
bb = sendAndGetResponse(bb);
|
||||
|
||||
Packet p = proto.parse(bb, packet);
|
||||
Map<String, Object> ret = p.toMap();
|
||||
@ -1397,6 +1398,10 @@ public class SydApi4j implements SydApi {
|
||||
} catch (Exception e) {
|
||||
log.warn("snmp告警消息发送失败:{}", e.getMessage());
|
||||
}
|
||||
// Map<String, String> msgMap = new HashMap<>();
|
||||
// msgMap.put("alarmMsg", message);
|
||||
// msgMap.put("certDn", dn);
|
||||
// throw new SydApiException(gson.toJson(msgMap) ,56300, true);
|
||||
}
|
||||
return dn;
|
||||
}
|
||||
@ -2357,14 +2362,14 @@ public class SydApi4j implements SydApi {
|
||||
// 优先尝试主节点
|
||||
if (!usingBackup.get()) {
|
||||
if (tryConnect(ha_ip1)) {
|
||||
System.out.println("连接到主节点: " + ha_ip1);
|
||||
// System.out.println("连接到主节点: " + ha_ip1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 连接到备节点
|
||||
if (tryConnect(ha_ip2)) {
|
||||
System.out.println("连接到备节点: " + ha_ip2);
|
||||
// System.out.println("连接到备节点: " + ha_ip2);
|
||||
usingBackup.set(true);
|
||||
return;
|
||||
}
|
||||
@ -2382,12 +2387,10 @@ public class SydApi4j implements SydApi {
|
||||
|
||||
if (ha_timeout > 0) {
|
||||
newSocket.connect(address, ha_timeout);
|
||||
newSocket.setSoTimeout(ha_timeout);
|
||||
} else {
|
||||
newSocket.connect(address);
|
||||
}
|
||||
|
||||
newSocket.setSoTimeout(ha_timeout);
|
||||
|
||||
// 关闭旧socket(如果存在)
|
||||
safeClose();
|
||||
this.socket = newSocket;
|
||||
@ -2400,31 +2403,29 @@ public class SydApi4j implements SydApi {
|
||||
|
||||
private synchronized void handleNetworkFailure(Exception e, int retryCount, int maxRetries) {
|
||||
if (retryCount > maxRetries) {
|
||||
System.out.println(Thread.currentThread().getName() + "重试次数耗尽");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.printf("线程[%s]网络故障,尝试切换节点 (重试: %d/%d)%n",
|
||||
Thread.currentThread().getName(), retryCount + 1, maxRetries);
|
||||
// System.out.printf("线程[%s]网络故障,尝试切换节点 (重试: %d/%d)%n",
|
||||
// Thread.currentThread().getName(), retryCount + 1, maxRetries);
|
||||
|
||||
boolean currentState = usingBackup.get();
|
||||
boolean newState = !currentState;
|
||||
|
||||
if (usingBackup.compareAndSet(currentState, newState)) {
|
||||
String targetIp = newState ? ha_ip2 : ha_ip1;
|
||||
System.out.println("全局节点切换: " + (currentState ? "备机" : "主机") + "→" +
|
||||
(newState ? "备机" : "主机"));
|
||||
// System.out.println("全局节点切换: " + (currentState ? "备机" : "主机") + "→" +
|
||||
// (newState ? "备机" : "主机"));
|
||||
|
||||
try {
|
||||
safeClose();
|
||||
connect(targetIp, ha_port, ha_timeout);
|
||||
System.out.println("线程[" + Thread.currentThread().getName() + "]连接重建成功");
|
||||
// System.out.println("线程[" + Thread.currentThread().getName() + "]连接重建成功");
|
||||
} catch (Exception ex) {
|
||||
System.out.println("线程[" + Thread.currentThread().getName() + "]节点切换失败: " + ex.getMessage());
|
||||
// System.out.println("线程[" + Thread.currentThread().getName() + "]节点切换失败: " + ex.getMessage());
|
||||
usingBackup.compareAndSet(newState, currentState);
|
||||
}
|
||||
} else {
|
||||
System.out.println("线程[" + Thread.currentThread().getName() + "]节点已被其他线程切换,使用新节点");
|
||||
System.err.println("线程[" + Thread.currentThread().getName() + "]节点已被其他线程切换,使用新节点");
|
||||
}
|
||||
}
|
||||
|
||||
@ -2433,7 +2434,7 @@ public class SydApi4j implements SydApi {
|
||||
try {
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("关闭连接异常: " + e.getMessage());
|
||||
System.err.println("关闭连接异常: " + e.getMessage());
|
||||
}
|
||||
socket = null;
|
||||
}
|
||||
@ -2633,15 +2634,16 @@ public class SydApi4j implements SydApi {
|
||||
private void connect(String ip, int port, int timeout) {
|
||||
synchronized (reqLock) {
|
||||
safeClose();
|
||||
|
||||
try {
|
||||
Socket newSocket = new Socket();
|
||||
SocketAddress address = new InetSocketAddress(ip, port);
|
||||
newSocket.connect(address, timeout);
|
||||
newSocket.setSoTimeout(timeout);
|
||||
if (timeout > 0) {
|
||||
newSocket.connect(address, timeout);
|
||||
newSocket.setSoTimeout(timeout);
|
||||
} else {
|
||||
newSocket.connect(address);
|
||||
}
|
||||
this.socket = newSocket;
|
||||
System.out.printf("线程[%s]连接到%s:%d%n",
|
||||
Thread.currentThread().getName(), ip, port);
|
||||
} catch (IOException e) {
|
||||
throw new SydApiException("连接失败: " + ip + ":" + port, -2, e);
|
||||
}
|
||||
@ -2660,10 +2662,10 @@ public class SydApi4j implements SydApi {
|
||||
try {
|
||||
if (timeout > 0) {
|
||||
socket.connect(address, timeout);
|
||||
socket.setSoTimeout(timeout);
|
||||
} else {
|
||||
socket.connect(address);
|
||||
}
|
||||
socket.setSoTimeout(timeout);
|
||||
} catch (Exception e) {
|
||||
socket = reConnect(new SydApiException(-2, e));
|
||||
}
|
||||
@ -2739,7 +2741,6 @@ public class SydApi4j implements SydApi {
|
||||
while (this.retryWhenNetError > 0) {
|
||||
try {
|
||||
println("Net error retry on " + this.retryWhenNetError);
|
||||
System.out.println("Net error retry on ");
|
||||
Socket socket = new Socket();
|
||||
SocketAddress address = new InetSocketAddress(ip, port);
|
||||
|
||||
|
||||
@ -1,13 +1,10 @@
|
||||
package sample;
|
||||
|
||||
import cn.hutool.core.date.StopWatch;
|
||||
import cn.hutool.core.date.Week;
|
||||
import com.google.gson.Gson;
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.proto.Util;
|
||||
import com.sunyard.snmp.SnmpTrapSender;
|
||||
import com.sunyard.util.IpLockUtils;
|
||||
import org.bouncycastle.util.encoders.Hex;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@ -56,8 +53,9 @@ public class Test4NongxinCW {
|
||||
@Test
|
||||
public void testHA() {
|
||||
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
byte[] nakedSign = api.SYD_NakedSign(orgData,SM2dn);
|
||||
// System.out.println("SM2签名:"+ Util.bytes2HexString(nakedSign));
|
||||
}
|
||||
}
|
||||
|
||||
@ -261,8 +259,13 @@ public class Test4NongxinCW {
|
||||
List<Thread> threads = new ArrayList<>();
|
||||
|
||||
// 创建100个线程
|
||||
for (int i = 0; i < 200; i++) {
|
||||
Thread thread = new Thread(this::importAllCert);
|
||||
for (int i = 0; i < 20; i++) {
|
||||
Thread thread = new Thread(() -> {
|
||||
for (int j = 0; j < 1000; j++) {
|
||||
api.SYD_NakedSign(orgData,SM2dn);
|
||||
importAllCert();
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
threads.add(thread);
|
||||
}
|
||||
@ -272,7 +275,7 @@ public class Test4NongxinCW {
|
||||
thread.join();
|
||||
}
|
||||
|
||||
System.out.println(IpLockUtils.getLockStatus("172.1.41.96:8889"));
|
||||
System.out.println(IpLockUtils.getLockStatus("172.1.41.139:8889"));
|
||||
|
||||
System.out.println("所有线程执行完毕");
|
||||
}
|
||||
@ -319,9 +322,9 @@ public class Test4NongxinCW {
|
||||
// masterNodes.add("172.16.18.99:8889");
|
||||
// masterNodes.add("172.16.18.5:8889");
|
||||
masterNodes.add("172.1.41.139:8889");
|
||||
masterNodes.add("172.1.41.96:8889");
|
||||
// masterNodes.add("172.1.41.96:8889");
|
||||
// masterNodes.add("172.16.18.8:8889");
|
||||
int timeout = 1;
|
||||
int timeout = 1000;
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("snmp.target.ip", "172.1.41.191");
|
||||
map.put("snmp.target.port", "162");
|
||||
@ -330,7 +333,14 @@ public class Test4NongxinCW {
|
||||
map.put("snmp.version", "1");
|
||||
String snmpConfig = new Gson().toJson(map);
|
||||
SydApi4j sydApi4j = new SydApi4j();
|
||||
sydApi4j.importCertToServers(masterNodes, timeout, cert1, "12345678", snmpConfig);
|
||||
sydApi4j.importCertToServers(masterNodes, timeout, cert, "12345678", snmpConfig);
|
||||
// try {
|
||||
// sydApi4j.importCertToServers(masterNodes, timeout, cert1, "12345678", snmpConfig);
|
||||
// } catch (SydApiException e) {
|
||||
//// if (e.getRetCode() == 56300) {
|
||||
//// System.out.println(e.getMessage());
|
||||
//// }
|
||||
// }
|
||||
stopWatch.stop();
|
||||
System.out.println(stopWatch.getTotalTimeSeconds());
|
||||
}
|
||||
@ -359,8 +369,8 @@ public class Test4NongxinCW {
|
||||
|
||||
@Test
|
||||
public void selCert() {
|
||||
String sign = "3045022100F2C494D20823AB12D9D4D23FA12C7D856D4993BE122089D86A515F217CF51C0C02207E06F505F4E70893226F7B22B34E6578570E2813CB0CF1A6775A7782C7C45F7B";
|
||||
String s = api.getCertBySign(sign);
|
||||
String data = "MIIDyQYKKoEcz1UGAQQCAqCCA7kwggO1AgEBMQ4wDAYIKoEcz1UBgxEFADAMBgoqgRzPVQYBBAIBoIICujCCArYwggJboAMCAQICBSBhWZmSMAwGCCqBHM9VAYN1BQAwXTELMAkGA1UEBhMCQ04xMDAuBgNVBAoMJ0NoaW5hIEZpbmFuY2lhbCBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTEcMBoGA1UEAwwTQ0ZDQSBURVNUIFNNMiBPQ0ExMTAeFw0yMjEwMjQwMTM4NDdaFw0yNTEwMjQxNTU5NTlaMG0xCzAJBgNVBAYTAmNuMRUwEwYDVQQKDAxDRkNBIFRFU1QgQ0Ex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