Search in sources :

Example 46 with StringResourceWrapper

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

the class MetricEntryCallbackTest method onBlocked.

@Test
public void onBlocked() 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);
    Context context = mock(Context.class);
    when(context.getOrigin()).thenReturn("origin1");
    int count = 2;
    Object[] args = { "args1", "args2" };
    entryCallback.onBlocked(new FlowException("xx"), context, resourceWrapper, null, count, args);
    // assert extension
    Assert.assertEquals(extension.block, count);
    // assert advancedExtension
    Assert.assertEquals(advancedExtension.block, count);
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Context(com.alibaba.csp.sentinel.context.Context) FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) Test(org.junit.Test)

Example 47 with StringResourceWrapper

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

the class MetricExitCallbackTest method advancedExtensionOnExit.

/**
 * @author bill_yip
 */
@Test
public void advancedExtensionOnExit() {
    FakeAdvancedMetricExtension extension = new FakeAdvancedMetricExtension();
    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.complete = 6;
    extension.concurrency = 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.complete, 6 + count);
    Assert.assertEquals(extension.concurrency, 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 48 with StringResourceWrapper

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

the class SystemRuleManagerTest method testCheckMaxCpuUsageNotBBR.

@Test
public void testCheckMaxCpuUsageNotBBR() throws Exception {
    SystemRule rule1 = new SystemRule();
    rule1.setHighestCpuUsage(0d);
    SystemRuleManager.loadRules(Collections.singletonList(rule1));
    // Wait until SystemStatusListener triggered the first CPU usage collecting.
    Thread.sleep(1500);
    boolean blocked = false;
    try {
        StringResourceWrapper resourceWrapper = new StringResourceWrapper("testCheckMaxCpuUsageNotBBR", EntryType.IN);
        SystemRuleManager.checkSystem(resourceWrapper, 1);
    } catch (BlockException ex) {
        blocked = true;
    }
    assertTrue("The entry should be blocked under SystemRule maxCpuUsage=0", blocked);
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) 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