使用GroboUtils多线程并发请求测试springmvc controller
来源:互联网 发布:好看的c语言编译器 编辑:程序博客网 时间:2024/05/21 19:26
1. 需求
有一个下载pdf的springmvc controller,希望模拟一下并发访问的情况,怎么办呢?
2. 解决方案
由于是pdf,其http响应为
那么我们访问时返回的是二进制,而不是网页那种text/html
于是写一个保存二进制文件的方法
public void saveBinaryFile(URL url) throws IOException { URLConnection uc = url.openConnection(); String contentType = uc.getContentType(); int contentLength = uc.getContentLength(); if (contentType.startsWith("text/") || contentLength == -1 ) { throw new IOException("This is not a binary file."); } InputStream raw = uc.getInputStream(); InputStream in = new BufferedInputStream(raw); byte[] data = new byte[contentLength]; int offset = 0; while (offset < contentLength) { int bytesRead = in.read(data, offset, data.length - offset); if (bytesRead == -1) break; offset += bytesRead; } if (offset != contentLength) { throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes"); } String filename = UUID.randomUUID()+".pdf"; try (FileOutputStream fout = new FileOutputStream(filename)) { fout.write(data); fout.flush(); } }
测试方法
Junit本身是不支持普通的多线程测试的,这是因为Junit的底层实现上,是用System.exit退出用例执行的。JVM都终止了,在测试线程启动的其他线程自然也无法执行。
使用Junit多线程测试的开源的第三方的工具包GroboUtils
pom.xml
<!--springmvc中进行多线程测试--><dependency><groupId>net.sourceforge.groboutils</groupId><artifactId>groboutils-core</artifactId><version>5</version><scope>system</scope><systemPath>F:/jar_package/groboutils-core-5.jar</systemPath></dependency>测试方法
@Test public void testSaveBinaryFile3() throws IOException { TestRunnable runner = new TestRunnable() { @Override public void runTest() throws Throwable { //测试内容 String urlStr = "http://localhost:8019/xxx”; URL url = null; try { url = new URL(urlStr); new RenderPdfControllerTest().saveBinaryFile(url); } catch (IOException e) { e.printStackTrace(); } } }; //Rnner数组,相当于并发多少个 int runnerCount = 5; TestRunnable[] trs = new TestRunnable[runnerCount]; for (int i = 0; i < runnerCount; i++) { trs[i] = runner; } // 用于执行多线程测试用例的Runner,将前面定义的单个Runner组成的数组传入 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs); try { // 开发并发执行数组里定义的内容 mttr.runTestRunnables(); } catch (Throwable e) { e.printStackTrace(); } }
3. 原理
GroboUtils实现原理是让测试主线程等待所有测试子线程执行完成后再退出。想到的办法是Thread中的join方法。看一下其关键的源码实现
参考:Junit spring 多线程测试
阅读全文
1 0
- 使用GroboUtils多线程并发请求测试springmvc controller
- SpringMVC使用MockMvc和GroboUtils进行多线程测试
- 使用GroboUtils进行多线程测试
- 多线程测试工具groboutils的使用
- 多线程测试工具groboutils的使用
- Junit使用GroboUtils进行多线程测试
- Junit使用GroboUtils进行多线程测试
- Junit使用GroboUtils进行多线程测试
- 在本地使用GroboUtils进行多线程测试
- GroboUtils进行多线程测试
- Junit4单元测试 (二) GroboUtils进行多线程测试
- springMVC一个Controller处理所有用户请求的并发问题
- springmvc的Controller并发
- SSH框架学习(十、Junit+GroboUtils进行多线程测试)
- SSH框架学习(十、Junit+GroboUtils进行多线程测试)
- SSH框架学习(八、Junit+GroboUtils进行多线程测试)
- 使Junit可以测试多线程的插件:groboutils
- springMVC,测试controller.
- Spring中使用Jdbcdaosupport、C3P0和properties
- mybatis-generator逆向工程生成model dao mapping
- 函数的四种调用方式
- JavaScript 验证
- 转载]金仕达、恒生、上期CTP,期货公司的哪一套后台系统适合于程序化下单
- 使用GroboUtils多线程并发请求测试springmvc controller
- javaWeb中使用URL的情况总结(相对路径)
- 快速认识和使用浏览器跟踪Http服务及Http服务调试工具
- C++中的命名空间(using name std)
- NIO系列(六)——线程通信(pipe)
- TI的CC2530单片机检测不同类型的方波
- STM32学习笔记之USB库文件分析
- 219. Contains Duplicate II
- HttpClient4.5.2模拟表单post提交上传文件