this repo has no description
at trunk 57 lines 1.8 kB view raw
1#!/usr/bin/env python3 2# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) 3import gc 4import unittest 5 6from test_support import pyro_only 7 8 9class GCModuleTest(unittest.TestCase): 10 def test_collect_collects_garbage(self): 11 # Can't really test that collection is happening so just call it 12 # to see we don't crash. 13 gc.collect() 14 15 def test_garbage_is_a_list(self): 16 self.assertIsInstance(gc.garbage, list) 17 18 @pyro_only 19 def test_immortalize_moves_objects_to_immortal_partition(self): 20 from _builtins import _gc 21 22 # make sure that garbage collection has run at least once 23 # which ensures that any code object will be transported 24 # to the immortal heap 25 _gc() 26 27 # That makes code objects and sub-objects immortal 28 def test(): 29 return 30 31 code = test.__code__ 32 self.assertTrue(gc._is_immortal(code)) 33 self.assertTrue(gc._is_immortal(code.co_consts)) 34 35 value = [11, 22, 33] 36 self.assertFalse(gc._is_immortal(value)) 37 self.assertFalse(gc._is_immortal(GCModuleTest)) 38 39 # We can only call immortalize_heap in one test since its effect 40 # is permanent 41 gc.immortalize_heap() 42 self.assertTrue(gc._is_immortal(value)) 43 self.assertTrue(gc._is_immortal(GCModuleTest)) 44 45 other_value = [44, 55, 66] 46 self.assertFalse(gc._is_immortal(other_value)) 47 48 # ... but we can call it multiple times to move more things into 49 # the immortal partition 50 gc.immortalize_heap() 51 self.assertTrue(gc._is_immortal(other_value)) 52 self.assertTrue(gc._is_immortal(value)) 53 self.assertTrue(gc._is_immortal(GCModuleTest)) 54 55 56if __name__ == "__main__": 57 unittest.main()