use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.
the class ClientFilterTest method shutdown.
@AfterClass
public static void shutdown() {
ctx.close();
Context context = ContextUtil.getContext();
if (context != null) {
context.setCurEntry(null);
ContextUtil.exit();
}
Constants.ROOT.removeChildList();
ClusterBuilderSlot.getClusterNodeMap().clear();
// Clear chainMap in CtSph
try {
Method resetChainMapMethod = CtSph.class.getDeclaredMethod("resetChainMap");
resetChainMapMethod.setAccessible(true);
resetChainMapMethod.invoke(null);
} catch (Exception e) {
// Empty
}
}
use of com.alibaba.csp.sentinel.context.Context 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.context.Context 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();
}
}
use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.
the class AsyncEntryTest method testCleanCurrentEntryInLocal.
@Test
public void testCleanCurrentEntryInLocal() {
final String contextName = "abc";
try {
ContextUtil.enter(contextName);
Context curContext = ContextUtil.getContext();
Entry previousEntry = new CtEntry(new StringResourceWrapper("entry-sync", EntryType.IN), null, curContext);
AsyncEntry entry = new AsyncEntry(new StringResourceWrapper("testCleanCurrentEntryInLocal", EntryType.OUT), null, curContext);
assertSame(entry, curContext.getCurEntry());
entry.cleanCurrentEntryInLocal();
assertNotSame(entry, curContext.getCurEntry());
assertSame(previousEntry, curContext.getCurEntry());
} finally {
ContextTestUtil.cleanUpContext();
}
}
use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.
the class CtEntryTest method testExitLastEntryWithDefaultContext.
@Test
public void testExitLastEntryWithDefaultContext() {
final Context defaultContext = getFakeDefaultContext();
ContextUtil.runOnContext(defaultContext, new Runnable() {
@Override
public void run() {
CtEntry entry = new CtEntry(new StringResourceWrapper("res", EntryType.IN), null, ContextUtil.getContext());
assertSame(entry, defaultContext.getCurEntry());
assertSame(defaultContext, ContextUtil.getContext());
entry.exit();
assertNull(defaultContext.getCurEntry());
// Default context will be automatically exited.
assertNull(ContextUtil.getContext());
}
});
}
Aggregations