-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use same default seed for method and class ordering (#3821)
Prior to this commit the default seeds were generated separately but the configuration parameter that allows using a fixed seed only allowed to set both to the same value making it impossible to reproduce a failure for different default seeds. Since the default seed is now identical, this scenario is avoided. Fixes #3817. (cherry picked from commit a422c5a)
- Loading branch information
1 parent
d29e3eb
commit 6c663b1
Showing
5 changed files
with
74 additions
and
75 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
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
53 changes: 53 additions & 0 deletions
53
junit-jupiter-api/src/main/java/org/junit/jupiter/api/RandomOrdererUtils.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,53 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.jupiter.api; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
import org.junit.platform.commons.logging.Logger; | ||
|
||
/** | ||
* Shared utility methods for ordering test classes and test methods randomly. | ||
* | ||
* @since 5.11 | ||
* @see ClassOrderer.Random | ||
* @see MethodOrderer.Random | ||
*/ | ||
class RandomOrdererUtils { | ||
|
||
static final String RANDOM_SEED_PROPERTY_NAME = "junit.jupiter.execution.order.random.seed"; | ||
|
||
static final long DEFAULT_SEED = System.nanoTime(); | ||
|
||
static Long getSeed(Function<String, Optional<String>> configurationParameterLookup, Logger logger) { | ||
return getCustomSeed(configurationParameterLookup, logger).orElse(DEFAULT_SEED); | ||
} | ||
|
||
private static Optional<Long> getCustomSeed(Function<String, Optional<String>> configurationParameterLookup, | ||
Logger logger) { | ||
return configurationParameterLookup.apply(RANDOM_SEED_PROPERTY_NAME).map(configurationParameter -> { | ||
try { | ||
logger.config(() -> String.format("Using custom seed for configuration parameter [%s] with value [%s].", | ||
RANDOM_SEED_PROPERTY_NAME, configurationParameter)); | ||
return Long.valueOf(configurationParameter); | ||
} | ||
catch (NumberFormatException ex) { | ||
logger.warn(ex, | ||
() -> String.format( | ||
"Failed to convert configuration parameter [%s] with value [%s] to a long. " | ||
+ "Using default seed [%s] as fallback.", | ||
RANDOM_SEED_PROPERTY_NAME, configurationParameter, DEFAULT_SEED)); | ||
return null; | ||
} | ||
}); | ||
} | ||
} |
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