File: test_gevent.py

package info (click to toggle)
psycopg3 3.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,836 kB
  • sloc: python: 46,657; sh: 403; ansic: 149; makefile: 73
file content (85 lines) | stat: -rw-r--r-- 1,773 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import sys
import json
import subprocess as sp

import pytest

import psycopg

pytest.importorskip("gevent")

pytestmark = [pytest.mark.gevent]


@pytest.mark.slow
@pytest.mark.timing
def test_gevent(dsn):
    TICK = 0.1
    script = f"""\
import gevent.monkey
gevent.monkey.patch_all()

import json
import time
import gevent
import psycopg

TICK = {TICK!r}
dts = []
queried = False

def ticker():
    t0 = time.time()
    for i in range(5):
        time.sleep(TICK)
        t = time.time()
        dts.append(t - t0)
        t0 = t

def querier():
    time.sleep(TICK * 2)
    with psycopg.connect({dsn!r}) as conn:
        conn.execute("select pg_sleep(0.3)")

    global queried
    queried = True

jobs = [gevent.spawn(ticker), gevent.spawn(querier)]
gevent.joinall(jobs, timeout=3)
print(json.dumps(dts))
"""
    cmdline = [sys.executable, "-c", script]
    rv = sp.run(cmdline, check=True, text=True, stdout=sp.PIPE)
    dts = json.loads(rv.stdout)

    for dt in dts:
        assert TICK <= dt < TICK * 1.2


@pytest.mark.skipif("not psycopg._cmodule._psycopg")
def test_patched_dont_use_wait_c():
    if psycopg.waiting.wait is not psycopg.waiting.wait_c:
        pytest.skip("wait_c not normally in use")

    script = """
import gevent.monkey
gevent.monkey.patch_all()

import psycopg
assert psycopg.waiting.wait is not psycopg.waiting.wait_c
"""
    sp.check_call([sys.executable, "-c", script])


@pytest.mark.skipif("not psycopg._cmodule._psycopg")
def test_unpatched_still_use_wait_c():
    if psycopg.waiting.wait is not psycopg.waiting.wait_c:
        pytest.skip("wait_c not normally in use")

    script = """
import gevent.monkey

import psycopg
assert psycopg.waiting.wait is psycopg.waiting.wait_c
"""
    sp.check_call([sys.executable, "-c", script])