zhouhao il y a 7 ans
Parent
commit
96bc62dd5a

+ 10 - 11
hsweb-concurrent/hsweb-concurrent-lock/hsweb-concurrent-lock-starter/src/test/java/org/hswebframework/web/concurrent/lock/starter/LockAnnotationTest.java

@@ -4,20 +4,25 @@ import org.hswebframework.web.concurrent.lock.LockManager;
 import org.hswebframework.web.tests.SimpleWebApplicationTests;
 import org.junit.Assert;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.redisson.Redisson;
 import org.redisson.api.RedissonClient;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
+import org.springframework.test.context.junit4.SpringRunner;
 
 /**
  * TODO 完成注释
  *
  * @author zhouhao
  */
-@Configuration
-public class LockAnnotationTest extends SimpleWebApplicationTests {
-
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = TestApplication.class)
+public class LockAnnotationTest extends AbstractTransactionalJUnit4SpringContextTests {
     @Autowired
     private LockService lockService;
 
@@ -27,15 +32,9 @@ public class LockAnnotationTest extends SimpleWebApplicationTests {
     @Test
     public void testLock() throws InterruptedException {
         new Thread(() -> {
-            try {
-                System.out.println("锁住");
-                lockManager.getLock("lock_test").lock();
-                Thread.sleep(2000);
-            } catch (InterruptedException e) {
-                e.printStackTrace();
-            }
+            System.out.println("锁住");
+            lockService.testLockSleep("test", 2000);
             System.out.println("解锁");
-            lockManager.getLock("lock_test").unlock();
         }).start();
         Thread.sleep(200);
         System.out.println("开始任务1");

+ 21 - 3
hsweb-concurrent/hsweb-concurrent-lock/hsweb-concurrent-lock-starter/src/test/java/org/hswebframework/web/concurrent/lock/starter/LockService.java

@@ -1,20 +1,38 @@
 package org.hswebframework.web.concurrent.lock.starter;
 
+import lombok.extern.slf4j.Slf4j;
 import org.hswebframework.web.concurrent.lock.annotation.Lock;
 import org.hswebframework.web.concurrent.lock.annotation.ReadLock;
 import org.hswebframework.web.concurrent.lock.annotation.WriteLock;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
 /**
- * TODO 完成注释
- *
  * @author zhouhao
  */
-@Service
+@Slf4j
 public class LockService {
 
     private long counter = 0;
 
+//    @Scheduled(cron = "0/1 * * * * ?")
+//    @Lock("test2")
+//    public void test() throws InterruptedException {
+//        log.info("try lock");
+//        Thread.sleep(5000);
+//        log.info("un lock");
+//    }
+
+    @Lock("lock_${#key}")
+    public long testLockSleep(String key, long time) {
+        try {
+            Thread.sleep(time);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        return counter;
+    }
+
     @Lock("lock_${#key}")
     public long testLock(String key) {
         return counter++;

+ 23 - 0
hsweb-concurrent/hsweb-concurrent-lock/hsweb-concurrent-lock-starter/src/test/java/org/hswebframework/web/concurrent/lock/starter/TestApplication.java

@@ -1,12 +1,35 @@
 package org.hswebframework.web.concurrent.lock.starter;
 
+import org.hswebframework.web.concurrent.lock.LockManager;
+import org.hswebframework.web.concurrent.lock.redis.RedissonLockManager;
+import org.redisson.Redisson;
+import org.redisson.api.RedissonClient;
+import org.redisson.config.Config;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableScheduling;
 
 @Configuration
+@SpringBootApplication
+@EnableScheduling
 public class TestApplication {
 //    @Bean
 //    public RedissonClient redissonClient() {
+//        Config config = new Config();
+//        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
+//        config.setLockWatchdogTimeout(60_1000);
+//
 //        return Redisson.create();
 //    }
 
+//    @Bean
+//    public LockManager lockManager(RedissonClient redissonClient) {
+//        return new RedissonLockManager(redissonClient);
+//    }
+
+    @Bean
+    public LockService lockService() {
+        return new LockService();
+    }
 }