MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_heap

Method test_heap

Lib/test/_test_multiprocessing.py:3996–4058  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

3994 super().tearDown()
3995
3996 def test_heap(self):
3997 iterations = 5000
3998 maxblocks = 50
3999 blocks = []
4000
4001 # get the heap object
4002 heap = multiprocessing.heap.BufferWrapper._heap
4003 heap._DISCARD_FREE_SPACE_LARGER_THAN = 0
4004
4005 # create and destroy lots of blocks of different sizes
4006 for i in range(iterations):
4007 size = int(random.lognormvariate(0, 1) * 1000)
4008 b = multiprocessing.heap.BufferWrapper(size)
4009 blocks.append(b)
4010 if len(blocks) > maxblocks:
4011 i = random.randrange(maxblocks)
4012 del blocks[i]
4013 del b
4014
4015 # verify the state of the heap
4016 with heap._lock:
4017 all = []
4018 free = 0
4019 occupied = 0
4020 for L in list(heap._len_to_seq.values()):
4021 # count all free blocks in arenas
4022 for arena, start, stop in L:
4023 all.append((heap._arenas.index(arena), start, stop,
4024 stop-start, 'free'))
4025 free += (stop-start)
4026 for arena, arena_blocks in heap._allocated_blocks.items():
4027 # count all allocated blocks in arenas
4028 for start, stop in arena_blocks:
4029 all.append((heap._arenas.index(arena), start, stop,
4030 stop-start, 'occupied'))
4031 occupied += (stop-start)
4032
4033 self.assertEqual(free + occupied,
4034 sum(arena.size for arena in heap._arenas))
4035
4036 all.sort()
4037
4038 for i in range(len(all)-1):
4039 (arena, start, stop) = all[i][:3]
4040 (narena, nstart, nstop) = all[i+1][:3]
4041 if arena != narena:
4042 # Two different arenas
4043 self.assertEqual(stop, heap._arenas[arena].size) # last block
4044 self.assertEqual(nstart, 0) # first block
4045 else:
4046 # Same arena: two adjacent blocks
4047 self.assertEqual(stop, nstart)
4048
4049 # test free'ing all blocks
4050 random.shuffle(blocks)
4051 while blocks:
4052 blocks.pop()
4053

Callers

nothing calls this directly

Calls 13

lenFunction · 0.85
listClass · 0.85
lognormvariateMethod · 0.80
randrangeMethod · 0.80
shuffleMethod · 0.80
sumFunction · 0.50
appendMethod · 0.45
valuesMethod · 0.45
indexMethod · 0.45
itemsMethod · 0.45
assertEqualMethod · 0.45
sortMethod · 0.45

Tested by

no test coverage detected