Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e9b2a4c

Browse files
committed
Issue #11761: make tests for gc.get_count() less fragile
2 parents 7cb20a8 + b35f29a commit e9b2a4c

1 file changed

Lines changed: 27 additions & 16 deletions

File tree

Lib/test/test_gc.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -239,30 +239,41 @@ def __del__(self):
239239
# The following two tests are fragile:
240240
# They precisely count the number of allocations,
241241
# which is highly implementation-dependent.
242-
# For example:
243-
# - disposed tuples are not freed, but reused
244-
# - the call to assertEqual somehow avoids building its args tuple
242+
# For example, disposed tuples are not freed, but reused.
243+
# To minimize variations, though, we first store the get_count() results
244+
# and check them at the end.
245245
def test_get_count(self):
246-
# Avoid future allocation of method object
247-
assertEqual = self._baseAssertEqual
248246
gc.collect()
249-
assertEqual(gc.get_count(), (0, 0, 0))
250-
a = dict()
251-
# since gc.collect(), we created two objects:
252-
# the dict, and the tuple returned by get_count()
253-
assertEqual(gc.get_count(), (2, 0, 0))
247+
a, b, c = gc.get_count()
248+
x = []
249+
d, e, f = gc.get_count()
250+
self.assertEqual((b, c), (0, 0))
251+
self.assertEqual((e, f), (0, 0))
252+
# This is less fragile than asserting that a equals 0.
253+
self.assertLess(a, 5)
254+
# Between the two calls to get_count(), at least one object was
255+
# created (the list).
256+
self.assertGreater(d, a)
254257

255258
def test_collect_generations(self):
256-
# Avoid future allocation of method object
257-
assertEqual = self.assertEqual
258259
gc.collect()
259-
a = dict()
260+
# This object will "trickle" into generation N + 1 after
261+
# each call to collect(N)
262+
x = []
260263
gc.collect(0)
261-
assertEqual(gc.get_count(), (0, 1, 0))
264+
# x is now in gen 1
265+
a, b, c = gc.get_count()
262266
gc.collect(1)
263-
assertEqual(gc.get_count(), (0, 0, 1))
267+
# x is now in gen 2
268+
d, e, f = gc.get_count()
264269
gc.collect(2)
265-
assertEqual(gc.get_count(), (0, 0, 0))
270+
# x is now in gen 3
271+
g, h, i = gc.get_count()
272+
# We don't check a, d, g since their exact values depends on
273+
# internal implementation details of the interpreter.
274+
self.assertEqual((b, c), (1, 0))
275+
self.assertEqual((e, f), (0, 1))
276+
self.assertEqual((h, i), (0, 0))
266277

267278
def test_trashcan(self):
268279
class Ouch:

0 commit comments

Comments
 (0)