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

org.junit.jupiter.engine.extension.SeparateThreadTimeoutInvocation Maven / Gradle / Ivy

There is a newer version: 5.11.3
Show newest version
/*
 * Copyright 2015-2022 the original author or authors.
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v2.0 which
 * accompanies this distribution and is available at
 *
 * https://www.eclipse.org/legal/epl-v20.html
 */

package org.junit.jupiter.engine.extension;

import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;

import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;

import org.junit.jupiter.api.extension.InvocationInterceptor.Invocation;
import org.opentest4j.AssertionFailedError;

/**
 * @since 5.9
 */
class SeparateThreadTimeoutInvocation implements Invocation {

	private final Invocation delegate;
	private final TimeoutDuration timeout;
	private final Supplier descriptionSupplier;

	SeparateThreadTimeoutInvocation(Invocation delegate, TimeoutDuration timeout,
			Supplier descriptionSupplier) {
		this.delegate = delegate;
		this.timeout = timeout;
		this.descriptionSupplier = descriptionSupplier;
	}

	@Override
	public T proceed() throws Throwable {
		try {
			return assertTimeoutPreemptively(timeout.toDuration(), delegate::proceed, descriptionSupplier);
		}
		catch (AssertionFailedError failure) {
			TimeoutException exception = TimeoutExceptionFactory.create(descriptionSupplier.get(), timeout, null);
			exception.initCause(failure.getCause());
			throw exception;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy