Search in sources :

Example 1 with StringResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.

the class MetricEntryCallbackTest method onPass.

@Test
public void onPass() throws Exception {
    FakeMetricExtension extension = new FakeMetricExtension();
    FakeAdvancedMetricExtension advancedExtension = new FakeAdvancedMetricExtension();
    MetricExtensionProvider.addMetricExtension(extension);
    MetricExtensionProvider.addMetricExtension(advancedExtension);
    MetricEntryCallback entryCallback = new MetricEntryCallback();
    StringResourceWrapper resourceWrapper = new StringResourceWrapper("resource", EntryType.OUT);
    int count = 2;
    Object[] args = { "args1", "args2" };
    entryCallback.onPass(null, resourceWrapper, null, count, args);
    // assert extension
    Assert.assertEquals(extension.pass, count);
    Assert.assertEquals(extension.thread, 1);
    // assert advancedExtension
    Assert.assertEquals(advancedExtension.pass, count);
    Assert.assertEquals(advancedExtension.concurrency, 1);
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Test(org.junit.Test)

Example 2 with StringResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.

the class MetricExitCallbackTest method onExit.

@Test
public void onExit() {
    FakeMetricExtension extension = new FakeMetricExtension();
    MetricExtensionProvider.addMetricExtension(extension);
    MetricExitCallback exitCallback = new MetricExitCallback();
    StringResourceWrapper resourceWrapper = new StringResourceWrapper("resource", EntryType.OUT);
    int count = 2;
    Object[] args = { "args1", "args2" };
    long prevRt = 20;
    extension.rt = prevRt;
    extension.success = 6;
    extension.thread = 10;
    Context context = mock(Context.class);
    Entry entry = mock(Entry.class);
    // Mock current time
    long curMillis = System.currentTimeMillis();
    setCurrentMillis(curMillis);
    int deltaMs = 100;
    when(entry.getError()).thenReturn(null);
    when(entry.getCreateTimestamp()).thenReturn(curMillis - deltaMs);
    when(context.getCurEntry()).thenReturn(entry);
    exitCallback.onExit(context, resourceWrapper, count, args);
    Assert.assertEquals(prevRt + deltaMs, extension.rt);
    Assert.assertEquals(extension.success, 6 + count);
    Assert.assertEquals(extension.thread, 10 - 1);
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Context(com.alibaba.csp.sentinel.context.Context) Entry(com.alibaba.csp.sentinel.Entry) AbstractTimeBasedTest(com.alibaba.csp.sentinel.test.AbstractTimeBasedTest) Test(org.junit.Test)

Example 3 with StringResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper 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 4 with StringResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper 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 5 with StringResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.

the class AsyncEntryTest method testInitAndGetAsyncContext.

@Test
public void testInitAndGetAsyncContext() {
    final String contextName = "abc";
    final String origin = "xxx";
    try {
        ContextUtil.enter(contextName, origin);
        Context curContext = ContextUtil.getContext();
        AsyncEntry entry = new AsyncEntry(new StringResourceWrapper("testInitAndGetAsyncContext", EntryType.OUT), null, curContext);
        assertNull(entry.getAsyncContext());
        entry.initAsyncContext();
        Context asyncContext = entry.getAsyncContext();
        assertNotNull(asyncContext);
        assertEquals(contextName, asyncContext.getName());
        assertEquals(origin, asyncContext.getOrigin());
        assertSame(curContext.getEntranceNode(), asyncContext.getEntranceNode());
        assertSame(entry, asyncContext.getCurEntry());
        assertTrue(asyncContext.isAsync());
    } finally {
        ContextTestUtil.cleanUpContext();
    }
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Test(org.junit.Test)

Aggregations

StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)48 Test (org.junit.Test)43 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)31 Context (com.alibaba.csp.sentinel.context.Context)17 AtomicLong (java.util.concurrent.atomic.AtomicLong)12 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)7 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)6 NullContext (com.alibaba.csp.sentinel.context.NullContext)5 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)4 HashMap (java.util.HashMap)3 Entry (com.alibaba.csp.sentinel.Entry)2 EntranceNode (com.alibaba.csp.sentinel.node.EntranceNode)2 Node (com.alibaba.csp.sentinel.node.Node)2 Function (com.alibaba.csp.sentinel.util.function.Function)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)1 FlowException (com.alibaba.csp.sentinel.slots.block.flow.FlowException)1 ConcurrentLinkedHashMapWrapper (com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper)1 Random (java.util.Random)1