|
1 | 1 | package io.arex.inst.runtime.match;
|
2 | 2 |
|
| 3 | +import io.arex.agent.bootstrap.model.MockCategoryType; |
3 | 4 | import io.arex.agent.bootstrap.model.MockStrategyEnum;
|
4 | 5 | import io.arex.agent.bootstrap.model.Mocker;
|
5 | 6 | import io.arex.agent.bootstrap.util.CollectionUtil;
|
@@ -47,23 +48,46 @@ public static Mocker match(Mocker requestMocker, MockStrategyEnum mockStrategy)
|
47 | 48 | private static void doMatch(MatchStrategyContext context) {
|
48 | 49 | Mocker requestMocker = context.getRequestMocker();
|
49 | 50 | Map<Integer, List<Mocker>> cachedReplayResultMap = ContextManager.currentContext().getCachedReplayResultMap();
|
50 |
| - // first fuzzy match, such as: category + operationName + requestType, ensure the same method |
51 |
| - requestMocker.setFuzzyMatchKey(MatchKeyFactory.INSTANCE.getFuzzyMatchKey(requestMocker)); |
52 |
| - List<Mocker> recordList = cachedReplayResultMap.get(requestMocker.getFuzzyMatchKey()); |
| 51 | + // pre match for all mocker category type |
| 52 | + List<Mocker> recordList = preMatch(context, requestMocker, cachedReplayResultMap); |
53 | 53 | if (CollectionUtil.isEmpty(recordList)) {
|
54 |
| - context.setReason("match no result, not exist this method signature, check if it has been recorded"); |
55 | 54 | return;
|
56 | 55 | }
|
57 | 56 | context.setRecordList(recordList);
|
58 | 57 | int fuzzyMatchResultCount = recordList.size();
|
59 | 58 | List<AbstractMatchStrategy> matchStrategyList = MatchStrategyRegister.getMatchStrategies(requestMocker, fuzzyMatchResultCount);
|
| 59 | + // multi thread match safe |
60 | 60 | synchronized (cachedReplayResultMap) {
|
61 | 61 | for (AbstractMatchStrategy matchStrategy : matchStrategyList) {
|
62 | 62 | matchStrategy.match(context);
|
63 | 63 | }
|
64 | 64 | }
|
65 | 65 | }
|
66 | 66 |
|
| 67 | + private static List<Mocker> preMatch(MatchStrategyContext context, Mocker requestMocker, Map<Integer, List<Mocker>> cachedReplayResultMap) { |
| 68 | + // first match, such as: category + operationName + requestType, ensure the same method |
| 69 | + requestMocker.setFuzzyMatchKey(MatchKeyFactory.INSTANCE.getFuzzyMatchKey(requestMocker)); |
| 70 | + List<Mocker> recordList = cachedReplayResultMap.get(requestMocker.getFuzzyMatchKey()); |
| 71 | + recordList = compatibleNoRequestType(recordList, cachedReplayResultMap, requestMocker); |
| 72 | + if (CollectionUtil.isEmpty(recordList)) { |
| 73 | + context.setReason("match no result, not exist this method signature, check if it has been recorded or request type is empty"); |
| 74 | + } |
| 75 | + return recordList; |
| 76 | + } |
| 77 | + |
| 78 | + private static List<Mocker> compatibleNoRequestType(List<Mocker> recordList, Map<Integer, List<Mocker>> cachedReplayResultMap, Mocker requestMocker) { |
| 79 | + if (CollectionUtil.isEmpty(recordList)) { |
| 80 | + String categoryType = requestMocker.getCategoryType().getName(); |
| 81 | + if (MockCategoryType.DYNAMIC_CLASS.getName().equals(categoryType) || MockCategoryType.REDIS.getName().equals(categoryType)) { |
| 82 | + // dynamic class or redis may not record requestType on old version |
| 83 | + requestMocker.getTargetRequest().setType(null); |
| 84 | + requestMocker.setFuzzyMatchKey(MatchKeyFactory.INSTANCE.getFuzzyMatchKey(requestMocker)); |
| 85 | + return cachedReplayResultMap.get(requestMocker.getFuzzyMatchKey()); |
| 86 | + } |
| 87 | + } |
| 88 | + return recordList; |
| 89 | + } |
| 90 | + |
67 | 91 | private static void logMatchResult(MatchStrategyContext context) {
|
68 | 92 | Mocker matchedMocker = context.getMatchMocker();
|
69 | 93 | Mocker requestMocker = context.getRequestMocker();
|
|
0 commit comments