mirror of https://github.com/apache/maven.git
[MNG-4544] Concurrent access to Maven internals involving active component collections is not thread-safe
o Added IT git-svn-id: https://svn.apache.org/repos/asf/maven/core-integration-testing/trunk@902426 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b081b4aca2
commit
58996c48bc
|
@ -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 );
|
||||
|
|
|
@ -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 <a href="http://jira.codehaus.org/browse/MNG-4544">MNG-4544</a>.
|
||||
*
|
||||
* @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" ) ) );
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!--
|
||||
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.
|
||||
-->
|
||||
|
||||
<project>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.apache.maven.its.mng4544</groupId>
|
||||
<artifactId>test</artifactId>
|
||||
<version>0.1</version>
|
||||
|
||||
<name>Maven Integration Test :: MNG-4544</name>
|
||||
<description>
|
||||
Test that concurrent access to active component collections is thread-safe.
|
||||
</description>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.its.plugins</groupId>
|
||||
<artifactId>maven-it-plugin-active-collection</artifactId>
|
||||
<version>2.1-SNAPSHOT</version>
|
||||
<configuration>
|
||||
<outputFile>target/thread.properties</outputFile>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>test</id>
|
||||
<phase>validate</phase>
|
||||
<goals>
|
||||
<goal>check-thread-safety</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -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 );
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue