2012-10-06 04:10:41 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""Test for issue #113: test with error during green processing
|
|
|
|
"""
|
|
|
|
|
|
|
|
DSN = 'dbname=test'
|
|
|
|
|
2012-10-06 14:58:52 +04:00
|
|
|
import eventlet.patcher
|
|
|
|
eventlet.patcher.monkey_patch()
|
2012-10-06 04:10:41 +04:00
|
|
|
|
|
|
|
import os
|
|
|
|
import signal
|
2012-10-06 14:58:52 +04:00
|
|
|
from time import sleep
|
|
|
|
|
2012-10-06 04:10:41 +04:00
|
|
|
import psycopg2
|
|
|
|
from psycopg2 import extensions
|
|
|
|
from eventlet.hubs import trampoline
|
|
|
|
|
2012-10-06 14:58:52 +04:00
|
|
|
|
|
|
|
# register a test wait callback that fails if SIGHUP is received
|
|
|
|
|
2012-10-06 04:10:41 +04:00
|
|
|
panic = []
|
|
|
|
|
|
|
|
def wait_cb(conn):
|
|
|
|
"""A wait callback useful to allow eventlet to work with Psycopg."""
|
|
|
|
while 1:
|
|
|
|
if panic:
|
|
|
|
raise Exception('whatever')
|
|
|
|
|
|
|
|
state = conn.poll()
|
|
|
|
if state == extensions.POLL_OK:
|
|
|
|
break
|
|
|
|
elif state == extensions.POLL_READ:
|
|
|
|
trampoline(conn.fileno(), read=True)
|
|
|
|
elif state == extensions.POLL_WRITE:
|
|
|
|
trampoline(conn.fileno(), write=True)
|
|
|
|
else:
|
|
|
|
raise psycopg2.OperationalError(
|
|
|
|
"Bad result from poll: %r" % state)
|
|
|
|
|
|
|
|
extensions.set_wait_callback(wait_cb)
|
|
|
|
|
2012-10-06 14:58:52 +04:00
|
|
|
|
|
|
|
# SIGHUP handler to inject a fail in the callback
|
|
|
|
|
2012-10-06 04:10:41 +04:00
|
|
|
def handler(signum, frame):
|
|
|
|
panic.append(True)
|
|
|
|
|
|
|
|
signal.signal(signal.SIGHUP, handler)
|
|
|
|
|
2012-10-06 14:58:52 +04:00
|
|
|
|
|
|
|
# Simulate another green thread working
|
|
|
|
|
|
|
|
def worker():
|
|
|
|
while 1:
|
2017-12-04 05:47:19 +03:00
|
|
|
print("I'm working")
|
2012-10-06 14:58:52 +04:00
|
|
|
sleep(1)
|
|
|
|
|
|
|
|
eventlet.spawn(worker)
|
|
|
|
|
|
|
|
|
|
|
|
# You can unplug the network cable etc. here.
|
|
|
|
# Kill -HUP will raise an exception in the callback.
|
|
|
|
|
2017-12-04 05:47:19 +03:00
|
|
|
print("PID", os.getpid())
|
2012-10-06 04:10:41 +04:00
|
|
|
conn = psycopg2.connect(DSN)
|
|
|
|
curs = conn.cursor()
|
2012-10-06 04:45:24 +04:00
|
|
|
try:
|
2012-10-06 14:58:52 +04:00
|
|
|
for i in range(1000):
|
|
|
|
curs.execute("select %s, pg_sleep(1)", (i,))
|
|
|
|
r = curs.fetchone()
|
2017-12-04 05:47:19 +03:00
|
|
|
print("selected", r)
|
2012-10-06 14:58:52 +04:00
|
|
|
|
2012-10-06 04:45:24 +04:00
|
|
|
except BaseException, e:
|
2017-12-04 05:47:19 +03:00
|
|
|
print("got exception:", e.__class__.__name__, e)
|
2012-10-06 04:45:24 +04:00
|
|
|
|
2012-10-06 14:58:52 +04:00
|
|
|
if conn.closed:
|
2017-12-04 05:47:19 +03:00
|
|
|
print("the connection is closed")
|
2012-10-06 14:58:52 +04:00
|
|
|
else:
|
|
|
|
conn.rollback()
|
|
|
|
curs.execute("select 1")
|
2017-12-04 05:47:19 +03:00
|
|
|
print(curs.fetchone())
|