aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2012-07-25 16:59:19 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2012-07-25 16:59:19 -0700
commit0dc356e733d5a06b2d6d7cfe643dfe7f3f3e02fd (patch)
tree75d6d96a14e51b4f634472756f773621bb1b07ed /activerecord/test
parentb5c2384a6132e3031e77e66632c07afaaa83cf91 (diff)
parent4b1bca04025a66c54e6e9d5eb6e4d4056bfa92f0 (diff)
downloadrails-0dc356e733d5a06b2d6d7cfe643dfe7f3f3e02fd.tar.gz
rails-0dc356e733d5a06b2d6d7cfe643dfe7f3f3e02fd.tar.bz2
rails-0dc356e733d5a06b2d6d7cfe643dfe7f3f3e02fd.zip
Merge pull request #6654 from stevecj/postgresql-auto-reconnect-2
Postgresql auto reconnect 2
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/adapters/postgresql/connection_test.rb72
-rw-r--r--activerecord/test/config.example.yml2
2 files changed, 74 insertions, 0 deletions
diff --git a/activerecord/test/cases/adapters/postgresql/connection_test.rb b/activerecord/test/cases/adapters/postgresql/connection_test.rb
index f823ce33d8..202f7e27c5 100644
--- a/activerecord/test/cases/adapters/postgresql/connection_test.rb
+++ b/activerecord/test/cases/adapters/postgresql/connection_test.rb
@@ -81,5 +81,77 @@ module ActiveRecord
assert_equal 'SCHEMA', @connection.logged[0][1]
end
+ def test_reconnection_after_simulated_disconnection_with_verify
+ assert @connection.active?
+ original_connection_pid = @connection.query('select pg_backend_pid()')
+
+ # Fail with bad connection after next query attempt.
+ connection_class = class << @connection ; self ; end
+ connection_class.class_eval <<-CODE
+ def query_fake(*args)
+ if @called ||= false
+ @connection.stubs(:status).returns(PCconn::CONNECTION_BAD)
+ raise PGError
+ else
+ @called = true
+ @connection.unstub(:status)
+ query_unfake(*args)
+ end
+ end
+
+ alias query_unfake query
+ alias query query_fake
+ CODE
+
+ begin
+ @connection.verify!
+ new_connection_pid = @connection.query('select pg_backend_pid()')
+ ensure
+ connection_class.class_eval <<-CODE
+ alias query query_unfake
+ undef query_fake
+ CODE
+ end
+
+ assert_equal original_connection_pid, new_connection_pid, "Should have a new underlying connection pid"
+ end
+
+ # Must have with_manual_interventions set to true for this
+ # test to run.
+ # When prompted, restart the PostgreSQL server with the
+ # "-m fast" option or kill the individual connection assuming
+ # you know the incantation to do that.
+ # To restart PostgreSQL 9.1 on OS X, installed via MacPorts, ...
+ # sudo su postgres -c "pg_ctl restart -D /opt/local/var/db/postgresql91/defaultdb/ -m fast"
+ def test_reconnection_after_actual_disconnection_with_verify
+ skip "with_manual_interventions is false in configuration" unless ARTest.config['with_manual_interventions']
+
+ original_connection_pid = @connection.query('select pg_backend_pid()')
+
+ # Sanity check.
+ assert @connection.active?
+
+ puts 'Kill the connection now (e.g. by restarting the PostgreSQL ' +
+ 'server with the "-m fast" option) and then press enter.'
+ $stdin.gets
+
+ @connection.verify!
+
+ assert @connection.active?
+
+ # If we get no exception here, then either we re-connected successfully, or
+ # we never actually got disconnected.
+ new_connection_pid = @connection.query('select pg_backend_pid()')
+
+ assert_not_equal original_connection_pid, new_connection_pid,
+ "umm -- looks like you didn't break the connection, because we're still " +
+ "successfully querying with the same connection pid."
+
+ # Repair all fixture connections so other tests won't break.
+ @fixture_connections.each do |c|
+ c.verify!
+ end
+ end
+
end
end
diff --git a/activerecord/test/config.example.yml b/activerecord/test/config.example.yml
index f450efd839..479b8c050d 100644
--- a/activerecord/test/config.example.yml
+++ b/activerecord/test/config.example.yml
@@ -1,5 +1,7 @@
default_connection: <%= defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3' %>
+with_manual_interventions: false
+
connections:
jdbcderby:
arunit: activerecord_unittest