aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters/postgresql/connection_test.rb
blob: 5c15de703956eb6d3c2acb5447ba421d92a175fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require "cases/helper"

module ActiveRecord
  class PostgresqlConnectionTest < ActiveRecord::TestCase
    def setup
      super
      @connection = ActiveRecord::Base.connection
    end

    def test_encoding
      assert_not_nil @connection.encoding
    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 on next query attempt.
      raw_connection = @connection.raw_connection
      raw_connection_class = class << raw_connection ; self ; end
      raw_connection_class.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def query_fake(*args)
          if !( @called ||= false )
            self.stubs(:status).returns(PGconn::CONNECTION_BAD)
            @called = true
            raise PGError
          else
            self.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
        raw_connection_class.class_eval <<-CODE
          alias query query_unfake
          undef query_fake
        CODE
      end

      assert_not_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