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

only set a new random seed if _the last_ Generator gets destruct'ed #871

Open
wants to merge 2 commits into
base: 2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,13 @@ class Generator

private $container;

/**
* In order top prevent resetting the seed while another Generator instance is still alive, we keep track of the
* total, global number of Generator being "active". Incremented in __construct, decremented in __destruct.
* This is necessary, because __destruct modified global state (`mt_srand`).
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of typos here:

Suggested change
* In order top prevent resetting the seed while another Generator instance is still alive, we keep track of the
* total, global number of Generator being "active". Incremented in __construct, decremented in __destruct.
* This is necessary, because __destruct modified global state (`mt_srand`).
* In order to prevent resetting the seed while another Generator instance is still alive, we keep track of the
* total, global number of Generators being "active". Incremented in __construct, decremented in __destruct.
* This is necessary, because __destruct modifies global state (`mt_srand`).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot. Fixed them.

*/
private static int $generatorsAlive = 0;

/**
* @var UniqueGenerator
*/
Expand All @@ -568,6 +575,7 @@ class Generator
public function __construct(ContainerInterface $container = null)
{
$this->container = $container ?: Container\ContainerBuilder::withDefaultExtensions()->build();
++self::$generatorsAlive;
}

/**
Expand Down Expand Up @@ -963,7 +971,9 @@ public function __call($method, $attributes)

public function __destruct()
{
$this->seed();
if ((--self::$generatorsAlive) <= 0) {
$this->seed();
}
}

public function __wakeup()
Expand Down
21 changes: 21 additions & 0 deletions test/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Faker\Extension\BloodExtension;
use Faker\Extension\ExtensionNotFound;
use Faker\Extension\FileExtension;
use Faker\Factory;
use Faker\Generator;
use Faker\Provider;
use Faker\UniqueGenerator;
Expand Down Expand Up @@ -327,4 +328,24 @@ public function word(): string

$uniqueGenerator->word();
}

public function testDestructingOldGeneratorDoesNotResetTheSeed(): void
{
$faker = Factory::create('en_US');
$faker->seed(1);

$expected1 = $faker->numberBetween(1000, 10000);
$expected2 = $faker->numberBetween(1000, 10000);
$faker = null;

for ($i = 0; $i < 3; ++$i) {
$faker = Factory::create('en_US');
$faker->seed(1);
self::assertSame($expected1, $faker->numberBetween(1000, 10000));

gc_collect_cycles();

self::assertSame($expected2, $faker->numberBetween(1000, 10000));
}
}
}