Commit b9333e00 authored by Kirill Smelkov's avatar Kirill Smelkov

golang: tests: Rework verifying blockforever

- put the logic to test-tweak what happens inside _blockforever into
  context manager pypanicWhenBlocked;
- place this manager in pyx code, where it can later be changed to tweak
  _blockforever at C level.
parent 4166dc65
...@@ -617,7 +617,10 @@ def pyselect(*casev): ...@@ -617,7 +617,10 @@ def pyselect(*casev):
# _blockforever blocks current goroutine forever. # _blockforever blocks current goroutine forever.
_tblockforever = None
def _blockforever(): def _blockforever():
if _tblockforever is not None:
_tblockforever()
# take a lock twice. It will forever block on the second lock attempt. # take a lock twice. It will forever block on the second lock attempt.
# Under gevent, similarly to Go, this raises "LoopExit: This operation # Under gevent, similarly to Go, this raises "LoopExit: This operation
# would block forever", if there are no other greenlets scheduled to be run. # would block forever", if there are no other greenlets scheduled to be run.
......
...@@ -28,6 +28,7 @@ from __future__ import print_function, absolute_import ...@@ -28,6 +28,7 @@ from __future__ import print_function, absolute_import
from golang cimport go, pychan, panic, pypanic, topyexc from golang cimport go, pychan, panic, pypanic, topyexc
from golang import nilchan from golang import nilchan
from golang import _golang
from golang import time from golang import time
...@@ -69,6 +70,21 @@ def pywaitBlocked(pychanop): ...@@ -69,6 +70,21 @@ def pywaitBlocked(pychanop):
time.sleep(0) # yield to another thread / coroutine time.sleep(0) # yield to another thread / coroutine
# `with pypanicWhenBlocked` hooks into _golang._blockforever to raise panic with
# "t: blocks forever" instead of blocking.
cdef class pypanicWhenBlocked:
def __enter__(t):
assert _golang._tblockforever is None
_golang._tblockforever = _panicblocked
return t
def __exit__(t, typ, val, tb):
_golang._tblockforever = None
def _panicblocked():
pypanic("t: blocks forever")
# small test to verify pyx(nogil) go. # small test to verify pyx(nogil) go.
cdef void _test_go_nogil() nogil except +topyexc: cdef void _test_go_nogil() nogil except +topyexc:
go(_work, 111) go(_work, 111)
......
...@@ -29,7 +29,7 @@ from six.moves import range as xrange ...@@ -29,7 +29,7 @@ from six.moves import range as xrange
import gc, weakref import gc, weakref
from golang._golang_test import pywaitBlocked as waitBlocked, pylen_recvq as len_recvq, \ from golang._golang_test import pywaitBlocked as waitBlocked, pylen_recvq as len_recvq, \
pylen_sendq as len_sendq pylen_sendq as len_sendq, pypanicWhenBlocked as panicWhenBlocked
# pyx/c/c++ tests -> test_pyx_* # pyx/c/c++ tests -> test_pyx_*
from golang import _golang_test from golang import _golang_test
...@@ -653,14 +653,8 @@ def bench_select(b): ...@@ -653,14 +653,8 @@ def bench_select(b):
def test_blockforever(): def test_blockforever():
from golang import _golang with panicWhenBlocked():
B = _golang._blockforever
def _(): panic("t: blocks forever")
_golang._blockforever = _
try:
_test_blockforever() _test_blockforever()
finally:
_golang._blockforever = B
def _test_blockforever(): def _test_blockforever():
z = nilchan z = nilchan
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment