dca9016965dd21b971a61f49b47b5f0ef3d73b7b.svn-base 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package org.jeecg.boot.starter.lock.test;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.test.context.junit4.SpringRunner;
  7. import java.util.concurrent.ExecutorService;
  8. import java.util.concurrent.Executors;
  9. import java.util.concurrent.TimeUnit;
  10. import java.util.stream.IntStream;
  11. @RunWith(SpringRunner.class)
  12. @SpringBootTest(classes = LockTestApplication.class)
  13. public class LockTest {
  14. @Autowired
  15. LockService lockService;
  16. /**
  17. * 测试分布式锁(模拟秒杀)
  18. */
  19. @Test
  20. public void test1() throws Exception {
  21. ExecutorService executorService = Executors.newFixedThreadPool(6);
  22. IntStream.range(0, 30).forEach(i -> executorService.submit(() -> {
  23. try {
  24. lockService.seckill("20120508784");
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. }));
  29. executorService.awaitTermination(30, TimeUnit.SECONDS);
  30. }
  31. /**
  32. * 测试分布式锁(模拟秒杀)
  33. */
  34. @Test
  35. public void test2() throws Exception {
  36. ExecutorService executorService = Executors.newFixedThreadPool(6);
  37. IntStream.range(0, 30).forEach(i -> executorService.submit(() -> {
  38. try {
  39. lockService.seckill2("20120508784");
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. }));
  44. executorService.awaitTermination(30, TimeUnit.SECONDS);
  45. }
  46. /**
  47. * 测试分布式锁(模拟重复提交)
  48. */
  49. @Test
  50. public void test3() throws Exception {
  51. ExecutorService executorService = Executors.newFixedThreadPool(6);
  52. IntStream.range(0, 20).forEach(i -> executorService.submit(() -> {
  53. try {
  54. lockService.reSubmit("test");
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. }));
  59. executorService.awaitTermination(30, TimeUnit.SECONDS);
  60. }
  61. }