use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.
the class CtEntryTest method testExitNotMatchCurEntry.
@Test
public void testExitNotMatchCurEntry() {
String contextName = "context-rpc";
ContextUtil.enter(contextName);
Context context = ContextUtil.getContext();
CtEntry entry1 = null;
CtEntry entry2 = null;
try {
entry1 = new CtEntry(new StringResourceWrapper("res1", EntryType.IN), null, ContextUtil.getContext());
assertSame(entry1, context.getCurEntry());
entry2 = new CtEntry(new StringResourceWrapper("res2", EntryType.IN), null, ContextUtil.getContext());
assertSame(entry2, context.getCurEntry());
// Forget to exit for entry 2...
// Directly exit for entry 1, then boom...
entry1.exit();
} catch (ErrorEntryFreeException ex) {
assertNotNull(entry1);
assertNotNull(entry2);
assertNull(entry1.context);
assertNull(entry2.context);
assertNull(context.getCurEntry());
return;
} finally {
ContextUtil.exit();
}
fail("Mismatch entry-exit should throw an ErrorEntryFreeException");
}
use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.
the class CtEntryTest method testEntryAndExitWithNullContext.
@Test
public void testEntryAndExitWithNullContext() {
Context context = new NullContext();
CtEntry entry = new CtEntry(new StringResourceWrapper("testEntryAndExitWithNullContext", EntryType.IN), null, context);
assertNull(context.getCurEntry());
entry.exit();
assertNull(context.getCurEntry());
// Won't true exit, so the context won't be cleared.
assertEquals(context, entry.context);
}
use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.
the class BaseTest method cleanUpAll.
/**
* Clean up resources.
*/
protected static void cleanUpAll() {
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 SentinelSofaRpcProviderFilterTest method verifyInvocationStructure.
/**
* Verify Sentinel invocation structure in memory:
* EntranceNode(methodResourceName)
* --InterfaceNode(interfaceResourceName)
* ----MethodNode(methodResourceName)
*/
private void verifyInvocationStructure(String applicationName, String interfaceResourceName, String methodResourceName) {
Context context = ContextUtil.getContext();
assertNotNull(context);
assertEquals(methodResourceName, context.getName());
assertEquals(applicationName, context.getOrigin());
DefaultNode entranceNode = context.getEntranceNode();
ResourceWrapper entranceResource = entranceNode.getId();
assertEquals(methodResourceName, entranceResource.getName());
assertSame(EntryType.IN, entranceResource.getEntryType());
// As SphU.entry(interfaceResourceName, EntryType.IN);
Set<Node> childList = entranceNode.getChildList();
assertEquals(1, childList.size());
DefaultNode interfaceNode = (DefaultNode) childList.iterator().next();
ResourceWrapper interfaceResource = interfaceNode.getId();
assertEquals(interfaceResourceName, interfaceResource.getName());
assertSame(EntryType.IN, interfaceResource.getEntryType());
// As SphU.entry(methodResourceName, EntryType.IN, 1, methodArguments);
childList = interfaceNode.getChildList();
assertEquals(1, childList.size());
DefaultNode methodNode = (DefaultNode) childList.iterator().next();
ResourceWrapper methodResource = methodNode.getId();
assertEquals(methodResourceName, methodResource.getName());
assertSame(EntryType.IN, methodResource.getEntryType());
// Verify curEntry
Entry curEntry = context.getCurEntry();
assertSame(methodNode, curEntry.getCurNode());
assertSame(interfaceNode, curEntry.getLastNode());
// As context origin is not "", originNode should be created
assertNotNull(curEntry.getOriginNode());
// Verify clusterNode
ClusterNode methodClusterNode = methodNode.getClusterNode();
ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
// Different resource->Different ProcessorSlot->Different ClusterNode
assertNotSame(methodClusterNode, interfaceClusterNode);
// As context origin is not "", the StatisticNode should be created in originCountMap of ClusterNode
Map<String, StatisticNode> methodOriginCountMap = methodClusterNode.getOriginCountMap();
assertEquals(1, methodOriginCountMap.size());
assertTrue(methodOriginCountMap.containsKey(applicationName));
Map<String, StatisticNode> interfaceOriginCountMap = interfaceClusterNode.getOriginCountMap();
assertEquals(1, interfaceOriginCountMap.size());
assertTrue(interfaceOriginCountMap.containsKey(applicationName));
}
use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.
the class ReactorSphU method entryWith.
public static <R> Mono<R> entryWith(String resourceName, EntryType entryType, Mono<R> actual) {
final AtomicReference<AsyncEntry> entryWrapper = new AtomicReference<>(null);
return Mono.defer(() -> {
try {
AsyncEntry entry = SphU.asyncEntry(resourceName, entryType);
entryWrapper.set(entry);
return actual.subscriberContext(context -> {
if (entry == null) {
return context;
}
Context sentinelContext = entry.getAsyncContext();
if (sentinelContext == null) {
return context;
}
// TODO: check GC friendly?
return context.put(SentinelReactorConstants.SENTINEL_CONTEXT_KEY, sentinelContext);
}).doOnSuccessOrError((o, t) -> {
if (entry != null && entryWrapper.compareAndSet(entry, null)) {
if (t != null) {
Tracer.traceContext(t, 1, entry.getAsyncContext());
}
entry.exit();
}
});
} catch (BlockException ex) {
return Mono.error(ex);
}
});
}
Aggregations