MCPcopy Index your code
hub / github.com/python/cpython / test_heap

Method test_heap

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

Source from the content-addressed store, hash-verified

4301 super().tearDown()
4302
4303 def test_heap(self):
4304 iterations = 5000
4305 maxblocks = 50
4306 blocks = []
4307
4308 # get the heap object
4309 heap = multiprocessing.heap.BufferWrapper._heap
4310 heap._DISCARD_FREE_SPACE_LARGER_THAN = 0
4311
4312 # create and destroy lots of blocks of different sizes
4313 for i in range(iterations):
4314 size = int(random.lognormvariate(0, 1) * 1000)
4315 b = multiprocessing.heap.BufferWrapper(size)
4316 blocks.append(b)
4317 if len(blocks) > maxblocks:
4318 i = random.randrange(maxblocks)
4319 del blocks[i]
4320 del b
4321
4322 # verify the state of the heap
4323 with heap._lock:
4324 all = []
4325 free = 0
4326 occupied = 0
4327 for L in list(heap._len_to_seq.values()):
4328 # count all free blocks in arenas
4329 for arena, start, stop in L:
4330 all.append((heap._arenas.index(arena), start, stop,
4331 stop-start, 'free'))
4332 free += (stop-start)
4333 for arena, arena_blocks in heap._allocated_blocks.items():
4334 # count all allocated blocks in arenas
4335 for start, stop in arena_blocks:
4336 all.append((heap._arenas.index(arena), start, stop,
4337 stop-start, 'occupied'))
4338 occupied += (stop-start)
4339
4340 self.assertEqual(free + occupied,
4341 sum(arena.size for arena in heap._arenas))
4342
4343 all.sort()
4344
4345 for i in range(len(all)-1):
4346 (arena, start, stop) = all[i][:3]
4347 (narena, nstart, nstop) = all[i+1][:3]
4348 if arena != narena:
4349 # Two different arenas
4350 self.assertEqual(stop, heap._arenas[arena].size) # last block
4351 self.assertEqual(nstart, 0) # first block
4352 else:
4353 # Same arena: two adjacent blocks
4354 self.assertEqual(stop, nstart)
4355
4356 # test free'ing all blocks
4357 random.shuffle(blocks)
4358 while blocks:
4359 blocks.pop()
4360

Callers

nothing calls this directly

Calls 11

listClass · 0.85
lognormvariateMethod · 0.80
randrangeMethod · 0.80
shuffleMethod · 0.80
appendMethod · 0.45
valuesMethod · 0.45
indexMethod · 0.45
itemsMethod · 0.45
assertEqualMethod · 0.45
sortMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected