use of com.alibaba.csp.sentinel.context.Context 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.context.Context 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.context.Context 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.context.Context in project Sentinel by alibaba.
the class CtSphTest method testEntryAmountExceeded.
private void testEntryAmountExceeded(boolean async) {
fillFullResources();
Entry entry = null;
try {
if (!async) {
entry = ctSph.entry("testSync", EntryType.IN, 1);
} else {
entry = ctSph.asyncEntry("testSync", EntryType.IN, 1);
}
assertNull(((CtEntry) entry).chain);
if (!async) {
assertSame(entry, ContextUtil.getContext().getCurEntry());
} else {
Context asyncContext = ((AsyncEntry) entry).getAsyncContext();
assertNotNull(asyncContext);
assertSame(entry, asyncContext.getCurEntry());
}
} catch (BlockException ex) {
fail("Unexpected blocked: " + ex.getClass().getCanonicalName());
} finally {
if (entry != null) {
entry.exit();
}
}
}
use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.
the class AbstractCircuitBreaker method fromOpenToHalfOpen.
protected boolean fromOpenToHalfOpen(Context context) {
if (currentState.compareAndSet(State.OPEN, State.HALF_OPEN)) {
notifyObservers(State.OPEN, State.HALF_OPEN, null);
Entry entry = context.getCurEntry();
entry.whenTerminate(new BiConsumer<Context, Entry>() {
@Override
public void accept(Context context, Entry entry) {
// when the request is actually blocked by upcoming rules (not only degrade rules).
if (entry.getBlockError() != null) {
// Fallback to OPEN due to detecting request is blocked
currentState.compareAndSet(State.HALF_OPEN, State.OPEN);
notifyObservers(State.HALF_OPEN, State.OPEN, 1.0d);
}
}
});
return true;
}
return false;
}
Aggregations