Search in sources :

Example 1 with ResourceWrapper

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();
    }
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Test(org.junit.Test)

Example 2 with ResourceWrapper

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();
    }
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Test(org.junit.Test)

Example 3 with ResourceWrapper

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();
    }
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException)

Example 4 with ResourceWrapper

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));
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Test(org.junit.Test)

Example 5 with ResourceWrapper

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;
    }
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) Test(org.junit.Test)

Aggregations

ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)42 StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)31 Test (org.junit.Test)26 AtomicLong (java.util.concurrent.atomic.AtomicLong)12 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)10 Context (com.alibaba.csp.sentinel.context.Context)7 Node (com.alibaba.csp.sentinel.node.Node)7 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)6 StatisticNode (com.alibaba.csp.sentinel.node.StatisticNode)6 Entry (com.alibaba.csp.sentinel.Entry)5 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)5 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)5 HashMap (java.util.HashMap)5 DemoService (com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AbstractSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)2 AsyncEntry (com.alibaba.csp.sentinel.AsyncEntry)1 MetricNode (com.alibaba.csp.sentinel.node.metric.MetricNode)1 MethodResourceWrapper (com.alibaba.csp.sentinel.slotchain.MethodResourceWrapper)1