Search in sources :

Example 1 with Context

use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.

the class ClientFilterTest method shutdown.

@AfterClass
public static void shutdown() {
    ctx.close();
    Context context = ContextUtil.getContext();
    if (context != null) {
        context.setCurEntry(null);
        ContextUtil.exit();
    }
    Constants.ROOT.removeChildList();
    ClusterBuilderSlot.getClusterNodeMap().clear();
    // Clear chainMap in CtSph
    try {
        Method resetChainMapMethod = CtSph.class.getDeclaredMethod("resetChainMap");
        resetChainMapMethod.setAccessible(true);
        resetChainMapMethod.invoke(null);
    } catch (Exception e) {
    // Empty
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Context(com.alibaba.csp.sentinel.context.Context) Method(java.lang.reflect.Method) ProcessingException(javax.ws.rs.ProcessingException) AfterClass(org.junit.AfterClass)

Example 2 with Context

use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.

the class MetricExitCallbackTest method onExit.

@Test
public void onExit() {
    FakeMetricExtension extension = new FakeMetricExtension();
    MetricExtensionProvider.addMetricExtension(extension);
    MetricExitCallback exitCallback = new MetricExitCallback();
    StringResourceWrapper resourceWrapper = new StringResourceWrapper("resource", EntryType.OUT);
    int count = 2;
    Object[] args = { "args1", "args2" };
    long prevRt = 20;
    extension.rt = prevRt;
    extension.success = 6;
    extension.thread = 10;
    Context context = mock(Context.class);
    Entry entry = mock(Entry.class);
    // Mock current time
    long curMillis = System.currentTimeMillis();
    setCurrentMillis(curMillis);
    int deltaMs = 100;
    when(entry.getError()).thenReturn(null);
    when(entry.getCreateTimestamp()).thenReturn(curMillis - deltaMs);
    when(context.getCurEntry()).thenReturn(entry);
    exitCallback.onExit(context, resourceWrapper, count, args);
    Assert.assertEquals(prevRt + deltaMs, extension.rt);
    Assert.assertEquals(extension.success, 6 + count);
    Assert.assertEquals(extension.thread, 10 - 1);
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Context(com.alibaba.csp.sentinel.context.Context) Entry(com.alibaba.csp.sentinel.Entry) AbstractTimeBasedTest(com.alibaba.csp.sentinel.test.AbstractTimeBasedTest) Test(org.junit.Test)

Example 3 with Context

use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.

the class AsyncEntryTest method testInitAndGetAsyncContext.

@Test
public void testInitAndGetAsyncContext() {
    final String contextName = "abc";
    final String origin = "xxx";
    try {
        ContextUtil.enter(contextName, origin);
        Context curContext = ContextUtil.getContext();
        AsyncEntry entry = new AsyncEntry(new StringResourceWrapper("testInitAndGetAsyncContext", EntryType.OUT), null, curContext);
        assertNull(entry.getAsyncContext());
        entry.initAsyncContext();
        Context asyncContext = entry.getAsyncContext();
        assertNotNull(asyncContext);
        assertEquals(contextName, asyncContext.getName());
        assertEquals(origin, asyncContext.getOrigin());
        assertSame(curContext.getEntranceNode(), asyncContext.getEntranceNode());
        assertSame(entry, asyncContext.getCurEntry());
        assertTrue(asyncContext.isAsync());
    } finally {
        ContextTestUtil.cleanUpContext();
    }
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Test(org.junit.Test)

Example 4 with Context

use of com.alibaba.csp.sentinel.context.Context 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 5 with Context

use of com.alibaba.csp.sentinel.context.Context 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)

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