Commit 352628b5 authored by Kirill Smelkov's avatar Kirill Smelkov

golang: tests: Factor out retrieving len(ch._recvq) and len(ch._sendq)

Chan implementation is going to be moved into C, and this way direct
access to chan internals won't be possible. C implementation will export
dedicated functions for chan tests which will be used.

Prepare for that and factor out retrieving len of chan recv/send queues
into separate functions, which will be adapted at the time of C port.
parent d98e42e3
......@@ -78,15 +78,25 @@ def waitBlocked(chanop):
t0 = time.now()
while 1:
with ch._mu:
if recv and len(ch._recvq) > 0:
if recv and len_recvq(ch) > 0:
return
if send and len(ch._sendq) > 0:
if send and len_sendq(ch) > 0:
return
now = time.now()
if now-t0 > 10: # waited > 10 seconds - likely deadlock
panic("deadlock")
time.sleep(0) # yield to another thread / coroutine
# len_{recv,send}q returns len(ch._{recv,send}q)
def len_recvq(ch):
if ch is nilchan:
raise AssertionError('len(.recvq) on nil channel')
return len(ch._recvq)
def len_sendq(ch):
if ch is nilchan:
raise AssertionError('len(.sendq) on nil channel')
return len(ch._sendq)
def test_chan():
# sync: pre-close vs send/recv
......@@ -282,8 +292,8 @@ def test_select():
)
assert (_, _rx) == (0, None)
done.recv()
assert len(ch1._sendq) == len(ch1._recvq) == 0
assert len(ch2._sendq) == len(ch2._recvq) == 0
assert len_sendq(ch1) == len_recvq(ch1) == 0
assert len_sendq(ch2) == len_recvq(ch2) == 0
# blocking 2·recv
......@@ -302,8 +312,8 @@ def test_select():
)
assert (_, _rx) == (0, 'a')
done.recv()
assert len(ch1._sendq) == len(ch1._recvq) == 0
assert len(ch2._sendq) == len(ch2._recvq) == 0
assert len_sendq(ch1) == len_recvq(ch1) == 0
assert len_sendq(ch2) == len_recvq(ch2) == 0
# blocking send/recv
......@@ -322,8 +332,8 @@ def test_select():
)
assert (_, _rx) == (0, None)
done.recv()
assert len(ch1._sendq) == len(ch1._recvq) == 0
assert len(ch2._sendq) == len(ch2._recvq) == 0
assert len_sendq(ch1) == len_recvq(ch1) == 0
assert len_sendq(ch2) == len_recvq(ch2) == 0
# blocking recv/send
......@@ -342,8 +352,8 @@ def test_select():
)
assert (_, _rx) == (0, 'a')
done.recv()
assert len(ch1._sendq) == len(ch1._recvq) == 0
assert len(ch2._sendq) == len(ch2._recvq) == 0
assert len_sendq(ch1) == len_recvq(ch1) == 0
assert len_sendq(ch2) == len_recvq(ch2) == 0
# blocking send + nil channel
......@@ -365,7 +375,7 @@ def test_select():
assert (_, _rx) == (2, None)
done.recv()
assert len(ch._sendq) == len(ch._recvq) == 0
assert len_sendq(ch) == len_recvq(ch) == 0
# blocking recv + nil channel
for i in range(N):
......@@ -385,7 +395,7 @@ def test_select():
assert (_, _rx) == (2, 'd')
done.recv()
assert len(ch._sendq) == len(ch._recvq) == 0
assert len_sendq(ch) == len_recvq(ch) == 0
# buffered ping-pong
......@@ -435,8 +445,8 @@ def test_select():
assert (_, _rx) == (1, None)
done.recv()
assert len(ch1._sendq) == len(ch1._recvq) == 0
assert len(ch2._sendq) == len(ch2._recvq) == 0
assert len_sendq(ch1) == len_recvq(ch1) == 0
assert len_sendq(ch2) == len_recvq(ch2) == 0
# select vs select
......@@ -477,8 +487,8 @@ def test_select():
assert (_, _rx) == (1, None)
done.recv()
assert len(ch1._sendq) == len(ch1._recvq) == 0
assert len(ch2._sendq) == len(ch2._recvq) == 0
assert len_sendq(ch1) == len_recvq(ch1) == 0
assert len_sendq(ch2) == len_recvq(ch2) == 0
# benchmark sync chan send vs recv on select side.
......
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