Adding retry to some tests because of async index updates

Adding tryAssert function that does repeatedly run the assert
function, because JCR Oak uses asynchronous index updates and changes
are not reflected immediately in the index.
This commit is contained in:
Martin Stockhammer 2017-09-19 13:20:54 +02:00
parent d9beaa1d2b
commit 3ae101f7a9
1 changed files with 92 additions and 63 deletions

View File

@ -19,39 +19,11 @@ package org.apache.archiva.metadata.repository;
* under the License. * under the License.
*/ */
import static org.assertj.core.api.Assertions.assertThat; import junit.framework.AssertionFailedError;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.archiva.metadata.generic.GenericMetadataFacet; import org.apache.archiva.metadata.generic.GenericMetadataFacet;
import org.apache.archiva.metadata.generic.GenericMetadataFacetFactory; import org.apache.archiva.metadata.generic.GenericMetadataFacetFactory;
import org.apache.archiva.metadata.model.ArtifactMetadata; import org.apache.archiva.metadata.model.*;
import org.apache.archiva.metadata.model.CiManagement;
import org.apache.archiva.metadata.model.Dependency;
import org.apache.archiva.metadata.model.IssueManagement;
import org.apache.archiva.metadata.model.License;
import org.apache.archiva.metadata.model.MailingList;
import org.apache.archiva.metadata.model.MetadataFacet;
import org.apache.archiva.metadata.model.MetadataFacetFactory;
import org.apache.archiva.metadata.model.Organization;
import org.apache.archiva.metadata.model.ProjectMetadata;
import org.apache.archiva.metadata.model.ProjectVersionMetadata;
import org.apache.archiva.metadata.model.ProjectVersionReference;
import org.apache.archiva.metadata.model.Scm;
import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
import org.assertj.core.util.Sets; import org.assertj.core.util.Sets;
import org.junit.Test; import org.junit.Test;
@ -60,6 +32,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import java.text.SimpleDateFormat;
import java.util.*;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith( ArchivaSpringJUnit4ClassRunner.class ) @RunWith( ArchivaSpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } ) @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
public abstract class AbstractMetadataRepositoryTest public abstract class AbstractMetadataRepositoryTest
@ -101,6 +78,46 @@ public abstract class AbstractMetadataRepositoryTest
protected Logger log = LoggerFactory.getLogger( getClass() ); protected Logger log = LoggerFactory.getLogger( getClass() );
/*
* Used by tryAssert to allow to throw exceptions in the lambda expression.
*/
@FunctionalInterface
private interface AssertFunction {
void accept() throws Exception;
}
private void tryAssert(AssertFunction func) throws Exception {
tryAssert(func, 5, 500);
}
/*
* Runs the assert method until the assert is successful or the number of retries
* is reached. Needed because the JCR Oak index update is asynchronous, so updates
* may not be visible immediately after the modification.
*/
private void tryAssert(AssertFunction func, int retries, int sleepMillis) throws Exception {
Throwable t = null;
int retry = retries;
while(retry-->0) {
try {
func.accept();
return;
} catch (Exception|AssertionError e ) {
t = e;
Thread.currentThread().sleep(sleepMillis);
log.warn("Retrying assert "+retry);
}
}
if (retry<=0 && t!=null) {
if (t instanceof RuntimeException) {
throw (RuntimeException)t;
} else if (t instanceof Exception) {
throw (Exception)t;
}
}
}
public static Map<String, MetadataFacetFactory> createTestMetadataFacetFactories() public static Map<String, MetadataFacetFactory> createTestMetadataFacetFactories()
{ {
Map<String, MetadataFacetFactory> factories = new HashMap<>(); Map<String, MetadataFacetFactory> factories = new HashMap<>();
@ -895,7 +912,7 @@ public abstract class AbstractMetadataRepositoryTest
List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ); List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
assertEquals( Collections.singletonList( artifact ), artifacts ); tryAssert(()->assertEquals( Collections.singletonList( artifact ), artifacts ));
} }
@Test @Test
@ -981,9 +998,10 @@ public abstract class AbstractMetadataRepositoryTest
repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
repository.save(); repository.save();
tryAssert(()-> {
List<ArtifactMetadata> artifacts = repository.getArtifacts(TEST_REPO_ID); List<ArtifactMetadata> artifacts = repository.getArtifacts(TEST_REPO_ID);
assertEquals(Collections.singletonList(artifact), artifacts); assertEquals(Collections.singletonList(artifact), artifacts);
});
} }
@Test @Test
@ -999,10 +1017,11 @@ public abstract class AbstractMetadataRepositoryTest
repository.save(); repository.save();
// test it restricts to the appropriate repository // test it restricts to the appropriate repository
assertEquals( Collections.singletonList( artifact ), repository.getArtifacts( TEST_REPO_ID ) ); tryAssert(() -> assertEquals( Collections.singletonList( artifact ), repository.getArtifacts( TEST_REPO_ID ) ));
assertEquals( Collections.singletonList( secondArtifact ), repository.getArtifacts( OTHER_REPO_ID ) ); tryAssert(() -> assertEquals( Collections.singletonList( secondArtifact ), repository.getArtifacts( OTHER_REPO_ID ) ));
} }
@Test @Test
public void testGetArtifactsByDateRangeMultipleCopies() public void testGetArtifactsByDateRangeMultipleCopies()
throws Exception throws Exception
@ -1154,10 +1173,10 @@ public abstract class AbstractMetadataRepositoryTest
repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
repository.save(); repository.save();
assertEquals( Collections.singletonList( artifact ), tryAssert(()->assertEquals( Collections.singletonList( artifact ),
new ArrayList<>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) ) ); new ArrayList<>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) ) ));
assertEquals( Collections.singletonList( artifact ), tryAssert(()->assertEquals( Collections.singletonList( artifact ),
new ArrayList<>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) ) ); new ArrayList<>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) ) ) );
} }
@Test @Test
@ -1173,14 +1192,18 @@ public abstract class AbstractMetadataRepositoryTest
repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 ); repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 );
repository.save(); repository.save();
tryAssert(()-> {
List<ArtifactMetadata> artifacts = List<ArtifactMetadata> artifacts =
new ArrayList<>(repository.getArtifactsByChecksum(TEST_REPO_ID, TEST_SHA1)); new ArrayList<>(repository.getArtifactsByChecksum(TEST_REPO_ID, TEST_SHA1));
Collections.sort(artifacts, new ArtifactMetadataComparator()); Collections.sort(artifacts, new ArtifactMetadataComparator());
assertEquals(Arrays.asList(artifact2, artifact1), artifacts); assertEquals(Arrays.asList(artifact2, artifact1), artifacts);
});
artifacts = new ArrayList<>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) ); tryAssert(() -> {
ArrayList<ArtifactMetadata> artifacts = new ArrayList<>(repository.getArtifactsByChecksum(TEST_REPO_ID, TEST_MD5));
Collections.sort(artifacts, new ArtifactMetadataComparator()); Collections.sort(artifacts, new ArtifactMetadataComparator());
assertEquals(Arrays.asList(artifact2, artifact1), artifacts); assertEquals(Arrays.asList(artifact2, artifact1), artifacts);
});
} }
@Test @Test
@ -1235,6 +1258,7 @@ public abstract class AbstractMetadataRepositoryTest
throws Exception throws Exception
{ {
createArtifactWithMavenArtifactFacet(); createArtifactWithMavenArtifactFacet();
tryAssert(()-> {
Collection<ArtifactMetadata> artifactsByMetadata = Collection<ArtifactMetadata> artifactsByMetadata =
repository.getArtifactsByMetadata("foo", TEST_METADATA_VALUE, null); repository.getArtifactsByMetadata("foo", TEST_METADATA_VALUE, null);
assertThat(artifactsByMetadata).hasSize(1); assertThat(artifactsByMetadata).hasSize(1);
@ -1245,6 +1269,7 @@ public abstract class AbstractMetadataRepositoryTest
MetadataFacet facet = artifactMetadata.getFacet(TEST_FACET_ID); MetadataFacet facet = artifactMetadata.getFacet(TEST_FACET_ID);
assertThat(facet).isNotNull(); assertThat(facet).isNotNull();
assertThat(facet.toProperties()).isEqualTo(Collections.singletonMap("foo", TEST_METADATA_VALUE)); assertThat(facet.toProperties()).isEqualTo(Collections.singletonMap("foo", TEST_METADATA_VALUE));
});
} }
@Test @Test
@ -1288,11 +1313,13 @@ public abstract class AbstractMetadataRepositoryTest
List<ArtifactMetadata> expected = Arrays.asList( artifact1, artifact2 ); List<ArtifactMetadata> expected = Arrays.asList( artifact1, artifact2 );
Collections.sort( expected, new ArtifactMetadataComparator() ); Collections.sort( expected, new ArtifactMetadataComparator() );
tryAssert(()-> {
List<ArtifactMetadata> actual = List<ArtifactMetadata> actual =
new ArrayList<>( repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) ); new ArrayList<>( repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
Collections.sort( actual, new ArtifactMetadataComparator() ); Collections.sort( actual, new ArtifactMetadataComparator() );
assertEquals(expected, actual); assertEquals(expected, actual);
});
repository.removeRepository( TEST_REPO_ID ); repository.removeRepository( TEST_REPO_ID );
@ -1527,8 +1554,10 @@ public abstract class AbstractMetadataRepositoryTest
throws Exception throws Exception
{ {
createArtifactWithGenericMetadataFacet(); createArtifactWithGenericMetadataFacet();
tryAssert(()-> {
Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( TEST_METADATA_KEY, TEST_METADATA_VALUE, null, false ); Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( TEST_METADATA_KEY, TEST_METADATA_VALUE, null, false );
assertThat( artifactsByProperty ).isNotNull().isNotEmpty(); assertThat( artifactsByProperty ).isNotNull().isNotEmpty();
});
} }
@Test @Test