psycopg2/tests
Greg Ward ac14957fbb Always detect when a connection is closed behind psycopg2's back.
There's a race condition that only seems to happen over Unix-domain
sockets. Sometimes, the closed socket is reported by the kernel to
libpq like this (captured with strace):

  sendto(3, "Q\0\0\0\34select pg_backend_pid()\0", 29, MSG_NOSIGNAL, NULL, 0) = 29
  recvfrom(3, "E\0\0\0mSFATAL\0C57P01\0Mterminating "..., 16384, 0, NULL, NULL) = 110
  recvfrom(3, 0x12d0330, 16384, 0, 0, 0)  = -1 ECONNRESET (Connection reset by peer)

That is, psycopg2/libpq sees no error when sending the first query
after the connection is closed, but gets an error reading the result.
In that case, everything worked fine.

But sometimes, the error manifests like this:

  sendto(3, "Q\0\0\0\34select pg_backend_pid()\0", 29, MSG_NOSIGNAL, NULL, 0) = -1 EPIPE (Broken pipe)
  recvfrom(3, "E\0\0\0mSFATAL\0C57P01\0Mterminating "..., 16384, 0, NULL, NULL) = 110
  recvfrom(3, "", 16274, 0, NULL, NULL)   = 0
  recvfrom(3, "", 16274, 0, NULL, NULL)   = 0

i.e. libpq received an error when sending the query. This manifests as
a slightly different exception from a slightly different place. More
importantly, in this case connection.closed is left at 0 rather than
being set to 2, and that is the bug I'm fixing here.

Note that we see almost identical behaviour for sync and async
connections, and the fixes are the same. So I added extremely similar
test cases.

Finally, there is still a bug here: for async connections, we
sometimes raise DatabaseError (incorrect) and sometimes raise
OperationalError (correct). Will fix that next.
2016-06-28 17:46:21 -04:00
..
__init__.py Fixed race condition on import in errorcodes.lookup 2015-12-16 12:00:52 +00:00
dbapi20_tpc.py hook up two phase commit tests. 2010-11-05 09:34:47 +00:00
dbapi20.py Fix several typos 2015-05-03 10:00:11 +01:00
test_async.py Added select.select timeout on AsyncTests.wait 2014-05-19 12:15:52 +01:00
test_bug_gc.py Testing boilerplate unified in a single base class 2013-04-07 00:23:30 +01:00
test_bugX000.py All unit test modules have a test_ prefix 2011-10-19 02:20:43 +01:00
test_cancel.py Testing boilerplate unified in a single base class 2013-04-07 00:23:30 +01:00
test_connection.py Report NotSupportedError for PGRES_COPY_BOTH and PGRES_SINGLE_TUPLE 2015-09-30 12:24:47 +01:00
test_copy.py Propagate read error messages in COPY FROM 2015-02-08 01:42:21 +00:00
test_cursor.py Always detect when a connection is closed behind psycopg2's back. 2016-06-28 17:46:21 -04:00
test_dates.py More tests tweaks 2015-02-08 12:51:36 +00:00
test_errcodes.py Fixed race condition on import in errorcodes.lookup 2015-12-16 12:00:52 +00:00
test_extras_dictcursor.py Testing boilerplate unified in a single base class 2013-04-07 00:23:30 +01:00
test_green.py Testing boilerplate unified in a single base class 2013-04-07 00:23:30 +01:00
test_lobject.py Accept overflow errors testing for LO64 funcs 2015-02-08 02:04:41 +00:00
test_module.py Added test to verify _psycopg can be imported 2014-08-28 02:17:49 +01:00
test_notify.py Testing external script independent from psycopg implementation 2013-04-19 23:01:28 +01:00
test_psycopg2_dbapi20.py Pulled down changes from dvarrazzo branch on gh 2011-02-10 15:59:31 -07:00
test_quote.py Testing boilerplate unified in a single base class 2013-04-07 00:23:30 +01:00
test_transaction.py Testing boilerplate unified in a single base class 2013-04-07 00:23:30 +01:00
test_types_basic.py Skip null array test on Postgres versions not supporting it 2015-06-14 19:04:04 +01:00
test_types_extras.py Clear adapters in test to maintain reference count 2014-08-28 02:17:54 +01:00
test_with.py More tests tweaks 2015-02-08 12:51:36 +00:00
testconfig.py Report NotSupportedError for PGRES_COPY_BOTH and PGRES_SINGLE_TUPLE 2015-09-30 12:24:47 +01:00
testutils.py Report NotSupportedError for PGRES_COPY_BOTH and PGRES_SINGLE_TUPLE 2015-09-30 12:24:47 +01:00