001-【テスト】同時テストクラス作成
2798 ワード
1、背景を書く
プロジェクトはきっとCRUDだけではなくて、更に多くの仕事はテストに置くべきで、性能、安定の上で
2、参考サイト
3、ツール類の作成を行う
プロジェクト機能の作成が完了したら、テスト用例を書き、性能テストを行う(必要)
@RunWith(SpringRunner.class)
@SpringBootTest
public class SeckillApplicationTests {
public static final String SKU = "iPhone7";
public static int UserCount = 5;
public static int UserBuyCount = 3;
public static AtomicInteger userSuccessCount = new AtomicInteger();
public static AtomicInteger stackSuccessCount = new AtomicInteger();
@Resource
KillService killService;
@Test
public void contextLoads() {
boolean flag = killService.updateGoodsCount(1, SKU);
System.out.println("------------------ :"+flag);
}
@Test
// thread.join()
public void contextLoadsThread() throws InterruptedException {
for (int i = 0; i < UserCount; i++) {
Thread task = new Thread() {
@Override
public void run() {
doSomeThing();
}
};
task.start();
task.join();
}
System.out.println("------------------ :"+userSuccessCount);
System.out.println("------------------ :"+stackSuccessCount);
}
//
public void doSomeThing() {
if (killService.updateGoodsCount(UserBuyCount, SKU)){
System.out.println("------------------ ");
userSuccessCount.incrementAndGet();//
stackSuccessCount.addAndGet(UserBuyCount);//
}else {
System.out.println("------------------ ");
}
}
}
public void contextLoadsThread() throws Exception {
ExecutorService executorService = Executors.newCachedThreadPool();
// final Semaphore semaphore = new Semaphore(UserCount);
final CountDownLatch countDownLatch = new CountDownLatch(UserCount);
for (int i = 0; i < UserCount ; i++) {
executorService.execute(() -> {
try {
// semaphore.acquire();
doSomeThing();
// semaphore.release();
} catch (Exception e) {
System.out.println("exception:"+e);
}
countDownLatch.countDown();
});
}
// countDownLatch.await();
executorService.shutdown();
// , ( )
Thread.sleep(1000);
System.out.println("--------------> ");
}