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

The first test always fails with a MySQL connection error #14

Closed
derekcroft opened this issue Nov 4, 2011 · 15 comments
Closed

The first test always fails with a MySQL connection error #14

derekcroft opened this issue Nov 4, 2011 · 15 comments
Labels

Comments

@derekcroft
Copy link

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.

Loaded suite testr-worker[6] test/unit/subscription_test
Started
E........

1) Error:
test: A simple test should pass. (SubscriptionTest): 
ActiveRecord::StatementInvalid: Mysql2::Error: MySQL server has gone away: SAVEPOINT active_record_2
   config/initializers/mysql2_reconnect.rb:22:in `execute'
   test/unit/subscription_test.rb:81:in `block (2 levels) in <class:SubscriptionTest>'
@sunaku
Copy link
Owner

sunaku commented Nov 4, 2011

Interesting. If we turn off the parallelism (set TestR::Config.max_forked_workers = 1 in your .testr.rb file) does the error still occur? Also, this stackoverflow thread might be relevant:

Basically the problem is caused by the max_allowed_packet variable being set to something smaller than the blog/text field size. The query can't be executed so the connection gets dropped.

@sunaku
Copy link
Owner

sunaku commented Dec 18, 2011

@derekcroft Any update on this?

@citrus
Copy link
Contributor

citrus commented Jan 18, 2012

I'm seeing something similar with postgres on rails 3.2 pre:

   PGError: connection not open
    :           SELECT tablename
              FROM pg_tables
              WHERE schemaname = ANY (current_schemas(false))
    STDERR:

This is happening for the first test of each test file...

@citrus
Copy link
Contributor

citrus commented Jan 18, 2012

I was able to get past this by adding the following to my .testr.rb

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 :/

@sunaku
Copy link
Owner

sunaku commented Jan 18, 2012

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 TestR::Config.max_forked_workers = 1 in your .testr.rb file) does the issue still occur?

@sunaku
Copy link
Owner

sunaku commented Jan 18, 2012

@citrus I confirmed that your workaround solves the problem for Mysql completely. /cc @derekcroft

@sunaku
Copy link
Owner

sunaku commented Jan 18, 2012

@citrus I also confirmed that your workaround solves the problem completely for Postgresql.

@citrus
Copy link
Contributor

citrus commented Jan 18, 2012

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

sunaku pushed a commit that referenced this issue Jan 18, 2012
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
@citrus
Copy link
Contributor

citrus commented Jan 18, 2012

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 test_helper.rb look something like this:

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

@citrus
Copy link
Contributor

citrus commented Jan 18, 2012

Ahhh shit.. I'm still having issues with fixtures.. somewhat randomly too :/

Basically when I load testr and keep running all tests with r, I eventually get a PG deadlock error:

 test: WhateverController should get index.                              ERROR
        PGError: ERROR:  deadlock detected
        DETAIL:  Process 5211 waits for AccessExclusiveLock on relation 200824 of database 200811; blocked by process 5212.
        Process 5212 waits for AccessShareLock on relation 200814 of database 200811; blocked by process 5211.
        HINT:  See server log for query details.

I'm guessing this has something to do with using DB transactions..

@sunaku
Copy link
Owner

sunaku commented Jan 18, 2012

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.

@sunaku
Copy link
Owner

sunaku commented Jan 25, 2012

@citrus any update on this? I didn't understand why DatabaseCleaner is needed in your example since you already have transactional fixtures enabled.

@citrus
Copy link
Contributor

citrus commented Jan 25, 2012

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.

@sunaku
Copy link
Owner

sunaku commented Jan 25, 2012

Sounds good. Thanks @citrus.

@sunaku sunaku closed this as completed Jan 25, 2012
@kurko
Copy link

kurko commented Mar 19, 2012

Actually, I'm getting this error in an app that's not using tork. I'm using PostgreSQL, though.

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

No branches or pull requests

4 participants