Search in sources :

Example 36 with StringResourceWrapper

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());
    }
}
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 37 with StringResourceWrapper

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);
        }
    }
}
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 38 with StringResourceWrapper

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();
    }
}
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 39 with StringResourceWrapper

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

Example 40 with StringResourceWrapper

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

Aggregations

StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)48 Test (org.junit.Test)43 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)31 Context (com.alibaba.csp.sentinel.context.Context)17 AtomicLong (java.util.concurrent.atomic.AtomicLong)12 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)7 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)6 NullContext (com.alibaba.csp.sentinel.context.NullContext)5 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)4 HashMap (java.util.HashMap)3 Entry (com.alibaba.csp.sentinel.Entry)2 EntranceNode (com.alibaba.csp.sentinel.node.EntranceNode)2 Node (com.alibaba.csp.sentinel.node.Node)2 Function (com.alibaba.csp.sentinel.util.function.Function)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)1 FlowException (com.alibaba.csp.sentinel.slots.block.flow.FlowException)1 ConcurrentLinkedHashMapWrapper (com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper)1 Random (java.util.Random)1