Skip to content

[Backwards Breaking change 2.22] Faker is reliably generating duplicate values  #2534

Closed
@danlo

Description

@danlo

Bug report

Versions

Faker 2.22 (Only this one, tested 2.20, 2.21), Rails 7.0.3.1
Ubuntu 20.04 LTS

Describe the bug

Sets of values are being generated in a non-random fashion.

To Reproduce

Execute the following code from a rails command line.

Rails 7.0.3.1 command line

clear; rails test test/rand1_test.rb test/rand2_test.rb

test/rand_test1.rb

# frozen_string_literal: true

require 'test_helper'

# clear; rails test test/rand_test.rb

class RandTest < ActiveSupport::TestCase
  test 'test #1' do
    $GLOBAL_CHECK={} unless defined?($GLOBAL_CHECK)
    $GLOBAL_CHECK.default = 0

    20.times do
      key = "#{Faker::Name.first_name} #{Faker::Name.last_name} #{Faker::Name.last_name}"
      $GLOBAL_CHECK[key] +=1
      puts "Duplicated1: #{key}" if $GLOBAL_CHECK[key] > 1
      puts "Test#1: #{key}"
    end
  end
end

test/rand_test2.rb

# frozen_string_literal: true

require 'test_helper'

# clear; rails test test/rand_test.rb

class Rand2Test < ActiveSupport::TestCase
  test 'test #2' do
    $GLOBAL_CHECK={} unless defined?($GLOBAL_CHECK)
    $GLOBAL_CHECK.default = 0

    20.times do
      key = "#{Faker::Name.first_name} #{Faker::Name.last_name} #{Faker::Name.last_name}"
      $GLOBAL_CHECK[key] +=1
      puts "Duplicated2: #{key}" if $GLOBAL_CHECK[key] > 1
      puts "Test#2: #{key}"
    end
  end
end

Expected behavior

Data is truly randomly generated

Additional context

This is not happening in 2.21 and only is reproducible in 2.22

The requirements for this to happen, is there MUST BE 2 rails tests (individual files).

This is due to this patch: 04d2703

I confirmed this by appending this code to the end of my test_helper.rb to monkey patch test it. I would remove the .new on end of Random and try it.

require 'faker'

module Faker
  module Config
    class << self
      def random
        @random || Random.new
      end
    end
  end
end

Suspicion(s)

I suspect, the issue, may be that the Rails test code is re-seeding the Random with the same seed at the beginning of every test case. Previously using Random.new gave Faker it's own random number generator, which was setup for the entire run of the test suite.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions