0

I've got an executable that runs my automated tests. Today I ran 4 sets of tests by running a .bat file that ran them all simultaneously.

The tests were building policies in our software. We got a SQL error that was caused by two instances entering the same policy holder information at the same time.

In each case the test randomly selects from a list of over 1500 names and addresses, so it's possible, though unlikely, that they both selected the same record.

What's baffling is that both also entered the same driver's license number, which is randomly generated in each case.

This has never happened before in 100's/1000's of runs.

Is it possible for two instances running the same executable file could somehow cross contaminate each other?

        Random rnd      = new Random();

        else if (Crawlspace.DLState == "NEW MEXICO")
            {
                int DLNum = rnd.Next(100000000, 999999999);
                Crawlspace.DLNumber = DLNum.ToString();

The tests were built this way to ensure a unique record in every case.

HeadlyvonNoggin
  • 313
  • 1
  • 3
  • 14
  • are you giving each thread a different seed to the random? – BugFinder Jun 20 '19 at 12:30
  • 2
    Random does not guarantee unique. People do actually win the lottery occasionally... $$$ – Idle_Mind Jun 20 '19 at 12:30
  • Where is the definition of `rnd`? Is this an instance of the `Random` class? The default seed is time-dependent and there is no guaratnee of a unique seed across threads. – Martin Jun 20 '19 at 12:31
  • BugFinder, the same executable is being run for each instance, so the code is the same for all. I guess what I'm looking to understand is how exactly two separate instances each running in it's own thread could cross over each other like this. – HeadlyvonNoggin Jun 20 '19 at 12:32
  • 1
    Imagine you and i are rolling a dice. If you roll a 6 with your dice, will it preclude me from rolling a 6? No. Does it mean that something between our two rolls "crossed over" if i roll a 6, too? No. –  Jun 20 '19 at 12:32
  • Elgonzo and Idle_Mind, I understand it's possible, but highly improbable, that both runs would select the same record from the external file AND in that same run, both generate the same random number between 100000000 and 999999999. So, it would seem, that these two instances aren't totally independent of each other. I just want to understand how exactly they can bump into each other. – HeadlyvonNoggin Jun 20 '19 at 12:34
  • In both cases the variables for each field in the policy holder creation process were populated using a method called "SeleniumFramework.populateVariables()." Is it possible for two instances on two different threads to mix up and use the same variables for both? All variables are set in a class called 'Crawlspace', so each are the same.. (ex. Crawlspace.DriverName, Crawlspace.StreetAddress, Crawlspace.DLNumber) – HeadlyvonNoggin Jun 20 '19 at 12:40
  • 1
    If you use the `Random` without a custom seed it'll use the system time as random seed. If you create 2 instances at the same time there's a good chance of them getting the same system time and therefor the same seed value. If they start with the same seed value they'll both generate the same random values. Thus selecting the same record and magically creating the same random driver's license –  Jun 20 '19 at 12:45

1 Answers1

-1

Thank you to Knoop. That explains what I wasn't understanding.

To fix, I will use this line to randomly generate the seed as well...

Random rand = new Random(Guid.NewGuid().GetHashCode());

(got this from a another very similar question)

HeadlyvonNoggin
  • 313
  • 1
  • 3
  • 14
  • If you solved your problem, you should edit an explanation here and mark it as the accepted answer. – scatter Jun 20 '19 at 12:47
  • This isn't an answer. Please make it an answer or delete it. – Enigmativity Jun 20 '19 at 12:48
  • 1
    Your implementation could still result in a conflict. As you are using the hashcode of a GUID, there is no guarantee that two different GUIDs will not result in the same hashcode and therefore the same seed - https://stackoverflow.com/questions/7326593/guid-gethashcode-uniqueness. – Kami Jun 20 '19 at 12:56
  • Cool, thank you Kami. Clearly I have a lot to learn. – HeadlyvonNoggin Jun 20 '19 at 13:16