From 53466443b6bb72130e354196129559e2d0b9079a Mon Sep 17 00:00:00 2001 From: tobato Date: Mon, 27 Jan 2020 14:32:13 +0800 Subject: [PATCH] =?UTF-8?q?refactor:2020=E6=98=A5=E8=8A=82=E5=A4=9A?= =?UTF-8?q?=E4=B8=AA=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BugFixes: - 修复DEFAULT_RETRY_AFTER_SECOND拼写错误(#181 @liangxiong3403) - 修复metaDataSet default value logic bug(#182 @liangxiong3403) Update: - JDK版本升级到1.8版本,做一个大的版本升级(#189 @HaoQiangJiang) - 重命名ConnectionManager ,解决与openfeign包冲突(#186 @summitxf) - 合并增加一种文件下载回调方法(#161 @xlb ),并在此基础上重构 - 增加连接池控制参数:连接空闲的最小时间、创建时是否进行连接测试、借出时是否检测有效性 - 调整连接池默认参数,关闭自动检查机制,如果需要自动检查,请手动开启 Docs: - 常见问题:生成的缩略图该怎么访问 (#179 @msh01) - 常见问题:能自定义上传路径么,或者有没有类似的解决方案(#173 @yj348382870) - 常见问题:请问当上传100M以上文件怎么才能获取进度(#172 @lxge) 升级提示: - 没有接口改动,默认JDK升级到1.8版本 close #181,#182,#189,#186,#161,#179,#173,#172 --- CHANGELOG.md | 15 +- pom.xml | 17 +- readme.md | 19 +- .../domain/conn/ConnectionManager.java | 189 ------------------ .../domain/conn/ConnectionPoolConfig.java | 49 ++++- .../domain/conn/FdfsConnectionManager.java | 13 +- .../domain/conn/PooledConnectionFactory.java | 13 ++ .../proto/storage/DownloadFileStream.java | 134 ++++++++----- .../github/tobato/fastdfs/TestConstants.java | 8 +- .../domain/conn/ConnectionPoolTest.java | 6 +- .../fastdfs/domain/conn/ConnectionTest.java | 7 +- .../domain/conn/MockServerTestBase.java | 5 +- .../conn/TrackerConnectionManagerTest.java | 9 +- .../proto/mapper/FdfsParamMapperTest.java | 28 ++- .../proto/storage/DownloadFileStreamTest.java | 43 ++++ .../fastdfs/socket/FdfsMockHandler.java | 13 +- .../fastdfs/socket/FdfsMockSocketServer.java | 16 +- src/test/resources/logback-test.xml | 2 +- 18 files changed, 279 insertions(+), 307 deletions(-) delete mode 100644 src/main/java/com/github/tobato/fastdfs/domain/conn/ConnectionManager.java create mode 100644 src/test/java/com/github/tobato/fastdfs/domain/proto/storage/DownloadFileStreamTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 88d69e9..811869d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,20 +1,29 @@ -## 1.26.8 (2019-12-30) +## 1.27.1 (2020-1-27) BugFixes: - 修复DEFAULT_RETRY_AFTER_SECOND拼写错误(#181 @liangxiong3403) - 修复metaDataSet default value logic bug(#182 @liangxiong3403) +Update: + + - JDK版本升级到1.8版本,做一个大的版本升级(#189 @HaoQiangJiang) + - 重命名ConnectionManager ,解决与openfeign包冲突(#186 @summitxf) + - 合并增加一种文件下载回调方法(#161 @xlb ),并在此基础上重构 + - 增加连接池控制参数:连接空闲的最小时间、创建时是否进行连接测试、借出时是否检测有效性 + - 调整连接池默认参数,关闭自动检查机制,如果需要自动检查,请手动开启 + + 感谢@HaoQiangJiang @summitxf 提交更新 + Docs: - 常见问题:生成的缩略图该怎么访问 (#179 @msh01) - 常见问题:能自定义上传路径么,或者有没有类似的解决方案(#173 @yj348382870) - 常见问题:请问当上传100M以上文件怎么才能获取进度(#172 @lxge) - 升级提示: - - 没有接口改动,可以直接从1.26.7升级 + - 默认JDK升级到1.8版本 ## 1.26.7 (2019-08-24) diff --git a/pom.xml b/pom.xml index c938e4a..92c6dd5 100644 --- a/pom.xml +++ b/pom.xml @@ -1,10 +1,9 @@ - + 4.0.0 com.github.tobato fastdfs-client - 1.26.8-SNAPSHOT + 1.27.1 jar @@ -30,14 +29,14 @@ scm:git:git@github.com:tobato/FastDFS_Client.git scm:git:git@github.com:tobato/FastDFS_Client.git https://github.com/tobato/FastDFS_Client - HEAD + fastdfs-client-1.27.1 org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE - + @@ -53,7 +52,7 @@ UTF-8 - 1.7 + 1.8 192.168.129.12:8081 @@ -277,6 +276,12 @@ spring-boot-configuration-processor true + + + + + + diff --git a/readme.md b/readme.md index 9f9fd6d..83705a8 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -FastDFS-Client 1.26.7(2019-08-24) +FastDFS-Client 1.27.1(2020-1-27) --- This is a java client lib for [FastDFS](https://github.com/happyfish100/fastdfs). @@ -34,7 +34,7 @@ This is a java client lib for [FastDFS](https://github.com/happyfish100/fastdfs) 2.0.0.RELEASE -* JDK环境要求 1.7 +* JDK环境要求 1.8 * FastDFS服务端 5.07 测试通过 ## 单元测试 @@ -71,7 +71,7 @@ Maven依赖为 com.github.tobato fastdfs-client - 1.26.7 + 1.27.1 @@ -97,7 +97,7 @@ Maven依赖为 // 导入依赖组件 } -对的,只需要一行注解 @Import(FdfsClientConfig.class)就可以拥有带有连接池的FastDFS Java客户端了。 +只需要一行注解 @Import(FdfsClientConfig.class)就可以拥有带有连接池的FastDFS Java客户端了。 >注意:`@EnableMBeanExport`解决问题JMX重复注册问题,[issue #8](../../issues/8) [issue #18](../../issues/8),不要再配置 `spring.jmx.enabled=false`,以免影响SpringBoot默认的JMX监控。 @@ -115,7 +115,16 @@ Maven依赖为 - 192.168.1.105:22122 - 192.168.1.106:22122 -如果有必要可以参考 apache.pool2 参数配置连接池属性,默认配置为 +### 4.连接池的管理参数 + +应用启动后拥有两个连接池管理对象: + +* Tracker连接池(`TrackerConnectionManager`) +* Storage连接池(`FdfsConnectionManager`) + +必要的时候可以注入这两个对象,跟踪打印并分析连接池的情况 + +两个连接池的参数配置一致,可参考 ConnectionPoolConfig 与 apache.pool2 进行优化配置,默认配置为 fdfs: ..其他配置信息.. diff --git a/src/main/java/com/github/tobato/fastdfs/domain/conn/ConnectionManager.java b/src/main/java/com/github/tobato/fastdfs/domain/conn/ConnectionManager.java deleted file mode 100644 index dc0487e..0000000 --- a/src/main/java/com/github/tobato/fastdfs/domain/conn/ConnectionManager.java +++ /dev/null @@ -1,189 +0,0 @@ -package com.github.tobato.fastdfs.domain.conn; - -import com.github.tobato.fastdfs.domain.proto.FdfsCommand; -import com.github.tobato.fastdfs.exception.FdfsException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.net.InetSocketAddress; - -/** - * 连接池管理 - *
- * 负责借出连接,在连接上执行业务逻辑,然后归还连
- *
- * ConnectionManager类主要负责StorageConnect连接管理
- * ConnectionManager类扩展的子类{@link TrackerConnectionManager}主要负责TrackerConnection连接管理
- * 
- * - * @author tobato - */ -@Component -public class ConnectionManager { - - /** - * 连接池 - */ - @Autowired - private FdfsConnectionPool pool; - /** - * 日志 - */ - protected static final Logger LOGGER = LoggerFactory.getLogger(ConnectionManager.class); - - /** - * 构造函数 - */ - public ConnectionManager() { - super(); - } - - /** - * 构造函数 - * - * @param pool - */ - public ConnectionManager(FdfsConnectionPool pool) { - super(); - this.pool = pool; - } - - /** - * 获取连接并执行交易 - * - * @param address - * @param command - * @return - */ - public T executeFdfsCmd(InetSocketAddress address, FdfsCommand command) { - - // 获取连接 - Connection conn = getConnection(address); - // 执行交易 - return execute(address, conn, command); - - } - - /** - * 执行交易 - * - * @param conn - * @param command - * @return - */ - protected T execute(InetSocketAddress address, Connection conn, FdfsCommand command) { - boolean isException = false; - try { - // 执行交易 - LOGGER.debug("对地址{}发出交易请求{}", address, command.getClass().getSimpleName()); - return command.execute(conn); - } catch (FdfsException e) { - LOGGER.error("execute fdfs command error", e); - isException = true; - throw e; - } catch (Exception e) { - LOGGER.error("execute fdfs command exception", e); - isException = true; - throw new RuntimeException("execute fdfs command error", e); - } finally { - if (isException) { - //移除连接 - LOGGER.debug("remove connect {}", conn); - removeConnect(address, conn); - } else { - //归还连接 - LOGGER.debug("return connect {}", conn); - returnConnect(address, conn); - } - - } - } - - /** - * 出现例外时从连接池移除连接 - * - * @param address - * @param conn - */ - private void removeConnect(InetSocketAddress address, Connection conn) { - try { - if (null != conn) { - //移除pool - pool.invalidateObject(address, conn); - } - } catch (Exception e) { - LOGGER.error("remove pooled connection error", e); - } - } - - /** - * 归还连接 - * - * @param address - * @param conn - */ - private void returnConnect(InetSocketAddress address, Connection conn) { - try { - if (null != conn) { - //归还连接 - pool.returnObject(address, conn); - } - } catch (Exception e) { - LOGGER.error("return pooled connection error", e); - } - } - - /** - * 获取连接 - * - * @param address - * @return - */ - protected Connection getConnection(InetSocketAddress address) { - Connection conn = null; - try { - // 获取连接 - conn = pool.borrowObject(address); - //dumpPoolInfo(address); - } catch (FdfsException e) { - throw e; - } catch (Exception e) { - LOGGER.error("Unable to borrow buffer from pool", e); - throw new RuntimeException("Unable to borrow buffer from pool", e); - } - return conn; - } - - public FdfsConnectionPool getPool() { - return pool; - } - - public void setPool(FdfsConnectionPool pool) { - this.pool = pool; - } - - /** - * 打印连接池情况 - * - * @param address - */ - public void dumpPoolInfo(InetSocketAddress address) { - - LOGGER.debug("==============Begin Dump Pool Info=========="); - LOGGER.debug("Address={}", address); - LOGGER.debug("连接池最大连接数配置{}", pool.getMaxTotal()); - LOGGER.debug("每个Key最大连接数配置{}", pool.getMaxTotalPerKey()); - LOGGER.debug("每个key对应连接池最大空闲连接数{}", pool.getMaxIdlePerKey()); - LOGGER.debug("每个key对应连接池最小空闲连接数{}", pool.getMinIdlePerKey()); - LOGGER.debug("活动连接{}", pool.getNumActive(address)); - LOGGER.debug("空闲连接{}", pool.getNumIdle(address)); - LOGGER.debug("连接获取总数统计{}", pool.getBorrowedCount()); - LOGGER.debug("连接返回总数统计{}", pool.getReturnedCount()); - LOGGER.debug("连接销毁总数统计{}", pool.getDestroyedCount()); - LOGGER.debug("==============END Dump Pool ================"); - - } - -} diff --git a/src/main/java/com/github/tobato/fastdfs/domain/conn/ConnectionPoolConfig.java b/src/main/java/com/github/tobato/fastdfs/domain/conn/ConnectionPoolConfig.java index 29c36d6..45da7c5 100644 --- a/src/main/java/com/github/tobato/fastdfs/domain/conn/ConnectionPoolConfig.java +++ b/src/main/java/com/github/tobato/fastdfs/domain/conn/ConnectionPoolConfig.java @@ -1,11 +1,10 @@ package com.github.tobato.fastdfs.domain.conn; +import com.github.tobato.fastdfs.FdfsClientConstants; import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; -import com.github.tobato.fastdfs.FdfsClientConstants; - /** * 连接池配置 * @@ -35,10 +34,11 @@ public class ConnectionPoolConfig extends GenericKeyedObjectPoolConfig { */ public static final int FDFS_MIN_IDLE_PER_KEY = 5; + /** - * 在空闲时检查有效性, 默认false + * 在空闲时检查有效性, 默认true */ - public static final boolean FDFS_TEST_WHILE_IDLE = true; + public static final boolean FDFS_TEST_WHILE_IDLE = false; /** * 连接耗尽时是否阻塞(默认true) @@ -52,19 +52,22 @@ public class ConnectionPoolConfig extends GenericKeyedObjectPoolConfig { */ public static final long FDFS_MAX_WAIT_MILLIS = 1000 * 5; - public static final long FDFS_MIN_EVICTABLE_IDLETIME_MILLIS = 180000; + /** + * 连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除 + */ + public static final long FDFS_MIN_EVICTABLE_IDLE_TIME_MILLIS = 60 * 30 * 1000; /** * 逐出扫描的时间间隔(毫秒) 每过60秒进行一次后台对象清理的行动 * 如果为负数,则不运行逐出线程, 默认-1 */ - public static final long FDFS_TIME_BETWEEN_EVICTION_RUNS_MILLIS = 60000; + public static final long FDFS_TIME_BETWEEN_EVICTION_RUNS_MILLIS = 60 * 1000; /** - * 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3 + * 对于“空闲链接”检测线程而言,每次检测的链接资源的个数,默认3 * -1表示清理时检查所有线程 */ - public static final int FDFS_NUM_TESTS_PEREVICTION_RUN = -1; + public static final int FDFS_NUM_TESTS_PEREVICTION_RUN = 3; /** * 默认jmx域名 @@ -76,6 +79,23 @@ public class ConnectionPoolConfig extends GenericKeyedObjectPoolConfig { */ public static final String FDFS_JMX_NAME_PREFIX = "fdfsPool"; + /** + * 连接空闲的最小时间,达到此值后空闲链接将会被移除,且保留“minIdle”个空闲连接数。默认为-1. + * 如果设置了FDFS_MIN_EVICTABLE_IDLE_TIME_MILLIS,则此参数被忽略 + */ + public static final int FDFS_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS = -1; + + /** + * 默认值false + */ + public static final boolean FDFS_TEST_ON_CREATE = false; + + /** + * 向调用者输出“链接”资源时,是否检测有效性,如果无效则从连接池中移除, + * 并尝试获取继续获取。默认为false。建议保持默认值. + */ + public static final boolean FDFS_TEST_ON_BORROW = true; + public ConnectionPoolConfig() { // 从池中借出的对象的最大数目 @@ -93,14 +113,21 @@ public ConnectionPoolConfig() { // 每个key对应的连接池最小空闲连接数 setMinIdlePerKey(FDFS_MIN_IDLE_PER_KEY); // 视休眠时间超过了180秒的对象为过期 - setMinEvictableIdleTimeMillis(FDFS_MIN_EVICTABLE_IDLETIME_MILLIS); + setMinEvictableIdleTimeMillis(FDFS_MIN_EVICTABLE_IDLE_TIME_MILLIS); // 每过60秒进行一次后台对象清理的行动 setTimeBetweenEvictionRunsMillis(FDFS_TIME_BETWEEN_EVICTION_RUNS_MILLIS); // 清理时候检查所有线程 setNumTestsPerEvictionRun(FDFS_NUM_TESTS_PEREVICTION_RUN); // 配置jmx - this.setJmxNameBase(FDFS_JMX_NAME_BASE); - this.setJmxNamePrefix(FDFS_JMX_NAME_PREFIX); + setJmxNameBase(FDFS_JMX_NAME_BASE); + setJmxNamePrefix(FDFS_JMX_NAME_PREFIX); + // 连接空闲的最小时间,达到此值后空闲链接将会被移除,且保留“minIdle”个空闲连接数 + setSoftMinEvictableIdleTimeMillis(FDFS_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS); + // 创建连接时进行连接测试 + setTestOnCreate(FDFS_TEST_ON_CREATE); + // 向调用者输出“连接”资源时,是否检测有效性 + setTestOnBorrow(FDFS_TEST_ON_BORROW); + } } \ No newline at end of file diff --git a/src/main/java/com/github/tobato/fastdfs/domain/conn/FdfsConnectionManager.java b/src/main/java/com/github/tobato/fastdfs/domain/conn/FdfsConnectionManager.java index 1ec26e1..0e3e882 100644 --- a/src/main/java/com/github/tobato/fastdfs/domain/conn/FdfsConnectionManager.java +++ b/src/main/java/com/github/tobato/fastdfs/domain/conn/FdfsConnectionManager.java @@ -12,10 +12,10 @@ /** * 连接池管理 *
- * 负责借出连接,在连接上执行业务逻辑,然后归还连
+ * 负责借出连接,在连接上执行业务逻辑,然后归还连接
  *
- * ConnectionManager类主要负责StorageConnect连接管理
- * ConnectionManager类扩展的子类{@link TrackerConnectionManager}主要负责TrackerConnection连接管理
+ * FdfsConnectionManager类主要负责StorageConnect连接管理
+ * FdfsConnectionManager类扩展的子类{@link TrackerConnectionManager}主要负责TrackerConnection连接管理
  * 
* * @author tobato @@ -179,11 +179,18 @@ public void dumpPoolInfo(InetSocketAddress address) { LOGGER.debug("每个key对应连接池最小空闲连接数{}", pool.getMinIdlePerKey()); LOGGER.debug("活动连接{}", pool.getNumActive(address)); LOGGER.debug("空闲连接{}", pool.getNumIdle(address)); + LOGGER.debug("获取前测试连接状态{}", pool.getTestOnBorrow()); + LOGGER.debug("归还前测试连接状态{}", pool.getTestOnReturn()); + LOGGER.debug("空闲时测试连接状态{}", pool.getTestWhileIdle()); LOGGER.debug("连接获取总数统计{}", pool.getBorrowedCount()); LOGGER.debug("连接返回总数统计{}", pool.getReturnedCount()); LOGGER.debug("连接销毁总数统计{}", pool.getDestroyedCount()); + LOGGER.debug("JmxName={}", pool.getJmxName()); LOGGER.debug("==============END Dump Pool ================"); + } + public void dumpFullPoolInfo() { + //pool.get } } diff --git a/src/main/java/com/github/tobato/fastdfs/domain/conn/PooledConnectionFactory.java b/src/main/java/com/github/tobato/fastdfs/domain/conn/PooledConnectionFactory.java index b9025b2..abcceff 100644 --- a/src/main/java/com/github/tobato/fastdfs/domain/conn/PooledConnectionFactory.java +++ b/src/main/java/com/github/tobato/fastdfs/domain/conn/PooledConnectionFactory.java @@ -84,11 +84,24 @@ public Charset getCharset() { return charset; } + /** + * 从池中移出 + * + * @param key + * @param p + * @throws Exception + */ @Override public void destroyObject(InetSocketAddress key, PooledObject p) throws Exception { p.getObject().close(); } + /*** + * 验证池中对象是否可用 + * @param key + * @param p + * @return + */ @Override public boolean validateObject(InetSocketAddress key, PooledObject p) { return p.getObject().isValid(); diff --git a/src/main/java/com/github/tobato/fastdfs/domain/proto/storage/DownloadFileStream.java b/src/main/java/com/github/tobato/fastdfs/domain/proto/storage/DownloadFileStream.java index 1f8b1e1..3eb39b0 100644 --- a/src/main/java/com/github/tobato/fastdfs/domain/proto/storage/DownloadFileStream.java +++ b/src/main/java/com/github/tobato/fastdfs/domain/proto/storage/DownloadFileStream.java @@ -1,48 +1,86 @@ -package com.github.tobato.fastdfs.domain.proto.storage; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.http.HttpServletResponse; - -/** - * 文件下载回调方法,1M1M读取,防止下载时内存溢出 - * 同时不要讲流返回出去,再进行操作,不然并发下载情况下会有各种问题 - * - * @author xulb - */ -public class DownloadFileStream implements DownloadCallback { - - private HttpServletResponse response; - - public DownloadFileStream(HttpServletResponse response) { - this.response=response; - } - - /** - * 文件接收处理 - * @return - */ - - @Override - public BufferedInputStream recv(InputStream ins) throws IOException { - BufferedInputStream bufferedInputStream = new BufferedInputStream(ins); - // 实现文件下载 - byte[] buffer = new byte[1024]; - OutputStream os=null; - try { - os = response.getOutputStream(); - int i = bufferedInputStream.read(buffer); - while (i != -1) { - os.write(buffer, 0, i); - i = bufferedInputStream.read(buffer); - } - } catch (Exception e) { - - throw new IOException("文件下载失败!",e); - }finally { - bufferedInputStream.close(); - os.flush(); - os.close(); - - } - return null; - - } - -} +package com.github.tobato.fastdfs.domain.proto.storage; + +import org.apache.commons.io.IOUtils; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Web环境下文件下载回调方法,默认按4K循环读取,防止下载时内存溢出 + *
+ *
+ * refactor:
+ * 将HttpServletResponse调整为OutputStream对象,
+ * 注意:使用时候在外层做response.getOutputStream(),使用完毕后,在外层做 os.close()
+ * 如:
+ *  os = response.getOutputStream();
+ *  DownloadFileStream stream = new DownloadFileStream(os);
+ *  ...
+ *  os.close();
+ *
+ * 
+ * + * @author xulb + */ +public class DownloadFileStream implements DownloadCallback { + + /** + * 默认缓存长度 + */ + private static final int DEFAULT_BUFFER_SIZE = 4096; + + /** + * 输出流 + * HttpServletResponse对象response.getOutputStream() + */ + private OutputStream outputStream; + + /** + * 默认缓存长度 + */ + private int bufferLength = DEFAULT_BUFFER_SIZE; + + + /** + * 从HttpServletResponse对象response.getOutputStream()构造 + * + * @param responseOutputStream 输出流 + */ + public DownloadFileStream(OutputStream responseOutputStream) { + this.outputStream = responseOutputStream; + } + + /** + * 从HttpServletResponse对象response.getOutputStream()构造 + * + * @param responseOutputStream 输出流 + * @param bufferLength 缓存长度 + */ + public DownloadFileStream(OutputStream responseOutputStream, int bufferLength) { + this.outputStream = responseOutputStream; + this.bufferLength = bufferLength; + } + + /** + * 文件接收处理 + * + * @return + */ + @Override + public BufferedInputStream recv(InputStream ins) throws IOException { + BufferedInputStream bufferedInputStream = new BufferedInputStream(ins); + // 实现文件下载 + byte[] buffer = new byte[bufferLength]; + try { + IOUtils.copyLarge(ins, outputStream, buffer); + } catch (IOException e) { + throw new IOException("文件下载失败!", e); + } finally { + IOUtils.closeQuietly(bufferedInputStream); + } + return null; + } + +} diff --git a/src/test/java/com/github/tobato/fastdfs/TestConstants.java b/src/test/java/com/github/tobato/fastdfs/TestConstants.java index e100a15..052a062 100644 --- a/src/test/java/com/github/tobato/fastdfs/TestConstants.java +++ b/src/test/java/com/github/tobato/fastdfs/TestConstants.java @@ -1,7 +1,5 @@ package com.github.tobato.fastdfs; -import com.github.tobato.fastdfs.socket.FdfsMockSocketServer; - import java.net.InetSocketAddress; import java.nio.charset.Charset; @@ -15,8 +13,10 @@ public class TestConstants { private static String ip_home = "172.17.0.2"; private static String ip_work = "10.0.75.2"; private static String ip_work_store = "192.168.174.43"; - public static InetSocketAddress address = new InetSocketAddress(ip_home, FdfsMockSocketServer.PORT); - public static InetSocketAddress store_address = new InetSocketAddress(ip_home, FdfsMockSocketServer.STORE_PORT); + public final static int PORT = 22122;//22122 + public final static int STORE_PORT = 23000; //23000 + public static InetSocketAddress address = new InetSocketAddress(ip_home, PORT); + public static InetSocketAddress store_address = new InetSocketAddress(ip_home, STORE_PORT); public static final int soTimeout = 550; public static final int connectTimeout = 500; public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); diff --git a/src/test/java/com/github/tobato/fastdfs/domain/conn/ConnectionPoolTest.java b/src/test/java/com/github/tobato/fastdfs/domain/conn/ConnectionPoolTest.java index 5e89498..ab96718 100644 --- a/src/test/java/com/github/tobato/fastdfs/domain/conn/ConnectionPoolTest.java +++ b/src/test/java/com/github/tobato/fastdfs/domain/conn/ConnectionPoolTest.java @@ -1,10 +1,10 @@ package com.github.tobato.fastdfs.domain.conn; -import static org.junit.Assert.*; +import org.junit.Test; import java.net.InetSocketAddress; -import org.junit.Test; +import static org.junit.Assert.assertFalse; /** * 连接池创建测试 @@ -28,6 +28,7 @@ public void testPoolUsage() { pool.returnObject(address, connA); // 清理连接 pool.clear(address); + LOGGER.debug("连接测试情况={}", pool.getTestOnBorrow()); } catch (Exception e) { e.printStackTrace(); @@ -40,6 +41,7 @@ public void testPoolStatus() { try { // 获取连接 printPoolStates("未获取前", address, pool); + //pool.setTestOnBorrow(true); Connection connA = pool.borrowObject(address); printPoolStates("获取一个连接", address, pool); // 获取第二个连接 diff --git a/src/test/java/com/github/tobato/fastdfs/domain/conn/ConnectionTest.java b/src/test/java/com/github/tobato/fastdfs/domain/conn/ConnectionTest.java index 0723940..af640ec 100644 --- a/src/test/java/com/github/tobato/fastdfs/domain/conn/ConnectionTest.java +++ b/src/test/java/com/github/tobato/fastdfs/domain/conn/ConnectionTest.java @@ -1,6 +1,7 @@ package com.github.tobato.fastdfs.domain.conn; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * socket连接测试 @@ -9,7 +10,7 @@ */ public class ConnectionTest extends MockServerTestBase { - // @Test + //@Test public void testClose() { // 创建连接 Connection conn = createConnection(); @@ -19,7 +20,7 @@ public void testClose() { assertTrue(conn.isClosed()); } - // @Test + //@Test public void testCheck() { // 创建连接测试 Connection conn = createConnection(); diff --git a/src/test/java/com/github/tobato/fastdfs/domain/conn/MockServerTestBase.java b/src/test/java/com/github/tobato/fastdfs/domain/conn/MockServerTestBase.java index f30f2b0..77d09fc 100644 --- a/src/test/java/com/github/tobato/fastdfs/domain/conn/MockServerTestBase.java +++ b/src/test/java/com/github/tobato/fastdfs/domain/conn/MockServerTestBase.java @@ -1,13 +1,12 @@ package com.github.tobato.fastdfs.domain.conn; -import java.net.InetSocketAddress; - +import com.github.tobato.fastdfs.socket.FdfsMockSocketServer; import org.junit.AfterClass; import org.junit.BeforeClass; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.github.tobato.fastdfs.socket.FdfsMockSocketServer; +import java.net.InetSocketAddress; /** * socket 测试基类 diff --git a/src/test/java/com/github/tobato/fastdfs/domain/conn/TrackerConnectionManagerTest.java b/src/test/java/com/github/tobato/fastdfs/domain/conn/TrackerConnectionManagerTest.java index 8bcaf54..e5223c5 100644 --- a/src/test/java/com/github/tobato/fastdfs/domain/conn/TrackerConnectionManagerTest.java +++ b/src/test/java/com/github/tobato/fastdfs/domain/conn/TrackerConnectionManagerTest.java @@ -1,6 +1,5 @@ package com.github.tobato.fastdfs.domain.conn; -import com.github.tobato.fastdfs.TestConstants; import com.github.tobato.fastdfs.domain.fdfs.GroupState; import com.github.tobato.fastdfs.domain.proto.tracker.TrackerListGroupsCommand; import com.github.tobato.fastdfs.exception.FdfsConnectException; @@ -60,7 +59,7 @@ public void testConnectionManager() { * @return */ private TrackerConnectionManager crtInvalidateIpListManager() { - String[] ips = {"192.168.174.141:22122", "192.168.1.115:22122"}; + String[] ips = {"192.168.1.1:22122", "192.168.1.115:212"}; List trackerIpList = Arrays.asList(ips); TrackerConnectionManager manager = new TrackerConnectionManager(createPool()); manager.setTrackerList(trackerIpList); @@ -75,9 +74,11 @@ private TrackerConnectionManager crtInvalidateIpListManager() { */ private FdfsConnectionPool createPool() { PooledConnectionFactory factory = new PooledConnectionFactory(); - factory.setConnectTimeout(TestConstants.connectTimeout); - factory.setSoTimeout(TestConstants.soTimeout); + //缩短连接超时时间 + factory.setConnectTimeout(2); + factory.setSoTimeout(2); return new FdfsConnectionPool(factory); } + } diff --git a/src/test/java/com/github/tobato/fastdfs/domain/proto/mapper/FdfsParamMapperTest.java b/src/test/java/com/github/tobato/fastdfs/domain/proto/mapper/FdfsParamMapperTest.java index 4fca9b2..c6feb12 100644 --- a/src/test/java/com/github/tobato/fastdfs/domain/proto/mapper/FdfsParamMapperTest.java +++ b/src/test/java/com/github/tobato/fastdfs/domain/proto/mapper/FdfsParamMapperTest.java @@ -1,22 +1,20 @@ package com.github.tobato.fastdfs.domain.proto.mapper; -import static org.junit.Assert.*; - -import java.nio.charset.Charset; -import java.util.Arrays; - +import com.github.tobato.fastdfs.TestConstants; +import com.github.tobato.fastdfs.domain.proto.OtherConstants; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.github.tobato.fastdfs.TestConstants; -import com.github.tobato.fastdfs.domain.proto.OtherConstants; +import java.nio.charset.Charset; +import java.util.Arrays; + +import static org.junit.Assert.*; /** * 测试对象序列化 - * - * @author tobato * + * @author tobato */ public class FdfsParamMapperTest { @@ -26,7 +24,9 @@ public class FdfsParamMapperTest { private static final String GROUP_NAME = TestConstants.DEFAULT_GROUP; private static final String IP = TestConstants.DEFAULT_STORAGE_IP; - /** 日志 */ + /** + * 日志 + */ protected static Logger LOGGER = LoggerFactory.getLogger(FdfsParamMapperTest.class); /** @@ -124,7 +124,7 @@ public byte[] prepareByte() { /** * 打印消息 - * + * * @param src * @param request */ @@ -140,4 +140,10 @@ private void printRequest(Object src, byte[] request) { LOGGER.debug("-------------------"); } + +// @Test +// public void testInstance() throws IllegalAccessException, InstantiationException { +// Object obj = java.lang.Void.class.newInstance(); +// } + } diff --git a/src/test/java/com/github/tobato/fastdfs/domain/proto/storage/DownloadFileStreamTest.java b/src/test/java/com/github/tobato/fastdfs/domain/proto/storage/DownloadFileStreamTest.java new file mode 100644 index 0000000..3e522fd --- /dev/null +++ b/src/test/java/com/github/tobato/fastdfs/domain/proto/storage/DownloadFileStreamTest.java @@ -0,0 +1,43 @@ +package com.github.tobato.fastdfs.domain.proto.storage; + +import com.github.tobato.fastdfs.TestConstants; +import org.apache.commons.io.IOUtils; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import static org.junit.Assert.assertEquals; + +/** + * 文件缓存方式下载机制测试 + * + * @author tobato + */ +public class DownloadFileStreamTest { + + @Test + public void recv() throws IOException { + + String firstText = "加入文件缓存方式下载机制\r\n"; + InputStream input = getTextInputStream(firstText); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + //模拟输出,输出 + DownloadFileStream downloadFileStream = new DownloadFileStream(output); + downloadFileStream.recv(input); + String result = output.toString("UTF-8"); + //比对结果 + assertEquals(firstText, result); + //关闭输入,输出 + IOUtils.closeQuietly(input); + IOUtils.closeQuietly(output); + + } + + private InputStream getTextInputStream(String text) throws IOException { + // 将String转换为InputStream + return new ByteArrayInputStream(text.getBytes(TestConstants.DEFAULT_CHARSET)); + } +} \ No newline at end of file diff --git a/src/test/java/com/github/tobato/fastdfs/socket/FdfsMockHandler.java b/src/test/java/com/github/tobato/fastdfs/socket/FdfsMockHandler.java index 95d5fd6..834497a 100644 --- a/src/test/java/com/github/tobato/fastdfs/socket/FdfsMockHandler.java +++ b/src/test/java/com/github/tobato/fastdfs/socket/FdfsMockHandler.java @@ -1,21 +1,22 @@ package com.github.tobato.fastdfs.socket; -import java.io.DataInputStream; -import java.net.Socket; - import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.DataInputStream; +import java.net.Socket; + /** * 模拟socket处理类 - * - * @author tobato * + * @author tobato */ public class FdfsMockHandler extends Thread { - /** 日志 */ + /** + * 日志 + */ private static final Logger LOGGER = LoggerFactory.getLogger(FdfsMockHandler.class); private Socket client; diff --git a/src/test/java/com/github/tobato/fastdfs/socket/FdfsMockSocketServer.java b/src/test/java/com/github/tobato/fastdfs/socket/FdfsMockSocketServer.java index 92effbf..98a718f 100644 --- a/src/test/java/com/github/tobato/fastdfs/socket/FdfsMockSocketServer.java +++ b/src/test/java/com/github/tobato/fastdfs/socket/FdfsMockSocketServer.java @@ -1,27 +1,27 @@ package com.github.tobato.fastdfs.socket; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.HashMap; import java.util.Map; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * 模拟测试服务 - * - * @author tobato * + * @author tobato */ public class FdfsMockSocketServer extends Thread { - /** 日志 */ + /** + * 日志 + */ private static final Logger LOGGER = LoggerFactory.getLogger(FdfsMockSocketServer.class); - public final static int PORT = 22122; - public final static int STORE_PORT = 23000; + public final static int PORT = 20001;//22122 private ServerSocket serverSocket; private boolean stop = false; private int index = 0; diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml index e8b595a..01278f5 100644 --- a/src/test/resources/logback-test.xml +++ b/src/test/resources/logback-test.xml @@ -7,7 +7,7 @@ - +