use of com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper in project Sentinel by alibaba.
the class ParamFlowSlotTest method testEntryWhenParamFlowExists.
@Test
public void testEntryWhenParamFlowExists() throws Throwable {
String resourceName = "testEntryWhenParamFlowExists";
ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
long argToGo = 1L;
double count = 1;
ParamFlowRule rule = new ParamFlowRule(resourceName).setCount(count).setBurstCount(0).setParamIdx(0);
ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
ParameterMetric metric = mock(ParameterMetric.class);
CacheMap<Object, AtomicLong> map = new ConcurrentLinkedHashMapWrapper<>(4000);
CacheMap<Object, AtomicLong> map2 = new ConcurrentLinkedHashMapWrapper<>(4000);
when(metric.getRuleTimeCounter(rule)).thenReturn(map);
when(metric.getRuleTokenCounter(rule)).thenReturn(map2);
map.put(argToGo, new AtomicLong(TimeUtil.currentTimeMillis()));
// Insert the mock metric to control pass or block.
ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric);
// The first entry will pass.
paramFlowSlot.entry(null, resourceWrapper, null, 1, false, argToGo);
// The second entry will be blocked.
try {
paramFlowSlot.entry(null, resourceWrapper, null, 1, false, argToGo);
} catch (ParamFlowException ex) {
assertEquals(String.valueOf(argToGo), ex.getMessage());
assertEquals(resourceName, ex.getResourceName());
return;
}
fail("The second entry should be blocked");
}
Aggregations