-
Notifications
You must be signed in to change notification settings - Fork 38.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Random seed in SocketUtils #25321
Conversation
I had an issue where System.currentTimeMillis() was a bad seed when a few tests were running in parallel. I want to override the random to use nanoTime().
@jryan128 Please sign the Contributor License Agreement! Click here to manually synchronize the status of this Pull Request. See the FAQ for frequently asked questions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs unit tests
* Default uses {@code System.currentTimeMillis()} instead of {@code Random}'s default. | ||
*/ | ||
public static void setRandom(Random random) { | ||
SocketUtils.random = random; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add null validation:
Assert.notNull(random, "'random' must not be null")
;
While I understand your problem, having mutable static state which affect behaviour of static methods is very dangerous from an architectural point of view. Any code could call |
Instead of making the private static final Random random = new Random(System.nanoTime()); Would that suit your needs? |
Yes
…On Thu, Jul 2, 2020, 08:17 Sam Brannen ***@***.***> wrote:
Instead of making the Random instance configurable, we are rather
considering instantiating it as follows.
private static final Random random = new Random(System.nanoTime());
Would that suit your needs?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#25321 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAE4JYX7UMTTIHSCYCTXTJ3RZR3EVANCNFSM4OJR4VIQ>
.
|
Thanks for the PR. As mentioned above, I implemented it slightly differently in 35582de for |
Thank you so much!!
…On Fri, Jul 3, 2020 at 6:20 AM Sam Brannen ***@***.***> wrote:
Thanks for the PR.
As mentioned above, I implemented it slightly differently in 35582de
<35582de>
for 5.2.8 and 5.3 M2.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#25321 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAE4JYSZ6LKDBBWKMVVD2GLRZWWFBANCNFSM4OJR4VIQ>
.
|
Prior to this commit, SocketUtils used System.currentTimeMillis() for the seed for the java.util.Random instance used internally. The use of the milliseconds value returned by currentTimeMillis() can lead to collisions for randomly selected free ports for tests executing in parallel on the same computer. This commit therefore switches to System.nanoTime() for the Random seed used in SocketUtils in an attempt to avoid such collisions for tests executing in parallel in different JVMs on the same computer. Closes spring-projectsgh-25321
I had an issue where
System.currentTimeMillis()
was a bad seed when a few tests were running in parallel. I want to be able to override the random to usenanoTime()
.