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
The first test always fails with a MySQL connection error #14
Comments
Interesting. If we turn off the parallelism (set
|
@derekcroft Any update on this? |
I'm seeing something similar with postgres on rails 3.2 pre:
This is happening for the first test of each test file... |
I was able to get past this by adding the following to my TestR::Config.before_fork_hooks << lambda do |worker_number, log_file, test_file, test_names|
defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect!
end
TestR::Config.after_fork_hooks << lambda do |worker_number, log_file, test_file, test_names|
defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection
end Now I'm having trouble with duplicate fixture data :/ |
Thanks for your report @citrus. Since this issue was originally reported, I have wondered if [re-establishing DB connections in forked workers](http://modrails.com/documentation/Users guide Apache.html#_example_1_memcached_connection_sharing_harmful) was necessary in TestR. Your report has cleared my doubt. 🎁 Regarding duplicate fixture data, perhaps we need to make Rails apply fixtures once in the test_helper so that they are inherited into forked workers. Also, if we turn off the parallelism (set |
@citrus I confirmed that your workaround solves the problem for Mysql completely. /cc @derekcroft |
@citrus I also confirmed that your workaround solves the problem completely for Postgresql. |
Awesome, glad I could help! As far as the fixtures go, I ended up using database_cleaner in my ActiveSupport::TestCase's teardown method to wipe the db after each test... Had some oddities but worked ok for the most part. Loading fixtures once per fork would make sense however and would alleviate the need for another dependency. In the mean time I'll try turning off parallelism and will report my findings. Cheers! -Spencer |
This fixes the following connection errors in Mysql and Postgresql: Mysql2::Error: MySQL server has gone away PGError: connection not open And from a forked resource sharing viewpoint, it is the proper solution: http://modrails.com/documentation/Users%20guide%20Apache.html#_example_1_memcached_connection_sharing_harmful
Thanks for adding my patch @sunaku! I fixed my fixture problems by forcing my ActiveSupport::TestCase to use transactional fixtures with database cleaner making my ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "database_cleaner"
DatabaseCleaner.strategy = :transaction
class ActiveSupport::TestCase
self.use_transactional_fixtures = true
def teardown
DatabaseCleaner.clean
end
end Let me know if that works for you too... -Spencer |
Ahhh shit.. I'm still having issues with fixtures.. somewhat randomly too :/ Basically when I load testr and keep running all tests with
I'm guessing this has something to do with using DB transactions.. |
I don't understand why DatabaseCleaner is needed when you're using transactional fixtures: any changes you make to the DB in your test will be rolled back at exit anyway. |
@citrus any update on this? I didn't understand why DatabaseCleaner is needed in your example since you already have transactional fixtures enabled. |
Sorry for the delay @sunaku.. I've been trying to create a demo app that replicates the issue... Let's close this issue for now and I'll open a new one if I can get consistently failing results. |
Sounds good. Thanks @citrus. |
Actually, I'm getting this error in an app that's not using tork. I'm using PostgreSQL, though. |
The first test that runs tries to use an old MySQL connection and fails, then subsequent tests pass. This happens in every file, every time.
The text was updated successfully, but these errors were encountered: