Search in sources :

Example 6 with StringResourceWrapper

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

Example 7 with StringResourceWrapper

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

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

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

Example 10 with StringResourceWrapper

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

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