Skip to content

Commit

Permalink
attempt to wait on results (for pypy)
Browse files Browse the repository at this point in the history
  • Loading branch information
fizyk committed Mar 24, 2021
1 parent ced80f5 commit 5af624f
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions tests/test_postgresql.py
@@ -1,5 +1,7 @@
"""All tests for pytest-postgresql."""
import decimal
from datetime import datetime, timedelta
from time import sleep

import psycopg2
import pytest
Expand Down Expand Up @@ -59,6 +61,21 @@ def test_rand_postgres_port(postgresql_rand):
assert postgresql_rand.status == psycopg2.extensions.STATUS_READY


def wait_for(func, timeout=60):
"""Check for function to finish."""
time: datetime = datetime.utcnow()
timeout_diff: timedelta = timedelta(seconds=timeout)
while True:
try:
func()
return
except AssertionError as e:
if time + timeout_diff < datetime.utcnow():
raise e
sleep(1)
pass


@pytest.mark.skipif(
decimal.Decimal(POSTGRESQL_VERSION) < 10,
reason="Test query not supported in those postgresql versions, and soon will not be supported."
Expand All @@ -71,11 +88,13 @@ def test_postgres_terminate_connection(
And check that only one exists at a time.
"""
cur = postgresql_version.cursor()
cur.execute(
'SELECT * FROM pg_stat_activity '
'WHERE backend_type = \'client backend\';'
)
existing_connections = cur.fetchall()
assert len(existing_connections) == 1, 'there is always only one connection'
cur.close()
def check_if_one_connection():
cur = postgresql_version.cursor()
cur.execute(
'SELECT * FROM pg_stat_activity '
'WHERE backend_type = \'client backend\';'
)
existing_connections = cur.fetchall()
assert len(existing_connections) == 1, 'there is always only one connection'
cur.close()
wait_for(check_if_one_connection)

0 comments on commit 5af624f

Please sign in to comment.