🎨 #3640 【微信支付】使用HttpClient发送http请求时调整为使用连接池的形式
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package com.github.binarywang.wxpay.service.impl;
|
||||
|
||||
import com.github.binarywang.wxpay.config.WxPayConfig;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* 演示连接池功能的示例测试
|
||||
*/
|
||||
public class ConnectionPoolUsageExampleTest {
|
||||
|
||||
@Test
|
||||
public void demonstrateConnectionPoolUsage() throws Exception {
|
||||
// 1. 创建配置并设置连接池参数
|
||||
WxPayConfig config = new WxPayConfig();
|
||||
config.setAppId("wx123456789");
|
||||
config.setMchId("1234567890");
|
||||
config.setMchKey("32位商户密钥32位商户密钥32位商户密钥");
|
||||
|
||||
// 设置连接池参数(可选,有默认值)
|
||||
config.setMaxConnTotal(50); // 最大连接数,默认20
|
||||
config.setMaxConnPerRoute(20); // 每个路由最大连接数,默认10
|
||||
|
||||
// 2. 初始化连接池
|
||||
CloseableHttpClient pooledClient = config.initHttpClient();
|
||||
Assert.assertNotNull(pooledClient);
|
||||
|
||||
// 3. 创建支付服务实例
|
||||
WxPayServiceApacheHttpImpl payService = new WxPayServiceApacheHttpImpl();
|
||||
payService.setConfig(config);
|
||||
|
||||
// 4. 现在所有的HTTP请求都会使用连接池
|
||||
// 对于非SSL请求,会复用同一个HttpClient实例
|
||||
CloseableHttpClient client1 = payService.createHttpClient(false);
|
||||
CloseableHttpClient client2 = payService.createHttpClient(false);
|
||||
Assert.assertSame(client1, client2, "非SSL请求应该复用同一个客户端实例");
|
||||
|
||||
// 对于SSL请求,也会复用同一个SSL HttpClient实例(需要配置证书后)
|
||||
System.out.println("连接池配置成功!");
|
||||
System.out.println("最大连接数:" + config.getMaxConnTotal());
|
||||
System.out.println("每路由最大连接数:" + config.getMaxConnPerRoute());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void demonstrateDefaultConfiguration() throws Exception {
|
||||
// 使用默认配置的示例
|
||||
WxPayConfig config = new WxPayConfig();
|
||||
config.setAppId("wx123456789");
|
||||
config.setMchId("1234567890");
|
||||
config.setMchKey("32位商户密钥32位商户密钥32位商户密钥");
|
||||
|
||||
// 不设置连接池参数,使用默认值
|
||||
CloseableHttpClient client = config.initHttpClient();
|
||||
Assert.assertNotNull(client);
|
||||
|
||||
// 验证默认配置
|
||||
Assert.assertEquals(config.getMaxConnTotal(), 20, "默认最大连接数应该是20");
|
||||
Assert.assertEquals(config.getMaxConnPerRoute(), 10, "默认每路由最大连接数应该是10");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.github.binarywang.wxpay.service.impl;
|
||||
|
||||
import com.github.binarywang.wxpay.config.WxPayConfig;
|
||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* 测试WxPayServiceApacheHttpImpl的连接池功能
|
||||
*/
|
||||
public class WxPayServiceApacheHttpImplConnectionPoolTest {
|
||||
|
||||
@Test
|
||||
public void testHttpClientConnectionPool() throws Exception {
|
||||
WxPayConfig config = new WxPayConfig();
|
||||
config.setAppId("test-app-id");
|
||||
config.setMchId("test-mch-id");
|
||||
config.setMchKey("test-mch-key");
|
||||
|
||||
// 测试初始化连接池
|
||||
CloseableHttpClient httpClient1 = config.initHttpClient();
|
||||
Assert.assertNotNull(httpClient1, "HttpClient should not be null");
|
||||
|
||||
// 再次获取,应该返回同一个实例
|
||||
CloseableHttpClient httpClient2 = config.getHttpClient();
|
||||
Assert.assertSame(httpClient1, httpClient2, "Should return the same HttpClient instance");
|
||||
|
||||
// 验证连接池配置
|
||||
WxPayServiceApacheHttpImpl service = new WxPayServiceApacheHttpImpl();
|
||||
service.setConfig(config);
|
||||
|
||||
// 测试不使用SSL的情况下应该使用连接池
|
||||
CloseableHttpClient clientForNonSSL = service.createHttpClient(false);
|
||||
Assert.assertSame(httpClient1, clientForNonSSL, "Should use pooled client for non-SSL requests");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSslHttpClientConnectionPool() throws Exception {
|
||||
WxPayConfig config = new WxPayConfig();
|
||||
config.setAppId("test-app-id");
|
||||
config.setMchId("test-mch-id");
|
||||
config.setMchKey("test-mch-key");
|
||||
|
||||
// 为了测试SSL客户端,我们需要设置一些基本的SSL配置
|
||||
// 注意:在实际使用中需要提供真实的证书
|
||||
try {
|
||||
CloseableHttpClient sslClient1 = config.initSslHttpClient();
|
||||
Assert.assertNotNull(sslClient1, "SSL HttpClient should not be null");
|
||||
|
||||
CloseableHttpClient sslClient2 = config.getSslHttpClient();
|
||||
Assert.assertSame(sslClient1, sslClient2, "Should return the same SSL HttpClient instance");
|
||||
|
||||
WxPayServiceApacheHttpImpl service = new WxPayServiceApacheHttpImpl();
|
||||
service.setConfig(config);
|
||||
|
||||
// 测试使用SSL的情况下应该使用SSL连接池
|
||||
CloseableHttpClient clientForSSL = service.createHttpClient(true);
|
||||
Assert.assertSame(sslClient1, clientForSSL, "Should use pooled SSL client for SSL requests");
|
||||
|
||||
} catch (WxPayException e) {
|
||||
// SSL初始化失败是预期的,因为我们没有提供真实的证书
|
||||
// 这里主要是测试代码路径是否正确
|
||||
Assert.assertTrue(e.getMessage().contains("证书") || e.getMessage().contains("商户号"),
|
||||
"Should fail with certificate or merchant ID related error");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionPoolConfiguration() throws Exception {
|
||||
WxPayConfig config = new WxPayConfig();
|
||||
config.setAppId("test-app-id");
|
||||
config.setMchId("test-mch-id");
|
||||
config.setMchKey("test-mch-key");
|
||||
config.setMaxConnTotal(50);
|
||||
config.setMaxConnPerRoute(20);
|
||||
|
||||
CloseableHttpClient httpClient = config.initHttpClient();
|
||||
Assert.assertNotNull(httpClient, "HttpClient should not be null");
|
||||
|
||||
// 验证配置值是否正确设置
|
||||
Assert.assertEquals(config.getMaxConnTotal(), 50, "Max total connections should be 50");
|
||||
Assert.assertEquals(config.getMaxConnPerRoute(), 20, "Max connections per route should be 20");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user