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