forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TestNG listeners for "fail fast" and resource cleanup (apache#10195)
- add listeners for cleaning up - Mockito mocking state in all threads - FastThreadLocal state with org.apache.pulsar.* class instances in all threads - add listener for detecting leaked threads - rewrite RetryAnalyzer based on TestNG's RetryAnalyzerCount class - add tests for RetryAnalyzer - Fix test dependencies in pulsar-io/flume and pulsar-io/netty - add custom fail fast solution - Maven Surefire built-in solution is broken with TestNG 7.3.0
- Loading branch information
Showing
18 changed files
with
1,115 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
buildtools/src/main/java/org/apache/pulsar/tests/BetweenTestClassesListenerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF 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 | ||
* | ||
* http://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 org.apache.pulsar.tests; | ||
|
||
import org.testng.IClassListener; | ||
import org.testng.ITestClass; | ||
import org.testng.ITestContext; | ||
import org.testng.ITestListener; | ||
|
||
/** | ||
* TestNG listener adapter for detecting when execution finishes in previous | ||
* test class and starts in a new class. | ||
*/ | ||
abstract class BetweenTestClassesListenerAdapter implements IClassListener, ITestListener { | ||
Class<?> lastTestClass; | ||
|
||
@Override | ||
public void onBeforeClass(ITestClass testClass) { | ||
checkIfTestClassChanged(testClass.getRealClass()); | ||
} | ||
|
||
private void checkIfTestClassChanged(Class<?> testClazz) { | ||
if (lastTestClass != testClazz) { | ||
onBetweenTestClasses(lastTestClass, testClazz); | ||
lastTestClass = testClazz; | ||
} | ||
} | ||
|
||
@Override | ||
public void onFinish(ITestContext context) { | ||
if (lastTestClass != null) { | ||
onBetweenTestClasses(lastTestClass, null); | ||
lastTestClass = null; | ||
} | ||
} | ||
|
||
/** | ||
* Call back hook for adding logic when test execution moves from test class to another. | ||
* | ||
* @param endedTestClass the test class which has finished execution. null if the started test class is the first | ||
* @param startedTestClass the test class which has started execution. null if the ended test class is the last | ||
*/ | ||
protected abstract void onBetweenTestClasses(Class<?> endedTestClass, Class<?> startedTestClass); | ||
} |
85 changes: 85 additions & 0 deletions
85
buildtools/src/main/java/org/apache/pulsar/tests/FailFastNotifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF 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 | ||
* | ||
* http://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 org.apache.pulsar.tests; | ||
|
||
import org.testng.IInvokedMethod; | ||
import org.testng.IInvokedMethodListener; | ||
import org.testng.ITestResult; | ||
import org.testng.SkipException; | ||
|
||
/** | ||
* Notifies TestNG core skipping remaining tests after first failure has appeared. | ||
* | ||
* Enabled when -DtestFailFast=true | ||
* | ||
* This is a workaround for https://issues.apache.org/jira/browse/SUREFIRE-1762 since | ||
* the bug makes the built-in fast-fast feature `-Dsurefire.skipAfterFailureCount=1` unusable. | ||
* Maven Surefire version 3.0.0-M5 contains the fix, but that version is unusable because of problems | ||
* with test output, https://issues.apache.org/jira/browse/SUREFIRE-1827. | ||
* It makes the Pulsar integration tests slow and to fail. | ||
* | ||
* This implementation is based on org.apache.maven.surefire.testng.utils.FailFastNotifier | ||
* implementation that is part of the Maven Surefire plugin. | ||
* | ||
*/ | ||
public class FailFastNotifier | ||
implements IInvokedMethodListener { | ||
private static final boolean FAIL_FAST_ENABLED = Boolean.valueOf( | ||
System.getProperty("testFailFast", "true")); | ||
|
||
static class FailFastEventsSingleton { | ||
private static final FailFastEventsSingleton INSTANCE = new FailFastEventsSingleton(); | ||
|
||
private volatile boolean skipAfterFailure; | ||
|
||
private FailFastEventsSingleton() { | ||
} | ||
|
||
public static FailFastEventsSingleton getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
public boolean isSkipAfterFailure() { | ||
return skipAfterFailure; | ||
} | ||
|
||
public void setSkipOnNextTest() { | ||
this.skipAfterFailure = true; | ||
} | ||
} | ||
|
||
static class FailFastSkipException extends SkipException { | ||
FailFastSkipException(String skipMessage) { | ||
super(skipMessage); | ||
reduceStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) { | ||
if (FAIL_FAST_ENABLED && FailFastEventsSingleton.getInstance().isSkipAfterFailure()) { | ||
throw new FailFastSkipException("Skipped after failure since testFailFast system property is set."); | ||
} | ||
} | ||
|
||
@Override | ||
public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) { | ||
|
||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
buildtools/src/main/java/org/apache/pulsar/tests/FastThreadLocalCleanupListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF 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 | ||
* | ||
* http://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 org.apache.pulsar.tests; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Cleanup Thread Local state attach to Netty's FastThreadLocal. | ||
*/ | ||
public class FastThreadLocalCleanupListener extends BetweenTestClassesListenerAdapter { | ||
private static final Logger LOG = LoggerFactory.getLogger(FastThreadLocalCleanupListener.class); | ||
private static final boolean FAST_THREAD_LOCAL_CLEANUP_ENABLED = | ||
Boolean.valueOf(System.getProperty("testFastThreadLocalCleanup", "true")); | ||
private static final String FAST_THREAD_LOCAL_CLEANUP_PACKAGE = | ||
System.getProperty("testFastThreadLocalCleanupPackage", "org.apache.pulsar"); | ||
private static final FastThreadLocalStateCleaner CLEANER = new FastThreadLocalStateCleaner(object -> { | ||
if ("*".equals(FAST_THREAD_LOCAL_CLEANUP_PACKAGE)) { | ||
return true; | ||
} | ||
Class<?> clazz = object.getClass(); | ||
if (clazz.isArray()) { | ||
clazz = clazz.getComponentType(); | ||
} | ||
Package pkg = clazz.getPackage(); | ||
if (pkg != null && pkg.getName() != null) { | ||
return pkg.getName() | ||
.startsWith(FAST_THREAD_LOCAL_CLEANUP_PACKAGE); | ||
} else { | ||
return false; | ||
} | ||
}); | ||
|
||
@Override | ||
protected void onBetweenTestClasses(Class<?> endedTestClass, Class<?> startedTestClass) { | ||
if (FAST_THREAD_LOCAL_CLEANUP_ENABLED && FastThreadLocalStateCleaner.isEnabled()) { | ||
LOG.info("Cleaning up FastThreadLocal thread local state."); | ||
CLEANER.cleanupAllFastThreadLocals((thread, value) -> { | ||
LOG.info("Cleaning FastThreadLocal state for thread {}, instance of class {}, value is {}", thread, | ||
value.getClass().getName(), value); | ||
}); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.