Skip to content
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

Closed
wants to merge 1 commit into from
Closed

Improve Random seed in SocketUtils #25321

wants to merge 1 commit into from

Conversation

jryan128
Copy link

@jryan128 jryan128 commented Jun 26, 2020

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 use nanoTime().

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().
@pivotal-issuemaster
Copy link

@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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 26, 2020
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels Jun 29, 2020
@sbrannen sbrannen self-assigned this Jun 29, 2020
@sbrannen sbrannen changed the title Add ability to set random in SocketUtils Add ability to set Random instance in SocketUtils Jun 29, 2020
@sbrannen sbrannen removed for: team-attention status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 29, 2020
Copy link

@NielsUll NielsUll left a 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;
Copy link

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");

@NielsUll
Copy link

NielsUll commented Jul 1, 2020

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 SocketUtils.setRandom(new FixedRandom(0.5)) and it could break the rest of the application in hard-to-find ways.

@sbrannen
Copy link
Member

sbrannen commented Jul 2, 2020

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?

@sbrannen sbrannen marked this pull request as draft July 2, 2020 12:17
@sbrannen sbrannen added the status: waiting-for-feedback We need additional information before we can continue label Jul 2, 2020
@sbrannen sbrannen added this to the 5.2.8 milestone Jul 2, 2020
@jryan128
Copy link
Author

jryan128 commented Jul 2, 2020 via email

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jul 2, 2020
@sbrannen sbrannen closed this in 35582de Jul 3, 2020
@sbrannen
Copy link
Member

sbrannen commented Jul 3, 2020

Thanks for the PR.

As mentioned above, I implemented it slightly differently in 35582de for 5.2.8 and 5.3 M2.

@sbrannen sbrannen changed the title Add ability to set Random instance in SocketUtils Improve Random seed in SocketUtils Jul 3, 2020
@jryan128
Copy link
Author

jryan128 commented Jul 6, 2020 via email

FelixFly pushed a commit to FelixFly/spring-framework that referenced this pull request Aug 16, 2020
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: feedback-provided Feedback has been provided type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants