use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.
the class CtSphTest method fillFullResources.
private void fillFullResources() {
for (int i = 0; i < Constants.MAX_SLOT_CHAIN_SIZE; i++) {
ResourceWrapper resourceWrapper = new StringResourceWrapper("test-resource-" + i, EntryType.IN);
CtSph.getChainMap().put(resourceWrapper, SlotChainProvider.newSlotChain());
}
}
use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.
the class CtSphTest method testAsyncEntryNestedInSyncEntryNormalBlocked.
@Test
public void testAsyncEntryNestedInSyncEntryNormalBlocked() {
String previousResourceName = "fff";
String resourceName = "testAsyncEntryNestedInSyncEntryNormalBlocked";
ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
// Prepare a slot that "must block".
MustBlockSlot slot = addMustBlockSlot(resourceWrapper);
assertFalse(slot.exited);
// Previous entry should pass.
addShouldPassSlotFor(new StringResourceWrapper(previousResourceName, EntryType.IN));
ContextUtil.enter("bcd-" + System.currentTimeMillis());
AsyncEntry entry = null;
Entry syncEntry = null;
Entry previousEntry = null;
try {
// First enter a sync resource.
syncEntry = ctSph.entry(previousResourceName, EntryType.IN, 1);
// Record current entry (previous for next).
previousEntry = ContextUtil.getContext().getCurEntry();
// Then enter an async resource.
entry = ctSph.asyncEntry(resourceName, EntryType.IN, 1);
// Should not pass here.
} catch (BlockException ex) {
assertNotNull(previousEntry);
assertNull(entry);
assertTrue(slot.exited);
assertSame(previousEntry, ContextUtil.getContext().getCurEntry());
return;
} finally {
assertNull(entry);
assertNotNull(syncEntry);
syncEntry.exit();
ContextUtil.exit();
}
fail("This async entry is expected to be blocked");
}
use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.
the class EntryTest method testEntryExitCounts.
@Test
public void testEntryExitCounts() {
ResourceWrapper resourceWrapper = new StringResourceWrapper("resA", EntryType.IN);
TestEntry entry = new TestEntry(resourceWrapper);
entry.exit();
assertEquals(-1, entry.count);
entry.exit(9);
assertEquals(-10, entry.count);
}
use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.
the class EntryTest method testEntryFieldsGetSet.
@Test
public void testEntryFieldsGetSet() {
ResourceWrapper resourceWrapper = new StringResourceWrapper("resA", EntryType.IN);
Entry entry = new TestEntry(resourceWrapper);
assertSame(resourceWrapper, entry.getResourceWrapper());
Throwable error = new IllegalStateException();
entry.setError(error);
assertSame(error, entry.getError());
Node curNode = mock(Node.class);
entry.setCurNode(curNode);
assertSame(curNode, entry.getCurNode());
Node originNode = mock(Node.class);
entry.setOriginNode(originNode);
assertSame(originNode, entry.getOriginNode());
}
use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.
the class ClusterBuilderSlot method entry.
@Override
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args) throws Throwable {
if (clusterNode == null) {
synchronized (lock) {
if (clusterNode == null) {
// Create the cluster node.
clusterNode = new ClusterNode(resourceWrapper.getName(), resourceWrapper.getResourceType());
HashMap<ResourceWrapper, ClusterNode> newMap = new HashMap<>(Math.max(clusterNodeMap.size(), 16));
newMap.putAll(clusterNodeMap);
newMap.put(node.getId(), clusterNode);
clusterNodeMap = newMap;
}
}
}
node.setClusterNode(clusterNode);
/*
* if context origin is set, we should get or create a new {@link Node} of
* the specific origin.
*/
if (!"".equals(context.getOrigin())) {
Node originNode = node.getClusterNode().getOrCreateOriginNode(context.getOrigin());
context.getCurEntry().setOriginNode(originNode);
}
fireEntry(context, resourceWrapper, node, count, prioritized, args);
}
Aggregations