use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.
the class CtSphTest method testLookUpSlotChain.
@Test
public void testLookUpSlotChain() {
ResourceWrapper r1 = new StringResourceWrapper("firstRes", EntryType.IN);
assertFalse(CtSph.getChainMap().containsKey(r1));
ProcessorSlot<Object> chainR1 = ctSph.lookProcessChain(r1);
assertNotNull("The slot chain for r1 should be created", chainR1);
assertSame("Should return the cached slot chain once it has been created", chainR1, ctSph.lookProcessChain(r1));
fillFullResources();
ResourceWrapper r2 = new StringResourceWrapper("secondRes", EntryType.IN);
assertFalse(CtSph.getChainMap().containsKey(r2));
assertNull("The slot chain for r2 should not be created because amount exceeded", ctSph.lookProcessChain(r2));
assertNull(ctSph.lookProcessChain(r2));
}
use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.
the class CtSphTest method testEntryAndAsyncEntryWhenSwitchOff.
@Test
public void testEntryAndAsyncEntryWhenSwitchOff() {
// Turn off the switch.
Constants.ON = false;
String resourceNameA = "resSync";
String resourceNameB = "resAsync";
ResourceWrapper resourceWrapperA = new StringResourceWrapper(resourceNameA, EntryType.IN);
ResourceWrapper resourceWrapperB = new StringResourceWrapper(resourceNameB, EntryType.IN);
// Prepare a slot that "should not pass". If entered the slot, exception will be thrown.
addShouldNotPassSlotFor(resourceWrapperA);
addShouldNotPassSlotFor(resourceWrapperB);
Entry entry = null;
AsyncEntry asyncEntry = null;
try {
entry = ctSph.entry(resourceWrapperA, 1);
asyncEntry = ctSph.asyncEntry(resourceNameB, resourceWrapperB.getEntryType(), 1);
} catch (BlockException ex) {
fail("Unexpected blocked: " + ex.getClass().getCanonicalName());
} finally {
if (asyncEntry != null) {
asyncEntry.exit();
}
if (entry != null) {
entry.exit();
}
Constants.ON = true;
}
}
use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.
the class FlowSlotTest method testCheckFlowPass.
@Test
@SuppressWarnings("unchecked")
public void testCheckFlowPass() throws Exception {
FlowRuleChecker checker = mock(FlowRuleChecker.class);
FlowSlot flowSlot = new FlowSlot(checker);
Context context = mock(Context.class);
DefaultNode node = mock(DefaultNode.class);
doCallRealMethod().when(checker).checkFlow(any(Function.class), any(ResourceWrapper.class), any(Context.class), any(DefaultNode.class), anyInt(), anyBoolean());
String resA = "resAK";
String resB = "resBK";
FlowRule rule1 = new FlowRule(resA).setCount(10);
FlowRule rule2 = new FlowRule(resB).setCount(10);
// Here we only load rules for resA.
FlowRuleManager.loadRules(Collections.singletonList(rule1));
when(checker.canPassCheck(eq(rule1), any(Context.class), any(DefaultNode.class), anyInt(), anyBoolean())).thenReturn(true);
when(checker.canPassCheck(eq(rule2), any(Context.class), any(DefaultNode.class), anyInt(), anyBoolean())).thenReturn(false);
flowSlot.checkFlow(new StringResourceWrapper(resA, EntryType.IN), context, node, 1, false);
flowSlot.checkFlow(new StringResourceWrapper(resB, EntryType.IN), context, node, 1, false);
}
use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.
the class ContextUtil method trueEnter.
protected static Context trueEnter(String name, String origin) {
Context context = contextHolder.get();
if (context == null) {
Map<String, DefaultNode> localCacheNameMap = contextNameNodeMap;
DefaultNode node = localCacheNameMap.get(name);
if (node == null) {
if (localCacheNameMap.size() > Constants.MAX_CONTEXT_NAME_SIZE) {
setNullContext();
return NULL_CONTEXT;
} else {
LOCK.lock();
try {
node = contextNameNodeMap.get(name);
if (node == null) {
if (contextNameNodeMap.size() > Constants.MAX_CONTEXT_NAME_SIZE) {
setNullContext();
return NULL_CONTEXT;
} else {
node = new EntranceNode(new StringResourceWrapper(name, EntryType.IN), null);
// Add entrance node.
Constants.ROOT.addChild(node);
Map<String, DefaultNode> newMap = new HashMap<>(contextNameNodeMap.size() + 1);
newMap.putAll(contextNameNodeMap);
newMap.put(name, node);
contextNameNodeMap = newMap;
}
}
} finally {
LOCK.unlock();
}
}
}
context = new Context(node, name);
context.setOrigin(origin);
contextHolder.set(context);
}
return context;
}
use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.
the class ParamFlowCheckerTest method testPassLocalCheckForArray.
@Test
public void testPassLocalCheckForArray() throws InterruptedException {
final String resourceName = "testPassLocalCheckForArray";
final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
int paramIdx = 0;
double globalThreshold = 1;
ParamFlowRule rule = new ParamFlowRule(resourceName).setParamIdx(paramIdx).setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER).setCount(globalThreshold);
TimeUtil.currentTimeMillis();
String v1 = "a", v2 = "B", v3 = "Cc";
Object arr = new String[] { v1, v2, v3 };
ParameterMetric metric = new ParameterMetric();
ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric);
metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
assertTrue(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, arr));
assertFalse(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, arr));
}
Aggregations