@@ -81,5 +81,77 @@ def test_schema_names_logs_name
8181 assert_equal 'SCHEMA' , @connection . logged [ 0 ] [ 1 ]
8282 end
8383
84+ def test_reconnection_after_simulated_disconnection_with_verify
85+ assert @connection . active?
86+ original_connection_pid = @connection . query ( 'select pg_backend_pid()' )
87+
88+ # Fail with bad connection after next query attempt.
89+ connection_class = class << @connection ; self ; end
90+ connection_class . class_eval <<-CODE
91+ def query_fake(*args)
92+ if @called ||= false
93+ @connection.stubs(:status).returns(PCconn::CONNECTION_BAD)
94+ raise PGError
95+ else
96+ @called = true
97+ @connection.unstub(:status)
98+ query_unfake(*args)
99+ end
100+ end
101+
102+ alias query_unfake query
103+ alias query query_fake
104+ CODE
105+
106+ begin
107+ @connection . verify!
108+ new_connection_pid = @connection . query ( 'select pg_backend_pid()' )
109+ ensure
110+ connection_class . class_eval <<-CODE
111+ alias query query_unfake
112+ undef query_fake
113+ CODE
114+ end
115+
116+ assert_equal original_connection_pid , new_connection_pid , "Should have a new underlying connection pid"
117+ end
118+
119+ # Must have with_manual_interventions set to true for this
120+ # test to run.
121+ # When prompted, restart the PostgreSQL server with the
122+ # "-m fast" option or kill the individual connection assuming
123+ # you know the incantation to do that.
124+ # To restart PostgreSQL 9.1 on OS X, installed via MacPorts, ...
125+ # sudo su postgres -c "pg_ctl restart -D /opt/local/var/db/postgresql91/defaultdb/ -m fast"
126+ def test_reconnection_after_actual_disconnection_with_verify
127+ skip "with_manual_interventions is false in configuration" unless ARTest . config [ 'with_manual_interventions' ]
128+
129+ original_connection_pid = @connection . query ( 'select pg_backend_pid()' )
130+
131+ # Sanity check.
132+ assert @connection . active?
133+
134+ puts 'Kill the connection now (e.g. by restarting the PostgreSQL ' +
135+ 'server with the "-m fast" option) and then press enter.'
136+ $stdin. gets
137+
138+ @connection . verify!
139+
140+ assert @connection . active?
141+
142+ # If we get no exception here, then either we re-connected successfully, or
143+ # we never actually got disconnected.
144+ new_connection_pid = @connection . query ( 'select pg_backend_pid()' )
145+
146+ assert_not_equal original_connection_pid , new_connection_pid ,
147+ "umm -- looks like you didn't break the connection, because we're still " +
148+ "successfully querying with the same connection pid."
149+
150+ # Repair all fixture connections so other tests won't break.
151+ @fixture_connections . each do |c |
152+ c . verify!
153+ end
154+ end
155+
84156 end
85157end
0 commit comments