All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.undefinedlabs.scope.rules.JUnit4TestContextManager Maven / Gradle / Ivy

Go to download

Scope is a APM for tests to give engineering teams unprecedented visibility into their CI process to quickly identify, troubleshoot and fix failed builds. This artifact contains the classes to instrument the JUnit4 tests.

There is a newer version: 0.15.1-beta.2
Show newest version
package com.undefinedlabs.scope.rules;

import com.undefinedlabs.scope.ScopeSpanContainer;
import com.undefinedlabs.scope.rules.model.JUnit4ScopeDescription;
import org.junit.runner.Description;

import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

public enum JUnit4TestContextManager {

    INSTANCE;

    private final Map> scopeSpanContainerQueueMap = new HashMap<>();

    public void add(final JUnit4ScopeDescription description, final ScopeSpanContainer scopeSpanContainer) {
        final String descriptionId = buildDescriptionId(description);

        synchronized (this.scopeSpanContainerQueueMap) {
            final Queue queue = this.scopeSpanContainerQueueMap.get(descriptionId);
            if(queue == null) {
                final Queue newQueue = new ConcurrentLinkedQueue<>();
                newQueue.add(scopeSpanContainer);
                this.scopeSpanContainerQueueMap.put(descriptionId, newQueue);
            } else {
                queue.add(scopeSpanContainer);
            }
        }
    }

    public ScopeSpanContainer poll(final JUnit4ScopeDescription description) {
        final String descriptionId = buildDescriptionId(description);

        synchronized (this.scopeSpanContainerQueueMap){
            final Queue queue = this.scopeSpanContainerQueueMap.get(descriptionId);
            if(queue == null) {
                return null;
            }

            final ScopeSpanContainer container = queue.poll();
            if(container == null){
                this.scopeSpanContainerQueueMap.remove(descriptionId);
            }
            return container;
        }
    }

    public ScopeSpanContainer peek(final JUnit4ScopeDescription description) {
        final String descriptionId = buildDescriptionId(description);

        synchronized (this.scopeSpanContainerQueueMap){
            final Queue queue = this.scopeSpanContainerQueueMap.get(descriptionId);
            if(queue == null) {
                return null;
            }

            final ScopeSpanContainer container = queue.peek();
            if(container == null){
                this.scopeSpanContainerQueueMap.remove(descriptionId);
            }
            return container;
        }
    }

    public String buildDescriptionId(final JUnit4ScopeDescription description) {
        return description.getClassName()+"_"+description.getMethodName()+(description.getIteration() != null ? "_"+ description.getIteration() : "");
    }

    protected Map> getScopeSpanContainerQueueMap() {
        return this.scopeSpanContainerQueueMap;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy