25 lines
919 B
Python
25 lines
919 B
Python
import datetime, pathlib, sys, tempfile, threading, time, unittest
|
|
from unittest.mock import patch
|
|
sys.path.insert(0, str(pathlib.Path(__file__).parent))
|
|
import executor as m
|
|
import runtime
|
|
from test_contract import lease
|
|
|
|
class Heartbeats(unittest.TestCase):
|
|
def test_ongoing_denial_kills_real_child(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
marker=pathlib.Path(root,'late')
|
|
code=f'import time,pathlib; time.sleep(1); pathlib.Path({str(marker)!r}).touch()'
|
|
beats=[]
|
|
def beat(binding):
|
|
beats.append(time.monotonic())
|
|
if len(beats)>1: raise PermissionError('revoked')
|
|
with patch.object(runtime,'HEARTBEAT_INTERVAL',.1):
|
|
with self.assertRaises(PermissionError):
|
|
m.execute(lease(),root,m.ProcessInference([sys.executable,'-c',code]),beat)
|
|
time.sleep(1.1)
|
|
self.assertFalse(marker.exists())
|
|
self.assertGreaterEqual(len(beats),2)
|
|
|
|
if __name__=='__main__': unittest.main()
|