Skip to content

Test using Random() should be seeded #7756

Closed

Description

@sdmaclea

There are many tests using unseeded Random number generators. When failures happen it is difficult to debug/repro.

Here are examples which are currently causing intermittent failures in arm64...

tests/src/JIT/Methodical/MDArray/basics/classarr.cs
tests/src/JIT/Methodical/MDArray/basics/doublearr.cs
tests/src/JIT/Methodical/MDArray/basics/jaggedarr.cs
tests/src/JIT/Methodical/MDArray/basics/stringarr.cs
tests/src/JIT/Methodical/MDArray/basics/structarr.cs

Many tests are using seeded random number generators using code in tests/src/Common/CoreCLRTestLibrary/TestFramework.cs.

It would be nice to use simialr code in place of all tests using Random() without a seed.
category:correctness
theme:testing
skill-level:beginner
cost:small

Activity

  1. sdmaclea commented on Mar 30, 2017

    @sdmaclea
    ContributorAuthor

    @jkotas Please mention correct people

  2. jkotas commented on Mar 30, 2017

    @jkotas
    Member

    cc @dotnet/jit-contrib

  3. stefannikolei commented on Apr 9, 2017

    @stefannikolei
    Contributor
            if (Environment.GetEnvironmentVariable("CORECLR_SEED") != null)
            {
                try
                {
                    seed = int.Parse(Environment.GetEnvironmentVariable("CORECLR_SEED"));
                }
                catch (FormatException) { seed = rand.Next(); }
            }
            else
            {
                seed = rand.Next();
            }
    

    You suggest to use this code on all tests which use Random?

  4. sdmaclea commented on Apr 9, 2017

    @sdmaclea
    ContributorAuthor

    @reaction1989 that code plus a fixed default seed and the code to print the seed into the trace. Fixed default seed may want to be unique per test.

  5. stefannikolei commented on Apr 10, 2017

    @stefannikolei
    Contributor

    i would tackle this. If someone wanna assign me

  6. pgavlin commented on Apr 10, 2017

    @pgavlin
    Contributor

    @reaction1989: I don't think we can formally assign you since you're not affiliated with any of the required GitHub organizations, but feel free to take this on in any case. Thanks!

  7. danmoseley commented on Jun 2, 2018

    @danmoseley
    Contributor

    @reaction1989 still interested?

  8. tannergooding commented on Jun 14, 2018

    @tannergooding
    Member

    @sdmaclea, @reaction1989. I don't believe a fixed seed is the right way to go. That causes you to lose the benefits of using RNG (which is testing arbitrary inputs behave correctly).

    The issue here is that:

    1. There is no way to tell the test the seed to use
    2. The tests do not tell you the seed that was used

    If we had a way of doing both, then it should resolve all the issues while keeping the benefits.

  9. sdmaclea commented on Jun 14, 2018

    @sdmaclea
    ContributorAuthor

    The typical usage in other tests is to use a fixed seed (i.e. the date) for CI and print the result. The seed may be from the command line. It is certainly acceptable to randomly generate a seed, print it, then use it as the seed. So that the failures can be reproduce. However, that means the logs for all failures must be available. I have not always been able to find the logs.

  10. tannergooding commented on Jun 14, 2018

    @tannergooding
    Member

    The typical usage in other tests is to use a fixed seed (i.e. the date)

    This is probably the disconnect. I wouldn't consider "the date" a fixed seed, as it could vary from run to run 😄 (I would probably say the seed comes from a fixed source instead)

    However, that means the logs for all failures must be available. I have not always been able to find the logs.

    Right, I think this is the real problem.

    Because the logs don't tell you the seed used, there is currently no chance for reproducability.

  11. sdmaclea commented on Jun 14, 2018

    @sdmaclea
    ContributorAuthor

    In the date cases, the date was the date the seed was chosen, not the current date.

  12. sdmaclea commented on Jun 14, 2018

    @sdmaclea
    ContributorAuthor

    For instance

    tests/src/Common/CoreCLRTestLibrary/TestFramework.cs:            seed = 20010415;
    
  13. tannergooding commented on Jun 14, 2018

    @tannergooding
    Member

    @sdmaclea, I don't see how that is useful. It means that you don't get any of the benefits that using random inputs would normally provide (one of which is testing values that the dev didn't consider "special" or needing explicit tests).

    At that point, why not just pick random inputs and hard code them.

  14. sdmaclea commented on Jun 14, 2018

    @sdmaclea
    ContributorAuthor

    Take a look at the file for the whole context. It allows environment variable to override the seed. It prints the seed used in the logs.... CI is not for exhaustive checking typically, it is generally for general coverage.

    If you want more exhaustive testing you can run the test with more seeds.... But that is not needed in general CI.

  15. tannergooding commented on Jun 14, 2018

    @tannergooding
    Member

    If you want more exhaustive testing you can run the test with more seeds.... But that is not needed in general CI.

    The point is that it is no more work (the same number of tests get run), but it provides "greater" coverage (as it tests more inputs over the course of multiple runs).

    The fact that there are tests without fixed seeds that are failing is "proof" that there is something wrong somewhere (some random input is causing a failure). If you change that to be a fixed seed, the inputs will never change and the tests will no longer "randomly" fail and you will miss out on the fact that something is wrong (either in the test code or in source code).

    That is why using a fixed source seed (such as DateTime.Now) combined with printing said seed to the output (for reproducability) is a better solution.

  16. 12 remaining items

  17. added
    needs-further-triage
    on Mar 23, 2021
  18. removed
    needs-further-triage
    on Mar 30, 2021
  19. modified the milestones: 6.0.0, Future on Mar 30, 2021
  20. AndyAyersMS commented on Mar 30, 2021

    @AndyAyersMS
    Member

    We've lived with this so hard to argue we have to fix it now.

    Still a good up-for-grabs issue, if somebody out there wants to pick it up.

  21. added
    Priority:3
    and removed
    JitUntriaged
    on Mar 30, 2021
  22. ADustyOldMuffin commented on Apr 6, 2021

    @ADustyOldMuffin
    Contributor

    I'd like to make my first contribution and pick this up, seems simple enough.

  23. danmoseley commented on Apr 6, 2021

    @danmoseley
    Contributor

    Welcome @ADustyOldMuffin it's all yours

  24. modified the milestones: Future, 6.0.0 on Apr 8, 2021
  25. ghost locked as resolved and limited conversation to collaborators on Jul 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

HackathonPriority:3area-CodeGen-coreclrgood first issuehelp wantedtest-enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions