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);
}
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);
}
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);
}
Aggregations