use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.
the class CtEntryTest method testExitTwoLastEntriesWithCustomContext.
@Test
public void testExitTwoLastEntriesWithCustomContext() {
String contextName = "context-rpc";
ContextUtil.enter(contextName);
Context context = ContextUtil.getContext();
try {
CtEntry entry1 = new CtEntry(new StringResourceWrapper("resA", EntryType.IN), null, context);
entry1.exit();
assertEquals(context, ContextUtil.getContext());
CtEntry entry2 = new CtEntry(new StringResourceWrapper("resB", EntryType.IN), null, context);
entry2.exit();
assertEquals(context, ContextUtil.getContext());
} finally {
ContextUtil.exit();
assertNull(ContextUtil.getContext());
}
}
use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.
the class CtSphTest method testDefaultContextEntryWithFullContextSize.
private void testDefaultContextEntryWithFullContextSize(String resourceName, boolean async) {
fillFullContext();
ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
// Prepare a slot that "should pass".
ShouldPassSlot slot = addShouldPassSlotFor(resourceWrapper);
assertFalse(slot.entered || slot.exited);
Entry entry = null;
try {
if (!async) {
entry = ctSph.entry(resourceWrapper, 1);
} else {
entry = ctSph.asyncEntry(resourceName, resourceWrapper.getEntryType(), 1);
Context asyncContext = ((AsyncEntry) entry).getAsyncContext();
assertTrue(ContextUtil.isDefaultContext(asyncContext));
assertTrue(asyncContext.isAsync());
}
assertTrue(ContextUtil.isDefaultContext(ContextUtil.getContext()));
assertTrue(slot.entered);
} catch (BlockException ex) {
fail("Unexpected blocked: " + ex.getClass().getCanonicalName());
} finally {
if (entry != null) {
entry.exit();
assertTrue(slot.exited);
}
}
}
use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.
the class CtSphTest method testAsyncEntryNormalPass.
@Test
public void testAsyncEntryNormalPass() {
String resourceName = "testAsyncEntryNormalPass";
ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
AsyncEntry entry = null;
// Prepare a slot that "should pass".
ShouldPassSlot slot = addShouldPassSlotFor(resourceWrapper);
assertFalse(slot.entered || slot.exited);
ContextUtil.enter("abc");
Entry previousEntry = ContextUtil.getContext().getCurEntry();
try {
entry = ctSph.asyncEntry(resourceName, EntryType.IN, 1);
assertTrue(slot.entered);
assertFalse(slot.exited);
Context asyncContext = entry.getAsyncContext();
assertNotNull(asyncContext);
assertSame(entry, asyncContext.getCurEntry());
assertNotSame("The async entry should not be added to current context", entry, ContextUtil.getContext().getCurEntry());
assertSame(previousEntry, ContextUtil.getContext().getCurEntry());
} catch (BlockException ex) {
fail("Unexpected blocked: " + ex.getClass().getCanonicalName());
} finally {
if (entry != null) {
Context asyncContext = entry.getAsyncContext();
entry.exit();
assertTrue(slot.exited);
assertNull(entry.getAsyncContext());
assertSame(previousEntry, asyncContext.getCurEntry());
}
ContextUtil.exit();
}
}
use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper 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.StringResourceWrapper 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");
}
Aggregations