Authored by LUOXC

test

... ... @@ -16,6 +16,7 @@ import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
... ... @@ -57,12 +58,12 @@ public class OrderHelpController {
@IgnoreSignature
@InnerApi
@RequestMapping(value = "/lock")
public ApiResponse lock(String key) {
public ApiResponse lock(String key, @RequestParam(required = false, defaultValue = "100") int taskNum) {
ExecutorService executorService = new ThreadPoolExecutor(5, 10,
60, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1000), NamedThreadFactory.newThreadFactory("test"));
IntStream.range(0, 100)
IntStream.range(0, taskNum)
.forEach(i -> executorService.submit(() -> lockTest(key)));
ExecutorServiceUtils.shutdownAndAwaitTermination(executorService);
return new ApiResponse.ApiResponseBuilder()
... ... @@ -75,18 +76,25 @@ public class OrderHelpController {
RedisKeyBuilder redisLockKey = RedisKeyBuilder.newInstance()
.appendFixed("ufo:order:lock:test:")
.appendVar(key);
RedisLock lock = redisLockFactory.newLock(redisLockKey,5, TimeUnit.SECONDS);
RedisLock lock = redisLockFactory.newLock(redisLockKey, 5, TimeUnit.SECONDS);
if (lock.tryLock()) {
try {
log.info("lock test {}, i got the lock", key);
Thread.sleep(RandomUtils.nextInt(10, 50));
} catch (InterruptedException e) {
sleep(RandomUtils.nextInt(20, 50));
} finally {
lock.unlock();
}
} else {
log.info("lock test {}, i not got the lock", key);
sleep(RandomUtils.nextInt(20, 30));
}
}
private void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
}
}
... ...