2010-03-26 06:03:54 +03:00
|
|
|
#!/usr/bin/env python
|
2011-01-07 04:44:19 +03:00
|
|
|
|
|
|
|
# test_async.py - unit test for asynchronous API
|
|
|
|
#
|
2019-02-17 04:34:52 +03:00
|
|
|
# Copyright (C) 2010-2019 Jan Urbański <wulczer@wulczer.org>
|
2021-06-15 02:37:22 +03:00
|
|
|
# Copyright (C) 2020-2021 The Psycopg Team
|
2011-01-07 04:44:19 +03:00
|
|
|
#
|
|
|
|
# psycopg2 is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# In addition, as a special exception, the copyright holders give
|
|
|
|
# permission to link this program with the OpenSSL library (or with
|
|
|
|
# modified versions of OpenSSL that use the same license as OpenSSL),
|
|
|
|
# and distribute linked combinations including the two.
|
|
|
|
#
|
|
|
|
# You must obey the GNU Lesser General Public License in all respects for
|
|
|
|
# all of the code used other than OpenSSL.
|
|
|
|
#
|
|
|
|
# psycopg2 is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
# License for more details.
|
|
|
|
|
2019-03-16 21:56:56 +03:00
|
|
|
import gc
|
|
|
|
import time
|
2017-12-02 04:59:53 +03:00
|
|
|
import unittest
|
2019-03-16 21:56:56 +03:00
|
|
|
import warnings
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
import psycopg2
|
2019-03-08 06:24:19 +03:00
|
|
|
import psycopg2.errors
|
2017-02-16 14:04:02 +03:00
|
|
|
from psycopg2 import extensions as ext
|
2010-03-26 06:03:54 +03:00
|
|
|
|
2020-07-21 03:42:34 +03:00
|
|
|
from .testutils import (ConnectingTestCase, StringIO, skip_before_postgres,
|
2020-07-21 03:55:22 +03:00
|
|
|
skip_if_crdb, crdb_version, slow)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2020-11-17 22:37:42 +03:00
|
|
|
class PollableStub:
|
2010-04-11 04:05:31 +04:00
|
|
|
"""A 'pollable' wrapper allowing analysis of the `poll()` calls."""
|
|
|
|
def __init__(self, pollable):
|
|
|
|
self.pollable = pollable
|
|
|
|
self.polls = []
|
|
|
|
|
|
|
|
def fileno(self):
|
|
|
|
return self.pollable.fileno()
|
|
|
|
|
|
|
|
def poll(self):
|
|
|
|
rv = self.pollable.poll()
|
|
|
|
self.polls.append(rv)
|
|
|
|
return rv
|
|
|
|
|
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
class AsyncTests(ConnectingTestCase):
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
def setUp(self):
|
2013-04-07 03:23:30 +04:00
|
|
|
ConnectingTestCase.setUp(self)
|
|
|
|
|
|
|
|
self.sync_conn = self.conn
|
2017-02-03 07:28:27 +03:00
|
|
|
self.conn = self.connect(async_=True)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(self.conn)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
curs = self.conn.cursor()
|
2020-07-21 03:42:34 +03:00
|
|
|
if crdb_version(self.sync_conn) is not None:
|
|
|
|
curs.execute("set experimental_enable_temp_tables = 'on'")
|
|
|
|
self.wait(curs)
|
|
|
|
|
2010-03-26 06:03:54 +03:00
|
|
|
curs.execute('''
|
|
|
|
CREATE TEMPORARY TABLE table1 (
|
|
|
|
id int PRIMARY KEY
|
|
|
|
)''')
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(curs)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
2010-03-31 04:00:52 +04:00
|
|
|
def test_connection_setup(self):
|
2010-03-26 06:03:54 +03:00
|
|
|
cur = self.conn.cursor()
|
|
|
|
sync_cur = self.sync_conn.cursor()
|
2016-10-11 02:10:53 +03:00
|
|
|
del cur, sync_cur
|
2010-03-26 06:03:54 +03:00
|
|
|
|
2017-02-03 07:28:27 +03:00
|
|
|
self.assert_(self.conn.async_)
|
|
|
|
self.assert_(not self.sync_conn.async_)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
2017-02-16 14:04:02 +03:00
|
|
|
# the async connection should be autocommit
|
|
|
|
self.assert_(self.conn.autocommit)
|
|
|
|
self.assertEquals(self.conn.isolation_level, ext.ISOLATION_LEVEL_DEFAULT)
|
2010-03-31 04:00:52 +04:00
|
|
|
|
2010-04-21 21:40:05 +04:00
|
|
|
# check other properties to be found on the connection
|
|
|
|
self.assert_(self.conn.server_version)
|
2016-10-11 02:10:53 +03:00
|
|
|
self.assert_(self.conn.protocol_version in (2, 3))
|
2017-02-16 14:04:02 +03:00
|
|
|
self.assert_(self.conn.encoding in ext.encodings)
|
2010-04-21 21:40:05 +04:00
|
|
|
|
2010-03-31 04:00:52 +04:00
|
|
|
def test_async_named_cursor(self):
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
self.conn.cursor, "name")
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
def test_async_select(self):
|
|
|
|
cur = self.conn.cursor()
|
2010-04-20 03:50:34 +04:00
|
|
|
self.assertFalse(self.conn.isexecuting())
|
2010-03-26 06:03:54 +03:00
|
|
|
cur.execute("select 'a'")
|
2010-04-20 03:50:34 +04:00
|
|
|
self.assertTrue(self.conn.isexecuting())
|
2010-03-26 06:03:54 +03:00
|
|
|
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
2010-04-20 03:50:34 +04:00
|
|
|
self.assertFalse(self.conn.isexecuting())
|
2010-03-26 06:03:54 +03:00
|
|
|
self.assertEquals(cur.fetchone()[0], "a")
|
|
|
|
|
2017-02-02 05:58:22 +03:00
|
|
|
@slow
|
2011-02-15 20:11:07 +03:00
|
|
|
@skip_before_postgres(8, 2)
|
2010-03-26 06:03:54 +03:00
|
|
|
def test_async_callproc(self):
|
|
|
|
cur = self.conn.cursor()
|
2010-11-28 18:03:34 +03:00
|
|
|
cur.callproc("pg_sleep", (0.1, ))
|
|
|
|
self.assertTrue(self.conn.isexecuting())
|
|
|
|
|
|
|
|
self.wait(cur)
|
|
|
|
self.assertFalse(self.conn.isexecuting())
|
2010-03-26 06:03:54 +03:00
|
|
|
|
2017-02-02 05:58:22 +03:00
|
|
|
@slow
|
2010-03-26 06:03:54 +03:00
|
|
|
def test_async_after_async(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur2 = self.conn.cursor()
|
2016-10-11 02:10:53 +03:00
|
|
|
del cur2
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
cur.execute("insert into table1 values (1)")
|
|
|
|
|
2010-03-31 04:00:52 +04:00
|
|
|
# an async execute after an async one raises an exception
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
cur.execute, "select * from table1")
|
|
|
|
# same for callproc
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
cur.callproc, "version")
|
|
|
|
# but after you've waited it should be good
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
cur.execute("select * from table1")
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
self.assertEquals(cur.fetchall()[0][0], 1)
|
|
|
|
|
|
|
|
cur.execute("delete from table1")
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
cur.execute("select * from table1")
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
self.assertEquals(cur.fetchone(), None)
|
|
|
|
|
|
|
|
def test_fetch_after_async(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select 'a'")
|
|
|
|
|
2010-03-31 04:00:52 +04:00
|
|
|
# a fetch after an asynchronous query should raise an error
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
cur.fetchall)
|
|
|
|
# but after waiting it should work
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
self.assertEquals(cur.fetchall()[0][0], "a")
|
|
|
|
|
|
|
|
def test_rollback_while_async(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
|
|
|
|
cur.execute("select 'a'")
|
|
|
|
|
2010-03-31 04:00:52 +04:00
|
|
|
# a rollback should not work in asynchronous mode
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, self.conn.rollback)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
def test_commit_while_async(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
|
2010-03-31 04:00:52 +04:00
|
|
|
cur.execute("begin")
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-31 04:00:52 +04:00
|
|
|
|
2010-03-26 06:03:54 +03:00
|
|
|
cur.execute("insert into table1 values (1)")
|
|
|
|
|
2010-03-31 04:00:52 +04:00
|
|
|
# a commit should not work in asynchronous mode
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, self.conn.commit)
|
2010-04-20 03:50:34 +04:00
|
|
|
self.assertTrue(self.conn.isexecuting())
|
2010-03-31 04:00:52 +04:00
|
|
|
|
|
|
|
# but a manual commit should
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-31 04:00:52 +04:00
|
|
|
cur.execute("commit")
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
cur.execute("select * from table1")
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
self.assertEquals(cur.fetchall()[0][0], 1)
|
|
|
|
|
|
|
|
cur.execute("delete from table1")
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
cur.execute("select * from table1")
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
self.assertEquals(cur.fetchone(), None)
|
|
|
|
|
|
|
|
def test_set_parameters_while_async(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
|
|
|
|
cur.execute("select 'c'")
|
2010-04-20 03:50:34 +04:00
|
|
|
self.assertTrue(self.conn.isexecuting())
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
# getting transaction status works
|
2018-10-13 05:28:42 +03:00
|
|
|
self.assertEquals(self.conn.info.transaction_status,
|
2017-02-16 14:04:02 +03:00
|
|
|
ext.TRANSACTION_STATUS_ACTIVE)
|
2010-04-20 03:50:34 +04:00
|
|
|
self.assertTrue(self.conn.isexecuting())
|
2010-03-26 06:03:54 +03:00
|
|
|
|
2010-03-31 04:00:52 +04:00
|
|
|
# setting connection encoding should fail
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
self.conn.set_client_encoding, "LATIN1")
|
2010-03-26 06:03:54 +03:00
|
|
|
|
2010-03-31 04:00:52 +04:00
|
|
|
# same for transaction isolation
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
self.conn.set_isolation_level, 1)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
def test_reset_while_async(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select 'c'")
|
2010-04-20 03:50:34 +04:00
|
|
|
self.assertTrue(self.conn.isexecuting())
|
2010-03-26 06:03:54 +03:00
|
|
|
|
2010-03-31 04:00:52 +04:00
|
|
|
# a reset should fail
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, self.conn.reset)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
def test_async_iter(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
|
2010-03-31 04:00:52 +04:00
|
|
|
cur.execute("begin")
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-11-19 06:55:37 +03:00
|
|
|
cur.execute("""
|
|
|
|
insert into table1 values (1);
|
|
|
|
insert into table1 values (2);
|
|
|
|
insert into table1 values (3);
|
|
|
|
""")
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
cur.execute("select id from table1 order by id")
|
|
|
|
|
2010-03-31 04:00:52 +04:00
|
|
|
# iteration fails if a query is underway
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, list, cur)
|
|
|
|
|
|
|
|
# but after it's done it should work
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
self.assertEquals(list(cur), [(1, ), (2, ), (3, )])
|
2010-04-20 03:50:34 +04:00
|
|
|
self.assertFalse(self.conn.isexecuting())
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
def test_copy_while_async(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select 'a'")
|
|
|
|
|
2010-03-31 04:00:52 +04:00
|
|
|
# copy should fail
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
cur.copy_from,
|
2017-12-04 05:47:19 +03:00
|
|
|
StringIO("1\n3\n5\n\\.\n"), "table1")
|
2010-03-26 06:03:54 +03:00
|
|
|
|
2010-03-31 04:00:52 +04:00
|
|
|
def test_lobject_while_async(self):
|
|
|
|
# large objects should be prohibited
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
self.conn.lobject)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
def test_async_executemany(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
self.assertRaises(
|
|
|
|
psycopg2.ProgrammingError,
|
|
|
|
cur.executemany, "insert into table1 values (%s)", [1, 2, 3])
|
|
|
|
|
|
|
|
def test_async_scroll(self):
|
|
|
|
cur = self.conn.cursor()
|
2010-11-19 06:55:37 +03:00
|
|
|
cur.execute("""
|
|
|
|
insert into table1 values (1);
|
|
|
|
insert into table1 values (2);
|
|
|
|
insert into table1 values (3);
|
|
|
|
""")
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
cur.execute("select id from table1 order by id")
|
|
|
|
|
2010-03-31 04:00:52 +04:00
|
|
|
# scroll should fail if a query is underway
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, cur.scroll, 1)
|
2010-04-20 03:50:34 +04:00
|
|
|
self.assertTrue(self.conn.isexecuting())
|
2010-03-31 04:00:52 +04:00
|
|
|
|
|
|
|
# but after it's done it should work
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
cur.scroll(1)
|
|
|
|
self.assertEquals(cur.fetchall(), [(2, ), (3, )])
|
|
|
|
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select id from table1 order by id")
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
cur2 = self.conn.cursor()
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, cur2.scroll, 1)
|
|
|
|
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, cur.scroll, 4)
|
|
|
|
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select id from table1 order by id")
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-31 04:00:52 +04:00
|
|
|
cur.scroll(2)
|
|
|
|
cur.scroll(-1)
|
|
|
|
self.assertEquals(cur.fetchall(), [(2, ), (3, )])
|
|
|
|
|
|
|
|
def test_scroll(self):
|
|
|
|
cur = self.sync_conn.cursor()
|
|
|
|
cur.execute("create table table1 (id int)")
|
2010-11-19 06:55:37 +03:00
|
|
|
cur.execute("""
|
|
|
|
insert into table1 values (1);
|
|
|
|
insert into table1 values (2);
|
|
|
|
insert into table1 values (3);
|
|
|
|
""")
|
2010-03-31 04:00:52 +04:00
|
|
|
cur.execute("select id from table1 order by id")
|
2010-03-26 06:03:54 +03:00
|
|
|
cur.scroll(2)
|
|
|
|
cur.scroll(-1)
|
|
|
|
self.assertEquals(cur.fetchall(), [(2, ), (3, )])
|
|
|
|
|
|
|
|
def test_async_dont_read_all(self):
|
|
|
|
cur = self.conn.cursor()
|
2010-03-31 04:00:52 +04:00
|
|
|
cur.execute("select repeat('a', 10000); select repeat('b', 10000)")
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
# fetch the result
|
2010-04-10 20:54:49 +04:00
|
|
|
self.wait(cur)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
|
|
|
# it should be the result of the second query
|
2010-03-31 04:00:52 +04:00
|
|
|
self.assertEquals(cur.fetchone()[0], "b" * 10000)
|
2010-03-26 06:03:54 +03:00
|
|
|
|
2010-04-08 15:51:33 +04:00
|
|
|
def test_async_subclass(self):
|
2017-02-16 14:04:02 +03:00
|
|
|
class MyConn(ext.connection):
|
2017-02-03 07:28:27 +03:00
|
|
|
def __init__(self, dsn, async_=0):
|
2017-02-16 14:04:02 +03:00
|
|
|
ext.connection.__init__(self, dsn, async_=async_)
|
2010-04-08 15:51:33 +04:00
|
|
|
|
2017-02-03 07:28:27 +03:00
|
|
|
conn = self.connect(connection_factory=MyConn, async_=True)
|
2010-04-08 15:51:33 +04:00
|
|
|
self.assert_(isinstance(conn, MyConn))
|
2017-02-03 07:28:27 +03:00
|
|
|
self.assert_(conn.async_)
|
2010-04-08 15:51:33 +04:00
|
|
|
conn.close()
|
|
|
|
|
2017-02-02 04:53:50 +03:00
|
|
|
@slow
|
2020-07-28 00:58:43 +03:00
|
|
|
@skip_if_crdb("flush on write flakey")
|
2010-04-11 04:05:31 +04:00
|
|
|
def test_flush_on_write(self):
|
|
|
|
# a very large query requires a flush loop to be sent to the backend
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
for mb in 1, 5, 10, 20, 50:
|
|
|
|
size = mb * 1024 * 1024
|
2010-04-18 17:51:58 +04:00
|
|
|
stub = PollableStub(self.conn)
|
2010-04-11 04:05:31 +04:00
|
|
|
curs.execute("select %s;", ('x' * size,))
|
|
|
|
self.wait(stub)
|
|
|
|
self.assertEqual(size, len(curs.fetchone()[0]))
|
2017-02-16 14:04:02 +03:00
|
|
|
if stub.polls.count(ext.POLL_WRITE) > 1:
|
2010-04-11 04:05:31 +04:00
|
|
|
return
|
|
|
|
|
2010-11-25 06:02:58 +03:00
|
|
|
# This is more a testing glitch than an error: it happens
|
|
|
|
# on high load on linux: probably because the kernel has more
|
|
|
|
# buffers ready. A warning may be useful during development,
|
|
|
|
# but an error is bad during regression testing.
|
|
|
|
warnings.warn("sending a large query didn't trigger block on write.")
|
2010-04-11 04:05:31 +04:00
|
|
|
|
2010-04-10 20:51:13 +04:00
|
|
|
def test_sync_poll(self):
|
|
|
|
cur = self.sync_conn.cursor()
|
2010-04-18 17:51:58 +04:00
|
|
|
cur.execute("select 1")
|
|
|
|
# polling with a sync query works
|
|
|
|
cur.connection.poll()
|
|
|
|
self.assertEquals(cur.fetchone()[0], 1)
|
2010-04-10 20:51:13 +04:00
|
|
|
|
2019-10-19 18:29:16 +03:00
|
|
|
@slow
|
2020-08-17 23:27:25 +03:00
|
|
|
@skip_if_crdb("notify")
|
2010-04-18 17:51:58 +04:00
|
|
|
def test_notify(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
sync_cur = self.sync_conn.cursor()
|
2010-04-10 20:51:13 +04:00
|
|
|
|
2010-04-18 17:51:58 +04:00
|
|
|
sync_cur.execute("listen test_notify")
|
|
|
|
self.sync_conn.commit()
|
|
|
|
cur.execute("notify test_notify")
|
|
|
|
self.wait(cur)
|
2010-04-10 20:51:13 +04:00
|
|
|
|
2010-04-18 17:51:58 +04:00
|
|
|
self.assertEquals(self.sync_conn.notifies, [])
|
|
|
|
|
2018-10-13 05:28:42 +03:00
|
|
|
pid = self.conn.info.backend_pid
|
2010-04-18 17:51:58 +04:00
|
|
|
for _ in range(5):
|
|
|
|
self.wait(self.sync_conn)
|
|
|
|
if not self.sync_conn.notifies:
|
|
|
|
time.sleep(0.5)
|
|
|
|
continue
|
|
|
|
self.assertEquals(len(self.sync_conn.notifies), 1)
|
|
|
|
self.assertEquals(self.sync_conn.notifies.pop(),
|
|
|
|
(pid, "test_notify"))
|
|
|
|
return
|
|
|
|
self.fail("No NOTIFY in 2.5 seconds")
|
2010-04-10 20:51:13 +04:00
|
|
|
|
|
|
|
def test_async_fetch_wrong_cursor(self):
|
|
|
|
cur1 = self.conn.cursor()
|
|
|
|
cur2 = self.conn.cursor()
|
|
|
|
cur1.execute("select 1")
|
|
|
|
|
2010-04-14 12:01:37 +04:00
|
|
|
self.wait(cur1)
|
2010-04-20 03:50:34 +04:00
|
|
|
self.assertFalse(self.conn.isexecuting())
|
2010-04-10 20:51:13 +04:00
|
|
|
# fetching from a cursor with no results is an error
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, cur2.fetchone)
|
|
|
|
# fetching from the correct cursor works
|
|
|
|
self.assertEquals(cur1.fetchone()[0], 1)
|
|
|
|
|
2022-03-28 19:44:50 +03:00
|
|
|
@skip_if_crdb("batch statements", version="< 22.1")
|
2010-04-11 22:25:17 +04:00
|
|
|
def test_error(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("insert into table1 values (%s)", (1, ))
|
|
|
|
self.wait(cur)
|
|
|
|
cur.execute("insert into table1 values (%s)", (1, ))
|
|
|
|
# this should fail
|
|
|
|
self.assertRaises(psycopg2.IntegrityError, self.wait, cur)
|
|
|
|
cur.execute("insert into table1 values (%s); "
|
|
|
|
"insert into table1 values (%s)", (2, 2))
|
2020-07-21 03:55:22 +03:00
|
|
|
# this should fail as well (Postgres behaviour)
|
2010-04-11 22:25:17 +04:00
|
|
|
self.assertRaises(psycopg2.IntegrityError, self.wait, cur)
|
|
|
|
# but this should work
|
2022-03-28 19:44:50 +03:00
|
|
|
cur.execute("insert into table1 values (%s)", (2, ))
|
|
|
|
self.wait(cur)
|
2010-04-11 22:25:17 +04:00
|
|
|
# and the cursor should be usable afterwards
|
|
|
|
cur.execute("insert into table1 values (%s)", (3, ))
|
|
|
|
self.wait(cur)
|
|
|
|
cur.execute("select * from table1 order by id")
|
|
|
|
self.wait(cur)
|
|
|
|
self.assertEquals(cur.fetchall(), [(1, ), (2, ), (3, )])
|
|
|
|
cur.execute("delete from table1")
|
|
|
|
self.wait(cur)
|
|
|
|
|
2019-03-08 06:24:19 +03:00
|
|
|
def test_stop_on_first_error(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select 1; select x; select 1/0; select 2")
|
|
|
|
self.assertRaises(psycopg2.errors.UndefinedColumn, self.wait, cur)
|
|
|
|
|
|
|
|
cur.execute("select 1")
|
|
|
|
self.wait(cur)
|
|
|
|
self.assertEqual(cur.fetchone(), (1,))
|
|
|
|
|
2010-04-12 01:12:01 +04:00
|
|
|
def test_error_two_cursors(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur2 = self.conn.cursor()
|
|
|
|
cur.execute("select * from no_such_table")
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, self.wait, cur)
|
|
|
|
cur2.execute("select 1")
|
|
|
|
self.wait(cur2)
|
|
|
|
self.assertEquals(cur2.fetchone()[0], 1)
|
|
|
|
|
2020-08-17 23:27:25 +03:00
|
|
|
@skip_if_crdb("notice")
|
2010-04-20 20:52:05 +04:00
|
|
|
def test_notices(self):
|
|
|
|
del self.conn.notices[:]
|
|
|
|
cur = self.conn.cursor()
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version >= 90300:
|
2013-03-15 21:10:13 +04:00
|
|
|
cur.execute("set client_min_messages=debug1")
|
|
|
|
self.wait(cur)
|
2010-04-20 20:52:05 +04:00
|
|
|
cur.execute("create temp table chatty (id serial primary key);")
|
|
|
|
self.wait(cur)
|
|
|
|
self.assertEqual("CREATE TABLE", cur.statusmessage)
|
|
|
|
self.assert_(self.conn.notices)
|
|
|
|
|
2011-01-03 15:41:36 +03:00
|
|
|
def test_async_cursor_gone(self):
|
|
|
|
cur = self.conn.cursor()
|
2016-10-11 02:10:53 +03:00
|
|
|
cur.execute("select 42;")
|
2011-01-03 15:41:36 +03:00
|
|
|
del cur
|
2011-12-27 01:35:33 +04:00
|
|
|
gc.collect()
|
2011-01-03 15:41:36 +03:00
|
|
|
self.assertRaises(psycopg2.InterfaceError, self.wait, self.conn)
|
|
|
|
|
|
|
|
# The connection is still usable
|
|
|
|
cur = self.conn.cursor()
|
2016-10-11 02:10:53 +03:00
|
|
|
cur.execute("select 42;")
|
2011-01-03 15:41:36 +03:00
|
|
|
self.wait(self.conn)
|
|
|
|
self.assertEqual(cur.fetchone(), (42,))
|
|
|
|
|
2020-08-17 23:27:25 +03:00
|
|
|
@skip_if_crdb("copy")
|
2013-10-16 18:28:16 +04:00
|
|
|
def test_async_connection_error_message(self):
|
|
|
|
try:
|
2017-02-03 07:28:27 +03:00
|
|
|
cnn = psycopg2.connect('dbname=thisdatabasedoesntexist', async_=True)
|
2013-10-16 18:28:16 +04:00
|
|
|
self.wait(cnn)
|
2017-11-21 07:00:35 +03:00
|
|
|
except psycopg2.Error as e:
|
2013-10-16 18:28:16 +04:00
|
|
|
self.assertNotEqual(str(e), "asynchronous connection failed",
|
|
|
|
"connection error reason lost")
|
|
|
|
else:
|
|
|
|
self.fail("no exception raised")
|
|
|
|
|
2018-10-11 01:23:56 +03:00
|
|
|
@skip_before_postgres(8, 2)
|
|
|
|
def test_copy_no_hang(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("copy (select 1) to stdout")
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, self.wait, self.conn)
|
|
|
|
|
2019-03-08 06:24:19 +03:00
|
|
|
@slow
|
2020-08-17 23:27:25 +03:00
|
|
|
@skip_if_crdb("notice")
|
2019-03-08 06:24:19 +03:00
|
|
|
@skip_before_postgres(9, 0)
|
|
|
|
def test_non_block_after_notification(self):
|
|
|
|
from select import select
|
|
|
|
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("""
|
|
|
|
select 1;
|
|
|
|
do $$
|
|
|
|
begin
|
|
|
|
raise notice 'hello';
|
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
|
|
|
select pg_sleep(1);
|
|
|
|
""")
|
|
|
|
|
|
|
|
polls = 0
|
|
|
|
while True:
|
|
|
|
state = self.conn.poll()
|
|
|
|
if state == psycopg2.extensions.POLL_OK:
|
|
|
|
break
|
|
|
|
elif state == psycopg2.extensions.POLL_READ:
|
|
|
|
select([self.conn], [], [], 0.1)
|
|
|
|
elif state == psycopg2.extensions.POLL_WRITE:
|
|
|
|
select([], [self.conn], [], 0.1)
|
|
|
|
else:
|
|
|
|
raise Exception("Unexpected result from poll: %r", state)
|
|
|
|
polls += 1
|
|
|
|
|
|
|
|
self.assert_(polls >= 8, polls)
|
|
|
|
|
2019-04-05 20:04:55 +03:00
|
|
|
def test_poll_noop(self):
|
|
|
|
self.conn.poll()
|
2019-04-05 20:24:19 +03:00
|
|
|
|
2020-08-17 23:27:25 +03:00
|
|
|
@skip_if_crdb("notify")
|
2019-04-05 20:24:19 +03:00
|
|
|
@skip_before_postgres(9, 0)
|
|
|
|
def test_poll_conn_for_notification(self):
|
|
|
|
with self.conn.cursor() as cur:
|
|
|
|
cur.execute("listen test")
|
|
|
|
self.wait(cur)
|
|
|
|
|
|
|
|
with self.sync_conn.cursor() as cur:
|
|
|
|
cur.execute("notify test, 'hello'")
|
|
|
|
self.sync_conn.commit()
|
|
|
|
|
|
|
|
for i in range(10):
|
|
|
|
self.conn.poll()
|
|
|
|
|
|
|
|
if self.conn.notifies:
|
|
|
|
n = self.conn.notifies.pop()
|
|
|
|
self.assertEqual(n.channel, 'test')
|
|
|
|
self.assertEqual(n.payload, 'hello')
|
|
|
|
break
|
|
|
|
time.sleep(0.1)
|
|
|
|
else:
|
|
|
|
self.fail("No notification received")
|
2019-04-05 20:04:55 +03:00
|
|
|
|
2020-07-21 04:15:53 +03:00
|
|
|
def test_close(self):
|
|
|
|
self.conn.close()
|
|
|
|
self.assertTrue(self.conn.closed)
|
|
|
|
self.assertTrue(self.conn.async_)
|
|
|
|
|
2010-04-20 20:52:05 +04:00
|
|
|
|
2010-03-26 06:03:54 +03:00
|
|
|
def test_suite():
|
|
|
|
return unittest.TestLoader().loadTestsFromName(__name__)
|
2010-04-11 03:06:54 +04:00
|
|
|
|
2018-10-23 02:39:14 +03:00
|
|
|
|
2010-04-11 03:06:54 +04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|