Skip to content

Commit bd97f81

Browse files
committed
rework waiting for gc
wait directly for gc ref to expire rather than relying on decrementing getrefcount, which is implementation specific
1 parent 85ff8d4 commit bd97f81

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

tests/test_message.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import copy
66
import gc
7-
import sys
87

98
try:
109
from sys import getrefcount
@@ -18,6 +17,7 @@
1817
import pytest
1918

2019
import zmq
20+
from zmq.utils.garbage import gc as zmq_gc
2121
from zmq_test_utils import PYPY, BaseZMQTestCase, SkipTest, skip_cpython_cffi, skip_pypy
2222

2323
# some useful constants:
@@ -30,29 +30,34 @@
3030
view_rc = grc(x) - rc0
3131

3232

33-
def await_gc(obj, rc):
34-
"""wait for refcount on an object to drop to an expected value
33+
def await_gc(gc_key):
34+
"""wait for zmq garbage collection
3535
3636
Necessary because of the zero-copy gc thread,
3737
which can take some time to receive its DECREF message.
3838
"""
39-
# count refs for this function
40-
if sys.version_info < (3, 11):
41-
my_refs = 2
42-
else:
43-
my_refs = 1
44-
for i in range(50):
45-
# rc + 2 because of the refs in this function
46-
if grc(obj) <= rc + my_refs:
39+
deadline = time.monotonic() + 3
40+
while time.monotonic() < deadline:
41+
if gc_key in zmq_gc.refs:
42+
time.sleep(0.05)
43+
else:
44+
gc.collect()
4745
return
48-
time.sleep(0.05)
46+
raise TimeoutError("gc not collected")
4947

5048

5149
class TestFrame(BaseZMQTestCase):
50+
def setUp(self):
51+
super().setUp()
52+
# make sure we are starting clean
53+
assert not zmq_gc.refs
54+
5255
def tearDown(self):
5356
super().tearDown()
5457
for i in range(3):
5558
gc.collect()
59+
# make sure we left no refs
60+
assert not zmq_gc.refs
5661

5762
@skip_pypy
5863
def test_above_30(self):
@@ -61,9 +66,10 @@ def test_above_30(self):
6166
s = (2**i) * x
6267
rc = grc(s)
6368
m = zmq.Frame(s, copy=False)
69+
_gc_ref = next(iter(zmq_gc.refs))
6470
assert grc(s) == rc + 2
6571
del m
66-
await_gc(s, rc)
72+
await_gc(_gc_ref)
6773
assert grc(s) == rc
6874
del s
6975

@@ -116,6 +122,7 @@ def test_lifecycle1(self):
116122
s = (2**i) * x
117123
rc = rc_0 = grc(s)
118124
m = zmq.Frame(s, copy=False)
125+
_gc_ref = next(iter(zmq_gc.refs))
119126
rc += 2
120127
assert grc(s) == rc
121128
m2 = copy.copy(m)
@@ -137,7 +144,7 @@ def test_lifecycle1(self):
137144
assert grc(s) == rc
138145
del m
139146
rc -= 2
140-
await_gc(s, rc)
147+
await_gc(_gc_ref)
141148
assert grc(s) == rc
142149
assert rc == rc_0
143150
del s
@@ -149,6 +156,7 @@ def test_lifecycle2(self):
149156
s = (2**i) * x
150157
rc = rc_0 = grc(s)
151158
m = zmq.Frame(s, copy=False)
159+
_gc_ref = next(iter(zmq_gc.refs))
152160
rc += 2
153161
assert grc(s) == rc
154162
m2 = copy.copy(m)
@@ -169,7 +177,7 @@ def test_lifecycle2(self):
169177
assert grc(s) == rc
170178
del m2
171179
rc -= 2
172-
await_gc(s, rc)
180+
await_gc(_gc_ref)
173181
assert grc(s) == rc
174182
assert rc == rc_0
175183
del s

0 commit comments

Comments
 (0)