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

Pararallel testing #94

Closed
yourivdlans opened this issue Mar 6, 2019 · 9 comments · Fixed by #102
Closed

Pararallel testing #94

yourivdlans opened this issue Mar 6, 2019 · 9 comments · Fixed by #102

Comments

@yourivdlans
Copy link

After creating a new Rails 6 project and writing some tests I found some tests to fail. After disabling parallelize(workers: :number_of_processors) the tests all pass like they should.

When writing regular minitest tests these issues don't arise when having parallelize enabled.

How to reproduce:

https://gist.github.com/yourivdlans/cd159ffd016d5560ce644f68daa5111e

I've seen testcases fail on "Database is locked" and "ActiveRecord::ActiveRecordError: Cannot expire connection, it is owned by a different thread".

@barrettkingram
Copy link

I dug into this a bit and I think this is an issue with minitest itself. When using parallel testing, Rails creates multiple processes and uses DRb to pass the test information to each of them (see this active_support method). Each one uses a separate database. But this requires the ability to Marshal and un-Marshal tests. Minitest Specs can't be marshalled (I've opened up an issue for this - minitest/minitest#794). This may be causing multiple tests to get run on the master process at the same time, creating the database contention issues you're seeing.

@blowmage
Copy link

FWIW, I have a fix for this in blowmage/minitest-rails#218

@barrettkingram
Copy link

I've opened a PR in minitest attempting to fix this (minitest/minitest#797). In the meantime it might be useful to do something like the fix in blowmage/minitest-rails#218 until we can get it resolved upstream.

@metaskills
Copy link
Owner

I did that fix upstream in Mintiest. I'd like to see a PR locally just to determine if the patching we have to do is similar to Mike's. Is anyone up for doing one? TIA.

@shime
Copy link
Contributor

shime commented Nov 10, 2019

Applying @blowmage's fix from https://github.com/blowmage/minitest-rails/pull/218/files to this project fixes the issue. I'd submit a PR but I'm having problems running a test suite locally.

@metaskills
Copy link
Owner

Master is now clean and ready to run tests again locally.

@saturnflyer
Copy link

I ran into this issue a few days ago and just added this to my test_helper.rb which fixed it:

# HACK: stolen and altered from https://github.com/blowmage/minitest-rails/pull/218/files
# Which was referenced in https://github.com/metaskills/minitest-spec-rails/issues/94
module Kernel #:nodoc:
  alias describe_before_minitest_spec_constant_fix describe
  private :describe_before_minitest_spec_constant_fix
  def describe *args, &block
    cls = describe_before_minitest_spec_constant_fix(*args, &block)
    cls_const = "Test__#{cls.name.to_s.split(/\W/).reject(&:empty?).join('_'.freeze)}"
    if block.source_location
      source_path, line_num = block.source_location
      source_path = Pathname.new(source_path).relative_path_from(Rails.root).to_s
      source_path = source_path.split(/\W/).reject(&:empty?).join("_".freeze)
      cls_const += "__#{source_path}__#{line_num}"
    end
    cls_const += "_1" while Object.const_defined? cls_const
    Object.const_set cls_const, cls
    cls
  end
end

This worked and is definitely copy/paste problem solving.

@metaskills
Copy link
Owner

Thanks a ton y'all for the issue and all the code, Jim, Mike, etc. Can y'all try v6.0.2 of this gem and let me know if that helps?

@evolve2k
Copy link

evolve2k commented Feb 10, 2022

@metaskills fwiw I added the gem at v6.1.0 and this resolved the issue for me. So Im guessing ur work in v6.0.2 worked.

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants