Home / Class/ LeakPresenceExtension Class — netty Architecture

LeakPresenceExtension Class — netty Architecture

Architecture documentation for the LeakPresenceExtension class in LeakPresenceExtension.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  4aa8c5e8_067e_2569_2cc8_34469837e207["LeakPresenceExtension"]
  4cc4359a_c25b_64d1_ace3_a8c8a76aece1["LeakPresenceExtension.java"]
  4aa8c5e8_067e_2569_2cc8_34469837e207 -->|defined in| 4cc4359a_c25b_64d1_ace3_a8c8a76aece1
  c8c6b3fb_0903_734e_5361_474d6783dcba["beforeAll()"]
  4aa8c5e8_067e_2569_2cc8_34469837e207 -->|method| c8c6b3fb_0903_734e_5361_474d6783dcba
  2714d0d3_3165_1afd_de67_d9b39f0dcf90["beforeEach()"]
  4aa8c5e8_067e_2569_2cc8_34469837e207 -->|method| 2714d0d3_3165_1afd_de67_d9b39f0dcf90
  8e69c9e3_8bb5_9596_b08e_92571de30579["afterEach()"]
  4aa8c5e8_067e_2569_2cc8_34469837e207 -->|method| 8e69c9e3_8bb5_9596_b08e_92571de30579
  3674c477_c44b_6459_665d_3a1b4ff3ea6c["afterAll()"]
  4aa8c5e8_067e_2569_2cc8_34469837e207 -->|method| 3674c477_c44b_6459_665d_3a1b4ff3ea6c

Relationship Graph

Source Code

testsuite-common/src/main/java/io/netty/util/test/LeakPresenceExtension.java lines 45–133

public final class LeakPresenceExtension
        implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback, AfterAllCallback {

    private static final Object SCOPE_KEY = new Object();
    private static final Object PREVIOUS_SCOPE_KEY = new Object();

    static {
        System.setProperty("io.netty.customResourceLeakDetector", WithTransferableScope.class.getName());
    }

    @Override
    public void beforeAll(ExtensionContext context) {
        ExtensionContext.Store store = context.getStore(ExtensionContext.Namespace.GLOBAL);
        if (store.get(SCOPE_KEY) != null) {
            throw new IllegalStateException("Weird context lifecycle");
        }
        LeakPresenceDetector.ResourceScope scope = new LeakPresenceDetector.ResourceScope(context.getDisplayName());
        store.put(SCOPE_KEY, scope);

        WithTransferableScope.SCOPE.set(scope);
    }

    @Override
    public void beforeEach(ExtensionContext context) {
        LeakPresenceDetector.ResourceScope outerScope;
        ExtensionContext outerContext = context;
        while (true) {
            outerScope = (LeakPresenceDetector.ResourceScope)
                    outerContext.getStore(ExtensionContext.Namespace.GLOBAL).get(SCOPE_KEY);
            if (outerScope != null) {
                break;
            }
            outerContext = outerContext.getParent()
                    .orElseThrow(() -> new IllegalStateException("No resource scope found"));
        }

        LeakPresenceDetector.ResourceScope previousScope = WithTransferableScope.SCOPE.get();
        WithTransferableScope.SCOPE.set(outerScope);
        if (previousScope != null) {
            context.getStore(ExtensionContext.Namespace.GLOBAL).put(PREVIOUS_SCOPE_KEY, previousScope);
        }
    }

    @Override
    public void afterEach(ExtensionContext context) {
        LeakPresenceDetector.ResourceScope previousScope = (LeakPresenceDetector.ResourceScope)
                context.getStore(ExtensionContext.Namespace.GLOBAL).get(PREVIOUS_SCOPE_KEY);
        if (previousScope != null) {
            WithTransferableScope.SCOPE.set(previousScope);
        }
    }

    @Override
    public void afterAll(ExtensionContext context) throws InterruptedException {
        LeakPresenceDetector.ResourceScope scope =
                (LeakPresenceDetector.ResourceScope) context.getStore(ExtensionContext.Namespace.GLOBAL).get(SCOPE_KEY);

        // Wait some time for resources to close. Many tests do loop.shutdownGracefully without waiting, and that's ok.
        long start = System.nanoTime();
        while (scope.hasOpenResources() && System.nanoTime() - start < TimeUnit.SECONDS.toNanos(5)) {
            TimeUnit.MILLISECONDS.sleep(100);
        }

        scope.close();
    }

    public static final class WithTransferableScope<T> extends LeakPresenceDetector<T> {
        static final InheritableThreadLocal<ResourceScope> SCOPE = new InheritableThreadLocal<>();

        @SuppressWarnings("unused")
        public WithTransferableScope(Class<?> resourceType, int samplingInterval) {
            super(resourceType);
        }

        @SuppressWarnings("unused")
        public WithTransferableScope(Class<?> resourceType, int samplingInterval, long maxActive) {
            super(resourceType);
        }

        @Override
        protected ResourceScope currentScope() throws AllocationProhibitedException {

Frequently Asked Questions

What is the LeakPresenceExtension class?
LeakPresenceExtension is a class in the netty codebase, defined in testsuite-common/src/main/java/io/netty/util/test/LeakPresenceExtension.java.
Where is LeakPresenceExtension defined?
LeakPresenceExtension is defined in testsuite-common/src/main/java/io/netty/util/test/LeakPresenceExtension.java at line 45.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free