use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.
the class AuthoritySlotTest method testCheckAuthorityNoExceptionItemsWhiteFail.
@Test(expected = AuthorityException.class)
public void testCheckAuthorityNoExceptionItemsWhiteFail() throws Exception {
String origin = "appA";
String resourceName = "testCheckAuthorityNoExceptionItemsWhiteFail";
ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
ContextUtil.enter("entrance", origin);
try {
AuthorityRule ruleB = new AuthorityRule().setResource(resourceName).setLimitApp("appB, appE").as(AuthorityRule.class).setStrategy(RuleConstant.AUTHORITY_WHITE);
AuthorityRuleManager.loadRules(Collections.singletonList(ruleB));
authoritySlot.checkBlackWhiteAuthority(resourceWrapper, ContextUtil.getContext());
} finally {
ContextUtil.exit();
}
}
use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.
the class AuthoritySlotTest method testCheckAuthorityNoExceptionItemsSuccess.
@Test
public void testCheckAuthorityNoExceptionItemsSuccess() throws Exception {
String origin = "appA";
String resourceName = "testCheckAuthorityNoExceptionItemsSuccess";
ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
ContextUtil.enter("entrance", origin);
try {
AuthorityRule ruleA = new AuthorityRule().setResource(resourceName).setLimitApp(origin + ",appB").as(AuthorityRule.class).setStrategy(RuleConstant.AUTHORITY_WHITE);
AuthorityRuleManager.loadRules(Collections.singletonList(ruleA));
authoritySlot.checkBlackWhiteAuthority(resourceWrapper, ContextUtil.getContext());
AuthorityRule ruleB = new AuthorityRule().setResource(resourceName).setLimitApp("appD").as(AuthorityRule.class).setStrategy(RuleConstant.AUTHORITY_BLACK);
AuthorityRuleManager.loadRules(Collections.singletonList(ruleB));
authoritySlot.checkBlackWhiteAuthority(resourceWrapper, ContextUtil.getContext());
} finally {
ContextUtil.exit();
}
}
use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.
the class CtSphTest method testCustomContextEntryWithFullContextSize.
private void testCustomContextEntryWithFullContextSize(String resourceName, boolean async) {
fillFullContext();
ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
String contextName = "custom-context-" + System.currentTimeMillis();
ContextUtil.enter(contextName, "9527");
// Prepare a slot that "should not pass". If entered the slot, exception will be thrown.
addShouldNotPassSlotFor(resourceWrapper);
Entry entry = null;
try {
if (async) {
entry = ctSph.asyncEntry(resourceName, resourceWrapper.getEntryType(), 1);
} else {
entry = ctSph.entry(resourceWrapper, 1);
}
} catch (BlockException ex) {
fail("Unexpected blocked: " + ex.getClass().getCanonicalName());
} finally {
if (entry != null) {
entry.exit();
}
ContextUtil.exit();
}
}
use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper 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.ResourceWrapper 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;
}
}
Aggregations