Java Code Examples for com.alibaba.csp.sentinel.context.ContextUtil#runOnContext()

The following examples show how to use com.alibaba.csp.sentinel.context.ContextUtil#runOnContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CtEntryTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@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());
        }
    });

}
 
Example 2
Source File: CtEntryTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@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());
        }
    });

}
 
Example 3
Source File: SentinelReactorSubscriber.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void doWithContextOrCurrent(Supplier<Optional<com.alibaba.csp.sentinel.context.Context>> contextSupplier,
                                    Runnable f) {
    Optional<com.alibaba.csp.sentinel.context.Context> contextOpt = contextSupplier.get();
    if (!contextOpt.isPresent()) {
        // Provided context is absent, use current context.
        f.run();
    } else {
        // Run on provided context.
        ContextUtil.runOnContext(contextOpt.get(), f);
    }
}
 
Example 4
Source File: SentinelReactorSubscriber.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void doWithContextOrCurrent(Supplier<Optional<com.alibaba.csp.sentinel.context.Context>> contextSupplier,
                                    Runnable f) {
    Optional<com.alibaba.csp.sentinel.context.Context> contextOpt = contextSupplier.get();
    if (!contextOpt.isPresent()) {
        // Provided context is absent, use current context.
        f.run();
    } else {
        // Run on provided context.
        ContextUtil.runOnContext(contextOpt.get(), f);
    }
}