Skip to content

Commit

Permalink
Segregate and deactivate performance flaky tests 馃敃
Browse files Browse the repository at this point in the history
Use `flaky` and `performance` JUnit `@Tag` annotations.
Cf. https://junit.org/junit5/docs/current/user-guide/#writing-tests-tagging-and-filtering

Use `@Nested` annotation when mixing common tests with performance tests.
Cf. https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested

Fixes assertj#3170.
  • Loading branch information
nicokosi committed Oct 25, 2023
1 parent 1723fac commit 33847e0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import java.util.concurrent.atomic.AtomicReference;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

// only run for perf metrics
@Disabled
@Disabled("only run on demand")
@Tag("performance")
@Tag("flaky")
class AbstractAssert_withFailMessage_performance_Test {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@
import org.assertj.core.api.AssertionInfo;
import org.assertj.core.internal.Iterables;
import org.assertj.core.internal.IterablesBaseTest;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

/**
* Tests for <code>{@link Iterables#assertDoesNotHaveDuplicates(AssertionInfo, Collection)}</code>.
* Tests for <code>{@link Iterables#assertDoesNotHaveDuplicates(AssertionInfo, Iterable)}</code>.
*
* @author Alex Ruiz
* @author Joel Costigliola
*/
class Iterables_assertDoesNotHaveDuplicates_Test extends IterablesBaseTest {

private static final int GENERATED_OBJECTS_NUMBER = 50000;
private final List<String> actual = newArrayList("Luke", "Yoda", "Leia");

@Test
Expand Down Expand Up @@ -74,18 +76,6 @@ void should_fail_if_actual_contains_duplicates() {
verify(failures).failure(info, shouldNotHaveDuplicates(actual, duplicates));
}

@Test
void should_pass_within_time_constraints() {
List<UUID> generated = Stream.generate(UUID::randomUUID).limit(GENERATED_OBJECTS_NUMBER).collect(toList());

long time = System.currentTimeMillis();
iterables.assertDoesNotHaveDuplicates(someInfo(), generated);
// check that it takes less than 2 seconds, usually it takes 100ms on an average computer
// with the previous implementation, it would take minutes ...
System.out.println("Time elapsed in ms for assertDoesNotHaveDuplicates : " + (System.currentTimeMillis() - time));
assertThat((System.currentTimeMillis() - time)).isLessThan(2000);
}

@Test
void should_fail_if_actual_contains_duplicates_array() {
Collection<String[]> actual = newArrayList(array("Luke", "Yoda"), array("Luke", "Yoda"));
Expand Down Expand Up @@ -121,17 +111,39 @@ void should_fail_if_actual_contains_duplicates_according_to_custom_comparison_st
verify(failures).failure(info, shouldNotHaveDuplicates(actual, duplicates, comparisonStrategy));
}

@Test
void should_pass_within_time_constraints_with_custom_comparison_strategy() {
List<String> generated = Stream.generate(() -> UUID.randomUUID().toString()).limit(GENERATED_OBJECTS_NUMBER)
.collect(toList());
long time = System.currentTimeMillis();
iterablesWithCaseInsensitiveComparisonStrategy.assertDoesNotHaveDuplicates(someInfo(), generated);
// check that it takes less than 10 seconds, usually it takes 1000ms on an average computer
// with the previous implementation, it would take minutes ...
System.out.println("Time elapsed in ms for assertDoesNotHaveDuplicates with custom comparison strategy : "
+ (System.currentTimeMillis() - time));
assertThat((System.currentTimeMillis() - time)).isLessThan(10000);
@Nested
@Tag("performance")
@Tag("flaky")
@Disabled("only run on demand")
class Performance {

private static final int GENERATED_OBJECTS_NUMBER = 50000;

@Test
void should_pass_within_time_constraints() {
List<UUID> generated = Stream.generate(UUID::randomUUID).limit(GENERATED_OBJECTS_NUMBER).collect(toList());

long time = System.currentTimeMillis();
iterables.assertDoesNotHaveDuplicates(someInfo(), generated);
// check that it takes less than 2 seconds, usually it takes 100ms on an average computer
// with the previous implementation, it would take minutes ...
System.out.println("Time elapsed in ms for assertDoesNotHaveDuplicates : " + (System.currentTimeMillis() - time));
assertThat((System.currentTimeMillis() - time)).isLessThan(2000);
}

@Test
void should_pass_within_time_constraints_with_custom_comparison_strategy() {
List<String> generated = Stream.generate(() -> UUID.randomUUID().toString()).limit(GENERATED_OBJECTS_NUMBER)
.collect(toList());
long time = System.currentTimeMillis();
iterablesWithCaseInsensitiveComparisonStrategy.assertDoesNotHaveDuplicates(someInfo(), generated);
// check that it takes less than 10 seconds, usually it takes 1000ms on an average computer
// with the previous implementation, it would take minutes ...
System.out.println("Time elapsed in ms for assertDoesNotHaveDuplicates with custom comparison strategy : "
+ (System.currentTimeMillis() - time));
assertThat((System.currentTimeMillis() - time)).isLessThan(10000);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@
import java.time.temporal.ChronoUnit;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Disabled("only run on demand")
@Tag("performance")
@Tag("flaky")
class TypeComparatorsPerfTest {

// execution time for 1000000:
// before change: ~700ms
// with Comparator.comparing(Class::getName) : ~240ms
// with anonymous class replacing Comparator.comparing(Class::getName) : ~160ms
@Disabled
@Test
void run_100_000_object_assertions() {
long start = System.currentTimeMillis();
Expand Down

0 comments on commit 33847e0

Please sign in to comment.