Search in sources :

Example 1 with ConcurrentLinkedHashMapWrapper

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");
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentLinkedHashMapWrapper(com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper) Test(org.junit.Test)

Aggregations

ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)1 StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)1 ConcurrentLinkedHashMapWrapper (com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Test (org.junit.Test)1