use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper 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.slotchain.StringResourceWrapper 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());
}
});
}
use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper 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.slotchain.StringResourceWrapper 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.slotchain.StringResourceWrapper in project Sentinel by alibaba.
the class CtSphTest method testCustomContextEntryWithFullContextSize.
private void testCustomContextEntryWithFullContextSize(String resourceName, boolean async) {
fillFullContext();
ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
String contextName = "custom-context-" + System.currentTimeMillis();
ContextUtil.enter(contextName, "9527");
// Prepare a slot that "should not pass". If entered the slot, exception will be thrown.
addShouldNotPassSlotFor(resourceWrapper);
Entry entry = null;
try {
if (async) {
entry = ctSph.asyncEntry(resourceName, resourceWrapper.getEntryType(), 1);
} else {
entry = ctSph.entry(resourceWrapper, 1);
}
} catch (BlockException ex) {
fail("Unexpected blocked: " + ex.getClass().getCanonicalName());
} finally {
if (entry != null) {
entry.exit();
}
ContextUtil.exit();
}
}
Aggregations