feat:测试代码
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package com.xiang.app.debugger;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-25 10:23
|
||||
*/
|
||||
@Slf4j
|
||||
public class OrderTest {
|
||||
private static final UserTest userA = new UserTest(1L, "123");
|
||||
private static final UserTest userB = new UserTest(2L, "234");
|
||||
private static final UserTest userC = new UserTest(3L, "345");
|
||||
private static final List<UserTest> users = Lists.newArrayList(userA, userB, userC);
|
||||
|
||||
private static final VenueInfoTest v1 = new VenueInfoTest(1L, "一号场地");
|
||||
private static final VenueInfoTest v2 = new VenueInfoTest(2L, "二号场地");
|
||||
private static final VenueInfoTest v3 = new VenueInfoTest(3L, "三号场地");
|
||||
private static final Map<String, VenueInfoTest> venueInfoMap = Maps.newHashMap();
|
||||
static {
|
||||
venueInfoMap.put(v1.getPlaceName(), v1);
|
||||
venueInfoMap.put(v2.getPlaceName(), v2);
|
||||
venueInfoMap.put(v3.getPlaceName(), v3);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
OrderTest orderTest = new OrderTest();
|
||||
orderTest.test();
|
||||
}
|
||||
|
||||
public void test() {
|
||||
users.parallelStream().forEach(user -> {
|
||||
try {
|
||||
for (String placeName : venueInfoMap.keySet()) {
|
||||
VenueInfoTest venueInfoTest = venueInfoMap.get(placeName);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
boolean order = createOrder(venueInfoTest, user);
|
||||
if (order) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
log.info("睡眠中 1.25s");
|
||||
Thread.sleep(1250);
|
||||
} catch (InterruptedException e) {
|
||||
log.error("睡眠失败~~~");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 关键点:异常只影响当前 user
|
||||
log.error("createOrder 异常,user={}", user.getId(), e);
|
||||
return; // 结束这个 user,不影响其他 user
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private boolean createOrder(VenueInfoTest venueInfoTest, UserTest userTest) {
|
||||
log.info("用户:{}======>请求三方接口中", userTest.getUsername());
|
||||
String order = order(venueInfoTest, userTest);
|
||||
log.info("用户:{}======>请求结果::{}", userTest.getUsername(), order);
|
||||
if (order.contains("成功")) {
|
||||
return true;
|
||||
}
|
||||
if (order.contains("锁卡")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String order(VenueInfoTest venueInfoTest, UserTest userTest) {
|
||||
String msg = "~~~";
|
||||
if (venueInfoTest.getId() == 1L && userTest.getId() == 2L) {
|
||||
msg = "用户" + userTest.getUsername() + "预定成功!";
|
||||
return msg;
|
||||
}
|
||||
if (userTest.getId() == 1L) {
|
||||
msg = "用户" + userTest.getUsername() + "频繁请求接口,将锁卡";
|
||||
return msg;
|
||||
}
|
||||
if (venueInfoTest.getId() == 2L) {
|
||||
msg = "场地" + venueInfoTest.getPlaceName() + "已被预订";
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.xiang.app.debugger;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-25 10:24
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class UserTest {
|
||||
|
||||
private Long id;
|
||||
private String username;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.xiang.app.debugger;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2026-02-25 10:28
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class VenueInfoTest {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String placeName;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user