Home / File/ LeakPresenceExtension.java — netty Source File

LeakPresenceExtension.java — netty Source File

Architecture documentation for LeakPresenceExtension.java, a java file in the netty codebase.

Entity Profile

Relationship Graph

Source Code

/*
 * Copyright 2025 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package io.netty.util.test;

import io.netty.util.LeakPresenceDetector;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

import java.util.concurrent.TimeUnit;

/**
 * Junit 5 extension for leak detection using {@link LeakPresenceDetector}.
 * <p>
 * Leak presence is checked at the class level. Any resource must be closed at the end of the test class, by the time
 * {@link org.junit.jupiter.api.AfterAll} has been called. Method-level detection is not possible because some tests
 * retain resources between methods on the same class, notably parameterized tests that allocate different buffers
 * before running the test methods with those buffers.
 * <p>
 * This extension supports parallel test execution, but has to make some assumptions about the thread lifecycle. The
 * resource scope for the class is created in {@link org.junit.jupiter.api.BeforeAll}, and then saved in a thread local
 * on each {@link org.junit.jupiter.api.BeforeEach}. This appears to work well with junit's default parallelism,
 * despite the use of a fork-join pool that can transfer tasks between threads, but it may lead to problems if tests
 * make use of fork-join machinery themselves.
 * <p>
 * The ThreadLocal holding the scope is {@link InheritableThreadLocal inheritable}, so that e.g. event loops created
 * in a test are assigned to the test resource scope.
 */
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");
        }
// ... (74 more lines)

Domain

Subdomains

Frequently Asked Questions

What does LeakPresenceExtension.java do?
LeakPresenceExtension.java is a source file in the netty codebase, written in java. It belongs to the Buffer domain, Telemetry subdomain.
Where is LeakPresenceExtension.java in the architecture?
LeakPresenceExtension.java is located at testsuite-common/src/main/java/io/netty/util/test/LeakPresenceExtension.java (domain: Buffer, subdomain: Telemetry, directory: testsuite-common/src/main/java/io/netty/util/test).

Analyze Your Own Codebase

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

Try Supermodel Free