Created
June 30, 2015 17:41
-
-
Save edpaget/9b62b74ad2b4e7a53a94 to your computer and use it in GitHub Desktop.
Postgresql Reconnect Error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
require 'bundler/inline' | |
rescue LoadError => e | |
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' | |
raise e | |
end | |
gemfile(true) do | |
source 'https://rubygems.org' | |
# Activate the gem you are reporting the issue against. | |
gem 'activerecord', '4.2.2' | |
gem 'pg' | |
end | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
# Ensure backward compatibility with Minitest 4 | |
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'test_db', username: 'test', password: 'test', host: 'localhost') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :posts, force: true do |t| | |
end | |
create_table :comments, force: true do |t| | |
t.integer :post_id | |
end | |
end | |
class Post < ActiveRecord::Base | |
has_many :comments | |
end | |
class Comment < ActiveRecord::Base | |
belongs_to :post | |
end | |
class BugTest < Minitest::Test | |
def pid | |
@pid ||= ActiveRecord::Base.connection.execute("SELECT pid FROM pg_stat_activity WHERE datname='test_db';").first['pid'] | |
end | |
def test_postgresql_reconnect_block | |
adapter = ActiveRecord::Base.connection | |
adapter.send :prepare_statement, 'SELECT 1' | |
`echo 'password' | sudo -S kill -9 #{pid}` | |
conn = adapter.instance_variable_get :@connection | |
assert_equal conn.status, 0 | |
pool = adapter.instance_variable_get :@statements | |
assert pool.send :connection_active? | |
assert_raises PG::UnableToSend do | |
adapter.reconnect! | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment