|
| 1 | +package com.codingapi.txlcn.tm; |
| 2 | + |
| 3 | +import com.codingapi.txlcn.common.exception.FastStorageException; |
| 4 | +import com.codingapi.txlcn.common.lock.DTXLocks; |
| 5 | +import com.codingapi.txlcn.tm.core.storage.LockValue; |
| 6 | +import com.codingapi.txlcn.tm.core.storage.redis.RedisStorage; |
| 7 | +import com.google.common.collect.Sets; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.context.SpringBootTest; |
| 12 | +import org.springframework.test.context.junit4.SpringRunner; |
| 13 | + |
| 14 | +import java.util.Set; |
| 15 | +import java.util.concurrent.ExecutorService; |
| 16 | +import java.util.concurrent.Executors; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | + |
| 19 | +@RunWith(SpringRunner.class) |
| 20 | +@SpringBootTest(classes = TMApplication.class) |
| 21 | +public class RedisStorageTest { |
| 22 | + |
| 23 | + |
| 24 | + @Autowired |
| 25 | + private RedisStorage redisStorage; |
| 26 | + |
| 27 | + String contextId = "A"; |
| 28 | + Set<String> locks = Sets.newHashSet("1","2"); |
| 29 | + LockValue lockValue = new LockValue(); |
| 30 | + |
| 31 | + @Test |
| 32 | + public void acquireLocks(){ |
| 33 | + ExecutorService service = Executors.newFixedThreadPool(100); |
| 34 | + |
| 35 | + for(int i=0;i<1000;i++){ |
| 36 | + service.execute(new Runnable() { |
| 37 | + @Override |
| 38 | + public void run() { |
| 39 | + lockValue.setLockType(DTXLocks.X_LOCK); |
| 40 | + try { |
| 41 | + redisStorage.acquireLocks(contextId,locks,lockValue); |
| 42 | + System.out.println("success"); |
| 43 | + } catch (FastStorageException e) { |
| 44 | + System.out.println("fail"); |
| 45 | + } |
| 46 | + } |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + try { |
| 51 | + service.shutdown(); |
| 52 | + service.awaitTermination(1, TimeUnit.DAYS); |
| 53 | + } catch (InterruptedException e) { |
| 54 | + e.printStackTrace(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | +} |
0 commit comments