Search in sources :

Example 26 with Context

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());
    }
}
Also used : NullContext(com.alibaba.csp.sentinel.context.NullContext) Context(com.alibaba.csp.sentinel.context.Context) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Test(org.junit.Test)

Example 27 with Context

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);
        }
    }
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Context(com.alibaba.csp.sentinel.context.Context) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException)

Example 28 with Context

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();
    }
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Context(com.alibaba.csp.sentinel.context.Context) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) Test(org.junit.Test)

Example 29 with Context

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();
        }
    }
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException)

Example 30 with Context

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;
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) Entry(com.alibaba.csp.sentinel.Entry)

Aggregations

Context (com.alibaba.csp.sentinel.context.Context)44 Test (org.junit.Test)29 StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)17 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)15 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)10 Entry (com.alibaba.csp.sentinel.Entry)9 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)9 NullContext (com.alibaba.csp.sentinel.context.NullContext)8 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)7 BaseTest (com.alibaba.csp.sentinel.BaseTest)6 Node (com.alibaba.csp.sentinel.node.Node)6 StatisticNode (com.alibaba.csp.sentinel.node.StatisticNode)5 DemoService (com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService)4 Method (java.lang.reflect.Method)4 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)2 Function (com.alibaba.csp.sentinel.util.function.Function)2 Invocation (com.alibaba.dubbo.rpc.Invocation)2 Invoker (com.alibaba.dubbo.rpc.Invoker)2 Result (com.alibaba.dubbo.rpc.Result)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2