2010-04-20 21:17:27 +04:00
|
|
|
#!/usr/bin/env python
|
2011-01-07 04:44:19 +03:00
|
|
|
|
|
|
|
# test_notify.py - unit test for async notifications
|
|
|
|
#
|
2019-02-17 04:34:52 +03:00
|
|
|
# Copyright (C) 2010-2019 Daniele Varrazzo <daniele.varrazzo@gmail.com>
|
2020-01-18 00:10:44 +03:00
|
|
|
# Copyright (C) 2020 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.
|
|
|
|
|
2017-12-02 04:59:53 +03:00
|
|
|
import unittest
|
2019-03-16 18:30:15 +03:00
|
|
|
from collections import deque
|
2010-04-20 21:17:27 +04:00
|
|
|
|
|
|
|
import psycopg2
|
|
|
|
from psycopg2 import extensions
|
2019-03-16 18:30:15 +03:00
|
|
|
from psycopg2.extensions import Notify
|
2017-12-04 05:47:19 +03:00
|
|
|
from .testutils import ConnectingTestCase, slow
|
|
|
|
from .testconfig import dsn
|
2010-04-20 21:17:27 +04:00
|
|
|
|
2010-12-21 07:58:38 +03:00
|
|
|
import sys
|
2010-04-20 21:17:27 +04:00
|
|
|
import time
|
|
|
|
import select
|
2010-10-15 20:14:47 +04:00
|
|
|
from subprocess import Popen, PIPE
|
2010-04-20 21:17:27 +04:00
|
|
|
|
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
class NotifiesTests(ConnectingTestCase):
|
2010-04-20 21:17:27 +04:00
|
|
|
|
|
|
|
def autocommit(self, conn):
|
|
|
|
"""Set a connection in autocommit mode."""
|
|
|
|
conn.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)
|
|
|
|
|
|
|
|
def listen(self, name):
|
|
|
|
"""Start listening for a name on self.conn."""
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
curs.execute("LISTEN " + name)
|
|
|
|
curs.close()
|
|
|
|
|
2010-10-15 12:42:44 +04:00
|
|
|
def notify(self, name, sec=0, payload=None):
|
2010-04-20 21:17:27 +04:00
|
|
|
"""Send a notification to the database, eventually after some time."""
|
2010-10-15 12:42:44 +04:00
|
|
|
if payload is None:
|
|
|
|
payload = ''
|
|
|
|
else:
|
|
|
|
payload = ", %r" % payload
|
|
|
|
|
2010-04-20 21:17:27 +04:00
|
|
|
script = ("""\
|
|
|
|
import time
|
|
|
|
time.sleep(%(sec)s)
|
2013-04-10 01:05:17 +04:00
|
|
|
import %(module)s as psycopg2
|
|
|
|
import %(module)s.extensions as ext
|
2010-04-20 21:17:27 +04:00
|
|
|
conn = psycopg2.connect(%(dsn)r)
|
2013-04-10 01:05:17 +04:00
|
|
|
conn.set_isolation_level(ext.ISOLATION_LEVEL_AUTOCOMMIT)
|
2018-10-13 05:28:42 +03:00
|
|
|
print(conn.info.backend_pid)
|
2010-04-20 21:17:27 +04:00
|
|
|
curs = conn.cursor()
|
2010-10-15 12:42:44 +04:00
|
|
|
curs.execute("NOTIFY " %(name)r %(payload)r)
|
2010-04-20 21:17:27 +04:00
|
|
|
curs.close()
|
|
|
|
conn.close()
|
2013-04-10 01:05:17 +04:00
|
|
|
""" % {
|
2016-10-11 02:10:53 +03:00
|
|
|
'module': psycopg2.__name__,
|
|
|
|
'dsn': dsn, 'sec': sec, 'name': name, 'payload': payload})
|
2010-04-20 21:17:27 +04:00
|
|
|
|
2017-11-29 07:58:41 +03:00
|
|
|
return Popen([sys.executable, '-c', script], stdout=PIPE)
|
2010-04-20 21:17:27 +04:00
|
|
|
|
2017-02-02 04:53:50 +03:00
|
|
|
@slow
|
2010-04-20 21:17:27 +04:00
|
|
|
def test_notifies_received_on_poll(self):
|
|
|
|
self.autocommit(self.conn)
|
|
|
|
self.listen('foo')
|
|
|
|
|
2010-10-15 20:14:47 +04:00
|
|
|
proc = self.notify('foo', 1)
|
2010-04-20 21:17:27 +04:00
|
|
|
|
|
|
|
t0 = time.time()
|
2016-10-11 02:10:53 +03:00
|
|
|
select.select([self.conn], [], [], 5)
|
2010-04-20 21:17:27 +04:00
|
|
|
t1 = time.time()
|
2010-04-22 20:43:50 +04:00
|
|
|
self.assert_(0.99 < t1 - t0 < 4, t1 - t0)
|
2010-04-20 21:17:27 +04:00
|
|
|
|
2010-10-15 20:14:47 +04:00
|
|
|
pid = int(proc.communicate()[0])
|
2010-04-20 21:17:27 +04:00
|
|
|
self.assertEqual(0, len(self.conn.notifies))
|
|
|
|
self.assertEqual(extensions.POLL_OK, self.conn.poll())
|
|
|
|
self.assertEqual(1, len(self.conn.notifies))
|
2010-10-15 20:14:47 +04:00
|
|
|
self.assertEqual(pid, self.conn.notifies[0][0])
|
2010-04-20 21:17:27 +04:00
|
|
|
self.assertEqual('foo', self.conn.notifies[0][1])
|
|
|
|
|
2017-02-02 04:53:50 +03:00
|
|
|
@slow
|
2010-04-20 21:17:27 +04:00
|
|
|
def test_many_notifies(self):
|
|
|
|
self.autocommit(self.conn)
|
|
|
|
for name in ['foo', 'bar', 'baz']:
|
|
|
|
self.listen(name)
|
|
|
|
|
2010-10-15 20:14:47 +04:00
|
|
|
pids = {}
|
2010-04-20 21:17:27 +04:00
|
|
|
for name in ['foo', 'bar', 'baz', 'qux']:
|
2010-10-15 20:14:47 +04:00
|
|
|
pids[name] = int(self.notify(name).communicate()[0])
|
2010-04-20 21:17:27 +04:00
|
|
|
|
|
|
|
self.assertEqual(0, len(self.conn.notifies))
|
2010-11-11 04:39:46 +03:00
|
|
|
for i in range(10):
|
|
|
|
self.assertEqual(extensions.POLL_OK, self.conn.poll())
|
2010-04-20 21:17:27 +04:00
|
|
|
self.assertEqual(3, len(self.conn.notifies))
|
2010-10-15 20:14:47 +04:00
|
|
|
|
|
|
|
names = dict.fromkeys(['foo', 'bar', 'baz'])
|
|
|
|
for (pid, name) in self.conn.notifies:
|
|
|
|
self.assertEqual(pids[name], pid)
|
2016-10-11 02:10:53 +03:00
|
|
|
names.pop(name) # raise if name found twice
|
2010-04-20 21:17:27 +04:00
|
|
|
|
2017-02-02 05:58:22 +03:00
|
|
|
@slow
|
2010-04-20 21:17:27 +04:00
|
|
|
def test_notifies_received_on_execute(self):
|
|
|
|
self.autocommit(self.conn)
|
|
|
|
self.listen('foo')
|
2010-10-15 20:14:47 +04:00
|
|
|
pid = int(self.notify('foo').communicate()[0])
|
2010-04-20 21:17:27 +04:00
|
|
|
self.assertEqual(0, len(self.conn.notifies))
|
|
|
|
self.conn.cursor().execute('select 1;')
|
|
|
|
self.assertEqual(1, len(self.conn.notifies))
|
2010-10-15 20:14:47 +04:00
|
|
|
self.assertEqual(pid, self.conn.notifies[0][0])
|
2010-04-20 21:17:27 +04:00
|
|
|
self.assertEqual('foo', self.conn.notifies[0][1])
|
|
|
|
|
2017-02-02 04:53:50 +03:00
|
|
|
@slow
|
2010-10-16 02:09:47 +04:00
|
|
|
def test_notify_object(self):
|
|
|
|
self.autocommit(self.conn)
|
|
|
|
self.listen('foo')
|
|
|
|
self.notify('foo').communicate()
|
2011-02-20 22:33:22 +03:00
|
|
|
time.sleep(0.5)
|
2010-10-16 02:09:47 +04:00
|
|
|
self.conn.poll()
|
|
|
|
notify = self.conn.notifies[0]
|
|
|
|
self.assert_(isinstance(notify, psycopg2.extensions.Notify))
|
|
|
|
|
2017-02-02 04:53:50 +03:00
|
|
|
@slow
|
2010-10-15 12:42:44 +04:00
|
|
|
def test_notify_attributes(self):
|
|
|
|
self.autocommit(self.conn)
|
|
|
|
self.listen('foo')
|
|
|
|
pid = int(self.notify('foo').communicate()[0])
|
2011-02-20 22:33:22 +03:00
|
|
|
time.sleep(0.5)
|
2010-10-15 12:42:44 +04:00
|
|
|
self.conn.poll()
|
|
|
|
self.assertEqual(1, len(self.conn.notifies))
|
|
|
|
notify = self.conn.notifies[0]
|
|
|
|
self.assertEqual(pid, notify.pid)
|
|
|
|
self.assertEqual('foo', notify.channel)
|
|
|
|
self.assertEqual('', notify.payload)
|
|
|
|
|
2017-02-02 04:53:50 +03:00
|
|
|
@slow
|
2010-10-15 12:42:44 +04:00
|
|
|
def test_notify_payload(self):
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version < 90000:
|
2010-11-19 06:55:37 +03:00
|
|
|
return self.skipTest("server version %s doesn't support notify payload"
|
2018-10-13 05:28:42 +03:00
|
|
|
% self.conn.info.server_version)
|
2010-10-15 12:42:44 +04:00
|
|
|
self.autocommit(self.conn)
|
|
|
|
self.listen('foo')
|
|
|
|
pid = int(self.notify('foo', payload="Hello, world!").communicate()[0])
|
2011-02-20 22:33:22 +03:00
|
|
|
time.sleep(0.5)
|
2010-10-15 12:42:44 +04:00
|
|
|
self.conn.poll()
|
|
|
|
self.assertEqual(1, len(self.conn.notifies))
|
|
|
|
notify = self.conn.notifies[0]
|
|
|
|
self.assertEqual(pid, notify.pid)
|
|
|
|
self.assertEqual('foo', notify.channel)
|
|
|
|
self.assertEqual('Hello, world!', notify.payload)
|
|
|
|
|
2017-02-02 04:53:50 +03:00
|
|
|
@slow
|
2015-06-02 19:02:04 +03:00
|
|
|
def test_notify_deque(self):
|
|
|
|
self.autocommit(self.conn)
|
|
|
|
self.conn.notifies = deque()
|
|
|
|
self.listen('foo')
|
|
|
|
self.notify('foo').communicate()
|
|
|
|
time.sleep(0.5)
|
|
|
|
self.conn.poll()
|
|
|
|
notify = self.conn.notifies.popleft()
|
|
|
|
self.assert_(isinstance(notify, psycopg2.extensions.Notify))
|
|
|
|
self.assertEqual(len(self.conn.notifies), 0)
|
|
|
|
|
2017-02-02 04:53:50 +03:00
|
|
|
@slow
|
2015-06-02 19:02:04 +03:00
|
|
|
def test_notify_noappend(self):
|
|
|
|
self.autocommit(self.conn)
|
|
|
|
self.conn.notifies = None
|
|
|
|
self.listen('foo')
|
|
|
|
self.notify('foo').communicate()
|
|
|
|
time.sleep(0.5)
|
|
|
|
self.conn.poll()
|
|
|
|
self.assertEqual(self.conn.notifies, None)
|
|
|
|
|
2010-10-16 02:09:47 +04:00
|
|
|
def test_notify_init(self):
|
|
|
|
n = psycopg2.extensions.Notify(10, 'foo')
|
|
|
|
self.assertEqual(10, n.pid)
|
|
|
|
self.assertEqual('foo', n.channel)
|
2010-11-02 01:25:15 +03:00
|
|
|
self.assertEqual('', n.payload)
|
2010-10-16 02:09:47 +04:00
|
|
|
(pid, channel) = n
|
|
|
|
self.assertEqual((pid, channel), (10, 'foo'))
|
|
|
|
|
|
|
|
n = psycopg2.extensions.Notify(42, 'bar', 'baz')
|
|
|
|
self.assertEqual(42, n.pid)
|
|
|
|
self.assertEqual('bar', n.channel)
|
|
|
|
self.assertEqual('baz', n.payload)
|
|
|
|
(pid, channel) = n
|
|
|
|
self.assertEqual((pid, channel), (42, 'bar'))
|
|
|
|
|
2010-11-02 00:56:53 +03:00
|
|
|
def test_compare(self):
|
|
|
|
data = [(10, 'foo'), (20, 'foo'), (10, 'foo', 'bar'), (10, 'foo', 'baz')]
|
|
|
|
for d1 in data:
|
|
|
|
for d2 in data:
|
|
|
|
n1 = psycopg2.extensions.Notify(*d1)
|
|
|
|
n2 = psycopg2.extensions.Notify(*d2)
|
|
|
|
self.assertEqual((n1 == n2), (d1 == d2))
|
|
|
|
self.assertEqual((n1 != n2), (d1 != d2))
|
|
|
|
|
|
|
|
def test_compare_tuple(self):
|
|
|
|
self.assertEqual((10, 'foo'), Notify(10, 'foo'))
|
|
|
|
self.assertEqual((10, 'foo'), Notify(10, 'foo', 'bar'))
|
|
|
|
self.assertNotEqual((10, 'foo'), Notify(20, 'foo'))
|
|
|
|
self.assertNotEqual((10, 'foo'), Notify(10, 'bar'))
|
|
|
|
|
2010-11-02 01:27:11 +03:00
|
|
|
def test_hash(self):
|
|
|
|
self.assertEqual(hash((10, 'foo')), hash(Notify(10, 'foo')))
|
|
|
|
self.assertNotEqual(hash(Notify(10, 'foo', 'bar')),
|
|
|
|
hash(Notify(10, 'foo')))
|
2010-10-15 12:42:44 +04:00
|
|
|
|
2015-06-02 19:02:04 +03:00
|
|
|
|
2010-04-20 21:17:27 +04:00
|
|
|
def test_suite():
|
|
|
|
return unittest.TestLoader().loadTestsFromName(__name__)
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2010-04-20 21:17:27 +04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|