Files
Athena-Deck/test_cgroup_headroom.py

24 lines
1.3 KiB
Python

import unittest
from pathlib import Path
from unittest.mock import patch
from image_test import cgroup_headroom,GIB
class HeadroomTests(unittest.TestCase):
def value(self,used,stat,limit=40*GIB):
files={'memory.max':str(limit),'memory.current':str(used),'memory.stat':'\n'.join(f'{k} {v}' for k,v in stat.items())}
with patch.object(Path,'read_text',lambda p,*a,**kw:files[p.name]):return cgroup_headroom()
def test_exited_mmap_worker_active_cache_does_not_block_next_model(self):
stat={'file':39*GIB,'active_file':31*GIB,'inactive_file':8*GIB,'file_mapped':0,'anon':GIB}
self.assertEqual(self.value(40*GIB,stat),39*GIB)
def test_mapped_dirty_shared_and_locked_pages_remain_charged(self):
stat={'file':30*GIB,'file_mapped':10*GIB,'file_dirty':2*GIB,'file_writeback':GIB,'shmem':3*GIB,'unevictable':GIB}
self.assertEqual(self.value(38*GIB,stat),15*GIB)
def test_anonymous_only_pressure_is_not_discarded(self):
self.assertEqual(self.value(39*GIB,{'anon':39*GIB,'file':0}),GIB)
def test_negative_headroom_retains_overlimit_pressure(self):
self.assertEqual(self.value(41*GIB,{'file':0}),-GIB)
def test_unlimited_cgroup(self):
with patch.object(Path,'read_text',return_value='max'):self.assertIsNone(cgroup_headroom())
if __name__=='__main__':unittest.main()