diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java b/its/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java index 3ecb4b07b7..34aee23e12 100644 --- a/its/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java @@ -85,6 +85,7 @@ public class IntegrationTestSuite // suite.addTestSuite( MavenIT0109ReleaseUpdateTest.class ); // suite.addTestSuite( MavenIT0108SnapshotUpdateTest.class ); -- MNG-3137 + suite.addTestSuite( MavenITmng4544ActiveComponentCollectionThreadSafeTest.class ); suite.addTestSuite( MavenITmng4500NoUpdateOfTimestampedSnapshotsTest.class ); suite.addTestSuite( MavenITmng4498IgnoreBrokenMetadataTest.class ); suite.addTestSuite( MavenITmng4489MirroringOfExtensionRepoTest.class ); diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng4544ActiveComponentCollectionThreadSafeTest.java b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng4544ActiveComponentCollectionThreadSafeTest.java new file mode 100644 index 0000000000..4894469223 --- /dev/null +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng4544ActiveComponentCollectionThreadSafeTest.java @@ -0,0 +1,62 @@ +package org.apache.maven.it; + +/* + * 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. + */ + +import org.apache.maven.it.Verifier; +import org.apache.maven.it.util.ResourceExtractor; + +import java.io.File; +import java.util.Properties; + +/** + * This is a test set for MNG-4544. + * + * @author Benjamin Bentmann + */ +public class MavenITmng4544ActiveComponentCollectionThreadSafeTest + extends AbstractMavenIntegrationTestCase +{ + + public MavenITmng4544ActiveComponentCollectionThreadSafeTest() + { + super( "[2.0.3,3.0-alpha-1),[3.0-alpha-7,)" ); + } + + /** + * Test that concurrent access to active component collections is thread-safe. + */ + public void testit() + throws Exception + { + File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4544" ); + + Verifier verifier = new Verifier( testDir.getAbsolutePath() ); + verifier.setAutoclean( false ); + verifier.deleteDirectory( "target" ); + verifier.executeGoal( "validate" ); + verifier.verifyErrorFreeLog(); + verifier.resetStreams(); + + Properties props = verifier.loadProperties( "target/thread.properties" ); + assertEquals( 0, Integer.parseInt( props.getProperty( "exceptions" ) ) ); + assertEquals( 2, Integer.parseInt( props.getProperty( "components" ) ) ); + } + +} diff --git a/its/core-it-suite/src/test/resources/mng-4544/pom.xml b/its/core-it-suite/src/test/resources/mng-4544/pom.xml new file mode 100644 index 0000000000..dc1ae3b4b3 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-4544/pom.xml @@ -0,0 +1,55 @@ + + + + + + 4.0.0 + + org.apache.maven.its.mng4544 + test + 0.1 + + Maven Integration Test :: MNG-4544 + + Test that concurrent access to active component collections is thread-safe. + + + + + + org.apache.maven.its.plugins + maven-it-plugin-active-collection + 2.1-SNAPSHOT + + target/thread.properties + + + + test + validate + + check-thread-safety + + + + + + + diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-active-collection/src/main/java/org/apache/maven/plugin/coreit/CheckThreadSafetyMojo.java b/its/core-it-support/core-it-plugins/maven-it-plugin-active-collection/src/main/java/org/apache/maven/plugin/coreit/CheckThreadSafetyMojo.java new file mode 100644 index 0000000000..8be16e7991 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-active-collection/src/main/java/org/apache/maven/plugin/coreit/CheckThreadSafetyMojo.java @@ -0,0 +1,188 @@ +package org.apache.maven.plugin.coreit; + +/* + * 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. + */ + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Vector; + +/** + * Checks the thread-safe retrieval of components from active component collections. + * + * @goal check-thread-safety + * @phase validate + * + * @author Benjamin Bentmann + */ +public class CheckThreadSafetyMojo + extends AbstractMojo +{ + + /** + * Project base directory used for manual path alignment. + * + * @parameter default-value="${basedir}" + * @readonly + */ + private File basedir; + + /** + * The available components, as a map. + * + * @component role="org.apache.maven.plugin.coreit.Component" + */ + private Map componentMap; + + /** + * The available components, as a list. + * + * @component role="org.apache.maven.plugin.coreit.Component" + */ + private List componentList; + + /** + * The path to the properties file to create. + * + * @parameter expression="${collections.outputFile}" + */ + private File outputFile; + + /** + * Runs this mojo. + * + * @throws MojoFailureException If the output file could not be created. + */ + public void execute() + throws MojoExecutionException + { + Properties componentProperties = new Properties(); + + getLog().info( "[MAVEN-CORE-IT-LOG] Testing concurrent component access" ); + + final Map map = componentMap; + final List list = componentList; + final List go = new Vector(); + final List exceptions = new Vector(); + + Thread[] threads = new Thread[2]; + for ( int i = 0; i < threads.length; i++ ) + { + // NOTE: The threads need to use different realms to trigger changes of the collections + final ClassLoader cl = + ( i % 2 ) == 0 ? getClass().getClassLoader() : MojoExecutionException.class.getClassLoader(); + threads[i] = new Thread() + { + private final ClassLoader tccl = cl; + + public void run() + { + getLog().info( "[MAVEN-CORE-IT-LOG] Thread " + this + " uses " + tccl ); + Thread.currentThread().setContextClassLoader( tccl ); + while ( go.isEmpty() ) + { + // wait for start + } + for ( int j = 0; j < 10000; j++ ) + { + try + { + for ( Iterator it = map.values().iterator(); it.hasNext(); ) + { + it.next().toString(); + } + for ( Iterator it = list.iterator(); it.hasNext(); ) + { + it.next().toString(); + } + } + catch ( Exception e ) + { + getLog().warn( "[MAVEN-CORE-IT-LOG] Thread " + this + " encountered concurrency issue", e ); + exceptions.add( e ); + } + } + } + }; + threads[i].start(); + } + + go.add( null ); + for ( int i = 0; i < threads.length; i++ ) + { + try + { + threads[i].join(); + } + catch ( InterruptedException e ) + { + getLog().warn( "[MAVEN-CORE-IT-LOG] Interrupted while joining " + threads[i] ); + } + } + + componentProperties.setProperty( "components", Integer.toString( componentList.size() ) ); + componentProperties.setProperty( "exceptions", Integer.toString( exceptions.size() ) ); + + if ( !outputFile.isAbsolute() ) + { + outputFile = new File( basedir, outputFile.getPath() ); + } + + getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + outputFile ); + + OutputStream out = null; + try + { + outputFile.getParentFile().mkdirs(); + out = new FileOutputStream( outputFile ); + componentProperties.store( out, "MAVEN-CORE-IT-LOG" ); + } + catch ( IOException e ) + { + throw new MojoExecutionException( "Output file could not be created: " + outputFile, e ); + } + finally + { + if ( out != null ) + { + try + { + out.close(); + } + catch ( IOException e ) + { + // just ignore + } + } + } + + getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + outputFile ); + } + +}