Java 使用junit进行多实例测试
我在服务器套接字上有一个java服务器。客户端连接到此服务器套接字,并通过输入/输出对象流进行交换。现在我需要测试应用程序的可伸缩性。这意味着我需要运行相同的测试发出请求,并测试服务器是否能够处理来自随机客户端的请求 下面基于junit的测试用例是否是测试随机连接/请求的正确方法。我感觉下面的代码正在依次测试客户机 我读了一些链接,但对我不起作用Java 使用junit进行多实例测试,java,unit-testing,junit,Java,Unit Testing,Junit,我在服务器套接字上有一个java服务器。客户端连接到此服务器套接字,并通过输入/输出对象流进行交换。现在我需要测试应用程序的可伸缩性。这意味着我需要运行相同的测试发出请求,并测试服务器是否能够处理来自随机客户端的请求 下面基于junit的测试用例是否是测试随机连接/请求的正确方法。我感觉下面的代码正在依次测试客户机 我读了一些链接,但对我不起作用 公共类ScalePostiveTestCases{ 发送队列发送队列; 套接字clientSocket=null; 公共静态void main(字符
公共类ScalePostiveTestCases{
发送队列发送队列;
套接字clientSocket=null;
公共静态void main(字符串[]args)抛出可丢弃的{
testsearch();
}
@抑制警告(“未选中”)
私有静态void testsearch()抛出ClassNotFoundException、InstanceionException、IllegalAccessException{
TestSuite tests=新的TestSuite();
对于(int i=0;i<99;i++){
类singleSearchTest=(类)类加载器
.getSystemClassLoader().loadClass(
“单一搜索测试”);
SingleSearchTest singleSearch=SingleSearchTest.newInstance();
tests.addTest(singleSearch);
}
TestRunner.run(测试);
}
公共类SingleSearchTest扩展了TestCase{
静态SingleSearchTest singleSearch=null;
专用字符串设备、连接、用户;
私人客户端会话;
私有发送队列发送队列;
公共静态SingleSearchTest main(字符串参数[]){
singleSearch=新的SingleSearchTest();
返回单个搜索;
}
公共单搜索测试(){
超级(“testSingleSearch”);
Random rand=新的Random();
}
}
您正在创建多个测试,每个测试都有一个客户端
您需要做的是有一个简单的测试,它创建了许多并发执行的客户机
在我的单元测试中
- 作为单独的线程启动服务器(这样我也可以关闭它)
- 将ExecutorService用作客户端的线程池
- 创建一个循环来创建所有任务,每个任务创建一个客户机并进行练习。完成后,从任务返回
- 关闭Executor服务
- 检查所有任务并检查它们是否通过。如果它们抛出了和错误,则错误将在当前(测试)线程中抛出
public static void testLoading() {
threadExecutor = Executors.newCachedThreadPool();
for (int i = 0; i < 50; i++) {
SingleClientTest clientTest = new SingleClientTest();
randomStringList.add(clientTest.getRandomString());
threadExecutor.execute(clientTest);
}
threadExecutor.shutdownNow();
try {
threadExecutor.awaitTermination(2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
// while (!threadExecutor.isShutdown()) {
// }
}
public static void testSearching() {
threadExecutor = Executors.newCachedThreadPool();
for (int i = 0; i < 50; i++) {
SingleSearchTest searchTest = new SingleSearchTest(randomStringList);
threadExecutor.execute(searchTest);
}
threadExecutor.shutdownNow();
try {
threadExecutor.awaitTermination(2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
// while (!threadExecutor.isShutdown()) {
// }
}
publicstaticvoidtestload(){
threadExecutor=Executors.newCachedThreadPool();
对于(int i=0;i<50;i++){
SingleClientTest clientTest=新的SingleClientTest();
添加(clientTest.getRandomString());
threadExecutor.execute(clientTest);
}
threadExecutor.shutdownNow();
试一试{
线程执行器。等待终止(2,时间单位。秒);
}捕捉(中断异常e){
e、 printStackTrace();
}
//而(!threadExecutor.IsShutton()){
// }
}
公共静态void testsearch(){
threadExecutor=Executors.newCachedThreadPool();
对于(int i=0;i<50;i++){
SingleSearchTest searchTest=新的SingleSearchTest(随机字符串列表);
threadExecutor.execute(searchTest);
}
threadExecutor.shutdownNow();
试一试{
线程执行器。等待终止(2,时间单位。秒);
}捕捉(中断异常e){
e、 printStackTrace();
}
//而(!threadExecutor.IsShutton()){
// }
}
也许这可以帮助您试用JUnitRef,它不够好,因为它不会自动为您创建这些测试。如果我在线程中创建测试,它将按顺序运行,而不是并行运行。太多的工作,ExecutorService是更好的方法。哦,是的,这是完全理解的。问题是如何创建?谢谢Peter。这s help.public static void main(String[]args)抛出可丢弃的{threadExecutor=Executors.newCachedThreadPool();for(int i=0;i<50;i++){SingleClientTest clientTest=new SingleClientTest();threadExecutor.execute(clientTest);}threadExecutor.shutdown();而(!threadExecutor.isTerminated()){}}您可以使用threadExecutor.awaitTermination(10,TimeUnit.MINUTES);
避免了繁忙等待(并烧掉CPU);)
public static void testLoading() {
threadExecutor = Executors.newCachedThreadPool();
for (int i = 0; i < 50; i++) {
SingleClientTest clientTest = new SingleClientTest();
randomStringList.add(clientTest.getRandomString());
threadExecutor.execute(clientTest);
}
threadExecutor.shutdownNow();
try {
threadExecutor.awaitTermination(2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
// while (!threadExecutor.isShutdown()) {
// }
}
public static void testSearching() {
threadExecutor = Executors.newCachedThreadPool();
for (int i = 0; i < 50; i++) {
SingleSearchTest searchTest = new SingleSearchTest(randomStringList);
threadExecutor.execute(searchTest);
}
threadExecutor.shutdownNow();
try {
threadExecutor.awaitTermination(2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
// while (!threadExecutor.isShutdown()) {
// }
}